blob: 0d954a0ee1dc6b95e6edfcbb7d818ed1d856e78b [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 Moolenaarf3af54e2017-08-30 14:53:06 +0200190 {VV_NAME("termrgbresp", VAR_STRING), VV_RO},
191 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
192 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
193 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000194};
195
196/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000197#define vv_type vv_di.di_tv.v_type
198#define vv_nr vv_di.di_tv.vval.v_number
199#define vv_float vv_di.di_tv.vval.v_float
200#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000201#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200202#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000203#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000204
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200205static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000206#define vimvarht vimvardict.dv_hashtab
207
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100208static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
209static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
210static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_glob_vars(int *first);
212static void list_buf_vars(int *first);
213static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static void list_vim_vars(int *first);
216static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
218static 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 +0100219static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
220static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
222static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
223static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
224static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000225
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static int eval2(char_u **arg, typval_T *rettv, int evaluate);
227static int eval3(char_u **arg, typval_T *rettv, int evaluate);
228static int eval4(char_u **arg, typval_T *rettv, int evaluate);
229static int eval5(char_u **arg, typval_T *rettv, int evaluate);
230static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
231static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000232
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
235static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200240static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static void delete_var(hashtab_T *ht, hashitem_T *hi);
243static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
244static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247/* for VIM_VERSION_ defines */
248#include "version.h"
249
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100250
251#if defined(EBCDIC) || defined(PROTO)
252/*
253 * Compare struct fst by function name.
254 */
255 static int
256compare_func_name(const void *s1, const void *s2)
257{
258 struct fst *p1 = (struct fst *)s1;
259 struct fst *p2 = (struct fst *)s2;
260
261 return STRCMP(p1->f_name, p2->f_name);
262}
263
264/*
265 * Sort the function table by function name.
266 * The sorting of the table above is ASCII dependant.
267 * On machines using EBCDIC we have to sort it.
268 */
269 static void
270sortFunctions(void)
271{
272 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
273
274 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
275}
276#endif
277
278
Bram Moolenaar33570922005-01-25 22:26:29 +0000279/*
280 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000281 */
282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100283eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000284{
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 int i;
286 struct vimvar *p;
287
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200288 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
289 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200290 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000291 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294 for (i = 0; i < VV_LEN; ++i)
295 {
296 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200297 if (STRLEN(p->vv_name) > 16)
298 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100299 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200300 getout(1);
301 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000302 STRCPY(p->vv_di.di_key, p->vv_name);
303 if (p->vv_flags & VV_RO)
304 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
305 else if (p->vv_flags & VV_RO_SBX)
306 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
307 else
308 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000309
310 /* add to v: scope dict, unless the value is not always available */
311 if (p->vv_type != VAR_UNKNOWN)
312 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000313 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000314 /* add to compat scope dict */
315 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000316 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100317 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
318
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000319 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100320 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200321 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100322 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100323
324 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
325 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
326 set_vim_var_nr(VV_NONE, VVAL_NONE);
327 set_vim_var_nr(VV_NULL, VVAL_NULL);
328
Bram Moolenaarf562e722016-07-19 17:25:25 +0200329 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
330 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
331 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
332 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
333 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
334 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
335 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
336 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
337 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
338 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
339
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200340 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200341
342#ifdef EBCDIC
343 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100344 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200345 */
346 sortFunctions();
347#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000348}
349
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000350#if defined(EXITFREE) || defined(PROTO)
351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100352eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000353{
354 int i;
355 struct vimvar *p;
356
357 for (i = 0; i < VV_LEN; ++i)
358 {
359 p = &vimvars[i];
360 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000361 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000362 vim_free(p->vv_str);
363 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 }
365 else if (p->vv_di.di_tv.v_type == VAR_LIST)
366 {
367 list_unref(p->vv_list);
368 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000369 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000370 }
371 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000372 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000373 hash_clear(&compat_hashtab);
374
Bram Moolenaard9fba312005-06-26 22:34:35 +0000375 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200377 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100378# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000379
380 /* global variables */
381 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000382
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000383 /* autoloaded script names */
384 ga_clear_strings(&ga_loaded);
385
Bram Moolenaarcca74132013-09-25 21:00:28 +0200386 /* Script-local variables. First clear all the variables and in a second
387 * loop free the scriptvar_T, because a variable in one script might hold
388 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 ga_clear(&ga_scripts);
394
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000395 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200396 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000397
398 /* functions */
399 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400}
401#endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 * Set an internal variable to a string value. Creates the variable if it does
406 * not already exist.
407 */
408 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100409set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000411 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000412 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 val = vim_strsave(value);
415 if (val != NULL)
416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000420 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
423 }
424}
425
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000426static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200427#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000428static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000429static char_u *redir_endp = NULL;
430static char_u *redir_varname = NULL;
431
432/*
433 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100434 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000435 * Returns OK if successfully completed the setup. FAIL otherwise.
436 */
437 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439{
440 int save_emsg;
441 int err;
442 typval_T tv;
443
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000444 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000445 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000446 {
447 EMSG(_(e_invarg));
448 return FAIL;
449 }
450
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000451 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 redir_varname = vim_strsave(name);
453 if (redir_varname == NULL)
454 return FAIL;
455
456 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
457 if (redir_lval == NULL)
458 {
459 var_redir_stop();
460 return FAIL;
461 }
462
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000463 /* The output is stored in growarray "redir_ga" until redirection ends. */
464 ga_init2(&redir_ga, (int)sizeof(char), 500);
465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000466 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100467 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000468 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
470 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200471 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 if (redir_endp != NULL && *redir_endp != NUL)
473 /* Trailing characters are present after the variable name */
474 EMSG(_(e_trailing));
475 else
476 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000477 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478 var_redir_stop();
479 return FAIL;
480 }
481
482 /* check if we can write to the variable: set it to or append an empty
483 * string */
484 save_emsg = did_emsg;
485 did_emsg = FALSE;
486 tv.v_type = VAR_STRING;
487 tv.vval.v_string = (char_u *)"";
488 if (append)
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
490 else
491 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200492 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000494 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 if (err)
496 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 var_redir_stop();
499 return FAIL;
500 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000501
502 return OK;
503}
504
505/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000506 * Append "value[value_len]" to the variable set by var_redir_start().
507 * The actual appending is postponed until redirection ends, because the value
508 * appended may in fact be the string we write to, changing it may cause freed
509 * memory to be used:
510 * :redir => foo
511 * :let foo
512 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 */
514 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100515var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000517 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518
519 if (redir_lval == NULL)
520 return;
521
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000522 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000526
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000528 {
529 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000531 }
532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534}
535
536/*
537 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000538 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 */
540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100541var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543 typval_T tv;
544
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200545 if (EVALCMD_BUSY)
546 {
547 redir_lval = NULL;
548 return;
549 }
550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 if (redir_lval != NULL)
552 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000553 /* If there was no error: assign the text to the variable. */
554 if (redir_endp != NULL)
555 {
556 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
557 tv.v_type = VAR_STRING;
558 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200559 /* Call get_lval() again, if it's inside a Dict or List it may
560 * have changed. */
561 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100562 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 if (redir_endp != NULL && redir_lval->ll_name != NULL)
564 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
565 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000568 /* free the collected output */
569 vim_free(redir_ga.ga_data);
570 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572 vim_free(redir_lval);
573 redir_lval = NULL;
574 }
575 vim_free(redir_varname);
576 redir_varname = NULL;
577}
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579# if defined(FEAT_MBYTE) || defined(PROTO)
580 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100581eval_charconvert(
582 char_u *enc_from,
583 char_u *enc_to,
584 char_u *fname_from,
585 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 int err = FALSE;
588
589 set_vim_var_string(VV_CC_FROM, enc_from, -1);
590 set_vim_var_string(VV_CC_TO, enc_to, -1);
591 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
592 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
593 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
594 err = TRUE;
595 set_vim_var_string(VV_CC_FROM, NULL, -1);
596 set_vim_var_string(VV_CC_TO, NULL, -1);
597 set_vim_var_string(VV_FNAME_IN, NULL, -1);
598 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
599
600 if (err)
601 return FAIL;
602 return OK;
603}
604# endif
605
606# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 int err = FALSE;
611
612 set_vim_var_string(VV_FNAME_IN, fname, -1);
613 set_vim_var_string(VV_CMDARG, args, -1);
614 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
615 err = TRUE;
616 set_vim_var_string(VV_FNAME_IN, NULL, -1);
617 set_vim_var_string(VV_CMDARG, NULL, -1);
618
619 if (err)
620 {
621 mch_remove(fname);
622 return FAIL;
623 }
624 return OK;
625}
626# endif
627
628# if defined(FEAT_DIFF) || defined(PROTO)
629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630eval_diff(
631 char_u *origfile,
632 char_u *newfile,
633 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int err = FALSE;
636
637 set_vim_var_string(VV_FNAME_IN, origfile, -1);
638 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
639 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
640 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
641 set_vim_var_string(VV_FNAME_IN, NULL, -1);
642 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
643 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
644}
645
646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100647eval_patch(
648 char_u *origfile,
649 char_u *difffile,
650 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err;
653
654 set_vim_var_string(VV_FNAME_IN, origfile, -1);
655 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
656 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
657 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
660 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
661}
662# endif
663
664/*
665 * Top level evaluation function, returning a boolean.
666 * Sets "error" to TRUE if there was an error.
667 * Return TRUE or FALSE.
668 */
669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100670eval_to_bool(
671 char_u *arg,
672 int *error,
673 char_u **nextcmd,
674 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
Bram Moolenaar33570922005-01-25 22:26:29 +0000676 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 if (skip)
680 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000681 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 else
684 {
685 *error = FALSE;
686 if (!skip)
687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000688 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691 }
692 if (skip)
693 --emsg_skip;
694
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200695 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697
698/*
699 * Top level evaluation function, returning a string. If "skip" is TRUE,
700 * only parsing to "nextcmd" is done, without reporting errors. Return
701 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
702 */
703 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704eval_to_string_skip(
705 char_u *arg,
706 char_u **nextcmd,
707 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 char_u *retval;
711
712 if (skip)
713 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = NULL;
716 else
717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000718 retval = vim_strsave(get_tv_string(&tv));
719 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 }
721 if (skip)
722 --emsg_skip;
723
724 return retval;
725}
726
727/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000728 * Skip over an expression at "*pp".
729 * Return FAIL for an error, OK otherwise.
730 */
731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100732skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000733{
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000735
736 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000737 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000738}
739
740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000742 * When "convert" is TRUE convert a List into a sequence of lines and convert
743 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * Return pointer to allocated memory, or NULL for failure.
745 */
746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100747eval_to_string(
748 char_u *arg,
749 char_u **nextcmd,
750 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
Bram Moolenaar33570922005-01-25 22:26:29 +0000752 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000754 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000755#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000756 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000759 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 retval = NULL;
761 else
762 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000763 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000764 {
765 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000766 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200767 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200768 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200769 if (tv.vval.v_list->lv_len > 0)
770 ga_append(&ga, NL);
771 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000772 ga_append(&ga, NUL);
773 retval = (char_u *)ga.ga_data;
774 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000775#ifdef FEAT_FLOAT
776 else if (convert && tv.v_type == VAR_FLOAT)
777 {
778 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
779 retval = vim_strsave(numbuf);
780 }
781#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000782 else
783 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786
787 return retval;
788}
789
790/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000791 * Call eval_to_string() without using current local variables and using
792 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 */
794 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100795eval_to_string_safe(
796 char_u *arg,
797 char_u **nextcmd,
798 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
800 char_u *retval;
801 void *save_funccalp;
802
803 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000804 if (use_sandbox)
805 ++sandbox;
806 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000807 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000808 if (use_sandbox)
809 --sandbox;
810 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 restore_funccal(save_funccalp);
812 return retval;
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Top level evaluation function, returning a number.
817 * Evaluates "expr" silently.
818 * Returns -1 for an error.
819 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200820 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar33570922005-01-25 22:26:29 +0000823 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200824 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000825 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 ++emsg_off;
828
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 retval = -1;
831 else
832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000833 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836 --emsg_off;
837
838 return retval;
839}
840
Bram Moolenaara40058a2005-07-11 22:42:07 +0000841/*
842 * Prepare v: variable "idx" to be used.
843 * Save the current typeval in "save_tv".
844 * When not used yet add the variable to the v: hashtable.
845 */
846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000848{
849 *save_tv = vimvars[idx].vv_tv;
850 if (vimvars[idx].vv_type == VAR_UNKNOWN)
851 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
852}
853
854/*
855 * Restore v: variable "idx" to typeval "save_tv".
856 * When no longer defined, remove the variable from the v: hashtable.
857 */
858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100859restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000860{
861 hashitem_T *hi;
862
Bram Moolenaara40058a2005-07-11 22:42:07 +0000863 vimvars[idx].vv_tv = *save_tv;
864 if (vimvars[idx].vv_type == VAR_UNKNOWN)
865 {
866 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
867 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100868 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000869 else
870 hash_remove(&vimvarht, hi);
871 }
872}
873
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000874#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000875/*
876 * Evaluate an expression to a list with suggestions.
877 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000878 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000879 */
880 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100881eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000882{
883 typval_T save_val;
884 typval_T rettv;
885 list_T *list = NULL;
886 char_u *p = skipwhite(expr);
887
888 /* Set "v:val" to the bad word. */
889 prepare_vimvar(VV_VAL, &save_val);
890 vimvars[VV_VAL].vv_type = VAR_STRING;
891 vimvars[VV_VAL].vv_str = badword;
892 if (p_verbose == 0)
893 ++emsg_off;
894
895 if (eval1(&p, &rettv, TRUE) == OK)
896 {
897 if (rettv.v_type != VAR_LIST)
898 clear_tv(&rettv);
899 else
900 list = rettv.vval.v_list;
901 }
902
903 if (p_verbose == 0)
904 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000905 restore_vimvar(VV_VAL, &save_val);
906
907 return list;
908}
909
910/*
911 * "list" is supposed to contain two items: a word and a number. Return the
912 * word in "pp" and the number as the return value.
913 * Return -1 if anything isn't right.
914 * Used to get the good word and score from the eval_spell_expr() result.
915 */
916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100917get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000918{
919 listitem_T *li;
920
921 li = list->lv_first;
922 if (li == NULL)
923 return -1;
924 *pp = get_tv_string(&li->li_tv);
925
926 li = li->li_next;
927 if (li == NULL)
928 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200929 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000930}
931#endif
932
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000933/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000934 * Top level evaluation function.
935 * Returns an allocated typval_T with the result.
936 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000937 */
938 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100939eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000940{
941 typval_T *tv;
942
943 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000944 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000945 {
946 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000947 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000948 }
949
950 return tv;
951}
952
953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100955 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000956 * Uses argv[argc] for the function arguments. Only Number and String
957 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000958 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200960 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100961call_vim_function(
962 char_u *func,
963 int argc,
964 char_u **argv,
965 int safe, /* use the sandbox */
966 int str_arg_only, /* all arguments are strings */
967 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200970 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 int len;
972 int i;
973 int doesrange;
974 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000975 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000977 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 for (i = 0; i < argc; i++)
982 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000983 /* Pass a NULL or empty argument as an empty string */
984 if (argv[i] == NULL || *argv[i] == NUL)
985 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000986 argvars[i].v_type = VAR_STRING;
987 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000988 continue;
989 }
990
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200991 if (str_arg_only)
992 len = 0;
993 else
994 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100995 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (len != 0 && len == (int)STRLEN(argv[i]))
997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000998 argvars[i].v_type = VAR_NUMBER;
999 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
1001 else
1002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 argvars[i].v_type = VAR_STRING;
1004 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 }
1007
1008 if (safe)
1009 {
1010 save_funccalp = save_funccal();
1011 ++sandbox;
1012 }
1013
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001014 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001015 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001017 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (safe)
1019 {
1020 --sandbox;
1021 restore_funccal(save_funccalp);
1022 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001023 vim_free(argvars);
1024
1025 if (ret == FAIL)
1026 clear_tv(rettv);
1027
1028 return ret;
1029}
1030
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001031/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001032 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033 * Returns -1 when calling the function fails.
1034 * Uses argv[argc] for the function arguments.
1035 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001036 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037call_func_retnr(
1038 char_u *func,
1039 int argc,
1040 char_u **argv,
1041 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001042{
1043 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001044 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001045
1046 /* All arguments are passed as strings, no conversion to number. */
1047 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1048 return -1;
1049
1050 retval = get_tv_number_chk(&rettv, NULL);
1051 clear_tv(&rettv);
1052 return retval;
1053}
1054
1055#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1056 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1057
Bram Moolenaar4f688582007-07-24 12:34:30 +00001058# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001059/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001060 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001061 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 * Uses argv[argc] for the function arguments.
1063 */
1064 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001065call_func_retstr(
1066 char_u *func,
1067 int argc,
1068 char_u **argv,
1069 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001070{
1071 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001072 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001073
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001074 /* All arguments are passed as strings, no conversion to number. */
1075 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001076 return NULL;
1077
1078 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 return retval;
1081}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001082# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001083
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001084/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001085 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001087 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088 */
1089 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090call_func_retlist(
1091 char_u *func,
1092 int argc,
1093 char_u **argv,
1094 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095{
1096 typval_T rettv;
1097
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001098 /* All arguments are passed as strings, no conversion to number. */
1099 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 return NULL;
1101
1102 if (rettv.v_type != VAR_LIST)
1103 {
1104 clear_tv(&rettv);
1105 return NULL;
1106 }
1107
1108 return rettv.vval.v_list;
1109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110#endif
1111
Bram Moolenaar05159a02005-02-26 23:04:13 +00001112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_FOLDING
1114/*
1115 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1116 * it in "*cp". Doesn't give error messages.
1117 */
1118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001119eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
Bram Moolenaar33570922005-01-25 22:26:29 +00001121 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001122 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001124 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1125 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001128 if (use_sandbox)
1129 ++sandbox;
1130 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 retval = 0;
1134 else
1135 {
1136 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 if (tv.v_type == VAR_NUMBER)
1138 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001139 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 retval = 0;
1141 else
1142 {
1143 /* If the result is a string, check if there is a non-digit before
1144 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (!VIM_ISDIGIT(*s) && *s != '-')
1147 *cp = *s++;
1148 retval = atol((char *)s);
1149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 }
1152 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001153 if (use_sandbox)
1154 --sandbox;
1155 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001157 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158}
1159#endif
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 * ":let" list all variable values
1163 * ":let var1 var2" list variable values
1164 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001165 * ":let var += expr" assignment command.
1166 * ":let var -= expr" assignment command.
1167 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
1170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
1173 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001175 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 int var_count = 0;
1178 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001180 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001181 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
Bram Moolenaardb552d602006-03-23 22:59:57 +00001183 argend = skip_var_list(arg, &var_count, &semicolon);
1184 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001185 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001186 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1187 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001188 expr = skipwhite(argend);
1189 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1190 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001192 /*
1193 * ":let" without "=": list variables
1194 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001195 if (*arg == '[')
1196 EMSG(_(e_invarg));
1197 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001199 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001203 list_glob_vars(&first);
1204 list_buf_vars(&first);
1205 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001206 list_tab_vars(&first);
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/*
1469 * List tab page variables.
1470 */
1471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001473{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001474 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001475 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001476}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477
Bram Moolenaara7043832005-01-21 11:56:39 +00001478/*
1479 * List Vim variables.
1480 */
1481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001484 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485}
1486
1487/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001488 * List script-local variables, if there is a script.
1489 */
1490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001492{
1493 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001494 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1495 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001496}
1497
1498/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 * List variables in "arg".
1500 */
1501 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503{
1504 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001505 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001507 char_u *name_start;
1508 char_u *arg_subsc;
1509 char_u *tofree;
1510 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511
1512 while (!ends_excmd(*arg) && !got_int)
1513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001514 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001515 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001516 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001517 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001518 {
1519 emsg_severe = TRUE;
1520 EMSG(_(e_trailing));
1521 break;
1522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 /* get_name_len() takes care of expanding curly braces */
1527 name_start = name = arg;
1528 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1529 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 /* This is mainly to keep test 49 working: when expanding
1532 * curly braces fails overrule the exception error message. */
1533 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 emsg_severe = TRUE;
1536 EMSG2(_(e_invarg2), arg);
1537 break;
1538 }
1539 error = TRUE;
1540 }
1541 else
1542 {
1543 if (tofree != NULL)
1544 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001545 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 else
1548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001549 /* handle d.key, l[idx], f(expr) */
1550 arg_subsc = arg;
1551 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001552 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001559 case 'g': list_glob_vars(first); break;
1560 case 'b': list_buf_vars(first); break;
1561 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001562 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001563 case 'v': list_vim_vars(first); break;
1564 case 's': list_script_vars(first); break;
1565 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 default:
1567 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001568 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 }
1570 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001571 {
1572 char_u numbuf[NUMBUFLEN];
1573 char_u *tf;
1574 int c;
1575 char_u *s;
1576
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001577 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 c = *arg;
1579 *arg = NUL;
1580 list_one_var_a((char_u *)"",
1581 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001582 tv.v_type,
1583 s == NULL ? (char_u *)"" : s,
1584 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001585 *arg = c;
1586 vim_free(tf);
1587 }
1588 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 }
1591 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592
1593 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595
1596 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 }
1598
1599 return arg;
1600}
1601
1602/*
1603 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1604 * Returns a pointer to the char just after the var name.
1605 * Returns NULL if there is an error.
1606 */
1607 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608ex_let_one(
1609 char_u *arg, /* points to variable name */
1610 typval_T *tv, /* value to assign to variable */
1611 int copy, /* copy value from "tv" */
1612 char_u *endchars, /* valid chars after variable name or NULL */
1613 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614{
1615 int c1;
1616 char_u *name;
1617 char_u *p;
1618 char_u *arg_end = NULL;
1619 int len;
1620 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622
1623 /*
1624 * ":let $VAR = expr": Set environment variable.
1625 */
1626 if (*arg == '$')
1627 {
1628 /* Find the end of the name. */
1629 ++arg;
1630 name = arg;
1631 len = get_env_len(&arg);
1632 if (len == 0)
1633 EMSG2(_(e_invarg2), name - 1);
1634 else
1635 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 if (op != NULL && (*op == '+' || *op == '-'))
1637 EMSG2(_(e_letwrong), op);
1638 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001640 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001641 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 {
1643 c1 = name[len];
1644 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001645 p = get_tv_string_chk(tv);
1646 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 {
1648 int mustfree = FALSE;
1649 char_u *s = vim_getenv(name, &mustfree);
1650
1651 if (s != NULL)
1652 {
1653 p = tofree = concat_str(s, p);
1654 if (mustfree)
1655 vim_free(s);
1656 }
1657 }
1658 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001661 if (STRICMP(name, "HOME") == 0)
1662 init_homedir();
1663 else if (didset_vim && STRICMP(name, "VIM") == 0)
1664 didset_vim = FALSE;
1665 else if (didset_vimruntime
1666 && STRICMP(name, "VIMRUNTIME") == 0)
1667 didset_vimruntime = FALSE;
1668 arg_end = arg;
1669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 }
1673 }
1674 }
1675
1676 /*
1677 * ":let &option = expr": Set option value.
1678 * ":let &l:option = expr": Set local option value.
1679 * ":let &g:option = expr": Set global option value.
1680 */
1681 else if (*arg == '&')
1682 {
1683 /* Find the end of the name. */
1684 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 if (p == NULL || (endchars != NULL
1686 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 EMSG(_(e_letunexp));
1688 else
1689 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 long n;
1691 int opt_type;
1692 long numval;
1693 char_u *stringval = NULL;
1694 char_u *s;
1695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 c1 = *p;
1697 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001699 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001700 s = get_tv_string_chk(tv); /* != NULL if number or string */
1701 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 {
1703 opt_type = get_option_value(arg, &numval,
1704 &stringval, opt_flags);
1705 if ((opt_type == 1 && *op == '.')
1706 || (opt_type == 0 && *op != '.'))
1707 EMSG2(_(e_letwrong), op);
1708 else
1709 {
1710 if (opt_type == 1) /* number */
1711 {
1712 if (*op == '+')
1713 n = numval + n;
1714 else
1715 n = numval - n;
1716 }
1717 else if (opt_type == 0 && stringval != NULL) /* string */
1718 {
1719 s = concat_str(stringval, s);
1720 vim_free(stringval);
1721 stringval = s;
1722 }
1723 }
1724 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001725 if (s != NULL)
1726 {
1727 set_option_value(arg, n, s, opt_flags);
1728 arg_end = p;
1729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 }
1733 }
1734
1735 /*
1736 * ":let @r = expr": Set register contents.
1737 */
1738 else if (*arg == '@')
1739 {
1740 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 if (op != NULL && (*op == '+' || *op == '-'))
1742 EMSG2(_(e_letwrong), op);
1743 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 EMSG(_(e_letunexp));
1746 else
1747 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001748 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 char_u *s;
1750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001751 p = get_tv_string_chk(tv);
1752 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001754 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 if (s != NULL)
1756 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 vim_free(s);
1759 }
1760 }
1761 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001764 arg_end = arg + 1;
1765 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001766 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 }
1768 }
1769
1770 /*
1771 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001772 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001774 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001778 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001779 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001781 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1782 EMSG(_(e_letunexp));
1783 else
1784 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001786 arg_end = p;
1787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001789 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 }
1791
1792 else
1793 EMSG2(_(e_invarg2), arg);
1794
1795 return arg_end;
1796}
1797
1798/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001799 * Get an lval: variable, Dict item or List item that can be assigned a value
1800 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1801 * "name.key", "name.key[expr]" etc.
1802 * Indexing only works if "name" is an existing List or Dictionary.
1803 * "name" points to the start of the name.
1804 * If "rettv" is not NULL it points to the value to be assigned.
1805 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1806 * wrong; must end in space or cmd separator.
1807 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001808 * flags:
1809 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001810 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001811 * GLV_NO_AUTOLOAD: do not use script autoloading
1812 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001813 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001814 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001815 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818get_lval(
1819 char_u *name,
1820 typval_T *rettv,
1821 lval_T *lp,
1822 int unlet,
1823 int skip,
1824 int flags, /* GLV_ values */
1825 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001828 char_u *expr_start, *expr_end;
1829 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001830 dictitem_T *v;
1831 typval_T var1;
1832 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001834 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001835 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001836 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001838 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001842
1843 if (skip)
1844 {
1845 /* When skipping just find the end of the name. */
1846 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001847 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 }
1849
1850 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001851 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 if (expr_start != NULL)
1853 {
1854 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001855 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 && *p != '[' && *p != '.')
1857 {
1858 EMSG(_(e_trailing));
1859 return NULL;
1860 }
1861
1862 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1863 if (lp->ll_exp_name == NULL)
1864 {
1865 /* Report an invalid expression in braces, unless the
1866 * expression evaluation has been cancelled due to an
1867 * aborting error, an interrupt, or an exception. */
1868 if (!aborting() && !quiet)
1869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001870 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001871 EMSG2(_(e_invarg2), name);
1872 return NULL;
1873 }
1874 }
1875 lp->ll_name = lp->ll_exp_name;
1876 }
1877 else
1878 lp->ll_name = name;
1879
1880 /* Without [idx] or .key we are done. */
1881 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1882 return p;
1883
1884 cc = *p;
1885 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001886 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001887 if (v == NULL && !quiet)
1888 EMSG2(_(e_undefvar), lp->ll_name);
1889 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 if (v == NULL)
1891 return NULL;
1892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001893 /*
1894 * Loop until no more [idx] or .key is following.
1895 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001897 var1.v_type = VAR_UNKNOWN;
1898 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1902 && !(lp->ll_tv->v_type == VAR_DICT
1903 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 if (!quiet)
1906 EMSG(_("E689: Can only index a List or Dictionary"));
1907 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 if (!quiet)
1912 EMSG(_("E708: [:] must come last"));
1913 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001915
Bram Moolenaar8c711452005-01-14 21:53:12 +00001916 len = -1;
1917 if (*p == '.')
1918 {
1919 key = p + 1;
1920 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1921 ;
1922 if (len == 0)
1923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 if (!quiet)
1925 EMSG(_(e_emptykey));
1926 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001927 }
1928 p = key + len;
1929 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001930 else
1931 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001932 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001933 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001934 if (*p == ':')
1935 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001936 else
1937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001938 empty1 = FALSE;
1939 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001941 if (get_tv_string_chk(&var1) == NULL)
1942 {
1943 /* not a number or string */
1944 clear_tv(&var1);
1945 return NULL;
1946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001947 }
1948
1949 /* Optionally get the second index [ :expr]. */
1950 if (*p == ':')
1951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001955 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001956 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (rettv != NULL && (rettv->v_type != VAR_LIST
1960 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (!quiet)
1963 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001964 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 }
1967 p = skipwhite(p + 1);
1968 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 else
1971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1974 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001975 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001978 if (get_tv_string_chk(&var2) == NULL)
1979 {
1980 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001981 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 clear_tv(&var2);
1983 return NULL;
1984 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001985 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001990
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 if (!quiet)
1994 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001995 clear_tv(&var1);
1996 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 }
1999
2000 /* Skip to past ']'. */
2001 ++p;
2002 }
2003
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 {
2006 if (len == -1)
2007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002009 key = get_tv_string_chk(&var1); /* is number or string */
2010 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002011 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 }
2015 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016 lp->ll_list = NULL;
2017 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002018 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002019
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002020 /* When assigning to a scope dictionary check that a function and
2021 * variable name is valid (only variable name unless it is l: or
2022 * g: dictionary). Disallow overwriting a builtin function. */
2023 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002024 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002025 int prevval;
2026 int wrong;
2027
2028 if (len != -1)
2029 {
2030 prevval = key[len];
2031 key[len] = NUL;
2032 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002033 else
2034 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002035 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2036 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002037 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002038 || !valid_varname(key);
2039 if (len != -1)
2040 key[len] = prevval;
2041 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002042 return NULL;
2043 }
2044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047 /* Can't add "v:" variable. */
2048 if (lp->ll_dict == &vimvardict)
2049 {
2050 EMSG2(_(e_illvar), name);
2051 return NULL;
2052 }
2053
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002054 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002055 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002058 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002059 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 }
2062 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002066 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002068 p = NULL;
2069 break;
2070 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002071 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002072 else if ((flags & GLV_READ_ONLY) == 0
2073 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002074 {
2075 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002076 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002077 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002078
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002079 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002081 }
2082 else
2083 {
2084 /*
2085 * Get the number and item for the only or first index of the List.
2086 */
2087 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002090 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002091 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002092 clear_tv(&var1);
2093
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 lp->ll_list = lp->ll_tv->vval.v_list;
2096 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2097 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002098 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002099 if (lp->ll_n1 < 0)
2100 {
2101 lp->ll_n1 = 0;
2102 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2103 }
2104 }
2105 if (lp->ll_li == NULL)
2106 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002107 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002108 if (!quiet)
2109 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002111 }
2112
2113 /*
2114 * May need to find the item or absolute index for the second
2115 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 * When no index given: "lp->ll_empty2" is TRUE.
2117 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002118 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002121 lp->ll_n2 = (long)get_tv_number(&var2);
2122 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002123 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002127 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002128 {
2129 if (!quiet)
2130 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002132 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 }
2135
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2137 if (lp->ll_n1 < 0)
2138 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2139 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002140 {
2141 if (!quiet)
2142 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002144 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002145 }
2146
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 }
2150
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002151 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 return p;
2153}
2154
2155/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002156 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002159clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160{
2161 vim_free(lp->ll_exp_name);
2162 vim_free(lp->ll_newkey);
2163}
2164
2165/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002166 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002168 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 */
2170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171set_var_lval(
2172 lval_T *lp,
2173 char_u *endp,
2174 typval_T *rettv,
2175 int copy,
2176 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177{
2178 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 listitem_T *ri;
2180 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181
2182 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002184 cc = *endp;
2185 *endp = NUL;
2186 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002188 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189
Bram Moolenaar79518e22017-02-17 16:31:35 +01002190 /* handle +=, -= and .= */
2191 di = NULL;
2192 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2193 &tv, &di, TRUE, FALSE) == OK)
2194 {
2195 if ((di == NULL
2196 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2197 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2198 FALSE)))
2199 && tv_op(&tv, rettv, op) == OK)
2200 set_var(lp->ll_name, &tv, FALSE);
2201 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002204 else
2205 set_var(lp->ll_name, rettv, copy);
2206 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002208 else if (tv_check_lock(lp->ll_newkey == NULL
2209 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002210 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002211 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 else if (lp->ll_range)
2213 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002214 listitem_T *ll_li = lp->ll_li;
2215 int ll_n1 = lp->ll_n1;
2216
2217 /*
2218 * Check whether any of the list items is locked
2219 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002220 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002221 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002222 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002223 return;
2224 ri = ri->li_next;
2225 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2226 break;
2227 ll_li = ll_li->li_next;
2228 ++ll_n1;
2229 }
2230
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 /*
2232 * Assign the List values to the list items.
2233 */
2234 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236 if (op != NULL && *op != '=')
2237 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2238 else
2239 {
2240 clear_tv(&lp->ll_li->li_tv);
2241 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2242 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 ri = ri->li_next;
2244 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2245 break;
2246 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002247 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002249 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 ri = NULL;
2252 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002253 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002254 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 lp->ll_li = lp->ll_li->li_next;
2256 ++lp->ll_n1;
2257 }
2258 if (ri != NULL)
2259 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002260 else if (lp->ll_empty2
2261 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 : lp->ll_n1 != lp->ll_n2)
2263 EMSG(_("E711: List value has not enough items"));
2264 }
2265 else
2266 {
2267 /*
2268 * Assign to a List or Dictionary item.
2269 */
2270 if (lp->ll_newkey != NULL)
2271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 if (op != NULL && *op != '=')
2273 {
2274 EMSG2(_(e_letwrong), op);
2275 return;
2276 }
2277
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002279 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 if (di == NULL)
2281 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002282 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2283 {
2284 vim_free(di);
2285 return;
2286 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002288 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289 else if (op != NULL && *op != '=')
2290 {
2291 tv_op(lp->ll_tv, rettv, op);
2292 return;
2293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002296
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 /*
2298 * Assign the value to the variable or list item.
2299 */
2300 if (copy)
2301 copy_tv(rettv, lp->ll_tv);
2302 else
2303 {
2304 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002305 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309}
2310
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002311/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2313 * Returns OK or FAIL.
2314 */
2315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002316tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002318 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 char_u numbuf[NUMBUFLEN];
2320 char_u *s;
2321
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002322 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2323 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2324 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 {
2326 switch (tv1->v_type)
2327 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002328 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 case VAR_DICT:
2330 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002331 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002332 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002333 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002334 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 break;
2336
2337 case VAR_LIST:
2338 if (*op != '+' || tv2->v_type != VAR_LIST)
2339 break;
2340 /* List += List */
2341 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2342 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2343 return OK;
2344
2345 case VAR_NUMBER:
2346 case VAR_STRING:
2347 if (tv2->v_type == VAR_LIST)
2348 break;
2349 if (*op == '+' || *op == '-')
2350 {
2351 /* nr += nr or nr -= nr*/
2352 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002353#ifdef FEAT_FLOAT
2354 if (tv2->v_type == VAR_FLOAT)
2355 {
2356 float_T f = n;
2357
2358 if (*op == '+')
2359 f += tv2->vval.v_float;
2360 else
2361 f -= tv2->vval.v_float;
2362 clear_tv(tv1);
2363 tv1->v_type = VAR_FLOAT;
2364 tv1->vval.v_float = f;
2365 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002367#endif
2368 {
2369 if (*op == '+')
2370 n += get_tv_number(tv2);
2371 else
2372 n -= get_tv_number(tv2);
2373 clear_tv(tv1);
2374 tv1->v_type = VAR_NUMBER;
2375 tv1->vval.v_number = n;
2376 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 }
2378 else
2379 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002380 if (tv2->v_type == VAR_FLOAT)
2381 break;
2382
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 /* str .= str */
2384 s = get_tv_string(tv1);
2385 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2386 clear_tv(tv1);
2387 tv1->v_type = VAR_STRING;
2388 tv1->vval.v_string = s;
2389 }
2390 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002391
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002392 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002393#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002394 {
2395 float_T f;
2396
2397 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2398 && tv2->v_type != VAR_NUMBER
2399 && tv2->v_type != VAR_STRING))
2400 break;
2401 if (tv2->v_type == VAR_FLOAT)
2402 f = tv2->vval.v_float;
2403 else
2404 f = get_tv_number(tv2);
2405 if (*op == '+')
2406 tv1->vval.v_float += f;
2407 else
2408 tv1->vval.v_float -= f;
2409 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002410#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002411 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 }
2413 }
2414
2415 EMSG2(_(e_letwrong), op);
2416 return FAIL;
2417}
2418
2419/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002420 * Evaluate the expression used in a ":for var in expr" command.
2421 * "arg" points to "var".
2422 * Set "*errp" to TRUE for an error, FALSE otherwise;
2423 * Return a pointer that holds the info. Null when there is an error.
2424 */
2425 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002426eval_for_line(
2427 char_u *arg,
2428 int *errp,
2429 char_u **nextcmdp,
2430 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431{
Bram Moolenaar33570922005-01-25 22:26:29 +00002432 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002434 typval_T tv;
2435 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002436
2437 *errp = TRUE; /* default: there is an error */
2438
Bram Moolenaar33570922005-01-25 22:26:29 +00002439 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440 if (fi == NULL)
2441 return NULL;
2442
2443 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2444 if (expr == NULL)
2445 return fi;
2446
2447 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002448 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002450 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 return fi;
2452 }
2453
2454 if (skip)
2455 ++emsg_skip;
2456 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2457 {
2458 *errp = FALSE;
2459 if (!skip)
2460 {
2461 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002462 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002463 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002465 clear_tv(&tv);
2466 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002467 else if (l == NULL)
2468 {
2469 /* a null list is like an empty list: do nothing */
2470 clear_tv(&tv);
2471 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472 else
2473 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002474 /* No need to increment the refcount, it's already set for the
2475 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002476 fi->fi_list = l;
2477 list_add_watch(l, &fi->fi_lw);
2478 fi->fi_lw.lw_item = l->lv_first;
2479 }
2480 }
2481 }
2482 if (skip)
2483 --emsg_skip;
2484
2485 return fi;
2486}
2487
2488/*
2489 * Use the first item in a ":for" list. Advance to the next.
2490 * Assign the values to the variable (list). "arg" points to the first one.
2491 * Return TRUE when a valid item was found, FALSE when at end of list or
2492 * something wrong.
2493 */
2494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002495next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002496{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002497 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002498 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002499 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002500
2501 item = fi->fi_lw.lw_item;
2502 if (item == NULL)
2503 result = FALSE;
2504 else
2505 {
2506 fi->fi_lw.lw_item = item->li_next;
2507 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2508 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2509 }
2510 return result;
2511}
2512
2513/*
2514 * Free the structure used to store info used by ":for".
2515 */
2516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002517free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002518{
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002520
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002521 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002522 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002524 list_unref(fi->fi_list);
2525 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526 vim_free(fi);
2527}
2528
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2530
2531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002532set_context_for_expression(
2533 expand_T *xp,
2534 char_u *arg,
2535 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 int got_eq = FALSE;
2538 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002539 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002541 if (cmdidx == CMD_let)
2542 {
2543 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002544 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545 {
2546 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002547 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002548 {
2549 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002550 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002551 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 break;
2553 }
2554 return;
2555 }
2556 }
2557 else
2558 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2559 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 while ((xp->xp_pattern = vim_strpbrk(arg,
2561 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2562 {
2563 c = *xp->xp_pattern;
2564 if (c == '&')
2565 {
2566 c = xp->xp_pattern[1];
2567 if (c == '&')
2568 {
2569 ++xp->xp_pattern;
2570 xp->xp_context = cmdidx != CMD_let || got_eq
2571 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2572 }
2573 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002576 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2577 xp->xp_pattern += 2;
2578
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 else if (c == '$')
2582 {
2583 /* environment variable */
2584 xp->xp_context = EXPAND_ENV_VARS;
2585 }
2586 else if (c == '=')
2587 {
2588 got_eq = TRUE;
2589 xp->xp_context = EXPAND_EXPRESSION;
2590 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002591 else if (c == '#'
2592 && xp->xp_context == EXPAND_EXPRESSION)
2593 {
2594 /* Autoload function/variable contains '#'. */
2595 break;
2596 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002597 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 && xp->xp_context == EXPAND_FUNCTIONS
2599 && vim_strchr(xp->xp_pattern, '(') == NULL)
2600 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002601 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 break;
2603 }
2604 else if (cmdidx != CMD_let || got_eq)
2605 {
2606 if (c == '"') /* string */
2607 {
2608 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2609 if (c == '\\' && xp->xp_pattern[1] != NUL)
2610 ++xp->xp_pattern;
2611 xp->xp_context = EXPAND_NOTHING;
2612 }
2613 else if (c == '\'') /* literal string */
2614 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2617 /* skip */ ;
2618 xp->xp_context = EXPAND_NOTHING;
2619 }
2620 else if (c == '|')
2621 {
2622 if (xp->xp_pattern[1] == '|')
2623 {
2624 ++xp->xp_pattern;
2625 xp->xp_context = EXPAND_EXPRESSION;
2626 }
2627 else
2628 xp->xp_context = EXPAND_COMMANDS;
2629 }
2630 else
2631 xp->xp_context = EXPAND_EXPRESSION;
2632 }
2633 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002634 /* Doesn't look like something valid, expand as an expression
2635 * anyway. */
2636 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 arg = xp->xp_pattern;
2638 if (*arg != NUL)
2639 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2640 /* skip */ ;
2641 }
2642 xp->xp_pattern = arg;
2643}
2644
2645#endif /* FEAT_CMDL_COMPL */
2646
2647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 * ":unlet[!] var1 ... " command.
2649 */
2650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002651ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002653 ex_unletlock(eap, eap->arg, 0);
2654}
2655
2656/*
2657 * ":lockvar" and ":unlockvar" commands
2658 */
2659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002660ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002661{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002663 int deep = 2;
2664
2665 if (eap->forceit)
2666 deep = -1;
2667 else if (vim_isdigit(*arg))
2668 {
2669 deep = getdigits(&arg);
2670 arg = skipwhite(arg);
2671 }
2672
2673 ex_unletlock(eap, arg, deep);
2674}
2675
2676/*
2677 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2678 */
2679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680ex_unletlock(
2681 exarg_T *eap,
2682 char_u *argstart,
2683 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002684{
2685 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 do
2691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002693 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002694 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lv.ll_name == NULL)
2696 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002697 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (name_end != NULL)
2701 {
2702 emsg_severe = TRUE;
2703 EMSG(_(e_trailing));
2704 }
2705 if (!(eap->skip || error))
2706 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 break;
2708 }
2709
2710 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002711 {
2712 if (eap->cmdidx == CMD_unlet)
2713 {
2714 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2715 error = TRUE;
2716 }
2717 else
2718 {
2719 if (do_lock_var(&lv, name_end, deep,
2720 eap->cmdidx == CMD_lockvar) == FAIL)
2721 error = TRUE;
2722 }
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!eap->skip)
2726 clear_lval(&lv);
2727
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 arg = skipwhite(name_end);
2729 } while (!ends_excmd(*arg));
2730
2731 eap->nextcmd = check_nextcmd(arg);
2732}
2733
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002735do_unlet_var(
2736 lval_T *lp,
2737 char_u *name_end,
2738 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 int ret = OK;
2741 int cc;
2742
2743 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 cc = *name_end;
2746 *name_end = NUL;
2747
2748 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002749 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002753 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002754 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002755 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002756 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002757 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 else if (lp->ll_range)
2759 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002760 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002761 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002762 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002763
2764 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2765 {
2766 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002767 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002768 return FAIL;
2769 ll_li = li;
2770 ++ll_n1;
2771 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772
2773 /* Delete a range of List items. */
2774 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2775 {
2776 li = lp->ll_li->li_next;
2777 listitem_remove(lp->ll_list, lp->ll_li);
2778 lp->ll_li = li;
2779 ++lp->ll_n1;
2780 }
2781 }
2782 else
2783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 /* unlet a List item. */
2786 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002789 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 }
2791
2792 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793}
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795/*
2796 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002797 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 */
2799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002800do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 hashtab_T *ht;
2803 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002804 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002805 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002806 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
Bram Moolenaar33570922005-01-25 22:26:29 +00002808 ht = find_var_ht(name, &varname);
2809 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002812 if (d == NULL)
2813 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 if (ht == &globvarht)
2815 d = &globvardict;
2816 else if (ht == &compat_hashtab)
2817 d = &vimvardict;
2818 else
2819 {
2820 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2821 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2822 }
2823 if (d == NULL)
2824 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002825 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826 return FAIL;
2827 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002828 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002830 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002831 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002832 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002833 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002834 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002835 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002836 || var_check_ro(di->di_flags, name, FALSE)
2837 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002838 return FAIL;
2839
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002840 delete_var(ht, hi);
2841 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 if (forceit)
2845 return OK;
2846 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 return FAIL;
2848}
2849
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002850/*
2851 * Lock or unlock variable indicated by "lp".
2852 * "deep" is the levels to go (-1 for unlimited);
2853 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2854 */
2855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002856do_lock_var(
2857 lval_T *lp,
2858 char_u *name_end,
2859 int deep,
2860 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002861{
2862 int ret = OK;
2863 int cc;
2864 dictitem_T *di;
2865
2866 if (deep == 0) /* nothing to do */
2867 return OK;
2868
2869 if (lp->ll_tv == NULL)
2870 {
2871 cc = *name_end;
2872 *name_end = NUL;
2873
2874 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002875 di = find_var(lp->ll_name, NULL, TRUE);
2876 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002877 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002878 else if ((di->di_flags & DI_FLAGS_FIX)
2879 && di->di_tv.v_type != VAR_DICT
2880 && di->di_tv.v_type != VAR_LIST)
2881 /* For historic reasons this error is not given for a list or dict.
2882 * E.g., the b: dict could be locked/unlocked. */
2883 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002884 else
2885 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002886 if (lock)
2887 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002888 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002889 di->di_flags &= ~DI_FLAGS_LOCK;
2890 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002891 }
2892 *name_end = cc;
2893 }
2894 else if (lp->ll_range)
2895 {
2896 listitem_T *li = lp->ll_li;
2897
2898 /* (un)lock a range of List items. */
2899 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2900 {
2901 item_lock(&li->li_tv, deep, lock);
2902 li = li->li_next;
2903 ++lp->ll_n1;
2904 }
2905 }
2906 else if (lp->ll_list != NULL)
2907 /* (un)lock a List item. */
2908 item_lock(&lp->ll_li->li_tv, deep, lock);
2909 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002910 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 item_lock(&lp->ll_di->di_tv, deep, lock);
2912
2913 return ret;
2914}
2915
2916/*
2917 * Lock or unlock an item. "deep" is nr of levels to go.
2918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921{
2922 static int recurse = 0;
2923 list_T *l;
2924 listitem_T *li;
2925 dict_T *d;
2926 hashitem_T *hi;
2927 int todo;
2928
2929 if (recurse >= DICT_MAXNEST)
2930 {
2931 EMSG(_("E743: variable nested too deep for (un)lock"));
2932 return;
2933 }
2934 if (deep == 0)
2935 return;
2936 ++recurse;
2937
2938 /* lock/unlock the item itself */
2939 if (lock)
2940 tv->v_lock |= VAR_LOCKED;
2941 else
2942 tv->v_lock &= ~VAR_LOCKED;
2943
2944 switch (tv->v_type)
2945 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002946 case VAR_UNKNOWN:
2947 case VAR_NUMBER:
2948 case VAR_STRING:
2949 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002950 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002951 case VAR_FLOAT:
2952 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002953 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002954 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 break;
2956
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002957 case VAR_LIST:
2958 if ((l = tv->vval.v_list) != NULL)
2959 {
2960 if (lock)
2961 l->lv_lock |= VAR_LOCKED;
2962 else
2963 l->lv_lock &= ~VAR_LOCKED;
2964 if (deep < 0 || deep > 1)
2965 /* recursive: lock/unlock the items the List contains */
2966 for (li = l->lv_first; li != NULL; li = li->li_next)
2967 item_lock(&li->li_tv, deep - 1, lock);
2968 }
2969 break;
2970 case VAR_DICT:
2971 if ((d = tv->vval.v_dict) != NULL)
2972 {
2973 if (lock)
2974 d->dv_lock |= VAR_LOCKED;
2975 else
2976 d->dv_lock &= ~VAR_LOCKED;
2977 if (deep < 0 || deep > 1)
2978 {
2979 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002980 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002981 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2982 {
2983 if (!HASHITEM_EMPTY(hi))
2984 {
2985 --todo;
2986 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2987 }
2988 }
2989 }
2990 }
2991 }
2992 --recurse;
2993}
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2996/*
2997 * Delete all "menutrans_" variables.
2998 */
2999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003003 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003006 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003007 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003008 {
3009 if (!HASHITEM_EMPTY(hi))
3010 {
3011 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3013 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003014 }
3015 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017}
3018#endif
3019
3020#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3021
3022/*
3023 * Local string buffer for the next two functions to store a variable name
3024 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3025 * get_user_var_name().
3026 */
3027
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003028static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030static char_u *varnamebuf = NULL;
3031static int varnamebuflen = 0;
3032
3033/*
3034 * Function to concatenate a prefix and a variable name.
3035 */
3036 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003037cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
3039 int len;
3040
3041 len = (int)STRLEN(name) + 3;
3042 if (len > varnamebuflen)
3043 {
3044 vim_free(varnamebuf);
3045 len += 10; /* some additional space */
3046 varnamebuf = alloc(len);
3047 if (varnamebuf == NULL)
3048 {
3049 varnamebuflen = 0;
3050 return NULL;
3051 }
3052 varnamebuflen = len;
3053 }
3054 *varnamebuf = prefix;
3055 varnamebuf[1] = ':';
3056 STRCPY(varnamebuf + 2, name);
3057 return varnamebuf;
3058}
3059
3060/*
3061 * Function given to ExpandGeneric() to obtain the list of user defined
3062 * (global/buffer/window/built-in) variable names.
3063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003065get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003067 static long_u gdone;
3068 static long_u bdone;
3069 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003070 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003071 static int vidx;
3072 static hashitem_T *hi;
3073 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
3075 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003077 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003078 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003080
3081 /* Global variables */
3082 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003084 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003086 else
3087 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003088 while (HASHITEM_EMPTY(hi))
3089 ++hi;
3090 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3091 return cat_prefix_varname('g', hi->hi_key);
3092 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003094
3095 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003096 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003097 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003099 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003100 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003101 else
3102 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003103 while (HASHITEM_EMPTY(hi))
3104 ++hi;
3105 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003107
3108 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003109 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003112 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003114 else
3115 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003116 while (HASHITEM_EMPTY(hi))
3117 ++hi;
3118 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003120
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003121 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003122 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003123 if (tdone < ht->ht_used)
3124 {
3125 if (tdone++ == 0)
3126 hi = ht->ht_array;
3127 else
3128 ++hi;
3129 while (HASHITEM_EMPTY(hi))
3130 ++hi;
3131 return cat_prefix_varname('t', hi->hi_key);
3132 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003133
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 /* v: variables */
3135 if (vidx < VV_LEN)
3136 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 vim_free(varnamebuf);
3139 varnamebuf = NULL;
3140 varnamebuflen = 0;
3141 return NULL;
3142}
3143
3144#endif /* FEAT_CMDL_COMPL */
3145
3146/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003147 * Return TRUE if "pat" matches "text".
3148 * Does not use 'cpo' and always uses 'magic'.
3149 */
3150 static int
3151pattern_match(char_u *pat, char_u *text, int ic)
3152{
3153 int matches = FALSE;
3154 char_u *save_cpo;
3155 regmatch_T regmatch;
3156
3157 /* avoid 'l' flag in 'cpoptions' */
3158 save_cpo = p_cpo;
3159 p_cpo = (char_u *)"";
3160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3161 if (regmatch.regprog != NULL)
3162 {
3163 regmatch.rm_ic = ic;
3164 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3165 vim_regfree(regmatch.regprog);
3166 }
3167 p_cpo = save_cpo;
3168 return matches;
3169}
3170
3171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 * types for expressions.
3173 */
3174typedef enum
3175{
3176 TYPE_UNKNOWN = 0
3177 , TYPE_EQUAL /* == */
3178 , TYPE_NEQUAL /* != */
3179 , TYPE_GREATER /* > */
3180 , TYPE_GEQUAL /* >= */
3181 , TYPE_SMALLER /* < */
3182 , TYPE_SEQUAL /* <= */
3183 , TYPE_MATCH /* =~ */
3184 , TYPE_NOMATCH /* !~ */
3185} exptype_T;
3186
3187/*
3188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3191 */
3192
3193/*
3194 * Handle zero level expression.
3195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * Return OK or FAIL.
3199 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201eval0(
3202 char_u *arg,
3203 typval_T *rettv,
3204 char_u **nextcmd,
3205 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 int ret;
3208 char_u *p;
3209
3210 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003211 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (ret == FAIL || !ends_excmd(*p))
3213 {
3214 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 /*
3217 * Report the invalid expression unless the expression evaluation has
3218 * been cancelled due to an aborting error, an interrupt, or an
3219 * exception.
3220 */
3221 if (!aborting())
3222 EMSG2(_(e_invexpr2), arg);
3223 ret = FAIL;
3224 }
3225 if (nextcmd != NULL)
3226 *nextcmd = check_nextcmd(p);
3227
3228 return ret;
3229}
3230
3231/*
3232 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003233 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 *
3235 * "arg" must point to the first non-white of the expression.
3236 * "arg" is advanced to the next non-white after the recognized expression.
3237 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003238 * Note: "rettv.v_lock" is not set.
3239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * Return OK or FAIL.
3241 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
3248 /*
3249 * Get the first variable.
3250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003251 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 return FAIL;
3253
3254 if ((*arg)[0] == '?')
3255 {
3256 result = FALSE;
3257 if (evaluate)
3258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003259 int error = FALSE;
3260
3261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003264 if (error)
3265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267
3268 /*
3269 * Get the second variable.
3270 */
3271 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 return FAIL;
3274
3275 /*
3276 * Check for the ":".
3277 */
3278 if ((*arg)[0] != ':')
3279 {
3280 EMSG(_("E109: Missing ':' after '?'"));
3281 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 return FAIL;
3284 }
3285
3286 /*
3287 * Get the third variable.
3288 */
3289 *arg = skipwhite(*arg + 1);
3290 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3291 {
3292 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 return FAIL;
3295 }
3296 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299
3300 return OK;
3301}
3302
3303/*
3304 * Handle first level expression:
3305 * expr2 || expr2 || expr2 logical OR
3306 *
3307 * "arg" must point to the first non-white of the expression.
3308 * "arg" is advanced to the next non-white after the recognized expression.
3309 *
3310 * Return OK or FAIL.
3311 */
3312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 long result;
3317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
3320 /*
3321 * Get the first variable.
3322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003323 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 return FAIL;
3325
3326 /*
3327 * Repeat until there is no following "||".
3328 */
3329 first = TRUE;
3330 result = FALSE;
3331 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3332 {
3333 if (evaluate && first)
3334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 if (error)
3339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 first = FALSE;
3341 }
3342
3343 /*
3344 * Get the second variable.
3345 */
3346 *arg = skipwhite(*arg + 2);
3347 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3348 return FAIL;
3349
3350 /*
3351 * Compute the result.
3352 */
3353 if (evaluate && !result)
3354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 if (error)
3359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 if (evaluate)
3362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 rettv->v_type = VAR_NUMBER;
3364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 }
3367
3368 return OK;
3369}
3370
3371/*
3372 * Handle second level expression:
3373 * expr3 && expr3 && expr3 logical AND
3374 *
3375 * "arg" must point to the first non-white of the expression.
3376 * "arg" is advanced to the next non-white after the recognized expression.
3377 *
3378 * Return OK or FAIL.
3379 */
3380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 long result;
3385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * Get the first variable.
3390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return FAIL;
3393
3394 /*
3395 * Repeat until there is no following "&&".
3396 */
3397 first = TRUE;
3398 result = TRUE;
3399 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3400 {
3401 if (evaluate && first)
3402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 if (error)
3407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 first = FALSE;
3409 }
3410
3411 /*
3412 * Get the second variable.
3413 */
3414 *arg = skipwhite(*arg + 2);
3415 if (eval4(arg, &var2, evaluate && result) == FAIL)
3416 return FAIL;
3417
3418 /*
3419 * Compute the result.
3420 */
3421 if (evaluate && result)
3422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003423 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 if (error)
3427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429 if (evaluate)
3430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 rettv->v_type = VAR_NUMBER;
3432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 }
3435
3436 return OK;
3437}
3438
3439/*
3440 * Handle third level expression:
3441 * var1 == var2
3442 * var1 =~ var2
3443 * var1 != var2
3444 * var1 !~ var2
3445 * var1 > var2
3446 * var1 >= var2
3447 * var1 < var2
3448 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003449 * var1 is var2
3450 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 *
3452 * "arg" must point to the first non-white of the expression.
3453 * "arg" is advanced to the next non-white after the recognized expression.
3454 *
3455 * Return OK or FAIL.
3456 */
3457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003458eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 char_u *p;
3462 int i;
3463 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003464 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003466 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 char_u *s1, *s2;
3468 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 /*
3472 * Get the first variable.
3473 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003474 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return FAIL;
3476
3477 p = *arg;
3478 switch (p[0])
3479 {
3480 case '=': if (p[1] == '=')
3481 type = TYPE_EQUAL;
3482 else if (p[1] == '~')
3483 type = TYPE_MATCH;
3484 break;
3485 case '!': if (p[1] == '=')
3486 type = TYPE_NEQUAL;
3487 else if (p[1] == '~')
3488 type = TYPE_NOMATCH;
3489 break;
3490 case '>': if (p[1] != '=')
3491 {
3492 type = TYPE_GREATER;
3493 len = 1;
3494 }
3495 else
3496 type = TYPE_GEQUAL;
3497 break;
3498 case '<': if (p[1] != '=')
3499 {
3500 type = TYPE_SMALLER;
3501 len = 1;
3502 }
3503 else
3504 type = TYPE_SEQUAL;
3505 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003506 case 'i': if (p[1] == 's')
3507 {
3508 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3509 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003510 i = p[len];
3511 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003512 {
3513 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3514 type_is = TRUE;
3515 }
3516 }
3517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
3520 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 */
3523 if (type != TYPE_UNKNOWN)
3524 {
3525 /* extra question mark appended: ignore case */
3526 if (p[len] == '?')
3527 {
3528 ic = TRUE;
3529 ++len;
3530 }
3531 /* extra '#' appended: match case */
3532 else if (p[len] == '#')
3533 {
3534 ic = FALSE;
3535 ++len;
3536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 else
3539 ic = p_ic;
3540
3541 /*
3542 * Get the second variable.
3543 */
3544 *arg = skipwhite(p + len);
3545 if (eval5(arg, &var2, evaluate) == FAIL)
3546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return FAIL;
3549 }
3550
3551 if (evaluate)
3552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003553 if (type_is && rettv->v_type != var2.v_type)
3554 {
3555 /* For "is" a different type always means FALSE, for "notis"
3556 * it means TRUE. */
3557 n1 = (type == TYPE_NEQUAL);
3558 }
3559 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3560 {
3561 if (type_is)
3562 {
3563 n1 = (rettv->v_type == var2.v_type
3564 && rettv->vval.v_list == var2.vval.v_list);
3565 if (type == TYPE_NEQUAL)
3566 n1 = !n1;
3567 }
3568 else if (rettv->v_type != var2.v_type
3569 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3570 {
3571 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003572 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003573 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003574 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003575 clear_tv(rettv);
3576 clear_tv(&var2);
3577 return FAIL;
3578 }
3579 else
3580 {
3581 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003582 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3583 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003584 if (type == TYPE_NEQUAL)
3585 n1 = !n1;
3586 }
3587 }
3588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003589 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3590 {
3591 if (type_is)
3592 {
3593 n1 = (rettv->v_type == var2.v_type
3594 && rettv->vval.v_dict == var2.vval.v_dict);
3595 if (type == TYPE_NEQUAL)
3596 n1 = !n1;
3597 }
3598 else if (rettv->v_type != var2.v_type
3599 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3600 {
3601 if (rettv->v_type != var2.v_type)
3602 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3603 else
3604 EMSG(_("E736: Invalid operation for Dictionary"));
3605 clear_tv(rettv);
3606 clear_tv(&var2);
3607 return FAIL;
3608 }
3609 else
3610 {
3611 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003612 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3613 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003614 if (type == TYPE_NEQUAL)
3615 n1 = !n1;
3616 }
3617 }
3618
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003619 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3620 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003621 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003622 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003624 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003625 clear_tv(rettv);
3626 clear_tv(&var2);
3627 return FAIL;
3628 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003629 if ((rettv->v_type == VAR_PARTIAL
3630 && rettv->vval.v_partial == NULL)
3631 || (var2.v_type == VAR_PARTIAL
3632 && var2.vval.v_partial == NULL))
3633 /* when a partial is NULL assume not equal */
3634 n1 = FALSE;
3635 else if (type_is)
3636 {
3637 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3638 /* strings are considered the same if their value is
3639 * the same */
3640 n1 = tv_equal(rettv, &var2, ic, FALSE);
3641 else if (rettv->v_type == VAR_PARTIAL
3642 && var2.v_type == VAR_PARTIAL)
3643 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3644 else
3645 n1 = FALSE;
3646 }
3647 else
3648 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003649 if (type == TYPE_NEQUAL)
3650 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003651 }
3652
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003653#ifdef FEAT_FLOAT
3654 /*
3655 * If one of the two variables is a float, compare as a float.
3656 * When using "=~" or "!~", always compare as string.
3657 */
3658 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3659 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3660 {
3661 float_T f1, f2;
3662
3663 if (rettv->v_type == VAR_FLOAT)
3664 f1 = rettv->vval.v_float;
3665 else
3666 f1 = get_tv_number(rettv);
3667 if (var2.v_type == VAR_FLOAT)
3668 f2 = var2.vval.v_float;
3669 else
3670 f2 = get_tv_number(&var2);
3671 n1 = FALSE;
3672 switch (type)
3673 {
3674 case TYPE_EQUAL: n1 = (f1 == f2); break;
3675 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3676 case TYPE_GREATER: n1 = (f1 > f2); break;
3677 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3678 case TYPE_SMALLER: n1 = (f1 < f2); break;
3679 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3680 case TYPE_UNKNOWN:
3681 case TYPE_MATCH:
3682 case TYPE_NOMATCH: break; /* avoid gcc warning */
3683 }
3684 }
3685#endif
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 /*
3688 * If one of the two variables is a number, compare as a number.
3689 * When using "=~" or "!~", always compare as string.
3690 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003691 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 n1 = get_tv_number(rettv);
3695 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 switch (type)
3697 {
3698 case TYPE_EQUAL: n1 = (n1 == n2); break;
3699 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3700 case TYPE_GREATER: n1 = (n1 > n2); break;
3701 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3702 case TYPE_SMALLER: n1 = (n1 < n2); break;
3703 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3704 case TYPE_UNKNOWN:
3705 case TYPE_MATCH:
3706 case TYPE_NOMATCH: break; /* avoid gcc warning */
3707 }
3708 }
3709 else
3710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 s1 = get_tv_string_buf(rettv, buf1);
3712 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3714 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3715 else
3716 i = 0;
3717 n1 = FALSE;
3718 switch (type)
3719 {
3720 case TYPE_EQUAL: n1 = (i == 0); break;
3721 case TYPE_NEQUAL: n1 = (i != 0); break;
3722 case TYPE_GREATER: n1 = (i > 0); break;
3723 case TYPE_GEQUAL: n1 = (i >= 0); break;
3724 case TYPE_SMALLER: n1 = (i < 0); break;
3725 case TYPE_SEQUAL: n1 = (i <= 0); break;
3726
3727 case TYPE_MATCH:
3728 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003729 n1 = pattern_match(s2, s1, ic);
3730 if (type == TYPE_NOMATCH)
3731 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 break;
3733
3734 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3735 }
3736 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 clear_tv(rettv);
3738 clear_tv(&var2);
3739 rettv->v_type = VAR_NUMBER;
3740 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 }
3743
3744 return OK;
3745}
3746
3747/*
3748 * Handle fourth level expression:
3749 * + number addition
3750 * - number subtraction
3751 * . string concatenation
3752 *
3753 * "arg" must point to the first non-white of the expression.
3754 * "arg" is advanced to the next non-white after the recognized expression.
3755 *
3756 * Return OK or FAIL.
3757 */
3758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003759eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
Bram Moolenaar33570922005-01-25 22:26:29 +00003761 typval_T var2;
3762 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003764 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003765#ifdef FEAT_FLOAT
3766 float_T f1 = 0, f2 = 0;
3767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 char_u *s1, *s2;
3769 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3770 char_u *p;
3771
3772 /*
3773 * Get the first variable.
3774 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003775 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return FAIL;
3777
3778 /*
3779 * Repeat computing, until no '+', '-' or '.' is following.
3780 */
3781 for (;;)
3782 {
3783 op = **arg;
3784 if (op != '+' && op != '-' && op != '.')
3785 break;
3786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003787 if ((op != '+' || rettv->v_type != VAR_LIST)
3788#ifdef FEAT_FLOAT
3789 && (op == '.' || rettv->v_type != VAR_FLOAT)
3790#endif
3791 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 {
3793 /* For "list + ...", an illegal use of the first operand as
3794 * a number cannot be determined before evaluating the 2nd
3795 * operand: if this is also a list, all is ok.
3796 * For "something . ...", "something - ..." or "non-list + ...",
3797 * we know that the first operand needs to be a string or number
3798 * without evaluating the 2nd operand. So check before to avoid
3799 * side effects after an error. */
3800 if (evaluate && get_tv_string_chk(rettv) == NULL)
3801 {
3802 clear_tv(rettv);
3803 return FAIL;
3804 }
3805 }
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /*
3808 * Get the second variable.
3809 */
3810 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003811 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return FAIL;
3815 }
3816
3817 if (evaluate)
3818 {
3819 /*
3820 * Compute the result.
3821 */
3822 if (op == '.')
3823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3825 s2 = get_tv_string_buf_chk(&var2, buf2);
3826 if (s2 == NULL) /* type error ? */
3827 {
3828 clear_tv(rettv);
3829 clear_tv(&var2);
3830 return FAIL;
3831 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003832 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 clear_tv(rettv);
3834 rettv->v_type = VAR_STRING;
3835 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003837 else if (op == '+' && rettv->v_type == VAR_LIST
3838 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003839 {
3840 /* concatenate Lists */
3841 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3842 &var3) == FAIL)
3843 {
3844 clear_tv(rettv);
3845 clear_tv(&var2);
3846 return FAIL;
3847 }
3848 clear_tv(rettv);
3849 *rettv = var3;
3850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
3852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 int error = FALSE;
3854
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003855#ifdef FEAT_FLOAT
3856 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003858 f1 = rettv->vval.v_float;
3859 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861 else
3862#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864 n1 = get_tv_number_chk(rettv, &error);
3865 if (error)
3866 {
3867 /* This can only happen for "list + non-list". For
3868 * "non-list + ..." or "something - ...", we returned
3869 * before evaluating the 2nd operand. */
3870 clear_tv(rettv);
3871 return FAIL;
3872 }
3873#ifdef FEAT_FLOAT
3874 if (var2.v_type == VAR_FLOAT)
3875 f1 = n1;
3876#endif
3877 }
3878#ifdef FEAT_FLOAT
3879 if (var2.v_type == VAR_FLOAT)
3880 {
3881 f2 = var2.vval.v_float;
3882 n2 = 0;
3883 }
3884 else
3885#endif
3886 {
3887 n2 = get_tv_number_chk(&var2, &error);
3888 if (error)
3889 {
3890 clear_tv(rettv);
3891 clear_tv(&var2);
3892 return FAIL;
3893 }
3894#ifdef FEAT_FLOAT
3895 if (rettv->v_type == VAR_FLOAT)
3896 f2 = n2;
3897#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003900
3901#ifdef FEAT_FLOAT
3902 /* If there is a float on either side the result is a float. */
3903 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3904 {
3905 if (op == '+')
3906 f1 = f1 + f2;
3907 else
3908 f1 = f1 - f2;
3909 rettv->v_type = VAR_FLOAT;
3910 rettv->vval.v_float = f1;
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003913#endif
3914 {
3915 if (op == '+')
3916 n1 = n1 + n2;
3917 else
3918 n1 = n1 - n2;
3919 rettv->v_type = VAR_NUMBER;
3920 rettv->vval.v_number = n1;
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003923 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925 }
3926 return OK;
3927}
3928
3929/*
3930 * Handle fifth level expression:
3931 * * number multiplication
3932 * / number division
3933 * % number modulo
3934 *
3935 * "arg" must point to the first non-white of the expression.
3936 * "arg" is advanced to the next non-white after the recognized expression.
3937 *
3938 * Return OK or FAIL.
3939 */
3940 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003941eval6(
3942 char_u **arg,
3943 typval_T *rettv,
3944 int evaluate,
3945 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003949 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003950#ifdef FEAT_FLOAT
3951 int use_float = FALSE;
3952 float_T f1 = 0, f2;
3953#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003954 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
3956 /*
3957 * Get the first variable.
3958 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003959 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return FAIL;
3961
3962 /*
3963 * Repeat computing, until no '*', '/' or '%' is following.
3964 */
3965 for (;;)
3966 {
3967 op = **arg;
3968 if (op != '*' && op != '/' && op != '%')
3969 break;
3970
3971 if (evaluate)
3972 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003973#ifdef FEAT_FLOAT
3974 if (rettv->v_type == VAR_FLOAT)
3975 {
3976 f1 = rettv->vval.v_float;
3977 use_float = TRUE;
3978 n1 = 0;
3979 }
3980 else
3981#endif
3982 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984 if (error)
3985 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 else
3988 n1 = 0;
3989
3990 /*
3991 * Get the second variable.
3992 */
3993 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003994 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 return FAIL;
3996
3997 if (evaluate)
3998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003999#ifdef FEAT_FLOAT
4000 if (var2.v_type == VAR_FLOAT)
4001 {
4002 if (!use_float)
4003 {
4004 f1 = n1;
4005 use_float = TRUE;
4006 }
4007 f2 = var2.vval.v_float;
4008 n2 = 0;
4009 }
4010 else
4011#endif
4012 {
4013 n2 = get_tv_number_chk(&var2, &error);
4014 clear_tv(&var2);
4015 if (error)
4016 return FAIL;
4017#ifdef FEAT_FLOAT
4018 if (use_float)
4019 f2 = n2;
4020#endif
4021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023 /*
4024 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004025 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004027#ifdef FEAT_FLOAT
4028 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004030 if (op == '*')
4031 f1 = f1 * f2;
4032 else if (op == '/')
4033 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004034# ifdef VMS
4035 /* VMS crashes on divide by zero, work around it */
4036 if (f2 == 0.0)
4037 {
4038 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004039 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004040 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004041 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004042 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004043 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004044 }
4045 else
4046 f1 = f1 / f2;
4047# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004048 /* We rely on the floating point library to handle divide
4049 * by zero to result in "inf" and not a crash. */
4050 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004051# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004054 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004055 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 return FAIL;
4057 }
4058 rettv->v_type = VAR_FLOAT;
4059 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004064 if (op == '*')
4065 n1 = n1 * n2;
4066 else if (op == '/')
4067 {
4068 if (n2 == 0) /* give an error message? */
4069 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004070 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004071 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004072 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004073 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004074 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004075 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076 }
4077 else
4078 n1 = n1 / n2;
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004081 {
4082 if (n2 == 0) /* give an error message? */
4083 n1 = 0;
4084 else
4085 n1 = n1 % n2;
4086 }
4087 rettv->v_type = VAR_NUMBER;
4088 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 }
4092
4093 return OK;
4094}
4095
4096/*
4097 * Handle sixth level expression:
4098 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004099 * "string" string constant
4100 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 * &option-name option value
4102 * @r register contents
4103 * identifier variable value
4104 * function() function call
4105 * $VAR environment variable
4106 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004107 * [expr, expr] List
4108 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 *
4110 * Also handle:
4111 * ! in front logical NOT
4112 * - in front unary minus
4113 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004114 * trailing [] subscript in String or List
4115 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * "arg" must point to the first non-white of the expression.
4118 * "arg" is advanced to the next non-white after the recognized expression.
4119 *
4120 * Return OK or FAIL.
4121 */
4122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004123eval7(
4124 char_u **arg,
4125 typval_T *rettv,
4126 int evaluate,
4127 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004129 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 int len;
4131 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_u *start_leader, *end_leader;
4133 int ret = OK;
4134 char_u *alias;
4135
4136 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004138 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004143 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
4145 start_leader = *arg;
4146 while (**arg == '!' || **arg == '-' || **arg == '+')
4147 *arg = skipwhite(*arg + 1);
4148 end_leader = *arg;
4149
4150 switch (**arg)
4151 {
4152 /*
4153 * Number constant.
4154 */
4155 case '0':
4156 case '1':
4157 case '2':
4158 case '3':
4159 case '4':
4160 case '5':
4161 case '6':
4162 case '7':
4163 case '8':
4164 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004165 {
4166#ifdef FEAT_FLOAT
4167 char_u *p = skipdigits(*arg + 1);
4168 int get_float = FALSE;
4169
4170 /* We accept a float when the format matches
4171 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004172 * strict to avoid backwards compatibility problems.
4173 * Don't look for a float after the "." operator, so that
4174 * ":let vers = 1.2.3" doesn't fail. */
4175 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004177 get_float = TRUE;
4178 p = skipdigits(p + 2);
4179 if (*p == 'e' || *p == 'E')
4180 {
4181 ++p;
4182 if (*p == '-' || *p == '+')
4183 ++p;
4184 if (!vim_isdigit(*p))
4185 get_float = FALSE;
4186 else
4187 p = skipdigits(p + 1);
4188 }
4189 if (ASCII_ISALPHA(*p) || *p == '.')
4190 get_float = FALSE;
4191 }
4192 if (get_float)
4193 {
4194 float_T f;
4195
4196 *arg += string2float(*arg, &f);
4197 if (evaluate)
4198 {
4199 rettv->v_type = VAR_FLOAT;
4200 rettv->vval.v_float = f;
4201 }
4202 }
4203 else
4204#endif
4205 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004206 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004207 *arg += len;
4208 if (evaluate)
4209 {
4210 rettv->v_type = VAR_NUMBER;
4211 rettv->vval.v_number = n;
4212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 /*
4218 * String constant: "string".
4219 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 break;
4222
4223 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004224 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004227 break;
4228
4229 /*
4230 * List: [expr, expr]
4231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234
4235 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004236 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004237 * Dictionary: {key: val, key: val}
4238 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004239 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4240 if (ret == NOTDONE)
4241 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004242 break;
4243
4244 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004245 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004247 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249
4250 /*
4251 * Environment variable: $VAR.
4252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 break;
4255
4256 /*
4257 * Register contents: @r.
4258 */
4259 case '@': ++*arg;
4260 if (evaluate)
4261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004263 rettv->vval.v_string = get_reg_contents(**arg,
4264 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 if (**arg != NUL)
4267 ++*arg;
4268 break;
4269
4270 /*
4271 * nested expression: (expression).
4272 */
4273 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (**arg == ')')
4276 ++*arg;
4277 else if (ret == OK)
4278 {
4279 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 ret = FAIL;
4282 }
4283 break;
4284
Bram Moolenaar8c711452005-01-14 21:53:12 +00004285 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 break;
4287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004288
4289 if (ret == NOTDONE)
4290 {
4291 /*
4292 * Must be a variable or function name.
4293 * Can also be a curly-braces kind of name: {expr}.
4294 */
4295 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004296 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 if (alias != NULL)
4298 s = alias;
4299
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004300 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301 ret = FAIL;
4302 else
4303 {
4304 if (**arg == '(') /* recursive! */
4305 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004306 partial_T *partial;
4307
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004308 if (!evaluate)
4309 check_vars(s, len);
4310
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 /* If "s" is the name of a variable of type VAR_FUNC
4312 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004313 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004315 /* Need to make a copy, in case evaluating the arguments makes
4316 * the name invalid. */
4317 s = vim_strsave(s);
4318 if (s == NULL)
4319 ret = FAIL;
4320 else
4321 /* Invoke the function. */
4322 ret = get_func_tv(s, len, rettv, arg,
4323 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4324 &len, evaluate, partial, NULL);
4325 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004326
4327 /* If evaluate is FALSE rettv->v_type was not set in
4328 * get_func_tv, but it's needed in handle_subscript() to parse
4329 * what follows. So set it here. */
4330 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4331 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004332 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004333 rettv->v_type = VAR_FUNC;
4334 }
4335
Bram Moolenaar8c711452005-01-14 21:53:12 +00004336 /* Stop the expression evaluation when immediately
4337 * aborting on error, or when an interrupt occurred or
4338 * an exception was thrown but not caught. */
4339 if (aborting())
4340 {
4341 if (ret == OK)
4342 clear_tv(rettv);
4343 ret = FAIL;
4344 }
4345 }
4346 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004347 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004348 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004349 {
4350 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004351 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004354 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004355 }
4356
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 *arg = skipwhite(*arg);
4358
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004359 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4360 * expr(expr). */
4361 if (ret == OK)
4362 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 /*
4365 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4366 */
4367 if (ret == OK && evaluate && end_leader > start_leader)
4368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004370 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371#ifdef FEAT_FLOAT
4372 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004374 if (rettv->v_type == VAR_FLOAT)
4375 f = rettv->vval.v_float;
4376 else
4377#endif
4378 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004379 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 clear_tv(rettv);
4382 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 else
4385 {
4386 while (end_leader > start_leader)
4387 {
4388 --end_leader;
4389 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004390 {
4391#ifdef FEAT_FLOAT
4392 if (rettv->v_type == VAR_FLOAT)
4393 f = !f;
4394 else
4395#endif
4396 val = !val;
4397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004399 {
4400#ifdef FEAT_FLOAT
4401 if (rettv->v_type == VAR_FLOAT)
4402 f = -f;
4403 else
4404#endif
4405 val = -val;
4406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408#ifdef FEAT_FLOAT
4409 if (rettv->v_type == VAR_FLOAT)
4410 {
4411 clear_tv(rettv);
4412 rettv->vval.v_float = f;
4413 }
4414 else
4415#endif
4416 {
4417 clear_tv(rettv);
4418 rettv->v_type = VAR_NUMBER;
4419 rettv->vval.v_number = val;
4420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423
4424 return ret;
4425}
4426
4427/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004428 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4429 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4431 */
4432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004433eval_index(
4434 char_u **arg,
4435 typval_T *rettv,
4436 int evaluate,
4437 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438{
4439 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004440 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004442 long len = -1;
4443 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004445 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004446
Bram Moolenaara03f2332016-02-06 18:09:59 +01004447 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004449 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004450 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004451 if (verbose)
4452 EMSG(_("E695: Cannot index a Funcref"));
4453 return FAIL;
4454 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004455#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004456 if (verbose)
4457 EMSG(_(e_float_as_string));
4458 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004459#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004460 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004461 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004462 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004463 if (verbose)
4464 EMSG(_("E909: Cannot index a special variable"));
4465 return FAIL;
4466 case VAR_UNKNOWN:
4467 if (evaluate)
4468 return FAIL;
4469 /* FALLTHROUGH */
4470
4471 case VAR_STRING:
4472 case VAR_NUMBER:
4473 case VAR_LIST:
4474 case VAR_DICT:
4475 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004478 init_tv(&var1);
4479 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004480 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004482 /*
4483 * dict.name
4484 */
4485 key = *arg + 1;
4486 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4487 ;
4488 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004489 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491 }
4492 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 /*
4495 * something[idx]
4496 *
4497 * Get the (first) variable from inside the [].
4498 */
4499 *arg = skipwhite(*arg + 1);
4500 if (**arg == ':')
4501 empty1 = TRUE;
4502 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4503 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004504 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4505 {
4506 /* not a number or string */
4507 clear_tv(&var1);
4508 return FAIL;
4509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510
4511 /*
4512 * Get the second variable from inside the [:].
4513 */
4514 if (**arg == ':')
4515 {
4516 range = TRUE;
4517 *arg = skipwhite(*arg + 1);
4518 if (**arg == ']')
4519 empty2 = TRUE;
4520 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004522 if (!empty1)
4523 clear_tv(&var1);
4524 return FAIL;
4525 }
4526 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4527 {
4528 /* not a number or string */
4529 if (!empty1)
4530 clear_tv(&var1);
4531 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 return FAIL;
4533 }
4534 }
4535
4536 /* Check for the ']'. */
4537 if (**arg != ']')
4538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004539 if (verbose)
4540 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 clear_tv(&var1);
4542 if (range)
4543 clear_tv(&var2);
4544 return FAIL;
4545 }
4546 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004547 }
4548
4549 if (evaluate)
4550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 n1 = 0;
4552 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 n1 = get_tv_number(&var1);
4555 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 }
4557 if (range)
4558 {
4559 if (empty2)
4560 n2 = -1;
4561 else
4562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004563 n2 = get_tv_number(&var2);
4564 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565 }
4566 }
4567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004570 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004571 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004572 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004573 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004574 case VAR_SPECIAL:
4575 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004576 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004577 break; /* not evaluating, skipping over subscript */
4578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 case VAR_NUMBER:
4580 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 len = (long)STRLEN(s);
4583 if (range)
4584 {
4585 /* The resulting variable is a substring. If the indexes
4586 * are out of range the result is empty. */
4587 if (n1 < 0)
4588 {
4589 n1 = len + n1;
4590 if (n1 < 0)
4591 n1 = 0;
4592 }
4593 if (n2 < 0)
4594 n2 = len + n2;
4595 else if (n2 >= len)
4596 n2 = len;
4597 if (n1 >= len || n2 < 0 || n1 > n2)
4598 s = NULL;
4599 else
4600 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4601 }
4602 else
4603 {
4604 /* The resulting variable is a string of a single
4605 * character. If the index is too big or negative the
4606 * result is empty. */
4607 if (n1 >= len || n1 < 0)
4608 s = NULL;
4609 else
4610 s = vim_strnsave(s + n1, 1);
4611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 clear_tv(rettv);
4613 rettv->v_type = VAR_STRING;
4614 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 break;
4616
4617 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 if (n1 < 0)
4620 n1 = len + n1;
4621 if (!empty1 && (n1 < 0 || n1 >= len))
4622 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004623 /* For a range we allow invalid values and return an empty
4624 * list. A list index out of range is an error. */
4625 if (!range)
4626 {
4627 if (verbose)
4628 EMSGN(_(e_listidx), n1);
4629 return FAIL;
4630 }
4631 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 }
4633 if (range)
4634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004635 list_T *l;
4636 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637
4638 if (n2 < 0)
4639 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004640 else if (n2 >= len)
4641 n2 = len - 1;
4642 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004643 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 l = list_alloc();
4645 if (l == NULL)
4646 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 n1 <= n2; ++n1)
4649 {
4650 if (list_append_tv(l, &item->li_tv) == FAIL)
4651 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004652 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004653 return FAIL;
4654 }
4655 item = item->li_next;
4656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004658 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 }
4660 else
4661 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004662 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 clear_tv(rettv);
4664 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 }
4666 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667
4668 case VAR_DICT:
4669 if (range)
4670 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004671 if (verbose)
4672 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 if (len == -1)
4674 clear_tv(&var1);
4675 return FAIL;
4676 }
4677 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004678 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679
4680 if (len == -1)
4681 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004682 key = get_tv_string_chk(&var1);
4683 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 clear_tv(&var1);
4686 return FAIL;
4687 }
4688 }
4689
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004690 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004692 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004693 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 if (len == -1)
4695 clear_tv(&var1);
4696 if (item == NULL)
4697 return FAIL;
4698
4699 copy_tv(&item->di_tv, &var1);
4700 clear_tv(rettv);
4701 *rettv = var1;
4702 }
4703 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 }
4705 }
4706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 return OK;
4708}
4709
4710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 * Get an option value.
4712 * "arg" points to the '&' or '+' before the option name.
4713 * "arg" is advanced to character after the option name.
4714 * Return OK or FAIL.
4715 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004717get_option_tv(
4718 char_u **arg,
4719 typval_T *rettv, /* when NULL, only check if option exists */
4720 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 char_u *option_end;
4723 long numval;
4724 char_u *stringval;
4725 int opt_type;
4726 int c;
4727 int working = (**arg == '+'); /* has("+option") */
4728 int ret = OK;
4729 int opt_flags;
4730
4731 /*
4732 * Isolate the option name and find its value.
4733 */
4734 option_end = find_option_end(arg, &opt_flags);
4735 if (option_end == NULL)
4736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 EMSG2(_("E112: Option name missing: %s"), *arg);
4739 return FAIL;
4740 }
4741
4742 if (!evaluate)
4743 {
4744 *arg = option_end;
4745 return OK;
4746 }
4747
4748 c = *option_end;
4749 *option_end = NUL;
4750 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 if (opt_type == -3) /* invalid name */
4754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 EMSG2(_("E113: Unknown option: %s"), *arg);
4757 ret = FAIL;
4758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761 if (opt_type == -2) /* hidden string option */
4762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 rettv->v_type = VAR_STRING;
4764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 else if (opt_type == -1) /* hidden number option */
4767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 rettv->v_type = VAR_NUMBER;
4769 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else if (opt_type == 1) /* number option */
4772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 rettv->v_type = VAR_NUMBER;
4774 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 else /* string option */
4777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 rettv->v_type = VAR_STRING;
4779 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 }
4782 else if (working && (opt_type == -2 || opt_type == -1))
4783 ret = FAIL;
4784
4785 *option_end = c; /* put back for error messages */
4786 *arg = option_end;
4787
4788 return ret;
4789}
4790
4791/*
4792 * Allocate a variable for a string constant.
4793 * Return OK or FAIL.
4794 */
4795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004796get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 char_u *p;
4799 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 int extra = 0;
4801
4802 /*
4803 * Find the end of the string, skipping backslashed characters.
4804 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004805 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
4807 if (*p == '\\' && p[1] != NUL)
4808 {
4809 ++p;
4810 /* A "\<x>" form occupies at least 4 characters, and produces up
4811 * to 6 characters: reserve space for 2 extra */
4812 if (*p == '<')
4813 extra += 2;
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816
4817 if (*p != '"')
4818 {
4819 EMSG2(_("E114: Missing quote: %s"), *arg);
4820 return FAIL;
4821 }
4822
4823 /* If only parsing, set *arg and return here */
4824 if (!evaluate)
4825 {
4826 *arg = p + 1;
4827 return OK;
4828 }
4829
4830 /*
4831 * Copy the string into allocated memory, handling backslashed
4832 * characters.
4833 */
4834 name = alloc((unsigned)(p - *arg + extra));
4835 if (name == NULL)
4836 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004837 rettv->v_type = VAR_STRING;
4838 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 if (*p == '\\')
4843 {
4844 switch (*++p)
4845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004846 case 'b': *name++ = BS; ++p; break;
4847 case 'e': *name++ = ESC; ++p; break;
4848 case 'f': *name++ = FF; ++p; break;
4849 case 'n': *name++ = NL; ++p; break;
4850 case 'r': *name++ = CAR; ++p; break;
4851 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
4853 case 'X': /* hex: "\x1", "\x12" */
4854 case 'x':
4855 case 'u': /* Unicode: "\u0023" */
4856 case 'U':
4857 if (vim_isxdigit(p[1]))
4858 {
4859 int n, nr;
4860 int c = toupper(*p);
4861
4862 if (c == 'X')
4863 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004864 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004866 else
4867 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 nr = 0;
4869 while (--n >= 0 && vim_isxdigit(p[1]))
4870 {
4871 ++p;
4872 nr = (nr << 4) + hex2nr(*p);
4873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004874 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875#ifdef FEAT_MBYTE
4876 /* For "\u" store the number according to
4877 * 'encoding'. */
4878 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004879 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 else
4881#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004882 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 break;
4885
4886 /* octal: "\1", "\12", "\123" */
4887 case '0':
4888 case '1':
4889 case '2':
4890 case '3':
4891 case '4':
4892 case '5':
4893 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004894 case '7': *name = *p++ - '0';
4895 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 *name = (*name << 3) + *p++ - '0';
4898 if (*p >= '0' && *p <= '7')
4899 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 break;
4903
4904 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004905 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (extra != 0)
4907 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910 }
4911 /* FALLTHROUGH */
4912
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 break;
4915 }
4916 }
4917 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004922 if (*p != NUL) /* just in case */
4923 ++p;
4924 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return OK;
4927}
4928
4929/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 * Return OK or FAIL.
4932 */
4933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004934get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004937 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004938 int reduce = 0;
4939
4940 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004942 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004943 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004948 break;
4949 ++reduce;
4950 ++p;
4951 }
4952 }
4953
Bram Moolenaar8c711452005-01-14 21:53:12 +00004954 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004957 return FAIL;
4958 }
4959
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004961 if (!evaluate)
4962 {
4963 *arg = p + 1;
4964 return OK;
4965 }
4966
4967 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004969 */
4970 str = alloc((unsigned)((p - *arg) - reduce));
4971 if (str == NULL)
4972 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 rettv->v_type = VAR_STRING;
4974 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004975
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004981 break;
4982 ++p;
4983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004987 *arg = p + 1;
4988
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989 return OK;
4990}
4991
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004992/*
4993 * Return the function name of the partial.
4994 */
4995 char_u *
4996partial_name(partial_T *pt)
4997{
4998 if (pt->pt_name != NULL)
4999 return pt->pt_name;
5000 return pt->pt_func->uf_name;
5001}
5002
Bram Moolenaarddecc252016-04-06 22:59:37 +02005003 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005004partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005005{
5006 int i;
5007
5008 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005009 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005010 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005011 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005012 if (pt->pt_name != NULL)
5013 {
5014 func_unref(pt->pt_name);
5015 vim_free(pt->pt_name);
5016 }
5017 else
5018 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005019 vim_free(pt);
5020}
5021
5022/*
5023 * Unreference a closure: decrement the reference count and free it when it
5024 * becomes zero.
5025 */
5026 void
5027partial_unref(partial_T *pt)
5028{
5029 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005030 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005031}
5032
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005033static int tv_equal_recurse_limit;
5034
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005035 static int
5036func_equal(
5037 typval_T *tv1,
5038 typval_T *tv2,
5039 int ic) /* ignore case */
5040{
5041 char_u *s1, *s2;
5042 dict_T *d1, *d2;
5043 int a1, a2;
5044 int i;
5045
5046 /* empty and NULL function name considered the same */
5047 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005048 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005049 if (s1 != NULL && *s1 == NUL)
5050 s1 = NULL;
5051 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005052 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005053 if (s2 != NULL && *s2 == NUL)
5054 s2 = NULL;
5055 if (s1 == NULL || s2 == NULL)
5056 {
5057 if (s1 != s2)
5058 return FALSE;
5059 }
5060 else if (STRCMP(s1, s2) != 0)
5061 return FALSE;
5062
5063 /* empty dict and NULL dict is different */
5064 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5065 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5066 if (d1 == NULL || d2 == NULL)
5067 {
5068 if (d1 != d2)
5069 return FALSE;
5070 }
5071 else if (!dict_equal(d1, d2, ic, TRUE))
5072 return FALSE;
5073
5074 /* empty list and no list considered the same */
5075 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5076 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5077 if (a1 != a2)
5078 return FALSE;
5079 for (i = 0; i < a1; ++i)
5080 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5081 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5082 return FALSE;
5083
5084 return TRUE;
5085}
5086
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005087/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005088 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005089 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005090 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005091 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005093tv_equal(
5094 typval_T *tv1,
5095 typval_T *tv2,
5096 int ic, /* ignore case */
5097 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005098{
5099 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005101 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005102 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005103
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005104 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005105 * recursiveness to a limit. We guess they are equal then.
5106 * A fixed limit has the problem of still taking an awful long time.
5107 * Reduce the limit every time running into it. That should work fine for
5108 * deeply linked structures that are not recursively linked and catch
5109 * recursiveness quickly. */
5110 if (!recursive)
5111 tv_equal_recurse_limit = 1000;
5112 if (recursive_cnt >= tv_equal_recurse_limit)
5113 {
5114 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005115 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005116 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005117
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005118 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5119 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005120 if ((tv1->v_type == VAR_FUNC
5121 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5122 && (tv2->v_type == VAR_FUNC
5123 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5124 {
5125 ++recursive_cnt;
5126 r = func_equal(tv1, tv2, ic);
5127 --recursive_cnt;
5128 return r;
5129 }
5130
5131 if (tv1->v_type != tv2->v_type)
5132 return FALSE;
5133
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005134 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005135 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005136 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005137 ++recursive_cnt;
5138 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5139 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005140 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005141
5142 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005143 ++recursive_cnt;
5144 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5145 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005146 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005147
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005148 case VAR_NUMBER:
5149 return tv1->vval.v_number == tv2->vval.v_number;
5150
5151 case VAR_STRING:
5152 s1 = get_tv_string_buf(tv1, buf1);
5153 s2 = get_tv_string_buf(tv2, buf2);
5154 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005155
5156 case VAR_SPECIAL:
5157 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005158
5159 case VAR_FLOAT:
5160#ifdef FEAT_FLOAT
5161 return tv1->vval.v_float == tv2->vval.v_float;
5162#endif
5163 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005164#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005165 return tv1->vval.v_job == tv2->vval.v_job;
5166#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005167 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005169 return tv1->vval.v_channel == tv2->vval.v_channel;
5170#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005171 case VAR_FUNC:
5172 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005173 case VAR_UNKNOWN:
5174 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005175 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005176
Bram Moolenaara03f2332016-02-06 18:09:59 +01005177 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5178 * does not equal anything, not even itself. */
5179 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005180}
5181
5182/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005183 * Return the next (unique) copy ID.
5184 * Used for serializing nested structures.
5185 */
5186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005187get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005188{
5189 current_copyID += COPYID_INC;
5190 return current_copyID;
5191}
5192
5193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005194 * Garbage collection for lists and dictionaries.
5195 *
5196 * We use reference counts to be able to free most items right away when they
5197 * are no longer used. But for composite items it's possible that it becomes
5198 * unused while the reference count is > 0: When there is a recursive
5199 * reference. Example:
5200 * :let l = [1, 2, 3]
5201 * :let d = {9: l}
5202 * :let l[1] = d
5203 *
5204 * Since this is quite unusual we handle this with garbage collection: every
5205 * once in a while find out which lists and dicts are not referenced from any
5206 * variable.
5207 *
5208 * Here is a good reference text about garbage collection (refers to Python
5209 * but it applies to all reference-counting mechanisms):
5210 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005211 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005212
5213/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005214 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005215 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005216 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005217 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005219garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005220{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005221 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005222 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223 buf_T *buf;
5224 win_T *wp;
5225 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005226 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005227 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005229 if (!testing)
5230 {
5231 /* Only do this once. */
5232 want_garbage_collect = FALSE;
5233 may_garbage_collect = FALSE;
5234 garbage_collect_at_exit = FALSE;
5235 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005236
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005237 /* We advance by two because we add one for items referenced through
5238 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005239 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005240
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241 /*
5242 * 1. Go through all accessible variables and mark all lists and dicts
5243 * with copyID.
5244 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005245
5246 /* Don't free variables in the previous_funccal list unless they are only
5247 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005248 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251 /* script-local variables */
5252 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005253 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005254
5255 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005256 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5258 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005259
5260 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005261 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5263 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005264#ifdef FEAT_AUTOCMD
5265 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5267 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005268#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005269
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005270 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005271 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005272 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5273 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005274 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005275 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276
5277 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005279
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005280 /* named functions (matters for closures) */
5281 abort = abort || set_ref_in_functions(copyID);
5282
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005283 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005285
Bram Moolenaard812df62008-11-09 12:46:09 +00005286 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005287 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005288
Bram Moolenaar1dced572012-04-05 16:54:08 +02005289#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005291#endif
5292
Bram Moolenaardb913952012-06-29 12:54:53 +02005293#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005295#endif
5296
5297#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005299#endif
5300
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005302 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005303 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005304#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005305#ifdef FEAT_NETBEANS_INTG
5306 abort = abort || set_ref_in_nb_channel(copyID);
5307#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005308
Bram Moolenaare3188e22016-05-31 21:13:04 +02005309#ifdef FEAT_TIMERS
5310 abort = abort || set_ref_in_timer(copyID);
5311#endif
5312
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005313#ifdef FEAT_QUICKFIX
5314 abort = abort || set_ref_in_quickfix(copyID);
5315#endif
5316
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005317#ifdef FEAT_TERMINAL
5318 abort = abort || set_ref_in_term(copyID);
5319#endif
5320
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005321 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005322 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005323 /*
5324 * 2. Free lists and dictionaries that are not referenced.
5325 */
5326 did_free = free_unref_items(copyID);
5327
5328 /*
5329 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005331 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005333 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005334 else if (p_verbose > 0)
5335 {
5336 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5337 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005338
5339 return did_free;
5340}
5341
5342/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005343 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005347{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005348 int did_free = FALSE;
5349
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005350 /* Let all "free" functions know that we are here. This means no
5351 * dictionaries, lists, channels or jobs are to be freed, because we will
5352 * do that here. */
5353 in_free_unref_items = TRUE;
5354
5355 /*
5356 * PASS 1: free the contents of the items. We don't free the items
5357 * themselves yet, so that it is possible to decrement refcount counters
5358 */
5359
Bram Moolenaarcd524592016-07-17 14:57:05 +02005360 /* Go through the list of dicts and free items without the copyID. */
5361 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362
Bram Moolenaarda861d62016-07-17 15:46:27 +02005363 /* Go through the list of lists and free items without the copyID. */
5364 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365
5366#ifdef FEAT_JOB_CHANNEL
5367 /* Go through the list of jobs and free items without the copyID. This
5368 * must happen before doing channels, because jobs refer to channels, but
5369 * the reference from the channel to the job isn't tracked. */
5370 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5371
5372 /* Go through the list of channels and free items without the copyID. */
5373 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5374#endif
5375
5376 /*
5377 * PASS 2: free the items themselves.
5378 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005379 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005380 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005381
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005382#ifdef FEAT_JOB_CHANNEL
5383 /* Go through the list of jobs and free items without the copyID. This
5384 * must happen before doing channels, because jobs refer to channels, but
5385 * the reference from the channel to the job isn't tracked. */
5386 free_unused_jobs(copyID, COPYID_MASK);
5387
5388 /* Go through the list of channels and free items without the copyID. */
5389 free_unused_channels(copyID, COPYID_MASK);
5390#endif
5391
5392 in_free_unref_items = FALSE;
5393
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005394 return did_free;
5395}
5396
5397/*
5398 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 * "list_stack" is used to add lists to be marked. Can be NULL.
5400 *
5401 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005402 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005404set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405{
5406 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005409 hashtab_T *cur_ht;
5410 ht_stack_T *ht_stack = NULL;
5411 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005412
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005413 cur_ht = ht;
5414 for (;;)
5415 {
5416 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005417 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 /* Mark each item in the hashtab. If the item contains a hashtab
5419 * it is added to ht_stack, if it contains a list it is added to
5420 * list_stack. */
5421 todo = (int)cur_ht->ht_used;
5422 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5423 if (!HASHITEM_EMPTY(hi))
5424 {
5425 --todo;
5426 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5427 &ht_stack, list_stack);
5428 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005430
5431 if (ht_stack == NULL)
5432 break;
5433
5434 /* take an item from the stack */
5435 cur_ht = ht_stack->ht;
5436 tempitem = ht_stack;
5437 ht_stack = ht_stack->prev;
5438 free(tempitem);
5439 }
5440
5441 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005442}
5443
5444/*
5445 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005446 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5447 *
5448 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005449 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005452{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005453 listitem_T *li;
5454 int abort = FALSE;
5455 list_T *cur_l;
5456 list_stack_T *list_stack = NULL;
5457 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005458
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005459 cur_l = l;
5460 for (;;)
5461 {
5462 if (!abort)
5463 /* Mark each item in the list. If the item contains a hashtab
5464 * it is added to ht_stack, if it contains a list it is added to
5465 * list_stack. */
5466 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5467 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5468 ht_stack, &list_stack);
5469 if (list_stack == NULL)
5470 break;
5471
5472 /* take an item from the stack */
5473 cur_l = list_stack->list;
5474 tempitem = list_stack;
5475 list_stack = list_stack->prev;
5476 free(tempitem);
5477 }
5478
5479 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480}
5481
5482/*
5483 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005484 * "list_stack" is used to add lists to be marked. Can be NULL.
5485 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5486 *
5487 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005488 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005490set_ref_in_item(
5491 typval_T *tv,
5492 int copyID,
5493 ht_stack_T **ht_stack,
5494 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005495{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005496 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005497
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005498 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005499 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005500 dict_T *dd = tv->vval.v_dict;
5501
Bram Moolenaara03f2332016-02-06 18:09:59 +01005502 if (dd != NULL && dd->dv_copyID != copyID)
5503 {
5504 /* Didn't see this dict yet. */
5505 dd->dv_copyID = copyID;
5506 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005507 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005508 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5509 }
5510 else
5511 {
5512 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5513 if (newitem == NULL)
5514 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 else
5516 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005517 newitem->ht = &dd->dv_hashtab;
5518 newitem->prev = *ht_stack;
5519 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005520 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005521 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005522 }
5523 }
5524 else if (tv->v_type == VAR_LIST)
5525 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005526 list_T *ll = tv->vval.v_list;
5527
Bram Moolenaara03f2332016-02-06 18:09:59 +01005528 if (ll != NULL && ll->lv_copyID != copyID)
5529 {
5530 /* Didn't see this list yet. */
5531 ll->lv_copyID = copyID;
5532 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005533 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005534 abort = set_ref_in_list(ll, copyID, ht_stack);
5535 }
5536 else
5537 {
5538 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005539 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005540 if (newitem == NULL)
5541 abort = TRUE;
5542 else
5543 {
5544 newitem->list = ll;
5545 newitem->prev = *list_stack;
5546 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005547 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005548 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005549 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005550 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005551 else if (tv->v_type == VAR_FUNC)
5552 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005553 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005554 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005555 else if (tv->v_type == VAR_PARTIAL)
5556 {
5557 partial_T *pt = tv->vval.v_partial;
5558 int i;
5559
5560 /* A partial does not have a copyID, because it cannot contain itself.
5561 */
5562 if (pt != NULL)
5563 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005564 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005565
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005566 if (pt->pt_dict != NULL)
5567 {
5568 typval_T dtv;
5569
5570 dtv.v_type = VAR_DICT;
5571 dtv.vval.v_dict = pt->pt_dict;
5572 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5573 }
5574
5575 for (i = 0; i < pt->pt_argc; ++i)
5576 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5577 ht_stack, list_stack);
5578 }
5579 }
5580#ifdef FEAT_JOB_CHANNEL
5581 else if (tv->v_type == VAR_JOB)
5582 {
5583 job_T *job = tv->vval.v_job;
5584 typval_T dtv;
5585
5586 if (job != NULL && job->jv_copyID != copyID)
5587 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005588 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005589 if (job->jv_channel != NULL)
5590 {
5591 dtv.v_type = VAR_CHANNEL;
5592 dtv.vval.v_channel = job->jv_channel;
5593 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5594 }
5595 if (job->jv_exit_partial != NULL)
5596 {
5597 dtv.v_type = VAR_PARTIAL;
5598 dtv.vval.v_partial = job->jv_exit_partial;
5599 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5600 }
5601 }
5602 }
5603 else if (tv->v_type == VAR_CHANNEL)
5604 {
5605 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005606 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005607 typval_T dtv;
5608 jsonq_T *jq;
5609 cbq_T *cq;
5610
5611 if (ch != NULL && ch->ch_copyID != copyID)
5612 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005613 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005614 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005615 {
5616 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5617 jq = jq->jq_next)
5618 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5619 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5620 cq = cq->cq_next)
5621 if (cq->cq_partial != NULL)
5622 {
5623 dtv.v_type = VAR_PARTIAL;
5624 dtv.vval.v_partial = cq->cq_partial;
5625 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5626 }
5627 if (ch->ch_part[part].ch_partial != NULL)
5628 {
5629 dtv.v_type = VAR_PARTIAL;
5630 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5631 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5632 }
5633 }
5634 if (ch->ch_partial != NULL)
5635 {
5636 dtv.v_type = VAR_PARTIAL;
5637 dtv.vval.v_partial = ch->ch_partial;
5638 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5639 }
5640 if (ch->ch_close_partial != NULL)
5641 {
5642 dtv.v_type = VAR_PARTIAL;
5643 dtv.vval.v_partial = ch->ch_close_partial;
5644 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5645 }
5646 }
5647 }
5648#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005650}
5651
Bram Moolenaar17a13432016-01-24 14:22:10 +01005652 static char *
5653get_var_special_name(int nr)
5654{
5655 switch (nr)
5656 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005657 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005658 case VVAL_TRUE: return "v:true";
5659 case VVAL_NONE: return "v:none";
5660 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005661 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005662 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005663 return "42";
5664}
5665
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 * Return a string with the string representation of a variable.
5668 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005669 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005670 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005671 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5672 * stings as "string()", otherwise does not put quotes around strings, as
5673 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005674 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5675 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005676 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005678 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005679echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005680 typval_T *tv,
5681 char_u **tofree,
5682 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005683 int copyID,
5684 int echo_style,
5685 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005686 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005688 static int recurse = 0;
5689 char_u *r = NULL;
5690
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005692 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005693 if (!did_echo_string_emsg)
5694 {
5695 /* Only give this message once for a recursive call to avoid
5696 * flooding the user with errors. And stop iterating over lists
5697 * and dicts. */
5698 did_echo_string_emsg = TRUE;
5699 EMSG(_("E724: variable nested too deep for displaying"));
5700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005701 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005702 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005703 }
5704 ++recurse;
5705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 switch (tv->v_type)
5707 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005708 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005709 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005710 {
5711 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005712 r = tv->vval.v_string;
5713 if (r == NULL)
5714 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005715 }
5716 else
5717 {
5718 *tofree = string_quote(tv->vval.v_string, FALSE);
5719 r = *tofree;
5720 }
5721 break;
5722
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005724 if (echo_style)
5725 {
5726 *tofree = NULL;
5727 r = tv->vval.v_string;
5728 }
5729 else
5730 {
5731 *tofree = string_quote(tv->vval.v_string, TRUE);
5732 r = *tofree;
5733 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005734 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005735
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005736 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005737 {
5738 partial_T *pt = tv->vval.v_partial;
5739 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005740 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005741 garray_T ga;
5742 int i;
5743 char_u *tf;
5744
5745 ga_init2(&ga, 1, 100);
5746 ga_concat(&ga, (char_u *)"function(");
5747 if (fname != NULL)
5748 {
5749 ga_concat(&ga, fname);
5750 vim_free(fname);
5751 }
5752 if (pt != NULL && pt->pt_argc > 0)
5753 {
5754 ga_concat(&ga, (char_u *)", [");
5755 for (i = 0; i < pt->pt_argc; ++i)
5756 {
5757 if (i > 0)
5758 ga_concat(&ga, (char_u *)", ");
5759 ga_concat(&ga,
5760 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5761 vim_free(tf);
5762 }
5763 ga_concat(&ga, (char_u *)"]");
5764 }
5765 if (pt != NULL && pt->pt_dict != NULL)
5766 {
5767 typval_T dtv;
5768
5769 ga_concat(&ga, (char_u *)", ");
5770 dtv.v_type = VAR_DICT;
5771 dtv.vval.v_dict = pt->pt_dict;
5772 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5773 vim_free(tf);
5774 }
5775 ga_concat(&ga, (char_u *)")");
5776
5777 *tofree = ga.ga_data;
5778 r = *tofree;
5779 break;
5780 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005783 if (tv->vval.v_list == NULL)
5784 {
5785 *tofree = NULL;
5786 r = NULL;
5787 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005788 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5789 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005790 {
5791 *tofree = NULL;
5792 r = (char_u *)"[...]";
5793 }
5794 else
5795 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005796 int old_copyID = tv->vval.v_list->lv_copyID;
5797
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005798 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005799 *tofree = list2string(tv, copyID, restore_copyID);
5800 if (restore_copyID)
5801 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005802 r = *tofree;
5803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005804 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005807 if (tv->vval.v_dict == NULL)
5808 {
5809 *tofree = NULL;
5810 r = NULL;
5811 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005812 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5813 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005814 {
5815 *tofree = NULL;
5816 r = (char_u *)"{...}";
5817 }
5818 else
5819 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005820 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005821 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005822 *tofree = dict2string(tv, copyID, restore_copyID);
5823 if (restore_copyID)
5824 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005825 r = *tofree;
5826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005827 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005829 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005830 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005831 *tofree = NULL;
5832 r = get_tv_string_buf(tv, numbuf);
5833 break;
5834
Bram Moolenaar835dc632016-02-07 14:27:38 +01005835 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005836 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005837 *tofree = NULL;
5838 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005839 if (composite_val)
5840 {
5841 *tofree = string_quote(r, FALSE);
5842 r = *tofree;
5843 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005845
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005846 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005848 *tofree = NULL;
5849 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5850 r = numbuf;
5851 break;
5852#endif
5853
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005854 case VAR_SPECIAL:
5855 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005856 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005857 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005859
Bram Moolenaar8502c702014-06-17 12:51:16 +02005860 if (--recurse == 0)
5861 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863}
5864
5865/*
5866 * Return a string with the string representation of a variable.
5867 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5868 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005869 * Does not put quotes around strings, as ":echo" displays values.
5870 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5871 * May return NULL.
5872 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005874echo_string(
5875 typval_T *tv,
5876 char_u **tofree,
5877 char_u *numbuf,
5878 int copyID)
5879{
5880 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5881}
5882
5883/*
5884 * Return a string with the string representation of a variable.
5885 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5886 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005888 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005891tv2string(
5892 typval_T *tv,
5893 char_u **tofree,
5894 char_u *numbuf,
5895 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005897 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898}
5899
5900/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 * Return string "str" in ' quotes, doubling ' characters.
5902 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005905 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005906string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907{
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 char_u *p, *r, *s;
5910
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 len = (function ? 13 : 3);
5912 if (str != NULL)
5913 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005914 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005915 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 if (*p == '\'')
5917 ++len;
5918 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 s = r = alloc(len);
5920 if (r != NULL)
5921 {
5922 if (function)
5923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 r += 10;
5926 }
5927 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 if (str != NULL)
5930 for (p = str; *p != NUL; )
5931 {
5932 if (*p == '\'')
5933 *r++ = '\'';
5934 MB_COPY_CHAR(p, r);
5935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 if (function)
5938 *r++ = ')';
5939 *r++ = NUL;
5940 }
5941 return s;
5942}
5943
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005944#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005945/*
5946 * Convert the string "text" to a floating point number.
5947 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5948 * this always uses a decimal point.
5949 * Returns the length of the text that was consumed.
5950 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005951 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005952string2float(
5953 char_u *text,
5954 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005955{
5956 char *s = (char *)text;
5957 float_T f;
5958
Bram Moolenaar62473612017-01-08 19:25:40 +01005959 /* MS-Windows does not deal with "inf" and "nan" properly. */
5960 if (STRNICMP(text, "inf", 3) == 0)
5961 {
5962 *value = INFINITY;
5963 return 3;
5964 }
5965 if (STRNICMP(text, "-inf", 3) == 0)
5966 {
5967 *value = -INFINITY;
5968 return 4;
5969 }
5970 if (STRNICMP(text, "nan", 3) == 0)
5971 {
5972 *value = NAN;
5973 return 3;
5974 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005975 f = strtod(s, &s);
5976 *value = f;
5977 return (int)((char_u *)s - text);
5978}
5979#endif
5980
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 * Get the value of an environment variable.
5983 * "arg" is pointing to the '$'. It is advanced to after the name.
5984 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005985 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 */
5987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 char_u *string = NULL;
5991 int len;
5992 int cc;
5993 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005994 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 ++*arg;
5997 name = *arg;
5998 len = get_env_len(arg);
5999 if (evaluate)
6000 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006001 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006002 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006003
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006004 cc = name[len];
6005 name[len] = NUL;
6006 /* first try vim_getenv(), fast for normal environment vars */
6007 string = vim_getenv(name, &mustfree);
6008 if (string != NULL && *string != NUL)
6009 {
6010 if (!mustfree)
6011 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006013 else
6014 {
6015 if (mustfree)
6016 vim_free(string);
6017
6018 /* next try expanding things like $VIM and ${HOME} */
6019 string = expand_env_save(name - 1);
6020 if (string != NULL && *string == '$')
6021 {
6022 vim_free(string);
6023 string = NULL;
6024 }
6025 }
6026 name[len] = cc;
6027
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006028 rettv->v_type = VAR_STRING;
6029 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
6031
6032 return OK;
6033}
6034
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006035
6036
6037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006039 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042var2fpos(
6043 typval_T *varp,
6044 int dollar_lnum, /* TRUE when $ is last line */
6045 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006047 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006049 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
Bram Moolenaara5525202006-03-02 22:52:09 +00006051 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006052 if (varp->v_type == VAR_LIST)
6053 {
6054 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006055 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006056 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006057 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006058
6059 l = varp->vval.v_list;
6060 if (l == NULL)
6061 return NULL;
6062
6063 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006064 pos.lnum = list_find_nr(l, 0L, &error);
6065 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006066 return NULL; /* invalid line number */
6067
6068 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006069 pos.col = list_find_nr(l, 1L, &error);
6070 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006071 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006072 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006073
6074 /* We accept "$" for the column number: last column. */
6075 li = list_find(l, 1L);
6076 if (li != NULL && li->li_tv.v_type == VAR_STRING
6077 && li->li_tv.vval.v_string != NULL
6078 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6079 pos.col = len + 1;
6080
Bram Moolenaara5525202006-03-02 22:52:09 +00006081 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006082 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006083 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006084 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006085
Bram Moolenaara5525202006-03-02 22:52:09 +00006086#ifdef FEAT_VIRTUALEDIT
6087 /* Get the virtual offset. Defaults to zero. */
6088 pos.coladd = list_find_nr(l, 2L, &error);
6089 if (error)
6090 pos.coladd = 0;
6091#endif
6092
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006093 return &pos;
6094 }
6095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006096 name = get_tv_string_chk(varp);
6097 if (name == NULL)
6098 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006099 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006101 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6102 {
6103 if (VIsual_active)
6104 return &VIsual;
6105 return &curwin->w_cursor;
6106 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006107 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006109 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6111 return NULL;
6112 return pp;
6113 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006114
6115#ifdef FEAT_VIRTUALEDIT
6116 pos.coladd = 0;
6117#endif
6118
Bram Moolenaar477933c2007-07-17 14:32:23 +00006119 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006120 {
6121 pos.col = 0;
6122 if (name[1] == '0') /* "w0": first visible line */
6123 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006124 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006125 /* In silent Ex mode topline is zero, but that's not a valid line
6126 * number; use one instead. */
6127 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006128 return &pos;
6129 }
6130 else if (name[1] == '$') /* "w$": last visible line */
6131 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006132 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006133 /* In silent Ex mode botline is zero, return zero then. */
6134 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006135 return &pos;
6136 }
6137 }
6138 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006140 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 pos.lnum = curbuf->b_ml.ml_line_count;
6143 pos.col = 0;
6144 }
6145 else
6146 {
6147 pos.lnum = curwin->w_cursor.lnum;
6148 pos.col = (colnr_T)STRLEN(ml_get_curline());
6149 }
6150 return &pos;
6151 }
6152 return NULL;
6153}
6154
6155/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006156 * Convert list in "arg" into a position and optional file number.
6157 * When "fnump" is NULL there is no file number, only 3 items.
6158 * Note that the column is passed on as-is, the caller may want to decrement
6159 * it to use 1 for the first column.
6160 * Return FAIL when conversion is not possible, doesn't check the position for
6161 * validity.
6162 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006164list2fpos(
6165 typval_T *arg,
6166 pos_T *posp,
6167 int *fnump,
6168 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006169{
6170 list_T *l = arg->vval.v_list;
6171 long i = 0;
6172 long n;
6173
Bram Moolenaar493c1782014-05-28 14:34:46 +02006174 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6175 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006176 if (arg->v_type != VAR_LIST
6177 || l == NULL
6178 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006179 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006180 return FAIL;
6181
6182 if (fnump != NULL)
6183 {
6184 n = list_find_nr(l, i++, NULL); /* fnum */
6185 if (n < 0)
6186 return FAIL;
6187 if (n == 0)
6188 n = curbuf->b_fnum; /* current buffer */
6189 *fnump = n;
6190 }
6191
6192 n = list_find_nr(l, i++, NULL); /* lnum */
6193 if (n < 0)
6194 return FAIL;
6195 posp->lnum = n;
6196
6197 n = list_find_nr(l, i++, NULL); /* col */
6198 if (n < 0)
6199 return FAIL;
6200 posp->col = n;
6201
6202#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006203 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006204 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006205 posp->coladd = 0;
6206 else
6207 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006208#endif
6209
Bram Moolenaar493c1782014-05-28 14:34:46 +02006210 if (curswantp != NULL)
6211 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6212
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006213 return OK;
6214}
6215
6216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 * Get the length of an environment variable name.
6218 * Advance "arg" to the first character after the name.
6219 * Return 0 for error.
6220 */
6221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006222get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 char_u *p;
6225 int len;
6226
6227 for (p = *arg; vim_isIDc(*p); ++p)
6228 ;
6229 if (p == *arg) /* no name found */
6230 return 0;
6231
6232 len = (int)(p - *arg);
6233 *arg = p;
6234 return len;
6235}
6236
6237/*
6238 * Get the length of the name of a function or internal variable.
6239 * "arg" is advanced to the first non-white character after the name.
6240 * Return 0 if something is wrong.
6241 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006243get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 char_u *p;
6246 int len;
6247
6248 /* Find the end of the name. */
6249 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006250 {
6251 if (*p == ':')
6252 {
6253 /* "s:" is start of "s:var", but "n:" is not and can be used in
6254 * slice "[n:]". Also "xx:" is not a namespace. */
6255 len = (int)(p - *arg);
6256 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6257 || len > 1)
6258 break;
6259 }
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if (p == *arg) /* no name found */
6262 return 0;
6263
6264 len = (int)(p - *arg);
6265 *arg = skipwhite(p);
6266
6267 return len;
6268}
6269
6270/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006271 * Get the length of the name of a variable or function.
6272 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006274 * Return -1 if curly braces expansion failed.
6275 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 * If the name contains 'magic' {}'s, expand them and return the
6277 * expanded name in an allocated string via 'alias' - caller must free.
6278 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006280get_name_len(
6281 char_u **arg,
6282 char_u **alias,
6283 int evaluate,
6284 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 char_u *p;
6288 char_u *expr_start;
6289 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 *alias = NULL; /* default to no alias */
6292
6293 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6294 && (*arg)[2] == (int)KE_SNR)
6295 {
6296 /* hard coded <SNR>, already translated */
6297 *arg += 3;
6298 return get_id_len(arg) + 3;
6299 }
6300 len = eval_fname_script(*arg);
6301 if (len > 0)
6302 {
6303 /* literal "<SID>", "s:" or "<SNR>" */
6304 *arg += len;
6305 }
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006308 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006310 p = find_name_end(*arg, &expr_start, &expr_end,
6311 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (expr_start != NULL)
6313 {
6314 char_u *temp_string;
6315
6316 if (!evaluate)
6317 {
6318 len += (int)(p - *arg);
6319 *arg = skipwhite(p);
6320 return len;
6321 }
6322
6323 /*
6324 * Include any <SID> etc in the expanded string:
6325 * Thus the -len here.
6326 */
6327 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6328 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006329 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 *alias = temp_string;
6331 *arg = skipwhite(p);
6332 return (int)STRLEN(temp_string);
6333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
6335 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006336 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 EMSG2(_(e_invexpr2), *arg);
6338
6339 return len;
6340}
6341
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006342/*
6343 * Find the end of a variable or function name, taking care of magic braces.
6344 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6345 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006346 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006347 * Return a pointer to just after the name. Equal to "arg" if there is no
6348 * valid name.
6349 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006351find_name_end(
6352 char_u *arg,
6353 char_u **expr_start,
6354 char_u **expr_end,
6355 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006357 int mb_nest = 0;
6358 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006360 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 *expr_start = NULL;
6365 *expr_end = NULL;
6366 }
6367
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006368 /* Quick check for valid starting character. */
6369 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6370 return arg;
6371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372 for (p = arg; *p != NUL
6373 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006374 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006375 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006377 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006378 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006379 if (*p == '\'')
6380 {
6381 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006382 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006383 ;
6384 if (*p == NUL)
6385 break;
6386 }
6387 else if (*p == '"')
6388 {
6389 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006390 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006391 if (*p == '\\' && p[1] != NUL)
6392 ++p;
6393 if (*p == NUL)
6394 break;
6395 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006396 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6397 {
6398 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006399 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006400 len = (int)(p - arg);
6401 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006402 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006403 break;
6404 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006406 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408 if (*p == '[')
6409 ++br_nest;
6410 else if (*p == ']')
6411 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006416 if (*p == '{')
6417 {
6418 mb_nest++;
6419 if (expr_start != NULL && *expr_start == NULL)
6420 *expr_start = p;
6421 }
6422 else if (*p == '}')
6423 {
6424 mb_nest--;
6425 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6426 *expr_end = p;
6427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 }
6430
6431 return p;
6432}
6433
6434/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006435 * Expands out the 'magic' {}'s in a variable/function name.
6436 * Note that this can call itself recursively, to deal with
6437 * constructs like foo{bar}{baz}{bam}
6438 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6439 * "in_start" ^
6440 * "expr_start" ^
6441 * "expr_end" ^
6442 * "in_end" ^
6443 *
6444 * Returns a new allocated string, which the caller must free.
6445 * Returns NULL for failure.
6446 */
6447 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006448make_expanded_name(
6449 char_u *in_start,
6450 char_u *expr_start,
6451 char_u *expr_end,
6452 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006453{
6454 char_u c1;
6455 char_u *retval = NULL;
6456 char_u *temp_result;
6457 char_u *nextcmd = NULL;
6458
6459 if (expr_end == NULL || in_end == NULL)
6460 return NULL;
6461 *expr_start = NUL;
6462 *expr_end = NUL;
6463 c1 = *in_end;
6464 *in_end = NUL;
6465
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006466 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006467 if (temp_result != NULL && nextcmd == NULL)
6468 {
6469 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6470 + (in_end - expr_end) + 1));
6471 if (retval != NULL)
6472 {
6473 STRCPY(retval, in_start);
6474 STRCAT(retval, temp_result);
6475 STRCAT(retval, expr_end + 1);
6476 }
6477 }
6478 vim_free(temp_result);
6479
6480 *in_end = c1; /* put char back for error messages */
6481 *expr_start = '{';
6482 *expr_end = '}';
6483
6484 if (retval != NULL)
6485 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006486 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006487 if (expr_start != NULL)
6488 {
6489 /* Further expansion! */
6490 temp_result = make_expanded_name(retval, expr_start,
6491 expr_end, temp_result);
6492 vim_free(retval);
6493 retval = temp_result;
6494 }
6495 }
6496
6497 return retval;
6498}
6499
6500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006507 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6508}
6509
6510/*
6511 * Return TRUE if character "c" can be used as the first character in a
6512 * variable or function name (excluding '{' and '}').
6513 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006515eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006516{
6517 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518}
6519
6520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 * Set number v: variable to "val".
6522 */
6523 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006524set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527}
6528
6529/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006530 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006532 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006533get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536}
6537
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006538/*
6539 * Get string v: variable value. Uses a static buffer, can only be used once.
6540 */
6541 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006542get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006543{
6544 return get_tv_string(&vimvars[idx].vv_tv);
6545}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006548 * Get List v: variable value. Caller must take care of reference count when
6549 * needed.
6550 */
6551 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006553{
6554 return vimvars[idx].vv_list;
6555}
6556
6557/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006558 * Set v:char to character "c".
6559 */
6560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006561set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006562{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006563 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006564
6565#ifdef FEAT_MBYTE
6566 if (has_mbyte)
6567 buf[(*mb_char2bytes)(c, buf)] = NUL;
6568 else
6569#endif
6570 {
6571 buf[0] = c;
6572 buf[1] = NUL;
6573 }
6574 set_vim_var_string(VV_CHAR, buf, -1);
6575}
6576
6577/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006578 * Set v:count to "count" and v:count1 to "count1".
6579 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 */
6581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006582set_vcount(
6583 long count,
6584 long count1,
6585 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006587 if (set_prevcount)
6588 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 vimvars[VV_COUNT].vv_nr = count;
6590 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591}
6592
6593/*
6594 * Set string v: variable to a copy of "val".
6595 */
6596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597set_vim_var_string(
6598 int idx,
6599 char_u *val,
6600 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaara542c682016-01-31 16:28:04 +01006602 clear_tv(&vimvars[idx].vv_di.di_tv);
6603 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610}
6611
6612/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006613 * Set List v: variable to "val".
6614 */
6615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006617{
Bram Moolenaara542c682016-01-31 16:28:04 +01006618 clear_tv(&vimvars[idx].vv_di.di_tv);
6619 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006620 vimvars[idx].vv_list = val;
6621 if (val != NULL)
6622 ++val->lv_refcount;
6623}
6624
6625/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006626 * Set Dictionary v: variable to "val".
6627 */
6628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006629set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006630{
6631 int todo;
6632 hashitem_T *hi;
6633
Bram Moolenaara542c682016-01-31 16:28:04 +01006634 clear_tv(&vimvars[idx].vv_di.di_tv);
6635 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006636 vimvars[idx].vv_dict = val;
6637 if (val != NULL)
6638 {
6639 ++val->dv_refcount;
6640
6641 /* Set readonly */
6642 todo = (int)val->dv_hashtab.ht_used;
6643 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6644 {
6645 if (HASHITEM_EMPTY(hi))
6646 continue;
6647 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006648 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006649 }
6650 }
6651}
6652
6653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 * Set v:register if needed.
6655 */
6656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006657set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
6659 char_u regname;
6660
6661 if (c == 0 || c == ' ')
6662 regname = '"';
6663 else
6664 regname = c;
6665 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 set_vim_var_string(VV_REG, &regname, 1);
6668}
6669
6670/*
6671 * Get or set v:exception. If "oldval" == NULL, return the current value.
6672 * Otherwise, restore the value to "oldval" and return NULL.
6673 * Must always be called in pairs to save and restore v:exception! Does not
6674 * take care of memory allocations.
6675 */
6676 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006677v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
6679 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 return NULL;
6684}
6685
6686/*
6687 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6688 * Otherwise, restore the value to "oldval" and return NULL.
6689 * Must always be called in pairs to save and restore v:throwpoint! Does not
6690 * take care of memory allocations.
6691 */
6692 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
Bram Moolenaare9a41262005-01-15 22:18:47 +00006698 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 return NULL;
6700}
6701
6702#if defined(FEAT_AUTOCMD) || defined(PROTO)
6703/*
6704 * Set v:cmdarg.
6705 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6706 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6707 * Must always be called in pairs!
6708 */
6709 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 char_u *oldval;
6713 char_u *newval;
6714 unsigned len;
6715
Bram Moolenaare9a41262005-01-15 22:18:47 +00006716 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006717 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006719 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006720 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006721 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006724 if (eap->force_bin == FORCE_BIN)
6725 len = 6;
6726 else if (eap->force_bin == FORCE_NOBIN)
6727 len = 8;
6728 else
6729 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006730
6731 if (eap->read_edit)
6732 len += 7;
6733
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006734 if (eap->force_ff != 0)
6735 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6736# ifdef FEAT_MBYTE
6737 if (eap->force_enc != 0)
6738 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006739 if (eap->bad_char != 0)
6740 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006741# endif
6742
6743 newval = alloc(len + 1);
6744 if (newval == NULL)
6745 return NULL;
6746
6747 if (eap->force_bin == FORCE_BIN)
6748 sprintf((char *)newval, " ++bin");
6749 else if (eap->force_bin == FORCE_NOBIN)
6750 sprintf((char *)newval, " ++nobin");
6751 else
6752 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006753
6754 if (eap->read_edit)
6755 STRCAT(newval, " ++edit");
6756
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006757 if (eap->force_ff != 0)
6758 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6759 eap->cmd + eap->force_ff);
6760# ifdef FEAT_MBYTE
6761 if (eap->force_enc != 0)
6762 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6763 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006764 if (eap->bad_char == BAD_KEEP)
6765 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6766 else if (eap->bad_char == BAD_DROP)
6767 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6768 else if (eap->bad_char != 0)
6769 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006770# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006772 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773}
6774#endif
6775
6776/*
6777 * Get the value of internal variable "name".
6778 * Return OK or FAIL.
6779 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006781get_var_tv(
6782 char_u *name,
6783 int len, /* length of "name" */
6784 typval_T *rettv, /* NULL when only checking existence */
6785 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6786 int verbose, /* may give error message */
6787 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006790 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
6794 /* truncate the name, so that we can use strcmp() */
6795 cc = name[len];
6796 name[len] = NUL;
6797
6798 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 * Check for user-defined variables.
6800 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006801 v = find_var(name, NULL, no_autoload);
6802 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006804 tv = &v->di_tv;
6805 if (dip != NULL)
6806 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006811 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 ret = FAIL;
6814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 name[len] = cc;
6819
6820 return ret;
6821}
6822
6823/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006824 * Check if variable "name[len]" is a local variable or an argument.
6825 * If so, "*eval_lavars_used" is set to TRUE.
6826 */
6827 static void
6828check_vars(char_u *name, int len)
6829{
6830 int cc;
6831 char_u *varname;
6832 hashtab_T *ht;
6833
6834 if (eval_lavars_used == NULL)
6835 return;
6836
6837 /* truncate the name, so that we can use strcmp() */
6838 cc = name[len];
6839 name[len] = NUL;
6840
6841 ht = find_var_ht(name, &varname);
6842 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6843 {
6844 if (find_var(name, NULL, TRUE) != NULL)
6845 *eval_lavars_used = TRUE;
6846 }
6847
6848 name[len] = cc;
6849}
6850
6851/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006852 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6853 * Also handle function call with Funcref variable: func(expr)
6854 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6855 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006857handle_subscript(
6858 char_u **arg,
6859 typval_T *rettv,
6860 int evaluate, /* do more than finding the end */
6861 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006862{
6863 int ret = OK;
6864 dict_T *selfdict = NULL;
6865 char_u *s;
6866 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006867 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868
6869 while (ret == OK
6870 && (**arg == '['
6871 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006872 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6873 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006874 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006875 {
6876 if (**arg == '(')
6877 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006878 partial_T *pt = NULL;
6879
Bram Moolenaard9fba312005-06-26 22:34:35 +00006880 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006881 if (evaluate)
6882 {
6883 functv = *rettv;
6884 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006885
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006886 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006887 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006888 {
6889 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006890 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006891 }
6892 else
6893 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006894 }
6895 else
6896 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006897 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006898 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006899 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006900
6901 /* Clear the funcref afterwards, so that deleting it while
6902 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006903 if (evaluate)
6904 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006905
6906 /* Stop the expression evaluation when immediately aborting on
6907 * error, or when an interrupt occurred or an exception was thrown
6908 * but not caught. */
6909 if (aborting())
6910 {
6911 if (ret == OK)
6912 clear_tv(rettv);
6913 ret = FAIL;
6914 }
6915 dict_unref(selfdict);
6916 selfdict = NULL;
6917 }
6918 else /* **arg == '[' || **arg == '.' */
6919 {
6920 dict_unref(selfdict);
6921 if (rettv->v_type == VAR_DICT)
6922 {
6923 selfdict = rettv->vval.v_dict;
6924 if (selfdict != NULL)
6925 ++selfdict->dv_refcount;
6926 }
6927 else
6928 selfdict = NULL;
6929 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6930 {
6931 clear_tv(rettv);
6932 ret = FAIL;
6933 }
6934 }
6935 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006936
Bram Moolenaar1d429612016-05-24 15:44:17 +02006937 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6938 * Don't do this when "Func" is already a partial that was bound
6939 * explicitly (pt_auto is FALSE). */
6940 if (selfdict != NULL
6941 && (rettv->v_type == VAR_FUNC
6942 || (rettv->v_type == VAR_PARTIAL
6943 && (rettv->vval.v_partial->pt_auto
6944 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006945 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006946
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006947 dict_unref(selfdict);
6948 return ret;
6949}
6950
6951/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006952 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006953 * value).
6954 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006955 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957{
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006959}
6960
6961/*
6962 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 * The string "s" must have been allocated, it is consumed.
6964 * Return NULL for out of memory, the variable otherwise.
6965 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 rettv = alloc_tv();
6972 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006974 rettv->v_type = VAR_STRING;
6975 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977 else
6978 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980}
6981
6982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006983 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 if (varp != NULL)
6989 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006990 switch (varp->v_type)
6991 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006992 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006993 func_unref(varp->vval.v_string);
6994 /*FALLTHROUGH*/
6995 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006996 vim_free(varp->vval.v_string);
6997 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006998 case VAR_PARTIAL:
6999 partial_unref(varp->vval.v_partial);
7000 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001 case VAR_LIST:
7002 list_unref(varp->vval.v_list);
7003 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007004 case VAR_DICT:
7005 dict_unref(varp->vval.v_dict);
7006 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007008#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009 job_unref(varp->vval.v_job);
7010 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007011#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007012 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007013#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007014 channel_unref(varp->vval.v_channel);
7015 break;
7016#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007017 case VAR_NUMBER:
7018 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007019 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007020 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007021 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 vim_free(varp);
7024 }
7025}
7026
7027/*
7028 * Free the memory for a variable value and set the value to NULL or 0.
7029 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007031clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 if (varp != NULL)
7034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007038 func_unref(varp->vval.v_string);
7039 /*FALLTHROUGH*/
7040 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 vim_free(varp->vval.v_string);
7042 varp->vval.v_string = NULL;
7043 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007044 case VAR_PARTIAL:
7045 partial_unref(varp->vval.v_partial);
7046 varp->vval.v_partial = NULL;
7047 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007048 case VAR_LIST:
7049 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007051 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 case VAR_DICT:
7053 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007056 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007057 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007058 varp->vval.v_number = 0;
7059 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007060 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007061#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007062 varp->vval.v_float = 0.0;
7063 break;
7064#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007065 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007067 job_unref(varp->vval.v_job);
7068 varp->vval.v_job = NULL;
7069#endif
7070 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007071 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007073 channel_unref(varp->vval.v_channel);
7074 varp->vval.v_channel = NULL;
7075#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007076 case VAR_UNKNOWN:
7077 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007079 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081}
7082
7083/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084 * Set the value of a variable to NULL without freeing items.
7085 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007087init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007088{
7089 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007091}
7092
7093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 * Get the number value of a variable.
7095 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 * For incompatible types, return 0.
7097 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7098 * caller of incompatible types: it sets *denote to TRUE if "denote"
7099 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007101 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007104 int error = FALSE;
7105
7106 return get_tv_number_chk(varp, &error); /* return 0L on error */
7107}
7108
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007109 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007111{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007112 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007117 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007118 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007119#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007120 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007121 break;
7122#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007123 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007124 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007125 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 break;
7127 case VAR_STRING:
7128 if (varp->vval.v_string != NULL)
7129 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007130 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007131 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007132 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007133 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007134 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007135 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007136 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007137 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007138 case VAR_SPECIAL:
7139 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7140 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007141 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007143 EMSG(_("E910: Using a Job as a Number"));
7144 break;
7145#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007146 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007148 EMSG(_("E913: Using a Channel as a Number"));
7149 break;
7150#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007151 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007152 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007155 if (denote == NULL) /* useful for values that must be unsigned */
7156 n = -1;
7157 else
7158 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007159 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160}
7161
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007162#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007165{
7166 switch (varp->v_type)
7167 {
7168 case VAR_NUMBER:
7169 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007170 case VAR_FLOAT:
7171 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007172 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007173 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007174 EMSG(_("E891: Using a Funcref as a Float"));
7175 break;
7176 case VAR_STRING:
7177 EMSG(_("E892: Using a String as a Float"));
7178 break;
7179 case VAR_LIST:
7180 EMSG(_("E893: Using a List as a Float"));
7181 break;
7182 case VAR_DICT:
7183 EMSG(_("E894: Using a Dictionary as a Float"));
7184 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007185 case VAR_SPECIAL:
7186 EMSG(_("E907: Using a special value as a Float"));
7187 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007188 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007189# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007190 EMSG(_("E911: Using a Job as a Float"));
7191 break;
7192# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007193 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007194# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007195 EMSG(_("E914: Using a Channel as a Float"));
7196 break;
7197# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007198 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007199 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007200 break;
7201 }
7202 return 0;
7203}
7204#endif
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 * Get the string value of a variable.
7208 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007209 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7210 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 * If the String variable has never been set, return an empty string.
7212 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007213 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7214 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007216 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007217get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 static char_u mybuf[NUMBUFLEN];
7220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222}
7223
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007224 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 char_u *res = get_tv_string_buf_chk(varp, buf);
7228
7229 return res != NULL ? res : (char_u *)"";
7230}
7231
Bram Moolenaar7d647822014-04-05 21:28:56 +02007232/*
7233 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7234 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007235 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237{
7238 static char_u mybuf[NUMBUFLEN];
7239
7240 return get_tv_string_buf_chk(varp, mybuf);
7241}
7242
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007243 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007245{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007246 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007249 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7250 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007251 return buf;
7252 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007253 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007254 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007255 break;
7256 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007257 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 break;
7259 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007260 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007261 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007262 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007263#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007264 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007265 break;
7266#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007267 case VAR_STRING:
7268 if (varp->vval.v_string != NULL)
7269 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007270 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007271 case VAR_SPECIAL:
7272 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7273 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007274 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007275#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007276 {
7277 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007278 char *status;
7279
7280 if (job == NULL)
7281 return (char_u *)"no process";
7282 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007283 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007284 : "run";
7285# ifdef UNIX
7286 vim_snprintf((char *)buf, NUMBUFLEN,
7287 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007288# elif defined(WIN32)
7289 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007290 "process %ld %s",
7291 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007292 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007293# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007294 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007295 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7296# endif
7297 return buf;
7298 }
7299#endif
7300 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007301 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007302#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007303 {
7304 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007305 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007306
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007307 if (channel == NULL)
7308 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7309 else
7310 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007311 "channel %d %s", channel->ch_id, status);
7312 return buf;
7313 }
7314#endif
7315 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007316 case VAR_UNKNOWN:
7317 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007318 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
7323/*
7324 * Find variable "name" in the list of variables.
7325 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007326 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007327 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007330 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007335 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
Bram Moolenaara7043832005-01-21 11:56:39 +00007337 ht = find_var_ht(name, &varname);
7338 if (htp != NULL)
7339 *htp = ht;
7340 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007342 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7343 if (ret != NULL)
7344 return ret;
7345
7346 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007347 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348}
7349
7350/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007351 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007352 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355find_var_in_ht(
7356 hashtab_T *ht,
7357 int htname,
7358 char_u *varname,
7359 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007360{
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 hashitem_T *hi;
7362
7363 if (*varname == NUL)
7364 {
7365 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007366 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007368 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 case 'g': return &globvars_var;
7370 case 'v': return &vimvars_var;
7371 case 'b': return &curbuf->b_bufvar;
7372 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007373 case 't': return &curtab->tp_winvar;
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 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007444 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 if (*name == 'v') /* v: variable */
7446 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007447 if (*name == 'a') /* a: function argument */
7448 return get_funccal_args_ht();
7449 if (*name == 'l') /* l: local function variable */
7450 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 if (*name == 's' /* script variable */
7452 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7453 return &SCRIPT_VARS(current_SID);
7454 return NULL;
7455}
7456
7457/*
7458 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007459 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 * Returns NULL when it doesn't exist.
7461 */
7462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007467 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (v == NULL)
7469 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471}
7472
7473/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 * sourcing this script and when executing functions defined in the script.
7476 */
7477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
Bram Moolenaara7043832005-01-21 11:56:39 +00007480 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 hashtab_T *ht;
7482 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7485 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007486 /* Re-allocating ga_data means that an ht_array pointing to
7487 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007489 for (i = 1; i <= ga_scripts.ga_len; ++i)
7490 {
7491 ht = &SCRIPT_VARS(i);
7492 if (ht->ht_mask == HT_INIT_SIZE - 1)
7493 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007494 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007496 }
7497
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 while (ga_scripts.ga_len < id)
7499 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007500 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007501 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007502 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505 }
7506}
7507
7508/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7510 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 */
7512 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007516 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007517 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007518 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007519 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 dict_var->di_tv.vval.v_dict = dict;
7521 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007522 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7524 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525}
7526
7527/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007528 * Unreference a dictionary initialized by init_var_dict().
7529 */
7530 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007531unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007532{
7533 /* Now the dict needs to be freed if no one else is using it, go back to
7534 * normal reference counting. */
7535 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7536 dict_unref(dict);
7537}
7538
7539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 * Frees all allocated variables and the value they contain.
7542 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 */
7544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007546{
7547 vars_clear_ext(ht, TRUE);
7548}
7549
7550/*
7551 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7552 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007553 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555{
Bram Moolenaara7043832005-01-21 11:56:39 +00007556 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 hashitem_T *hi;
7558 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007561 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007562 for (hi = ht->ht_array; todo > 0; ++hi)
7563 {
7564 if (!HASHITEM_EMPTY(hi))
7565 {
7566 --todo;
7567
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007569 * ht_array might change then. hash_clear() takes care of it
7570 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 v = HI2DI(hi);
7572 if (free_val)
7573 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007574 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007576 }
7577 }
7578 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007579 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580}
7581
Bram Moolenaara7043832005-01-21 11:56:39 +00007582/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 * Delete a variable from hashtab "ht" at item "hi".
7584 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007590
7591 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 clear_tv(&di->di_tv);
7593 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594}
7595
7596/*
7597 * List the value of one internal variable.
7598 */
7599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007600list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 char_u *tofree;
7603 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007604 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007606 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007607 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007608 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
7611
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007613list_one_var_a(
7614 char_u *prefix,
7615 char_u *name,
7616 int type,
7617 char_u *string,
7618 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
Bram Moolenaar31859182007-08-14 20:41:13 +00007620 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7621 msg_start();
7622 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (name != NULL) /* "a:" vars don't have a name stored */
7624 msg_puts(name);
7625 msg_putchar(' ');
7626 msg_advance(22);
7627 if (type == VAR_NUMBER)
7628 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007629 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007630 msg_putchar('*');
7631 else if (type == VAR_LIST)
7632 {
7633 msg_putchar('[');
7634 if (*string == '[')
7635 ++string;
7636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 else if (type == VAR_DICT)
7638 {
7639 msg_putchar('{');
7640 if (*string == '{')
7641 ++string;
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 else
7644 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007647
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007648 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007650 if (*first)
7651 {
7652 msg_clr_eos();
7653 *first = FALSE;
7654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655}
7656
7657/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 * If the variable already exists, the value is updated.
7660 * Otherwise the variable is created.
7661 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007663set_var(
7664 char_u *name,
7665 typval_T *tv,
7666 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007672 ht = find_var_ht(name, &varname);
7673 if (ht == NULL || *varname == NUL)
7674 {
7675 EMSG2(_(e_illvar), name);
7676 return;
7677 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007678 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007679
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007680 /* Search in parent scope which is possible to reference from lambda */
7681 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007682 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007683
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007684 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7685 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007686 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007691 if (var_check_ro(v->di_flags, name, FALSE)
7692 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007694
7695 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007696 * Handle setting internal v: variables separately where needed to
7697 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 */
7699 if (ht == &vimvarht)
7700 {
7701 if (v->di_tv.v_type == VAR_STRING)
7702 {
7703 vim_free(v->di_tv.vval.v_string);
7704 if (copy || tv->v_type != VAR_STRING)
7705 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7706 else
7707 {
7708 /* Take over the string to avoid an extra alloc/free. */
7709 v->di_tv.vval.v_string = tv->vval.v_string;
7710 tv->vval.v_string = NULL;
7711 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007712 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007714 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007715 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717 if (STRCMP(varname, "searchforward") == 0)
7718 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007719#ifdef FEAT_SEARCH_EXTRA
7720 else if (STRCMP(varname, "hlsearch") == 0)
7721 {
7722 no_hlsearch = !v->di_tv.vval.v_number;
7723 redraw_all_later(SOME_VALID);
7724 }
7725#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007726 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007727 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007728 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007729 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 }
7731
7732 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734 else /* add a new variable */
7735 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007736 /* Can't add "v:" variable. */
7737 if (ht == &vimvarht)
7738 {
7739 EMSG2(_(e_illvar), name);
7740 return;
7741 }
7742
Bram Moolenaar92124a32005-06-17 22:03:40 +00007743 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007744 if (!valid_varname(varname))
7745 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007746
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007747 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7748 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007749 if (v == NULL)
7750 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007754 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 return;
7756 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007757 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007759
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007760 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007762 else
7763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007765 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768}
7769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007771 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 * Also give an error message.
7773 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007775var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007776{
7777 if (flags & DI_FLAGS_RO)
7778 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007779 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 return TRUE;
7781 }
7782 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7783 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007784 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 return TRUE;
7786 }
7787 return FALSE;
7788}
7789
7790/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007791 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7792 * Also give an error message.
7793 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007795var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007796{
7797 if (flags & DI_FLAGS_FIX)
7798 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007799 EMSG2(_("E795: Cannot delete variable %s"),
7800 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007801 return TRUE;
7802 }
7803 return FALSE;
7804}
7805
7806/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007807 * Check if a funcref is assigned to a valid variable name.
7808 * Return TRUE and give an error if not.
7809 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007811var_check_func_name(
7812 char_u *name, /* points to start of variable name */
7813 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007814{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007815 /* Allow for w: b: s: and t:. */
7816 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007817 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7818 ? name[2] : name[0]))
7819 {
7820 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7821 name);
7822 return TRUE;
7823 }
7824 /* Don't allow hiding a function. When "v" is not NULL we might be
7825 * assigning another function to the same var, the type is checked
7826 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007827 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007828 {
7829 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7830 name);
7831 return TRUE;
7832 }
7833 return FALSE;
7834}
7835
7836/*
7837 * Check if a variable name is valid.
7838 * Return FALSE and give an error if not.
7839 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007842{
7843 char_u *p;
7844
7845 for (p = varname; *p != NUL; ++p)
7846 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7847 && *p != AUTOLOAD_CHAR)
7848 {
7849 EMSG2(_(e_illvar), varname);
7850 return FALSE;
7851 }
7852 return TRUE;
7853}
7854
7855/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007856 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007857 * Also give an error message, using "name" or _("name") when use_gettext is
7858 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007859 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007862{
7863 if (lock & VAR_LOCKED)
7864 {
7865 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007866 name == NULL ? (char_u *)_("Unknown")
7867 : use_gettext ? (char_u *)_(name)
7868 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007869 return TRUE;
7870 }
7871 if (lock & VAR_FIXED)
7872 {
7873 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007874 name == NULL ? (char_u *)_("Unknown")
7875 : use_gettext ? (char_u *)_(name)
7876 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007877 return TRUE;
7878 }
7879 return FALSE;
7880}
7881
7882/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007886 * It is OK for "from" and "to" to point to the same item. This is used to
7887 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007890copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007893 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 switch (from->v_type)
7895 {
7896 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007897 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898 to->vval.v_number = from->vval.v_number;
7899 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007900 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007901#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902 to->vval.v_float = from->vval.v_float;
7903 break;
7904#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007905 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007906#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007907 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007908 if (to->vval.v_job != NULL)
7909 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007910 break;
7911#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007912 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007914 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007915 if (to->vval.v_channel != NULL)
7916 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007917 break;
7918#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 case VAR_STRING:
7920 case VAR_FUNC:
7921 if (from->vval.v_string == NULL)
7922 to->vval.v_string = NULL;
7923 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007924 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007926 if (from->v_type == VAR_FUNC)
7927 func_ref(to->vval.v_string);
7928 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007930 case VAR_PARTIAL:
7931 if (from->vval.v_partial == NULL)
7932 to->vval.v_partial = NULL;
7933 else
7934 {
7935 to->vval.v_partial = from->vval.v_partial;
7936 ++to->vval.v_partial->pt_refcount;
7937 }
7938 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007939 case VAR_LIST:
7940 if (from->vval.v_list == NULL)
7941 to->vval.v_list = NULL;
7942 else
7943 {
7944 to->vval.v_list = from->vval.v_list;
7945 ++to->vval.v_list->lv_refcount;
7946 }
7947 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 case VAR_DICT:
7949 if (from->vval.v_dict == NULL)
7950 to->vval.v_dict = NULL;
7951 else
7952 {
7953 to->vval.v_dict = from->vval.v_dict;
7954 ++to->vval.v_dict->dv_refcount;
7955 }
7956 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007957 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007958 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007959 break;
7960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961}
7962
7963/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007964 * Make a copy of an item.
7965 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007966 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7967 * reference to an already copied list/dict can be used.
7968 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007971item_copy(
7972 typval_T *from,
7973 typval_T *to,
7974 int deep,
7975 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976{
7977 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007978 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007979
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981 {
7982 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007983 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984 }
7985 ++recurse;
7986
7987 switch (from->v_type)
7988 {
7989 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007991 case VAR_STRING:
7992 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007993 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007994 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007995 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007996 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007997 copy_tv(from, to);
7998 break;
7999 case VAR_LIST:
8000 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008001 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008002 if (from->vval.v_list == NULL)
8003 to->vval.v_list = NULL;
8004 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8005 {
8006 /* use the copy made earlier */
8007 to->vval.v_list = from->vval.v_list->lv_copylist;
8008 ++to->vval.v_list->lv_refcount;
8009 }
8010 else
8011 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8012 if (to->vval.v_list == NULL)
8013 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 break;
8015 case VAR_DICT:
8016 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008017 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008018 if (from->vval.v_dict == NULL)
8019 to->vval.v_dict = NULL;
8020 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8021 {
8022 /* use the copy made earlier */
8023 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8024 ++to->vval.v_dict->dv_refcount;
8025 }
8026 else
8027 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8028 if (to->vval.v_dict == NULL)
8029 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008030 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008031 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008032 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008033 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008034 }
8035 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008036 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037}
8038
8039/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 * This function is used by f_input() and f_inputdialog() functions. The third
8041 * argument to f_input() specifies the type of completion to use at the
8042 * prompt. The third argument to f_inputdialog() specifies the value to return
8043 * when the user cancels the prompt.
8044 */
8045 void
8046get_user_input(
8047 typval_T *argvars,
8048 typval_T *rettv,
8049 int inputdialog,
8050 int secret)
8051{
8052 char_u *prompt = get_tv_string_chk(&argvars[0]);
8053 char_u *p = NULL;
8054 int c;
8055 char_u buf[NUMBUFLEN];
8056 int cmd_silent_save = cmd_silent;
8057 char_u *defstr = (char_u *)"";
8058 int xp_type = EXPAND_NOTHING;
8059 char_u *xp_arg = NULL;
8060
8061 rettv->v_type = VAR_STRING;
8062 rettv->vval.v_string = NULL;
8063
8064#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008065 /* While starting up, there is no place to enter text. When running tests
8066 * with --not-a-term we assume feedkeys() will be used. */
8067 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 return;
8069#endif
8070
8071 cmd_silent = FALSE; /* Want to see the prompt. */
8072 if (prompt != NULL)
8073 {
8074 /* Only the part of the message after the last NL is considered as
8075 * prompt for the command line */
8076 p = vim_strrchr(prompt, '\n');
8077 if (p == NULL)
8078 p = prompt;
8079 else
8080 {
8081 ++p;
8082 c = *p;
8083 *p = NUL;
8084 msg_start();
8085 msg_clr_eos();
8086 msg_puts_attr(prompt, echo_attr);
8087 msg_didout = FALSE;
8088 msg_starthere();
8089 *p = c;
8090 }
8091 cmdline_row = msg_row;
8092
8093 if (argvars[1].v_type != VAR_UNKNOWN)
8094 {
8095 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8096 if (defstr != NULL)
8097 stuffReadbuffSpec(defstr);
8098
8099 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8100 {
8101 char_u *xp_name;
8102 int xp_namelen;
8103 long argt;
8104
8105 /* input() with a third argument: completion */
8106 rettv->vval.v_string = NULL;
8107
8108 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8109 if (xp_name == NULL)
8110 return;
8111
8112 xp_namelen = (int)STRLEN(xp_name);
8113
8114 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8115 &xp_arg) == FAIL)
8116 return;
8117 }
8118 }
8119
8120 if (defstr != NULL)
8121 {
8122 int save_ex_normal_busy = ex_normal_busy;
8123 ex_normal_busy = 0;
8124 rettv->vval.v_string =
8125 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8126 xp_type, xp_arg);
8127 ex_normal_busy = save_ex_normal_busy;
8128 }
8129 if (inputdialog && rettv->vval.v_string == NULL
8130 && argvars[1].v_type != VAR_UNKNOWN
8131 && argvars[2].v_type != VAR_UNKNOWN)
8132 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8133 &argvars[2], buf));
8134
8135 vim_free(xp_arg);
8136
8137 /* since the user typed this, no need to wait for return */
8138 need_wait_return = FALSE;
8139 msg_didout = FALSE;
8140 }
8141 cmd_silent = cmd_silent_save;
8142}
8143
8144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * ":echo expr1 ..." print each argument separated with a space, add a
8146 * newline at the end.
8147 * ":echon expr1 ..." print each argument plain.
8148 */
8149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008150ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008153 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 char_u *p;
8156 int needclr = TRUE;
8157 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008158 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
8160 if (eap->skip)
8161 ++emsg_skip;
8162 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8163 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164 /* If eval1() causes an error message the text from the command may
8165 * still need to be cleared. E.g., "echo 22,44". */
8166 need_clr_eos = needclr;
8167
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {
8171 /*
8172 * Report the invalid expression unless the expression evaluation
8173 * has been cancelled due to an aborting error, an interrupt, or an
8174 * exception.
8175 */
8176 if (!aborting())
8177 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 break;
8180 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008181 need_clr_eos = FALSE;
8182
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (!eap->skip)
8184 {
8185 if (atstart)
8186 {
8187 atstart = FALSE;
8188 /* Call msg_start() after eval1(), evaluating the expression
8189 * may cause a message to appear. */
8190 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008191 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008192 /* Mark the saved text as finishing the line, so that what
8193 * follows is displayed on a new line when scrolling back
8194 * at the more prompt. */
8195 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199 else if (eap->cmdidx == CMD_echo)
8200 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008201 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008202 if (p != NULL)
8203 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008205 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008207 if (*p != TAB && needclr)
8208 {
8209 /* remove any text still there from the command */
8210 msg_clr_eos();
8211 needclr = FALSE;
8212 }
8213 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 }
8215 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008216 {
8217#ifdef FEAT_MBYTE
8218 if (has_mbyte)
8219 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008220 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008221
8222 (void)msg_outtrans_len_attr(p, i, echo_attr);
8223 p += i - 1;
8224 }
8225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008227 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008230 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 arg = skipwhite(arg);
8234 }
8235 eap->nextcmd = check_nextcmd(arg);
8236
8237 if (eap->skip)
8238 --emsg_skip;
8239 else
8240 {
8241 /* remove text that may still be there from the command */
8242 if (needclr)
8243 msg_clr_eos();
8244 if (eap->cmdidx == CMD_echo)
8245 msg_end();
8246 }
8247}
8248
8249/*
8250 * ":echohl {name}".
8251 */
8252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008253ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254{
8255 int id;
8256
8257 id = syn_name2id(eap->arg);
8258 if (id == 0)
8259 echo_attr = 0;
8260 else
8261 echo_attr = syn_id2attr(id);
8262}
8263
8264/*
8265 * ":execute expr1 ..." execute the result of an expression.
8266 * ":echomsg expr1 ..." Print a message
8267 * ":echoerr expr1 ..." Print an error
8268 * Each gets spaces around each argument and a newline at the end for
8269 * echo commands
8270 */
8271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008272ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008275 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 int ret = OK;
8277 char_u *p;
8278 garray_T ga;
8279 int len;
8280 int save_did_emsg;
8281
8282 ga_init2(&ga, 1, 80);
8283
8284 if (eap->skip)
8285 ++emsg_skip;
8286 while (*arg != NUL && *arg != '|' && *arg != '\n')
8287 {
8288 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
8291 /*
8292 * Report the invalid expression unless the expression evaluation
8293 * has been cancelled due to an aborting error, an interrupt, or an
8294 * exception.
8295 */
8296 if (!aborting())
8297 EMSG2(_(e_invexpr2), p);
8298 ret = FAIL;
8299 break;
8300 }
8301
8302 if (!eap->skip)
8303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 len = (int)STRLEN(p);
8306 if (ga_grow(&ga, len + 2) == FAIL)
8307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 ret = FAIL;
8310 break;
8311 }
8312 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 ga.ga_len += len;
8316 }
8317
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 arg = skipwhite(arg);
8320 }
8321
8322 if (ret != FAIL && ga.ga_data != NULL)
8323 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008324 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8325 {
8326 /* Mark the already saved text as finishing the line, so that what
8327 * follows is displayed on a new line when scrolling back at the
8328 * more prompt. */
8329 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008330 }
8331
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008335 out_flush();
8336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 else if (eap->cmdidx == CMD_echoerr)
8338 {
8339 /* We don't want to abort following commands, restore did_emsg. */
8340 save_did_emsg = did_emsg;
8341 EMSG((char_u *)ga.ga_data);
8342 if (!force_abort)
8343 did_emsg = save_did_emsg;
8344 }
8345 else if (eap->cmdidx == CMD_execute)
8346 do_cmdline((char_u *)ga.ga_data,
8347 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8348 }
8349
8350 ga_clear(&ga);
8351
8352 if (eap->skip)
8353 --emsg_skip;
8354
8355 eap->nextcmd = check_nextcmd(arg);
8356}
8357
8358/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 * Find window specified by "vp" in tabpage "tp".
8360 */
8361 win_T *
8362find_win_by_nr(
8363 typval_T *vp,
8364 tabpage_T *tp UNUSED) /* NULL for current tab page */
8365{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 int nr;
8368
8369 nr = (int)get_tv_number_chk(vp, NULL);
8370
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 if (nr < 0)
8372 return NULL;
8373 if (nr == 0)
8374 return curwin;
8375
Bram Moolenaar29323592016-07-24 22:04:11 +02008376 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8377 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 if (nr >= LOWEST_WIN_ID)
8379 {
8380 if (wp->w_id == nr)
8381 return wp;
8382 }
8383 else if (--nr <= 0)
8384 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008385 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 if (nr >= LOWEST_WIN_ID)
8387 return NULL;
8388 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008389}
8390
8391/*
8392 * Find window specified by "wvp" in tabpage "tvp".
8393 */
8394 win_T *
8395find_tabwin(
8396 typval_T *wvp, /* VAR_UNKNOWN for current window */
8397 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8398{
8399 win_T *wp = NULL;
8400 tabpage_T *tp = NULL;
8401 long n;
8402
8403 if (wvp->v_type != VAR_UNKNOWN)
8404 {
8405 if (tvp->v_type != VAR_UNKNOWN)
8406 {
8407 n = (long)get_tv_number(tvp);
8408 if (n >= 0)
8409 tp = find_tabpage(n);
8410 }
8411 else
8412 tp = curtab;
8413
8414 if (tp != NULL)
8415 wp = find_win_by_nr(wvp, tp);
8416 }
8417 else
8418 wp = curwin;
8419
8420 return wp;
8421}
8422
8423/*
8424 * getwinvar() and gettabwinvar()
8425 */
8426 void
8427getwinvar(
8428 typval_T *argvars,
8429 typval_T *rettv,
8430 int off) /* 1 for gettabwinvar() */
8431{
8432 win_T *win;
8433 char_u *varname;
8434 dictitem_T *v;
8435 tabpage_T *tp = NULL;
8436 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 win_T *oldcurwin;
8438 tabpage_T *oldtabpage;
8439 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008441 if (off == 1)
8442 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8443 else
8444 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445 win = find_win_by_nr(&argvars[off], tp);
8446 varname = get_tv_string_chk(&argvars[off + 1]);
8447 ++emsg_off;
8448
8449 rettv->v_type = VAR_STRING;
8450 rettv->vval.v_string = NULL;
8451
8452 if (win != NULL && varname != NULL)
8453 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454 /* Set curwin to be our win, temporarily. Also set the tabpage,
8455 * otherwise the window is not valid. Only do this when needed,
8456 * autocommands get blocked. */
8457 need_switch_win = !(tp == curtab && win == curwin);
8458 if (!need_switch_win
8459 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008461 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008463 if (varname[1] == NUL)
8464 {
8465 /* get all window-local options in a dict */
8466 dict_T *opts = get_winbuf_options(FALSE);
8467
8468 if (opts != NULL)
8469 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008470 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008471 done = TRUE;
8472 }
8473 }
8474 else if (get_option_tv(&varname, rettv, 1) == OK)
8475 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 done = TRUE;
8477 }
8478 else
8479 {
8480 /* Look up the variable. */
8481 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8482 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8483 varname, FALSE);
8484 if (v != NULL)
8485 {
8486 copy_tv(&v->di_tv, rettv);
8487 done = TRUE;
8488 }
8489 }
8490 }
8491
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 if (need_switch_win)
8493 /* restore previous notion of curwin */
8494 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 }
8496
8497 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8498 /* use the default return value */
8499 copy_tv(&argvars[off + 2], rettv);
8500
8501 --emsg_off;
8502}
8503
8504/*
8505 * "setwinvar()" and "settabwinvar()" functions
8506 */
8507 void
8508setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8509{
8510 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511 win_T *save_curwin;
8512 tabpage_T *save_curtab;
8513 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 char_u *varname, *winvarname;
8515 typval_T *varp;
8516 char_u nbuf[NUMBUFLEN];
8517 tabpage_T *tp = NULL;
8518
8519 if (check_restricted() || check_secure())
8520 return;
8521
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 if (off == 1)
8523 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8524 else
8525 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 win = find_win_by_nr(&argvars[off], tp);
8527 varname = get_tv_string_chk(&argvars[off + 1]);
8528 varp = &argvars[off + 2];
8529
8530 if (win != NULL && varname != NULL && varp != NULL)
8531 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 need_switch_win = !(tp == curtab && win == curwin);
8533 if (!need_switch_win
8534 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 {
8536 if (*varname == '&')
8537 {
8538 long numval;
8539 char_u *strval;
8540 int error = FALSE;
8541
8542 ++varname;
8543 numval = (long)get_tv_number_chk(varp, &error);
8544 strval = get_tv_string_buf_chk(varp, nbuf);
8545 if (!error && strval != NULL)
8546 set_option_value(varname, numval, strval, OPT_LOCAL);
8547 }
8548 else
8549 {
8550 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8551 if (winvarname != NULL)
8552 {
8553 STRCPY(winvarname, "w:");
8554 STRCPY(winvarname + 2, varname);
8555 set_var(winvarname, varp, TRUE);
8556 vim_free(winvarname);
8557 }
8558 }
8559 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 if (need_switch_win)
8561 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 }
8563}
8564
8565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8567 * "arg" points to the "&" or '+' when called, to "option" when returning.
8568 * Returns NULL when no option name found. Otherwise pointer to the char
8569 * after the option name.
8570 */
8571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008572find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 char_u *p = *arg;
8575
8576 ++p;
8577 if (*p == 'g' && p[1] == ':')
8578 {
8579 *opt_flags = OPT_GLOBAL;
8580 p += 2;
8581 }
8582 else if (*p == 'l' && p[1] == ':')
8583 {
8584 *opt_flags = OPT_LOCAL;
8585 p += 2;
8586 }
8587 else
8588 *opt_flags = 0;
8589
8590 if (!ASCII_ISALPHA(*p))
8591 return NULL;
8592 *arg = p;
8593
8594 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8595 p += 4; /* termcap option */
8596 else
8597 while (ASCII_ISALPHA(*p))
8598 ++p;
8599 return p;
8600}
8601
8602/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008603 * Return the autoload script name for a function or variable name.
8604 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008606 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008607autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008608{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008609 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008610 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008611
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008612 /* Get the script file name: replace '#' with '/', append ".vim". */
8613 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8614 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008615 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008616 STRCPY(scriptname, "autoload/");
8617 STRCAT(scriptname, name);
8618 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8619 STRCAT(scriptname, ".vim");
8620 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8621 *p = '/';
8622 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008623}
8624
8625/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008626 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008627 * Return TRUE if a package was loaded.
8628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008630script_autoload(
8631 char_u *name,
8632 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008633{
8634 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008635 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008636 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008637 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008638
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008639 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008640 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008641 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008642 return FALSE;
8643
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008644 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008645
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008646 /* Find the name in the list of previously loaded package names. Skip
8647 * "autoload/", it's always the same. */
8648 for (i = 0; i < ga_loaded.ga_len; ++i)
8649 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8650 break;
8651 if (!reload && i < ga_loaded.ga_len)
8652 ret = FALSE; /* was loaded already */
8653 else
8654 {
8655 /* Remember the name if it wasn't loaded already. */
8656 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8657 {
8658 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8659 tofree = NULL;
8660 }
8661
8662 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008663 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008664 ret = TRUE;
8665 }
8666
8667 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008668 return ret;
8669}
8670
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8672typedef enum
8673{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008674 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8675 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8676 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677} var_flavour_T;
8678
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008679static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008682var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 char_u *p = varname;
8685
8686 if (ASCII_ISUPPER(*p))
8687 {
8688 while (*(++p))
8689 if (ASCII_ISLOWER(*p))
8690 return VAR_FLAVOUR_SESSION;
8691 return VAR_FLAVOUR_VIMINFO;
8692 }
8693 else
8694 return VAR_FLAVOUR_DEFAULT;
8695}
8696#endif
8697
8698#if defined(FEAT_VIMINFO) || defined(PROTO)
8699/*
8700 * Restore global vars that start with a capital from the viminfo file
8701 */
8702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008703read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008706 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008708 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
8710 if (!writing && (find_viminfo_parameter('!') != NULL))
8711 {
8712 tab = vim_strchr(virp->vir_line + 1, '\t');
8713 if (tab != NULL)
8714 {
8715 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008716 switch (*tab)
8717 {
8718 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008720 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008721#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008722 case 'D': type = VAR_DICT; break;
8723 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008724 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726
8727 tab = vim_strchr(tab, '\t');
8728 if (tab != NULL)
8729 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008730 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008731 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008732 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008734#ifdef FEAT_FLOAT
8735 else if (type == VAR_FLOAT)
8736 (void)string2float(tab + 1, &tv.vval.v_float);
8737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008739 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008740 if (type == VAR_DICT || type == VAR_LIST)
8741 {
8742 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8743
8744 if (etv == NULL)
8745 /* Failed to parse back the dict or list, use it as a
8746 * string. */
8747 tv.v_type = VAR_STRING;
8748 else
8749 {
8750 vim_free(tv.vval.v_string);
8751 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008752 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008753 }
8754 }
8755
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008756 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008757 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008758 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008759 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008760
8761 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008762 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008763 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8764 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 }
8766 }
8767 }
8768
8769 return viminfo_readline(virp);
8770}
8771
8772/*
8773 * Write global vars that start with a capital to the viminfo file
8774 */
8775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008776write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
Bram Moolenaar33570922005-01-25 22:26:29 +00008778 hashitem_T *hi;
8779 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008780 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008781 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008782 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008784 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 if (find_viminfo_parameter('!') == NULL)
8787 return;
8788
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008789 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008790
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008791 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008794 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008796 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 this_var = HI2DI(hi);
8798 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008801 {
8802 case VAR_STRING: s = "STR"; break;
8803 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008804 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008805 case VAR_DICT: s = "DIC"; break;
8806 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008807 case VAR_SPECIAL: s = "XPL"; break;
8808
8809 case VAR_UNKNOWN:
8810 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008811 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008812 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008813 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008814 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008815 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008816 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008817 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008818 if (p != NULL)
8819 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008820 vim_free(tofree);
8821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823 }
8824}
8825#endif
8826
8827#if defined(FEAT_SESSION) || defined(PROTO)
8828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008829store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 hashitem_T *hi;
8832 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008833 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 char_u *p, *t;
8835
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008836 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008837 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008839 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008841 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 this_var = HI2DI(hi);
8843 if ((this_var->di_tv.v_type == VAR_NUMBER
8844 || this_var->di_tv.v_type == VAR_STRING)
8845 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008846 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008847 /* Escape special characters with a backslash. Turn a LF and
8848 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008849 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008850 (char_u *)"\\\"\n\r");
8851 if (p == NULL) /* out of memory */
8852 break;
8853 for (t = p; *t != NUL; ++t)
8854 if (*t == '\n')
8855 *t = 'n';
8856 else if (*t == '\r')
8857 *t = 'r';
8858 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 this_var->di_key,
8860 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8861 : ' ',
8862 p,
8863 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8864 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008865 || put_eol(fd) == FAIL)
8866 {
8867 vim_free(p);
8868 return FAIL;
8869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 vim_free(p);
8871 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872#ifdef FEAT_FLOAT
8873 else if (this_var->di_tv.v_type == VAR_FLOAT
8874 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8875 {
8876 float_T f = this_var->di_tv.vval.v_float;
8877 int sign = ' ';
8878
8879 if (f < 0)
8880 {
8881 f = -f;
8882 sign = '-';
8883 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008884 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885 this_var->di_key, sign, f) < 0)
8886 || put_eol(fd) == FAIL)
8887 return FAIL;
8888 }
8889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891 }
8892 return OK;
8893}
8894#endif
8895
Bram Moolenaar661b1822005-07-28 22:36:45 +00008896/*
8897 * Display script name where an item was last set.
8898 * Should only be invoked when 'verbose' is non-zero.
8899 */
8900 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008901last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008902{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008903 char_u *p;
8904
Bram Moolenaar661b1822005-07-28 22:36:45 +00008905 if (scriptID != 0)
8906 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008907 p = home_replace_save(NULL, get_scriptname(scriptID));
8908 if (p != NULL)
8909 {
8910 verbose_enter();
8911 MSG_PUTS(_("\n\tLast set from "));
8912 MSG_PUTS(p);
8913 vim_free(p);
8914 verbose_leave();
8915 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008916 }
8917}
8918
Bram Moolenaar53744302015-07-17 17:38:22 +02008919/* reset v:option_new, v:option_old and v:option_type */
8920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008921reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008922{
8923 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8924 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8925 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8926}
8927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928/*
8929 * Prepare "gap" for an assert error and add the sourcing position.
8930 */
8931 void
8932prepare_assert_error(garray_T *gap)
8933{
8934 char buf[NUMBUFLEN];
8935
8936 ga_init2(gap, 1, 100);
8937 if (sourcing_name != NULL)
8938 {
8939 ga_concat(gap, sourcing_name);
8940 if (sourcing_lnum > 0)
8941 ga_concat(gap, (char_u *)" ");
8942 }
8943 if (sourcing_lnum > 0)
8944 {
8945 sprintf(buf, "line %ld", (long)sourcing_lnum);
8946 ga_concat(gap, (char_u *)buf);
8947 }
8948 if (sourcing_name != NULL || sourcing_lnum > 0)
8949 ga_concat(gap, (char_u *)": ");
8950}
8951
8952/*
8953 * Add an assert error to v:errors.
8954 */
8955 void
8956assert_error(garray_T *gap)
8957{
8958 struct vimvar *vp = &vimvars[VV_ERRORS];
8959
8960 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8961 /* Make sure v:errors is a list. */
8962 set_vim_var_list(VV_ERRORS, list_alloc());
8963 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8964}
8965
8966 void
8967assert_equal_common(typval_T *argvars, assert_type_T atype)
8968{
8969 garray_T ga;
8970
8971 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8972 != (atype == ASSERT_EQUAL))
8973 {
8974 prepare_assert_error(&ga);
8975 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8976 atype);
8977 assert_error(&ga);
8978 ga_clear(&ga);
8979 }
8980}
8981
8982 void
8983assert_match_common(typval_T *argvars, assert_type_T atype)
8984{
8985 garray_T ga;
8986 char_u buf1[NUMBUFLEN];
8987 char_u buf2[NUMBUFLEN];
8988 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8989 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8990
8991 if (pat == NULL || text == NULL)
8992 EMSG(_(e_invarg));
8993 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8994 {
8995 prepare_assert_error(&ga);
8996 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8997 atype);
8998 assert_error(&ga);
8999 ga_clear(&ga);
9000 }
9001}
9002
Bram Moolenaar61c04492016-07-23 15:35:35 +02009003 void
9004assert_inrange(typval_T *argvars)
9005{
9006 garray_T ga;
9007 int error = FALSE;
9008 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9009 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9010 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9011 char_u *tofree;
9012 char msg[200];
9013 char_u numbuf[NUMBUFLEN];
9014
9015 if (error)
9016 return;
9017 if (actual < lower || actual > upper)
9018 {
9019 prepare_assert_error(&ga);
9020 if (argvars[3].v_type != VAR_UNKNOWN)
9021 {
9022 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9023 vim_free(tofree);
9024 }
9025 else
9026 {
9027 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9028 (long)lower, (long)upper, (long)actual);
9029 ga_concat(&ga, (char_u *)msg);
9030 }
9031 assert_error(&ga);
9032 ga_clear(&ga);
9033 }
9034}
9035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036/*
9037 * Common for assert_true() and assert_false().
9038 */
9039 void
9040assert_bool(typval_T *argvars, int isTrue)
9041{
9042 int error = FALSE;
9043 garray_T ga;
9044
9045 if (argvars[0].v_type == VAR_SPECIAL
9046 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9047 return;
9048 if (argvars[0].v_type != VAR_NUMBER
9049 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9050 || error)
9051 {
9052 prepare_assert_error(&ga);
9053 fill_assert_error(&ga, &argvars[1],
9054 (char_u *)(isTrue ? "True" : "False"),
9055 NULL, &argvars[0], ASSERT_OTHER);
9056 assert_error(&ga);
9057 ga_clear(&ga);
9058 }
9059}
9060
9061 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009062assert_report(typval_T *argvars)
9063{
9064 garray_T ga;
9065
9066 prepare_assert_error(&ga);
9067 ga_concat(&ga, get_tv_string(&argvars[0]));
9068 assert_error(&ga);
9069 ga_clear(&ga);
9070}
9071
9072 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073assert_exception(typval_T *argvars)
9074{
9075 garray_T ga;
9076 char_u *error = get_tv_string_chk(&argvars[0]);
9077
9078 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9079 {
9080 prepare_assert_error(&ga);
9081 ga_concat(&ga, (char_u *)"v:exception is not set");
9082 assert_error(&ga);
9083 ga_clear(&ga);
9084 }
9085 else if (error != NULL
9086 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9087 {
9088 prepare_assert_error(&ga);
9089 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9090 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9091 assert_error(&ga);
9092 ga_clear(&ga);
9093 }
9094}
9095
9096 void
9097assert_fails(typval_T *argvars)
9098{
9099 char_u *cmd = get_tv_string_chk(&argvars[0]);
9100 garray_T ga;
9101
9102 called_emsg = FALSE;
9103 suppress_errthrow = TRUE;
9104 emsg_silent = TRUE;
9105 do_cmdline_cmd(cmd);
9106 if (!called_emsg)
9107 {
9108 prepare_assert_error(&ga);
9109 ga_concat(&ga, (char_u *)"command did not fail: ");
9110 ga_concat(&ga, cmd);
9111 assert_error(&ga);
9112 ga_clear(&ga);
9113 }
9114 else if (argvars[1].v_type != VAR_UNKNOWN)
9115 {
9116 char_u buf[NUMBUFLEN];
9117 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9118
9119 if (error == NULL
9120 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9121 {
9122 prepare_assert_error(&ga);
9123 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9124 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9125 assert_error(&ga);
9126 ga_clear(&ga);
9127 }
9128 }
9129
9130 called_emsg = FALSE;
9131 suppress_errthrow = FALSE;
9132 emsg_silent = FALSE;
9133 emsg_on_display = FALSE;
9134 set_vim_var_string(VV_ERRMSG, NULL, 0);
9135}
9136
9137/*
9138 * Append "str" to "gap", escaping unprintable characters.
9139 * Changes NL to \n, CR to \r, etc.
9140 */
9141 static void
9142ga_concat_esc(garray_T *gap, char_u *str)
9143{
9144 char_u *p;
9145 char_u buf[NUMBUFLEN];
9146
9147 if (str == NULL)
9148 {
9149 ga_concat(gap, (char_u *)"NULL");
9150 return;
9151 }
9152
9153 for (p = str; *p != NUL; ++p)
9154 switch (*p)
9155 {
9156 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9157 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9158 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9159 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9160 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9161 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9162 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9163 default:
9164 if (*p < ' ')
9165 {
9166 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9167 ga_concat(gap, buf);
9168 }
9169 else
9170 ga_append(gap, *p);
9171 break;
9172 }
9173}
9174
9175/*
9176 * Fill "gap" with information about an assert error.
9177 */
9178 void
9179fill_assert_error(
9180 garray_T *gap,
9181 typval_T *opt_msg_tv,
9182 char_u *exp_str,
9183 typval_T *exp_tv,
9184 typval_T *got_tv,
9185 assert_type_T atype)
9186{
9187 char_u numbuf[NUMBUFLEN];
9188 char_u *tofree;
9189
9190 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9191 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009192 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9193 vim_free(tofree);
9194 ga_concat(gap, (char_u *)": ");
9195 }
9196
9197 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9198 ga_concat(gap, (char_u *)"Pattern ");
9199 else if (atype == ASSERT_NOTEQUAL)
9200 ga_concat(gap, (char_u *)"Expected not equal to ");
9201 else
9202 ga_concat(gap, (char_u *)"Expected ");
9203 if (exp_str == NULL)
9204 {
9205 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206 vim_free(tofree);
9207 }
9208 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009209 ga_concat_esc(gap, exp_str);
9210 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009211 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009212 if (atype == ASSERT_MATCH)
9213 ga_concat(gap, (char_u *)" does not match ");
9214 else if (atype == ASSERT_NOTMATCH)
9215 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009216 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009217 ga_concat(gap, (char_u *)" but got ");
9218 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9219 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009220 }
9221}
9222
Bram Moolenaar53744302015-07-17 17:38:22 +02009223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224#endif /* FEAT_EVAL */
9225
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009227#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
9229#ifdef WIN3264
9230/*
9231 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9232 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009233static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9234static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9235static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009238 * Get the short path (8.3) for the filename in "fnamep".
9239 * Only works for a valid file name.
9240 * When the path gets longer "fnamep" is changed and the allocated buffer
9241 * is put in "bufp".
9242 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9243 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 */
9245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009246get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009248 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 char_u *newbuf;
9250
9251 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009252 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (l > len - 1)
9254 {
9255 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009256 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 newbuf = vim_strnsave(*fnamep, l);
9258 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
9261 vim_free(*bufp);
9262 *fnamep = *bufp = newbuf;
9263
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009264 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009265 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
9267
9268 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009269 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270}
9271
9272/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009273 * Get the short path (8.3) for the filename in "fname". The converted
9274 * path is returned in "bufp".
9275 *
9276 * Some of the directories specified in "fname" may not exist. This function
9277 * will shorten the existing directories at the beginning of the path and then
9278 * append the remaining non-existing path.
9279 *
9280 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009281 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009282 * bufp - Pointer to an allocated buffer for the filename.
9283 * fnamelen - Length of the filename pointed to by fname
9284 *
9285 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 */
9287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288shortpath_for_invalid_fname(
9289 char_u **fname,
9290 char_u **bufp,
9291 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009293 char_u *short_fname, *save_fname, *pbuf_unused;
9294 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009296 int old_len, len;
9297 int new_len, sfx_len;
9298 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
9300 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009301 old_len = *fnamelen;
9302 save_fname = vim_strnsave(*fname, old_len);
9303 pbuf_unused = NULL;
9304 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009306 endp = save_fname + old_len - 1; /* Find the end of the copy */
9307 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009309 /*
9310 * Try shortening the supplied path till it succeeds by removing one
9311 * directory at a time from the tail of the path.
9312 */
9313 len = 0;
9314 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009316 /* go back one path-separator */
9317 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9318 --endp;
9319 if (endp <= save_fname)
9320 break; /* processed the complete path */
9321
9322 /*
9323 * Replace the path separator with a NUL and try to shorten the
9324 * resulting path.
9325 */
9326 ch = *endp;
9327 *endp = 0;
9328 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009329 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009330 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9331 {
9332 retval = FAIL;
9333 goto theend;
9334 }
9335 *endp = ch; /* preserve the string */
9336
9337 if (len > 0)
9338 break; /* successfully shortened the path */
9339
9340 /* failed to shorten the path. Skip the path separator */
9341 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 }
9343
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009344 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009346 /*
9347 * Succeeded in shortening the path. Now concatenate the shortened
9348 * path with the remaining path at the tail.
9349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009351 /* Compute the length of the new path. */
9352 sfx_len = (int)(save_endp - endp) + 1;
9353 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009355 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009357 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009359 /* There is not enough space in the currently allocated string,
9360 * copy it to a buffer big enough. */
9361 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009363 {
9364 retval = FAIL;
9365 goto theend;
9366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 }
9368 else
9369 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009370 /* Transfer short_fname to the main buffer (it's big enough),
9371 * unless get_short_pathname() did its work in-place. */
9372 *fname = *bufp = save_fname;
9373 if (short_fname != save_fname)
9374 vim_strncpy(save_fname, short_fname, len);
9375 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009377
9378 /* concat the not-shortened part of the path */
9379 vim_strncpy(*fname + len, endp, sfx_len);
9380 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009382
9383theend:
9384 vim_free(pbuf_unused);
9385 vim_free(save_fname);
9386
9387 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
9390/*
9391 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009392 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 */
9394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395shortpath_for_partial(
9396 char_u **fnamep,
9397 char_u **bufp,
9398 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
9400 int sepcount, len, tflen;
9401 char_u *p;
9402 char_u *pbuf, *tfname;
9403 int hasTilde;
9404
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009405 /* Count up the path separators from the RHS.. so we know which part
9406 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009408 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 if (vim_ispathsep(*p))
9410 ++sepcount;
9411
9412 /* Need full path first (use expand_env() to remove a "~/") */
9413 hasTilde = (**fnamep == '~');
9414 if (hasTilde)
9415 pbuf = tfname = expand_env_save(*fnamep);
9416 else
9417 pbuf = tfname = FullName_save(*fnamep, FALSE);
9418
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009419 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009421 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423
9424 if (len == 0)
9425 {
9426 /* Don't have a valid filename, so shorten the rest of the
9427 * path if we can. This CAN give us invalid 8.3 filenames, but
9428 * there's not a lot of point in guessing what it might be.
9429 */
9430 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009431 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 }
9434
9435 /* Count the paths backward to find the beginning of the desired string. */
9436 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009437 {
9438#ifdef FEAT_MBYTE
9439 if (has_mbyte)
9440 p -= mb_head_off(tfname, p);
9441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 if (vim_ispathsep(*p))
9443 {
9444 if (sepcount == 0 || (hasTilde && sepcount == 1))
9445 break;
9446 else
9447 sepcount --;
9448 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (hasTilde)
9451 {
9452 --p;
9453 if (p >= tfname)
9454 *p = '~';
9455 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 }
9458 else
9459 ++p;
9460
9461 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9462 vim_free(*bufp);
9463 *fnamelen = (int)STRLEN(p);
9464 *bufp = pbuf;
9465 *fnamep = p;
9466
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009467 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468}
9469#endif /* WIN3264 */
9470
9471/*
9472 * Adjust a filename, according to a string of modifiers.
9473 * *fnamep must be NUL terminated when called. When returning, the length is
9474 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009475 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 * When there is an error, *fnamep is set to NULL.
9477 */
9478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009479modify_fname(
9480 char_u *src, /* string with modifiers */
9481 int *usedlen, /* characters after src that are used */
9482 char_u **fnamep, /* file name so far */
9483 char_u **bufp, /* buffer for allocated file name or NULL */
9484 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 int valid = 0;
9487 char_u *tail;
9488 char_u *s, *p, *pbuf;
9489 char_u dirname[MAXPATHL];
9490 int c;
9491 int has_fullname = 0;
9492#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009493 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 int has_shortname = 0;
9495#endif
9496
9497repeat:
9498 /* ":p" - full path/file_name */
9499 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9500 {
9501 has_fullname = 1;
9502
9503 valid |= VALID_PATH;
9504 *usedlen += 2;
9505
9506 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9507 if ((*fnamep)[0] == '~'
9508#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9509 && ((*fnamep)[1] == '/'
9510# ifdef BACKSLASH_IN_FILENAME
9511 || (*fnamep)[1] == '\\'
9512# endif
9513 || (*fnamep)[1] == NUL)
9514
9515#endif
9516 )
9517 {
9518 *fnamep = expand_env_save(*fnamep);
9519 vim_free(*bufp); /* free any allocated file name */
9520 *bufp = *fnamep;
9521 if (*fnamep == NULL)
9522 return -1;
9523 }
9524
9525 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009526 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 {
9528 if (vim_ispathsep(*p)
9529 && p[1] == '.'
9530 && (p[2] == NUL
9531 || vim_ispathsep(p[2])
9532 || (p[2] == '.'
9533 && (p[3] == NUL || vim_ispathsep(p[3])))))
9534 break;
9535 }
9536
9537 /* FullName_save() is slow, don't use it when not needed. */
9538 if (*p != NUL || !vim_isAbsName(*fnamep))
9539 {
9540 *fnamep = FullName_save(*fnamep, *p != NUL);
9541 vim_free(*bufp); /* free any allocated file name */
9542 *bufp = *fnamep;
9543 if (*fnamep == NULL)
9544 return -1;
9545 }
9546
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009547#ifdef WIN3264
9548# if _WIN32_WINNT >= 0x0500
9549 if (vim_strchr(*fnamep, '~') != NULL)
9550 {
9551 /* Expand 8.3 filename to full path. Needed to make sure the same
9552 * file does not have two different names.
9553 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9554 p = alloc(_MAX_PATH + 1);
9555 if (p != NULL)
9556 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009557 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009558 {
9559 vim_free(*bufp);
9560 *bufp = *fnamep = p;
9561 }
9562 else
9563 vim_free(p);
9564 }
9565 }
9566# endif
9567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 /* Append a path separator to a directory. */
9569 if (mch_isdir(*fnamep))
9570 {
9571 /* Make room for one or two extra characters. */
9572 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9573 vim_free(*bufp); /* free any allocated file name */
9574 *bufp = *fnamep;
9575 if (*fnamep == NULL)
9576 return -1;
9577 add_pathsep(*fnamep);
9578 }
9579 }
9580
9581 /* ":." - path relative to the current directory */
9582 /* ":~" - path relative to the home directory */
9583 /* ":8" - shortname path - postponed till after */
9584 while (src[*usedlen] == ':'
9585 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9586 {
9587 *usedlen += 2;
9588 if (c == '8')
9589 {
9590#ifdef WIN3264
9591 has_shortname = 1; /* Postpone this. */
9592#endif
9593 continue;
9594 }
9595 pbuf = NULL;
9596 /* Need full path first (use expand_env() to remove a "~/") */
9597 if (!has_fullname)
9598 {
9599 if (c == '.' && **fnamep == '~')
9600 p = pbuf = expand_env_save(*fnamep);
9601 else
9602 p = pbuf = FullName_save(*fnamep, FALSE);
9603 }
9604 else
9605 p = *fnamep;
9606
9607 has_fullname = 0;
9608
9609 if (p != NULL)
9610 {
9611 if (c == '.')
9612 {
9613 mch_dirname(dirname, MAXPATHL);
9614 s = shorten_fname(p, dirname);
9615 if (s != NULL)
9616 {
9617 *fnamep = s;
9618 if (pbuf != NULL)
9619 {
9620 vim_free(*bufp); /* free any allocated file name */
9621 *bufp = pbuf;
9622 pbuf = NULL;
9623 }
9624 }
9625 }
9626 else
9627 {
9628 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9629 /* Only replace it when it starts with '~' */
9630 if (*dirname == '~')
9631 {
9632 s = vim_strsave(dirname);
9633 if (s != NULL)
9634 {
9635 *fnamep = s;
9636 vim_free(*bufp);
9637 *bufp = s;
9638 }
9639 }
9640 }
9641 vim_free(pbuf);
9642 }
9643 }
9644
9645 tail = gettail(*fnamep);
9646 *fnamelen = (int)STRLEN(*fnamep);
9647
9648 /* ":h" - head, remove "/file_name", can be repeated */
9649 /* Don't remove the first "/" or "c:\" */
9650 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9651 {
9652 valid |= VALID_HEAD;
9653 *usedlen += 2;
9654 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009655 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009656 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 *fnamelen = (int)(tail - *fnamep);
9658#ifdef VMS
9659 if (*fnamelen > 0)
9660 *fnamelen += 1; /* the path separator is part of the path */
9661#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009662 if (*fnamelen == 0)
9663 {
9664 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9665 p = vim_strsave((char_u *)".");
9666 if (p == NULL)
9667 return -1;
9668 vim_free(*bufp);
9669 *bufp = *fnamep = tail = p;
9670 *fnamelen = 1;
9671 }
9672 else
9673 {
9674 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009675 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 }
9678
9679 /* ":8" - shortname */
9680 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9681 {
9682 *usedlen += 2;
9683#ifdef WIN3264
9684 has_shortname = 1;
9685#endif
9686 }
9687
9688#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009689 /*
9690 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 */
9692 if (has_shortname)
9693 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009694 /* Copy the string if it is shortened by :h and when it wasn't copied
9695 * yet, because we are going to change it in place. Avoids changing
9696 * the buffer name for "%:8". */
9697 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 {
9699 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009700 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 return -1;
9702 vim_free(*bufp);
9703 *bufp = *fnamep = p;
9704 }
9705
9706 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009707 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (!has_fullname && !vim_isAbsName(*fnamep))
9709 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009710 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 return -1;
9712 }
9713 else
9714 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009715 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716
Bram Moolenaardc935552011-08-17 15:23:23 +02009717 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009719 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 return -1;
9721
9722 if (l == 0)
9723 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009724 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009726 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 return -1;
9728 }
9729 *fnamelen = l;
9730 }
9731 }
9732#endif /* WIN3264 */
9733
9734 /* ":t" - tail, just the basename */
9735 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9736 {
9737 *usedlen += 2;
9738 *fnamelen -= (int)(tail - *fnamep);
9739 *fnamep = tail;
9740 }
9741
9742 /* ":e" - extension, can be repeated */
9743 /* ":r" - root, without extension, can be repeated */
9744 while (src[*usedlen] == ':'
9745 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9746 {
9747 /* find a '.' in the tail:
9748 * - for second :e: before the current fname
9749 * - otherwise: The last '.'
9750 */
9751 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9752 s = *fnamep - 2;
9753 else
9754 s = *fnamep + *fnamelen - 1;
9755 for ( ; s > tail; --s)
9756 if (s[0] == '.')
9757 break;
9758 if (src[*usedlen + 1] == 'e') /* :e */
9759 {
9760 if (s > tail)
9761 {
9762 *fnamelen += (int)(*fnamep - (s + 1));
9763 *fnamep = s + 1;
9764#ifdef VMS
9765 /* cut version from the extension */
9766 s = *fnamep + *fnamelen - 1;
9767 for ( ; s > *fnamep; --s)
9768 if (s[0] == ';')
9769 break;
9770 if (s > *fnamep)
9771 *fnamelen = s - *fnamep;
9772#endif
9773 }
9774 else if (*fnamep <= tail)
9775 *fnamelen = 0;
9776 }
9777 else /* :r */
9778 {
9779 if (s > tail) /* remove one extension */
9780 *fnamelen = (int)(s - *fnamep);
9781 }
9782 *usedlen += 2;
9783 }
9784
9785 /* ":s?pat?foo?" - substitute */
9786 /* ":gs?pat?foo?" - global substitute */
9787 if (src[*usedlen] == ':'
9788 && (src[*usedlen + 1] == 's'
9789 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9790 {
9791 char_u *str;
9792 char_u *pat;
9793 char_u *sub;
9794 int sep;
9795 char_u *flags;
9796 int didit = FALSE;
9797
9798 flags = (char_u *)"";
9799 s = src + *usedlen + 2;
9800 if (src[*usedlen + 1] == 'g')
9801 {
9802 flags = (char_u *)"g";
9803 ++s;
9804 }
9805
9806 sep = *s++;
9807 if (sep)
9808 {
9809 /* find end of pattern */
9810 p = vim_strchr(s, sep);
9811 if (p != NULL)
9812 {
9813 pat = vim_strnsave(s, (int)(p - s));
9814 if (pat != NULL)
9815 {
9816 s = p + 1;
9817 /* find end of substitution */
9818 p = vim_strchr(s, sep);
9819 if (p != NULL)
9820 {
9821 sub = vim_strnsave(s, (int)(p - s));
9822 str = vim_strnsave(*fnamep, *fnamelen);
9823 if (sub != NULL && str != NULL)
9824 {
9825 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009826 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 if (s != NULL)
9828 {
9829 *fnamep = s;
9830 *fnamelen = (int)STRLEN(s);
9831 vim_free(*bufp);
9832 *bufp = s;
9833 didit = TRUE;
9834 }
9835 }
9836 vim_free(sub);
9837 vim_free(str);
9838 }
9839 vim_free(pat);
9840 }
9841 }
9842 /* after using ":s", repeat all the modifiers */
9843 if (didit)
9844 goto repeat;
9845 }
9846 }
9847
Bram Moolenaar26df0922014-02-23 23:39:13 +01009848 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9849 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009850 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009851 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009852 if (c != NUL)
9853 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009854 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009855 if (c != NUL)
9856 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009857 if (p == NULL)
9858 return -1;
9859 vim_free(*bufp);
9860 *bufp = *fnamep = p;
9861 *fnamelen = (int)STRLEN(p);
9862 *usedlen += 2;
9863 }
9864
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 return valid;
9866}
9867
9868/*
9869 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009870 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 * "flags" can be "g" to do a global substitute.
9872 * Returns an allocated string, NULL for error.
9873 */
9874 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009875do_string_sub(
9876 char_u *str,
9877 char_u *pat,
9878 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009879 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009880 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 int sublen;
9883 regmatch_T regmatch;
9884 int i;
9885 int do_all;
9886 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009887 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 garray_T ga;
9889 char_u *ret;
9890 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009891 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
9893 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9894 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009895 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
9897 ga_init2(&ga, 1, 200);
9898
9899 do_all = (flags[0] == 'g');
9900
9901 regmatch.rm_ic = p_ic;
9902 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9903 if (regmatch.regprog != NULL)
9904 {
9905 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009906 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9908 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009909 /* Skip empty match except for first match. */
9910 if (regmatch.startp[0] == regmatch.endp[0])
9911 {
9912 if (zero_width == regmatch.startp[0])
9913 {
9914 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009915 i = MB_PTR2LEN(tail);
9916 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9917 (size_t)i);
9918 ga.ga_len += i;
9919 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009920 continue;
9921 }
9922 zero_width = regmatch.startp[0];
9923 }
9924
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 /*
9926 * Get some space for a temporary buffer to do the substitution
9927 * into. It will contain:
9928 * - The text up to where the match is.
9929 * - The substituted text.
9930 * - The text after the match.
9931 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009932 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009933 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9935 {
9936 ga_clear(&ga);
9937 break;
9938 }
9939
9940 /* copy the text up to where the match is */
9941 i = (int)(regmatch.startp[0] - tail);
9942 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9943 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009944 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 + ga.ga_len + i, TRUE, TRUE, FALSE);
9946 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009947 tail = regmatch.endp[0];
9948 if (*tail == NUL)
9949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (!do_all)
9951 break;
9952 }
9953
9954 if (ga.ga_data != NULL)
9955 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9956
Bram Moolenaar473de612013-06-08 18:19:48 +02009957 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 }
9959
9960 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9961 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009962 if (p_cpo == empty_option)
9963 p_cpo = save_cpo;
9964 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009965 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009966 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967
9968 return ret;
9969}
9970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009971 static int
9972filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9973{
9974 typval_T rettv;
9975 typval_T argv[3];
9976 char_u buf[NUMBUFLEN];
9977 char_u *s;
9978 int retval = FAIL;
9979 int dummy;
9980
9981 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9982 argv[0] = vimvars[VV_KEY].vv_tv;
9983 argv[1] = vimvars[VV_VAL].vv_tv;
9984 if (expr->v_type == VAR_FUNC)
9985 {
9986 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009987 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9988 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989 goto theend;
9990 }
9991 else if (expr->v_type == VAR_PARTIAL)
9992 {
9993 partial_T *partial = expr->vval.v_partial;
9994
Bram Moolenaar437bafe2016-08-01 15:40:54 +02009995 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009996 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9997 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998 goto theend;
9999 }
10000 else
10001 {
10002 s = get_tv_string_buf_chk(expr, buf);
10003 if (s == NULL)
10004 goto theend;
10005 s = skipwhite(s);
10006 if (eval1(&s, &rettv, TRUE) == FAIL)
10007 goto theend;
10008 if (*s != NUL) /* check for trailing chars after expr */
10009 {
10010 EMSG2(_(e_invexpr2), s);
10011 goto theend;
10012 }
10013 }
10014 if (map)
10015 {
10016 /* map(): replace the list item value */
10017 clear_tv(tv);
10018 rettv.v_lock = 0;
10019 *tv = rettv;
10020 }
10021 else
10022 {
10023 int error = FALSE;
10024
10025 /* filter(): when expr is zero remove the item */
10026 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10027 clear_tv(&rettv);
10028 /* On type error, nothing has been removed; return FAIL to stop the
10029 * loop. The error message was given by get_tv_number_chk(). */
10030 if (error)
10031 goto theend;
10032 }
10033 retval = OK;
10034theend:
10035 clear_tv(&vimvars[VV_VAL].vv_tv);
10036 return retval;
10037}
10038
10039
10040/*
10041 * Implementation of map() and filter().
10042 */
10043 void
10044filter_map(typval_T *argvars, typval_T *rettv, int map)
10045{
10046 typval_T *expr;
10047 listitem_T *li, *nli;
10048 list_T *l = NULL;
10049 dictitem_T *di;
10050 hashtab_T *ht;
10051 hashitem_T *hi;
10052 dict_T *d = NULL;
10053 typval_T save_val;
10054 typval_T save_key;
10055 int rem;
10056 int todo;
10057 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10058 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10059 : N_("filter() argument"));
10060 int save_did_emsg;
10061 int idx = 0;
10062
10063 if (argvars[0].v_type == VAR_LIST)
10064 {
10065 if ((l = argvars[0].vval.v_list) == NULL
10066 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10067 return;
10068 }
10069 else if (argvars[0].v_type == VAR_DICT)
10070 {
10071 if ((d = argvars[0].vval.v_dict) == NULL
10072 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10073 return;
10074 }
10075 else
10076 {
10077 EMSG2(_(e_listdictarg), ermsg);
10078 return;
10079 }
10080
10081 expr = &argvars[1];
10082 /* On type errors, the preceding call has already displayed an error
10083 * message. Avoid a misleading error message for an empty string that
10084 * was not passed as argument. */
10085 if (expr->v_type != VAR_UNKNOWN)
10086 {
10087 prepare_vimvar(VV_VAL, &save_val);
10088
10089 /* We reset "did_emsg" to be able to detect whether an error
10090 * occurred during evaluation of the expression. */
10091 save_did_emsg = did_emsg;
10092 did_emsg = FALSE;
10093
10094 prepare_vimvar(VV_KEY, &save_key);
10095 if (argvars[0].v_type == VAR_DICT)
10096 {
10097 vimvars[VV_KEY].vv_type = VAR_STRING;
10098
10099 ht = &d->dv_hashtab;
10100 hash_lock(ht);
10101 todo = (int)ht->ht_used;
10102 for (hi = ht->ht_array; todo > 0; ++hi)
10103 {
10104 if (!HASHITEM_EMPTY(hi))
10105 {
10106 int r;
10107
10108 --todo;
10109 di = HI2DI(hi);
10110 if (map &&
10111 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10112 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10113 break;
10114 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10115 r = filter_map_one(&di->di_tv, expr, map, &rem);
10116 clear_tv(&vimvars[VV_KEY].vv_tv);
10117 if (r == FAIL || did_emsg)
10118 break;
10119 if (!map && rem)
10120 {
10121 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10122 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10123 break;
10124 dictitem_remove(d, di);
10125 }
10126 }
10127 }
10128 hash_unlock(ht);
10129 }
10130 else
10131 {
10132 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10133
10134 for (li = l->lv_first; li != NULL; li = nli)
10135 {
10136 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10137 break;
10138 nli = li->li_next;
10139 vimvars[VV_KEY].vv_nr = idx;
10140 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10141 || did_emsg)
10142 break;
10143 if (!map && rem)
10144 listitem_remove(l, li);
10145 ++idx;
10146 }
10147 }
10148
10149 restore_vimvar(VV_KEY, &save_key);
10150 restore_vimvar(VV_VAL, &save_val);
10151
10152 did_emsg |= save_did_emsg;
10153 }
10154
10155 copy_tv(&argvars[0], rettv);
10156}
10157
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */