blob: 85f607cb77890e2597243fcc019e7ecb3596a151 [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 Moolenaar65e4c4f2017-10-14 23:24:25 +0200190 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
191 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200192 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
193 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
194 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100195 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000196};
197
198/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_type vv_di.di_tv.v_type
200#define vv_nr vv_di.di_tv.vval.v_number
201#define vv_float vv_di.di_tv.vval.v_float
202#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000203#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200204#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000205#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000206
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200207static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000208#define vimvarht vimvardict.dv_hashtab
209
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100210static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
211static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
212static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_glob_vars(int *first);
214static void list_buf_vars(int *first);
215static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100216static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_vim_vars(int *first);
218static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
220static 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 +0100221static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
222static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
224static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
225static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
226static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000227
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static int eval2(char_u **arg, typval_T *rettv, int evaluate);
229static int eval3(char_u **arg, typval_T *rettv, int evaluate);
230static int eval4(char_u **arg, typval_T *rettv, int evaluate);
231static int eval5(char_u **arg, typval_T *rettv, int evaluate);
232static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
233static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000234
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
237static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static 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 +0200242static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static void delete_var(hashtab_T *ht, hashitem_T *hi);
245static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
246static 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 +0100247static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249/* for VIM_VERSION_ defines */
250#include "version.h"
251
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100252
253#if defined(EBCDIC) || defined(PROTO)
254/*
255 * Compare struct fst by function name.
256 */
257 static int
258compare_func_name(const void *s1, const void *s2)
259{
260 struct fst *p1 = (struct fst *)s1;
261 struct fst *p2 = (struct fst *)s2;
262
263 return STRCMP(p1->f_name, p2->f_name);
264}
265
266/*
267 * Sort the function table by function name.
268 * The sorting of the table above is ASCII dependant.
269 * On machines using EBCDIC we have to sort it.
270 */
271 static void
272sortFunctions(void)
273{
274 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
275
276 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
277}
278#endif
279
280
Bram Moolenaar33570922005-01-25 22:26:29 +0000281/*
282 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000283 */
284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100285eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000286{
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 int i;
288 struct vimvar *p;
289
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200290 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
291 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200292 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000293 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200294 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
296 for (i = 0; i < VV_LEN; ++i)
297 {
298 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200299 if (STRLEN(p->vv_name) > 16)
300 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100301 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 getout(1);
303 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 STRCPY(p->vv_di.di_key, p->vv_name);
305 if (p->vv_flags & VV_RO)
306 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
307 else if (p->vv_flags & VV_RO_SBX)
308 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
309 else
310 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000311
312 /* add to v: scope dict, unless the value is not always available */
313 if (p->vv_type != VAR_UNKNOWN)
314 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000315 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000316 /* add to compat scope dict */
317 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100319 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
320
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000321 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100322 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100323 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100324 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100325 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100326
327 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
328 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
329 set_vim_var_nr(VV_NONE, VVAL_NONE);
330 set_vim_var_nr(VV_NULL, VVAL_NULL);
331
Bram Moolenaarf562e722016-07-19 17:25:25 +0200332 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
333 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
334 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
335 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
336 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
337 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
338 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
339 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
340 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
341 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
342
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200343 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200344
345#ifdef EBCDIC
346 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100347 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348 */
349 sortFunctions();
350#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000351}
352
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000353#if defined(EXITFREE) || defined(PROTO)
354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100355eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000356{
357 int i;
358 struct vimvar *p;
359
360 for (i = 0; i < VV_LEN; ++i)
361 {
362 p = &vimvars[i];
363 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000364 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000365 vim_free(p->vv_str);
366 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000367 }
368 else if (p->vv_di.di_tv.v_type == VAR_LIST)
369 {
370 list_unref(p->vv_list);
371 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000372 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000373 }
374 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000375 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000376 hash_clear(&compat_hashtab);
377
Bram Moolenaard9fba312005-06-26 22:34:35 +0000378 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100379# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200380 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100381# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000382
383 /* global variables */
384 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000385
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000386 /* autoloaded script names */
387 ga_clear_strings(&ga_loaded);
388
Bram Moolenaarcca74132013-09-25 21:00:28 +0200389 /* Script-local variables. First clear all the variables and in a second
390 * loop free the scriptvar_T, because a variable in one script might hold
391 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200394 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200395 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200396 ga_clear(&ga_scripts);
397
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000398 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200399 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000400
401 /* functions */
402 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000403}
404#endif
405
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 * Set an internal variable to a string value. Creates the variable if it does
409 * not already exist.
410 */
411 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100412set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000414 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000415 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
417 val = vim_strsave(value);
418 if (val != NULL)
419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000420 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000421 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000423 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000424 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 }
426 }
427}
428
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000429static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200430#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000431static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000432static char_u *redir_endp = NULL;
433static char_u *redir_varname = NULL;
434
435/*
436 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100437 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000438 * Returns OK if successfully completed the setup. FAIL otherwise.
439 */
440 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100441var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000442{
443 int save_emsg;
444 int err;
445 typval_T tv;
446
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000447 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000448 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000449 {
450 EMSG(_(e_invarg));
451 return FAIL;
452 }
453
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000454 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000455 redir_varname = vim_strsave(name);
456 if (redir_varname == NULL)
457 return FAIL;
458
459 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
460 if (redir_lval == NULL)
461 {
462 var_redir_stop();
463 return FAIL;
464 }
465
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000466 /* The output is stored in growarray "redir_ga" until redirection ends. */
467 ga_init2(&redir_ga, (int)sizeof(char), 500);
468
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100470 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000471 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
473 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200474 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000475 if (redir_endp != NULL && *redir_endp != NUL)
476 /* Trailing characters are present after the variable name */
477 EMSG(_(e_trailing));
478 else
479 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000480 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000481 var_redir_stop();
482 return FAIL;
483 }
484
485 /* check if we can write to the variable: set it to or append an empty
486 * string */
487 save_emsg = did_emsg;
488 did_emsg = FALSE;
489 tv.v_type = VAR_STRING;
490 tv.vval.v_string = (char_u *)"";
491 if (append)
492 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
493 else
494 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200495 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000497 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 if (err)
499 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000500 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000501 var_redir_stop();
502 return FAIL;
503 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000504
505 return OK;
506}
507
508/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000509 * Append "value[value_len]" to the variable set by var_redir_start().
510 * The actual appending is postponed until redirection ends, because the value
511 * appended may in fact be the string we write to, changing it may cause freed
512 * memory to be used:
513 * :redir => foo
514 * :let foo
515 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516 */
517 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100518var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000519{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000520 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521
522 if (redir_lval == NULL)
523 return;
524
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000525 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000526 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000529
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000531 {
532 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000533 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000534 }
535 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537}
538
539/*
540 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000541 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542 */
543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100544var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000545{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000546 typval_T tv;
547
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200548 if (EVALCMD_BUSY)
549 {
550 redir_lval = NULL;
551 return;
552 }
553
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000554 if (redir_lval != NULL)
555 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000556 /* If there was no error: assign the text to the variable. */
557 if (redir_endp != NULL)
558 {
559 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
560 tv.v_type = VAR_STRING;
561 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200562 /* Call get_lval() again, if it's inside a Dict or List it may
563 * have changed. */
564 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100565 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200566 if (redir_endp != NULL && redir_lval->ll_name != NULL)
567 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
568 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000569 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000570
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000571 /* free the collected output */
572 vim_free(redir_ga.ga_data);
573 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000575 vim_free(redir_lval);
576 redir_lval = NULL;
577 }
578 vim_free(redir_varname);
579 redir_varname = NULL;
580}
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582# if defined(FEAT_MBYTE) || defined(PROTO)
583 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100584eval_charconvert(
585 char_u *enc_from,
586 char_u *enc_to,
587 char_u *fname_from,
588 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 int err = FALSE;
591
592 set_vim_var_string(VV_CC_FROM, enc_from, -1);
593 set_vim_var_string(VV_CC_TO, enc_to, -1);
594 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
595 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
596 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
597 err = TRUE;
598 set_vim_var_string(VV_CC_FROM, NULL, -1);
599 set_vim_var_string(VV_CC_TO, NULL, -1);
600 set_vim_var_string(VV_FNAME_IN, NULL, -1);
601 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
602
603 if (err)
604 return FAIL;
605 return OK;
606}
607# endif
608
609# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
610 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 int err = FALSE;
614
615 set_vim_var_string(VV_FNAME_IN, fname, -1);
616 set_vim_var_string(VV_CMDARG, args, -1);
617 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
618 err = TRUE;
619 set_vim_var_string(VV_FNAME_IN, NULL, -1);
620 set_vim_var_string(VV_CMDARG, NULL, -1);
621
622 if (err)
623 {
624 mch_remove(fname);
625 return FAIL;
626 }
627 return OK;
628}
629# endif
630
631# if defined(FEAT_DIFF) || defined(PROTO)
632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100633eval_diff(
634 char_u *origfile,
635 char_u *newfile,
636 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 int err = FALSE;
639
640 set_vim_var_string(VV_FNAME_IN, origfile, -1);
641 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
642 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
643 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
644 set_vim_var_string(VV_FNAME_IN, NULL, -1);
645 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
646 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
647}
648
649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100650eval_patch(
651 char_u *origfile,
652 char_u *difffile,
653 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 int err;
656
657 set_vim_var_string(VV_FNAME_IN, origfile, -1);
658 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
659 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
660 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
661 set_vim_var_string(VV_FNAME_IN, NULL, -1);
662 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
663 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
664}
665# endif
666
667/*
668 * Top level evaluation function, returning a boolean.
669 * Sets "error" to TRUE if there was an error.
670 * Return TRUE or FALSE.
671 */
672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100673eval_to_bool(
674 char_u *arg,
675 int *error,
676 char_u **nextcmd,
677 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
Bram Moolenaar33570922005-01-25 22:26:29 +0000679 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200680 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 if (skip)
683 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000684 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 else
687 {
688 *error = FALSE;
689 if (!skip)
690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000691 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000692 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694 }
695 if (skip)
696 --emsg_skip;
697
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200698 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
Bram Moolenaar48570482017-10-30 21:48:41 +0100701 static int
702eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
703{
704 char_u *s;
705 int dummy;
706 char_u buf[NUMBUFLEN];
707
708 if (expr->v_type == VAR_FUNC)
709 {
710 s = expr->vval.v_string;
711 if (s == NULL || *s == NUL)
712 return FAIL;
713 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
714 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
715 return FAIL;
716 }
717 else if (expr->v_type == VAR_PARTIAL)
718 {
719 partial_T *partial = expr->vval.v_partial;
720
721 s = partial_name(partial);
722 if (s == NULL || *s == NUL)
723 return FAIL;
724 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
725 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
726 return FAIL;
727 }
728 else
729 {
730 s = get_tv_string_buf_chk(expr, buf);
731 if (s == NULL)
732 return FAIL;
733 s = skipwhite(s);
734 if (eval1(&s, rettv, TRUE) == FAIL)
735 return FAIL;
736 if (*s != NUL) /* check for trailing chars after expr */
737 {
738 EMSG2(_(e_invexpr2), s);
739 return FAIL;
740 }
741 }
742 return OK;
743}
744
745/*
746 * Like eval_to_bool() but using a typval_T instead of a string.
747 * Works for string, funcref and partial.
748 */
749 int
750eval_expr_to_bool(typval_T *expr, int *error)
751{
752 typval_T rettv;
753 int res;
754
755 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
756 {
757 *error = TRUE;
758 return FALSE;
759 }
760 res = (get_tv_number_chk(&rettv, error) != 0);
761 clear_tv(&rettv);
762 return res;
763}
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/*
766 * Top level evaluation function, returning a string. If "skip" is TRUE,
767 * only parsing to "nextcmd" is done, without reporting errors. Return
768 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
769 */
770 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100771eval_to_string_skip(
772 char_u *arg,
773 char_u **nextcmd,
774 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 char_u *retval;
778
779 if (skip)
780 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 retval = NULL;
783 else
784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785 retval = vim_strsave(get_tv_string(&tv));
786 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788 if (skip)
789 --emsg_skip;
790
791 return retval;
792}
793
794/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000795 * Skip over an expression at "*pp".
796 * Return FAIL for an error, OK otherwise.
797 */
798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100799skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800{
Bram Moolenaar33570922005-01-25 22:26:29 +0000801 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000802
803 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000804 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000805}
806
807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000809 * When "convert" is TRUE convert a List into a sequence of lines and convert
810 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 * Return pointer to allocated memory, or NULL for failure.
812 */
813 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100814eval_to_string(
815 char_u *arg,
816 char_u **nextcmd,
817 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000821 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000822#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000823 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000826 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 retval = NULL;
828 else
829 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000830 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000831 {
832 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000833 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200834 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200835 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200836 if (tv.vval.v_list->lv_len > 0)
837 ga_append(&ga, NL);
838 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000839 ga_append(&ga, NUL);
840 retval = (char_u *)ga.ga_data;
841 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000842#ifdef FEAT_FLOAT
843 else if (convert && tv.v_type == VAR_FLOAT)
844 {
845 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
846 retval = vim_strsave(numbuf);
847 }
848#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000849 else
850 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853
854 return retval;
855}
856
857/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000858 * Call eval_to_string() without using current local variables and using
859 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100862eval_to_string_safe(
863 char_u *arg,
864 char_u **nextcmd,
865 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
867 char_u *retval;
868 void *save_funccalp;
869
870 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000871 if (use_sandbox)
872 ++sandbox;
873 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000874 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000875 if (use_sandbox)
876 --sandbox;
877 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 restore_funccal(save_funccalp);
879 return retval;
880}
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882/*
883 * Top level evaluation function, returning a number.
884 * Evaluates "expr" silently.
885 * Returns -1 for an error.
886 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200887 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200891 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000892 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 ++emsg_off;
895
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 retval = -1;
898 else
899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000900 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 --emsg_off;
904
905 return retval;
906}
907
Bram Moolenaara40058a2005-07-11 22:42:07 +0000908/*
909 * Prepare v: variable "idx" to be used.
910 * Save the current typeval in "save_tv".
911 * When not used yet add the variable to the v: hashtable.
912 */
913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100914prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000915{
916 *save_tv = vimvars[idx].vv_tv;
917 if (vimvars[idx].vv_type == VAR_UNKNOWN)
918 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
919}
920
921/*
922 * Restore v: variable "idx" to typeval "save_tv".
923 * When no longer defined, remove the variable from the v: hashtable.
924 */
925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100926restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000927{
928 hashitem_T *hi;
929
Bram Moolenaara40058a2005-07-11 22:42:07 +0000930 vimvars[idx].vv_tv = *save_tv;
931 if (vimvars[idx].vv_type == VAR_UNKNOWN)
932 {
933 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
934 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100935 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000936 else
937 hash_remove(&vimvarht, hi);
938 }
939}
940
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000941#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000942/*
943 * Evaluate an expression to a list with suggestions.
944 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000945 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000946 */
947 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000949{
950 typval_T save_val;
951 typval_T rettv;
952 list_T *list = NULL;
953 char_u *p = skipwhite(expr);
954
955 /* Set "v:val" to the bad word. */
956 prepare_vimvar(VV_VAL, &save_val);
957 vimvars[VV_VAL].vv_type = VAR_STRING;
958 vimvars[VV_VAL].vv_str = badword;
959 if (p_verbose == 0)
960 ++emsg_off;
961
962 if (eval1(&p, &rettv, TRUE) == OK)
963 {
964 if (rettv.v_type != VAR_LIST)
965 clear_tv(&rettv);
966 else
967 list = rettv.vval.v_list;
968 }
969
970 if (p_verbose == 0)
971 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000972 restore_vimvar(VV_VAL, &save_val);
973
974 return list;
975}
976
977/*
978 * "list" is supposed to contain two items: a word and a number. Return the
979 * word in "pp" and the number as the return value.
980 * Return -1 if anything isn't right.
981 * Used to get the good word and score from the eval_spell_expr() result.
982 */
983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100984get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000985{
986 listitem_T *li;
987
988 li = list->lv_first;
989 if (li == NULL)
990 return -1;
991 *pp = get_tv_string(&li->li_tv);
992
993 li = li->li_next;
994 if (li == NULL)
995 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200996 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000997}
998#endif
999
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001000/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001001 * Top level evaluation function.
1002 * Returns an allocated typval_T with the result.
1003 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001004 */
1005 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001007{
1008 typval_T *tv;
1009
1010 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001011 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001012 {
1013 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001014 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001015 }
1016
1017 return tv;
1018}
1019
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001022 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001023 * Uses argv[argc] for the function arguments. Only Number and String
1024 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001025 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001028call_vim_function(
1029 char_u *func,
1030 int argc,
1031 char_u **argv,
1032 int safe, /* use the sandbox */
1033 int str_arg_only, /* all arguments are strings */
1034 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001037 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 int len;
1039 int i;
1040 int doesrange;
1041 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001042 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001044 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048 for (i = 0; i < argc; i++)
1049 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001050 /* Pass a NULL or empty argument as an empty string */
1051 if (argv[i] == NULL || *argv[i] == NUL)
1052 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001053 argvars[i].v_type = VAR_STRING;
1054 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001055 continue;
1056 }
1057
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001058 if (str_arg_only)
1059 len = 0;
1060 else
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001061 {
1062 /* Recognize a number argument, the others must be strings. A dash
1063 * is a string too. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001064 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001065 if (len == 1 && *argv[i] == '-')
1066 len = 0;
1067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (len != 0 && len == (int)STRLEN(argv[i]))
1069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001070 argvars[i].v_type = VAR_NUMBER;
1071 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073 else
1074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001075 argvars[i].v_type = VAR_STRING;
1076 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079
1080 if (safe)
1081 {
1082 save_funccalp = save_funccal();
1083 ++sandbox;
1084 }
1085
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001087 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001089 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (safe)
1091 {
1092 --sandbox;
1093 restore_funccal(save_funccalp);
1094 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095 vim_free(argvars);
1096
1097 if (ret == FAIL)
1098 clear_tv(rettv);
1099
1100 return ret;
1101}
1102
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001103/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001104 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001105 * Returns -1 when calling the function fails.
1106 * Uses argv[argc] for the function arguments.
1107 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001108 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001109call_func_retnr(
1110 char_u *func,
1111 int argc,
1112 char_u **argv,
1113 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114{
1115 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001116 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001117
1118 /* All arguments are passed as strings, no conversion to number. */
1119 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1120 return -1;
1121
1122 retval = get_tv_number_chk(&rettv, NULL);
1123 clear_tv(&rettv);
1124 return retval;
1125}
1126
1127#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1128 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1129
Bram Moolenaar4f688582007-07-24 12:34:30 +00001130# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001131/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001132 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001133 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001134 * Uses argv[argc] for the function arguments.
1135 */
1136 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001137call_func_retstr(
1138 char_u *func,
1139 int argc,
1140 char_u **argv,
1141 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001142{
1143 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001144 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001145
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001146 /* All arguments are passed as strings, no conversion to number. */
1147 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001148 return NULL;
1149
1150 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001151 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 return retval;
1153}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001154# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001155
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001156/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001157 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001159 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001160 */
1161 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001162call_func_retlist(
1163 char_u *func,
1164 int argc,
1165 char_u **argv,
1166 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001167{
1168 typval_T rettv;
1169
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001170 /* All arguments are passed as strings, no conversion to number. */
1171 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001172 return NULL;
1173
1174 if (rettv.v_type != VAR_LIST)
1175 {
1176 clear_tv(&rettv);
1177 return NULL;
1178 }
1179
1180 return rettv.vval.v_list;
1181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183
Bram Moolenaar05159a02005-02-26 23:04:13 +00001184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef FEAT_FOLDING
1186/*
1187 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1188 * it in "*cp". Doesn't give error messages.
1189 */
1190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001191eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar33570922005-01-25 22:26:29 +00001193 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001194 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001196 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1197 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
1199 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001200 if (use_sandbox)
1201 ++sandbox;
1202 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 retval = 0;
1206 else
1207 {
1208 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (tv.v_type == VAR_NUMBER)
1210 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001211 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 retval = 0;
1213 else
1214 {
1215 /* If the result is a string, check if there is a non-digit before
1216 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (!VIM_ISDIGIT(*s) && *s != '-')
1219 *cp = *s++;
1220 retval = atol((char *)s);
1221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001225 if (use_sandbox)
1226 --sandbox;
1227 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001229 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230}
1231#endif
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 * ":let" list all variable values
1235 * ":let var1 var2" list variable values
1236 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 * ":let var += expr" assignment command.
1238 * ":let var -= expr" assignment command.
1239 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
1242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001243ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001247 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 int var_count = 0;
1250 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001252 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001253 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
Bram Moolenaardb552d602006-03-23 22:59:57 +00001255 argend = skip_var_list(arg, &var_count, &semicolon);
1256 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001257 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001258 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1259 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001260 expr = skipwhite(argend);
1261 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1262 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001264 /*
1265 * ":let" without "=": list variables
1266 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001267 if (*arg == '[')
1268 EMSG(_(e_invarg));
1269 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001271 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001275 list_glob_vars(&first);
1276 list_buf_vars(&first);
1277 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001278 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001279 list_script_vars(&first);
1280 list_func_vars(&first);
1281 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 eap->nextcmd = check_nextcmd(arg);
1284 }
1285 else
1286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 op[0] = '=';
1288 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001289 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001290 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001291 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1292 op[0] = *expr; /* +=, -= or .= */
1293 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001294 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001295 else
1296 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (eap->skip)
1299 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001300 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (eap->skip)
1302 {
1303 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 --emsg_skip;
1306 }
1307 else if (i != FAIL)
1308 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001309 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
1314}
1315
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001316/*
1317 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1318 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001319 * When "nextchars" is not NULL it points to a string with characters that
1320 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1321 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001322 * Returns OK or FAIL;
1323 */
1324 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001325ex_let_vars(
1326 char_u *arg_start,
1327 typval_T *tv,
1328 int copy, /* copy values from "tv", don't move */
1329 int semicolon, /* from skip_var_list() */
1330 int var_count, /* from skip_var_list() */
1331 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332{
1333 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001335 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 listitem_T *item;
1337 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001338
1339 if (*arg != '[')
1340 {
1341 /*
1342 * ":let var = expr" or ":for var in list"
1343 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001344 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001345 return FAIL;
1346 return OK;
1347 }
1348
1349 /*
1350 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1351 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001352 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001353 {
1354 EMSG(_(e_listreq));
1355 return FAIL;
1356 }
1357
1358 i = list_len(l);
1359 if (semicolon == 0 && var_count < i)
1360 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001361 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001362 return FAIL;
1363 }
1364 if (var_count - semicolon > i)
1365 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001366 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001367 return FAIL;
1368 }
1369
1370 item = l->lv_first;
1371 while (*arg != ']')
1372 {
1373 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001374 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001375 item = item->li_next;
1376 if (arg == NULL)
1377 return FAIL;
1378
1379 arg = skipwhite(arg);
1380 if (*arg == ';')
1381 {
1382 /* Put the rest of the list (may be empty) in the var after ';'.
1383 * Create a new list for this. */
1384 l = list_alloc();
1385 if (l == NULL)
1386 return FAIL;
1387 while (item != NULL)
1388 {
1389 list_append_tv(l, &item->li_tv);
1390 item = item->li_next;
1391 }
1392
1393 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001394 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001395 ltv.vval.v_list = l;
1396 l->lv_refcount = 1;
1397
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1399 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001400 clear_tv(&ltv);
1401 if (arg == NULL)
1402 return FAIL;
1403 break;
1404 }
1405 else if (*arg != ',' && *arg != ']')
1406 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001407 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 return FAIL;
1409 }
1410 }
1411
1412 return OK;
1413}
1414
1415/*
1416 * Skip over assignable variable "var" or list of variables "[var, var]".
1417 * Used for ":let varvar = expr" and ":for varvar in expr".
1418 * For "[var, var]" increment "*var_count" for each variable.
1419 * for "[var, var; var]" set "semicolon".
1420 * Return NULL for an error.
1421 */
1422 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001423skip_var_list(
1424 char_u *arg,
1425 int *var_count,
1426 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427{
1428 char_u *p, *s;
1429
1430 if (*arg == '[')
1431 {
1432 /* "[var, var]": find the matching ']'. */
1433 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435 {
1436 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1437 s = skip_var_one(p);
1438 if (s == p)
1439 {
1440 EMSG2(_(e_invarg2), p);
1441 return NULL;
1442 }
1443 ++*var_count;
1444
1445 p = skipwhite(s);
1446 if (*p == ']')
1447 break;
1448 else if (*p == ';')
1449 {
1450 if (*semicolon == 1)
1451 {
1452 EMSG(_("Double ; in list of variables"));
1453 return NULL;
1454 }
1455 *semicolon = 1;
1456 }
1457 else if (*p != ',')
1458 {
1459 EMSG2(_(e_invarg2), p);
1460 return NULL;
1461 }
1462 }
1463 return p + 1;
1464 }
1465 else
1466 return skip_var_one(arg);
1467}
1468
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001469/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001470 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001471 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001472 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001473 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001474skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001476 if (*arg == '@' && arg[1] != NUL)
1477 return arg + 2;
1478 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1479 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001480}
1481
Bram Moolenaara7043832005-01-21 11:56:39 +00001482/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001483 * List variables for hashtab "ht" with prefix "prefix".
1484 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001485 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487list_hashtable_vars(
1488 hashtab_T *ht,
1489 char_u *prefix,
1490 int empty,
1491 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001492{
Bram Moolenaar33570922005-01-25 22:26:29 +00001493 hashitem_T *hi;
1494 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001495 int todo;
1496
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001497 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001498 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1499 {
1500 if (!HASHITEM_EMPTY(hi))
1501 {
1502 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001503 di = HI2DI(hi);
1504 if (empty || di->di_tv.v_type != VAR_STRING
1505 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001506 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001507 }
1508 }
1509}
1510
1511/*
1512 * List global variables.
1513 */
1514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001516{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001517 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001518}
1519
1520/*
1521 * List buffer variables.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001525{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001526 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001527 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001528}
1529
1530/*
1531 * List window variables.
1532 */
1533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001535{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001536 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001537 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001538}
1539
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001540/*
1541 * List tab page variables.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001545{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001546 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001547 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001548}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001549
Bram Moolenaara7043832005-01-21 11:56:39 +00001550/*
1551 * List Vim variables.
1552 */
1553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001554list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001556 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557}
1558
1559/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001560 * List script-local variables, if there is a script.
1561 */
1562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001563list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001564{
1565 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001566 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1567 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001568}
1569
1570/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 * List variables in "arg".
1572 */
1573 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575{
1576 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 char_u *name_start;
1580 char_u *arg_subsc;
1581 char_u *tofree;
1582 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583
1584 while (!ends_excmd(*arg) && !got_int)
1585 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001586 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001588 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001589 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590 {
1591 emsg_severe = TRUE;
1592 EMSG(_(e_trailing));
1593 break;
1594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001598 /* get_name_len() takes care of expanding curly braces */
1599 name_start = name = arg;
1600 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1601 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001603 /* This is mainly to keep test 49 working: when expanding
1604 * curly braces fails overrule the exception error message. */
1605 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001607 emsg_severe = TRUE;
1608 EMSG2(_(e_invarg2), arg);
1609 break;
1610 }
1611 error = TRUE;
1612 }
1613 else
1614 {
1615 if (tofree != NULL)
1616 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001617 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 else
1620 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001621 /* handle d.key, l[idx], f(expr) */
1622 arg_subsc = arg;
1623 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001624 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001626 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001628 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001629 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001630 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001631 case 'g': list_glob_vars(first); break;
1632 case 'b': list_buf_vars(first); break;
1633 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001634 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001635 case 'v': list_vim_vars(first); break;
1636 case 's': list_script_vars(first); break;
1637 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001638 default:
1639 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001640 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001641 }
1642 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001643 {
1644 char_u numbuf[NUMBUFLEN];
1645 char_u *tf;
1646 int c;
1647 char_u *s;
1648
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001649 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001650 c = *arg;
1651 *arg = NUL;
1652 list_one_var_a((char_u *)"",
1653 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001654 tv.v_type,
1655 s == NULL ? (char_u *)"" : s,
1656 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001657 *arg = c;
1658 vim_free(tf);
1659 }
1660 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 }
1663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664
1665 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001667
1668 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 }
1670
1671 return arg;
1672}
1673
1674/*
1675 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1676 * Returns a pointer to the char just after the var name.
1677 * Returns NULL if there is an error.
1678 */
1679 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680ex_let_one(
1681 char_u *arg, /* points to variable name */
1682 typval_T *tv, /* value to assign to variable */
1683 int copy, /* copy value from "tv" */
1684 char_u *endchars, /* valid chars after variable name or NULL */
1685 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001686{
1687 int c1;
1688 char_u *name;
1689 char_u *p;
1690 char_u *arg_end = NULL;
1691 int len;
1692 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001693 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694
1695 /*
1696 * ":let $VAR = expr": Set environment variable.
1697 */
1698 if (*arg == '$')
1699 {
1700 /* Find the end of the name. */
1701 ++arg;
1702 name = arg;
1703 len = get_env_len(&arg);
1704 if (len == 0)
1705 EMSG2(_(e_invarg2), name - 1);
1706 else
1707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 if (op != NULL && (*op == '+' || *op == '-'))
1709 EMSG2(_(e_letwrong), op);
1710 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001713 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 {
1715 c1 = name[len];
1716 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001717 p = get_tv_string_chk(tv);
1718 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 int mustfree = FALSE;
1721 char_u *s = vim_getenv(name, &mustfree);
1722
1723 if (s != NULL)
1724 {
1725 p = tofree = concat_str(s, p);
1726 if (mustfree)
1727 vim_free(s);
1728 }
1729 }
1730 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733 if (STRICMP(name, "HOME") == 0)
1734 init_homedir();
1735 else if (didset_vim && STRICMP(name, "VIM") == 0)
1736 didset_vim = FALSE;
1737 else if (didset_vimruntime
1738 && STRICMP(name, "VIMRUNTIME") == 0)
1739 didset_vimruntime = FALSE;
1740 arg_end = arg;
1741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 }
1745 }
1746 }
1747
1748 /*
1749 * ":let &option = expr": Set option value.
1750 * ":let &l:option = expr": Set local option value.
1751 * ":let &g:option = expr": Set global option value.
1752 */
1753 else if (*arg == '&')
1754 {
1755 /* Find the end of the name. */
1756 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 if (p == NULL || (endchars != NULL
1758 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 EMSG(_(e_letunexp));
1760 else
1761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 long n;
1763 int opt_type;
1764 long numval;
1765 char_u *stringval = NULL;
1766 char_u *s;
1767
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 c1 = *p;
1769 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001771 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772 s = get_tv_string_chk(tv); /* != NULL if number or string */
1773 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 {
1775 opt_type = get_option_value(arg, &numval,
1776 &stringval, opt_flags);
1777 if ((opt_type == 1 && *op == '.')
1778 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001779 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001780 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001781 s = NULL; /* don't set the value */
1782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 else
1784 {
1785 if (opt_type == 1) /* number */
1786 {
1787 if (*op == '+')
1788 n = numval + n;
1789 else
1790 n = numval - n;
1791 }
1792 else if (opt_type == 0 && stringval != NULL) /* string */
1793 {
1794 s = concat_str(stringval, s);
1795 vim_free(stringval);
1796 stringval = s;
1797 }
1798 }
1799 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001800 if (s != NULL)
1801 {
1802 set_option_value(arg, n, s, opt_flags);
1803 arg_end = p;
1804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 }
1808 }
1809
1810 /*
1811 * ":let @r = expr": Set register contents.
1812 */
1813 else if (*arg == '@')
1814 {
1815 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001816 if (op != NULL && (*op == '+' || *op == '-'))
1817 EMSG2(_(e_letwrong), op);
1818 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 EMSG(_(e_letunexp));
1821 else
1822 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001823 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 char_u *s;
1825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001826 p = get_tv_string_chk(tv);
1827 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001829 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 if (s != NULL)
1831 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001832 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 vim_free(s);
1834 }
1835 }
1836 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001838 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001839 arg_end = arg + 1;
1840 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001841 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 }
1843 }
1844
1845 /*
1846 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001849 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001853 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1857 EMSG(_(e_letunexp));
1858 else
1859 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001861 arg_end = p;
1862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 }
1866
1867 else
1868 EMSG2(_(e_invarg2), arg);
1869
1870 return arg_end;
1871}
1872
1873/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 * Get an lval: variable, Dict item or List item that can be assigned a value
1875 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1876 * "name.key", "name.key[expr]" etc.
1877 * Indexing only works if "name" is an existing List or Dictionary.
1878 * "name" points to the start of the name.
1879 * If "rettv" is not NULL it points to the value to be assigned.
1880 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1881 * wrong; must end in space or cmd separator.
1882 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001883 * flags:
1884 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001885 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001886 * GLV_NO_AUTOLOAD: do not use script autoloading
1887 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001889 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001890 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001892 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893get_lval(
1894 char_u *name,
1895 typval_T *rettv,
1896 lval_T *lp,
1897 int unlet,
1898 int skip,
1899 int flags, /* GLV_ values */
1900 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001903 char_u *expr_start, *expr_end;
1904 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 dictitem_T *v;
1906 typval_T var1;
1907 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001910 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001911 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001913 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917
1918 if (skip)
1919 {
1920 /* When skipping just find the end of the name. */
1921 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001922 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923 }
1924
1925 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001926 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 if (expr_start != NULL)
1928 {
1929 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001930 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001931 && *p != '[' && *p != '.')
1932 {
1933 EMSG(_(e_trailing));
1934 return NULL;
1935 }
1936
1937 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1938 if (lp->ll_exp_name == NULL)
1939 {
1940 /* Report an invalid expression in braces, unless the
1941 * expression evaluation has been cancelled due to an
1942 * aborting error, an interrupt, or an exception. */
1943 if (!aborting() && !quiet)
1944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001945 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 EMSG2(_(e_invarg2), name);
1947 return NULL;
1948 }
1949 }
1950 lp->ll_name = lp->ll_exp_name;
1951 }
1952 else
1953 lp->ll_name = name;
1954
1955 /* Without [idx] or .key we are done. */
1956 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1957 return p;
1958
1959 cc = *p;
1960 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001961 /* Only pass &ht when we would write to the variable, it prevents autoload
1962 * as well. */
1963 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1964 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 if (v == NULL && !quiet)
1966 EMSG2(_(e_undefvar), lp->ll_name);
1967 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 if (v == NULL)
1969 return NULL;
1970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 /*
1972 * Loop until no more [idx] or .key is following.
1973 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001974 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001975 var1.v_type = VAR_UNKNOWN;
1976 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1980 && !(lp->ll_tv->v_type == VAR_DICT
1981 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 if (!quiet)
1984 EMSG(_("E689: Can only index a List or Dictionary"));
1985 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 if (!quiet)
1990 EMSG(_("E708: [:] must come last"));
1991 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001993
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 len = -1;
1995 if (*p == '.')
1996 {
1997 key = p + 1;
1998 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1999 ;
2000 if (len == 0)
2001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 if (!quiet)
2003 EMSG(_(e_emptykey));
2004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
2006 p = key + len;
2007 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002008 else
2009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002010 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002011 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 if (*p == ':')
2013 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002014 else
2015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 empty1 = FALSE;
2017 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002019 if (get_tv_string_chk(&var1) == NULL)
2020 {
2021 /* not a number or string */
2022 clear_tv(&var1);
2023 return NULL;
2024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 }
2026
2027 /* Optionally get the second index [ :expr]. */
2028 if (*p == ':')
2029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002031 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002036 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 if (rettv != NULL && (rettv->v_type != VAR_LIST
2038 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 if (!quiet)
2041 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002042 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 }
2045 p = skipwhite(p + 1);
2046 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002048 else
2049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2052 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002053 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002056 if (get_tv_string_chk(&var2) == NULL)
2057 {
2058 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002059 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002060 clear_tv(&var2);
2061 return NULL;
2062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002063 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002068
Bram Moolenaar8c711452005-01-14 21:53:12 +00002069 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002070 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 if (!quiet)
2072 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002073 clear_tv(&var1);
2074 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002076 }
2077
2078 /* Skip to past ']'. */
2079 ++p;
2080 }
2081
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002083 {
2084 if (len == -1)
2085 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002087 key = get_tv_string_chk(&var1); /* is number or string */
2088 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002090 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 }
2093 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 lp->ll_list = NULL;
2095 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002096 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002097
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002098 /* When assigning to a scope dictionary check that a function and
2099 * variable name is valid (only variable name unless it is l: or
2100 * g: dictionary). Disallow overwriting a builtin function. */
2101 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002102 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002103 int prevval;
2104 int wrong;
2105
2106 if (len != -1)
2107 {
2108 prevval = key[len];
2109 key[len] = NUL;
2110 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002111 else
2112 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002113 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2114 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002115 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002116 || !valid_varname(key);
2117 if (len != -1)
2118 key[len] = prevval;
2119 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002120 return NULL;
2121 }
2122
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002125 /* Can't add "v:" variable. */
2126 if (lp->ll_dict == &vimvardict)
2127 {
2128 EMSG2(_(e_illvar), name);
2129 return NULL;
2130 }
2131
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002132 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002137 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002139 }
2140 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002142 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002144 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 p = NULL;
2147 break;
2148 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002149 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002150 else if ((flags & GLV_READ_ONLY) == 0
2151 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002152 {
2153 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002154 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002155 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002156
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002157 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002159 }
2160 else
2161 {
2162 /*
2163 * Get the number and item for the only or first index of the List.
2164 */
2165 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002166 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002167 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002168 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002169 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002170 clear_tv(&var1);
2171
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 lp->ll_list = lp->ll_tv->vval.v_list;
2174 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2175 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002176 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002177 if (lp->ll_n1 < 0)
2178 {
2179 lp->ll_n1 = 0;
2180 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2181 }
2182 }
2183 if (lp->ll_li == NULL)
2184 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002185 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002186 if (!quiet)
2187 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 }
2190
2191 /*
2192 * May need to find the item or absolute index for the second
2193 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 * When no index given: "lp->ll_empty2" is TRUE.
2195 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002196 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002198 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002199 lp->ll_n2 = (long)get_tv_number(&var2);
2200 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002206 {
2207 if (!quiet)
2208 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002210 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 }
2213
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2215 if (lp->ll_n1 < 0)
2216 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2217 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002218 {
2219 if (!quiet)
2220 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002222 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002223 }
2224
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 }
2228
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002229 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 return p;
2231}
2232
2233/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002234 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238{
2239 vim_free(lp->ll_exp_name);
2240 vim_free(lp->ll_newkey);
2241}
2242
2243/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002244 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 */
2248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249set_var_lval(
2250 lval_T *lp,
2251 char_u *endp,
2252 typval_T *rettv,
2253 int copy,
2254 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255{
2256 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002257 listitem_T *ri;
2258 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259
2260 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002262 cc = *endp;
2263 *endp = NUL;
2264 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002266 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267
Bram Moolenaar79518e22017-02-17 16:31:35 +01002268 /* handle +=, -= and .= */
2269 di = NULL;
2270 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2271 &tv, &di, TRUE, FALSE) == OK)
2272 {
2273 if ((di == NULL
2274 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2275 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2276 FALSE)))
2277 && tv_op(&tv, rettv, op) == OK)
2278 set_var(lp->ll_name, &tv, FALSE);
2279 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002282 else
2283 set_var(lp->ll_name, rettv, copy);
2284 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002286 else if (tv_check_lock(lp->ll_newkey == NULL
2287 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002288 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002289 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 else if (lp->ll_range)
2291 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002292 listitem_T *ll_li = lp->ll_li;
2293 int ll_n1 = lp->ll_n1;
2294
2295 /*
2296 * Check whether any of the list items is locked
2297 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002298 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002299 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002300 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002301 return;
2302 ri = ri->li_next;
2303 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2304 break;
2305 ll_li = ll_li->li_next;
2306 ++ll_n1;
2307 }
2308
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 /*
2310 * Assign the List values to the list items.
2311 */
2312 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 if (op != NULL && *op != '=')
2315 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2316 else
2317 {
2318 clear_tv(&lp->ll_li->li_tv);
2319 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2320 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 ri = ri->li_next;
2322 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2323 break;
2324 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002327 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 ri = NULL;
2330 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002331 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002332 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 lp->ll_li = lp->ll_li->li_next;
2334 ++lp->ll_n1;
2335 }
2336 if (ri != NULL)
2337 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 else if (lp->ll_empty2
2339 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 : lp->ll_n1 != lp->ll_n2)
2341 EMSG(_("E711: List value has not enough items"));
2342 }
2343 else
2344 {
2345 /*
2346 * Assign to a List or Dictionary item.
2347 */
2348 if (lp->ll_newkey != NULL)
2349 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 if (op != NULL && *op != '=')
2351 {
2352 EMSG2(_(e_letwrong), op);
2353 return;
2354 }
2355
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002357 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 if (di == NULL)
2359 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002360 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2361 {
2362 vim_free(di);
2363 return;
2364 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002366 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 else if (op != NULL && *op != '=')
2368 {
2369 tv_op(lp->ll_tv, rettv, op);
2370 return;
2371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 /*
2376 * Assign the value to the variable or list item.
2377 */
2378 if (copy)
2379 copy_tv(rettv, lp->ll_tv);
2380 else
2381 {
2382 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002383 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 }
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387}
2388
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002389/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2391 * Returns OK or FAIL.
2392 */
2393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002394tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002396 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 char_u numbuf[NUMBUFLEN];
2398 char_u *s;
2399
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002400 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2401 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2402 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 switch (tv1->v_type)
2405 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002406 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 case VAR_DICT:
2408 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002409 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002410 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002411 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002412 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 break;
2414
2415 case VAR_LIST:
2416 if (*op != '+' || tv2->v_type != VAR_LIST)
2417 break;
2418 /* List += List */
2419 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2420 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2421 return OK;
2422
2423 case VAR_NUMBER:
2424 case VAR_STRING:
2425 if (tv2->v_type == VAR_LIST)
2426 break;
2427 if (*op == '+' || *op == '-')
2428 {
2429 /* nr += nr or nr -= nr*/
2430 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002431#ifdef FEAT_FLOAT
2432 if (tv2->v_type == VAR_FLOAT)
2433 {
2434 float_T f = n;
2435
2436 if (*op == '+')
2437 f += tv2->vval.v_float;
2438 else
2439 f -= tv2->vval.v_float;
2440 clear_tv(tv1);
2441 tv1->v_type = VAR_FLOAT;
2442 tv1->vval.v_float = f;
2443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002445#endif
2446 {
2447 if (*op == '+')
2448 n += get_tv_number(tv2);
2449 else
2450 n -= get_tv_number(tv2);
2451 clear_tv(tv1);
2452 tv1->v_type = VAR_NUMBER;
2453 tv1->vval.v_number = n;
2454 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 }
2456 else
2457 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002458 if (tv2->v_type == VAR_FLOAT)
2459 break;
2460
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 /* str .= str */
2462 s = get_tv_string(tv1);
2463 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2464 clear_tv(tv1);
2465 tv1->v_type = VAR_STRING;
2466 tv1->vval.v_string = s;
2467 }
2468 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002469
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002470 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002471#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002472 {
2473 float_T f;
2474
2475 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2476 && tv2->v_type != VAR_NUMBER
2477 && tv2->v_type != VAR_STRING))
2478 break;
2479 if (tv2->v_type == VAR_FLOAT)
2480 f = tv2->vval.v_float;
2481 else
2482 f = get_tv_number(tv2);
2483 if (*op == '+')
2484 tv1->vval.v_float += f;
2485 else
2486 tv1->vval.v_float -= f;
2487 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002488#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002489 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 }
2491 }
2492
2493 EMSG2(_(e_letwrong), op);
2494 return FAIL;
2495}
2496
2497/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002498 * Evaluate the expression used in a ":for var in expr" command.
2499 * "arg" points to "var".
2500 * Set "*errp" to TRUE for an error, FALSE otherwise;
2501 * Return a pointer that holds the info. Null when there is an error.
2502 */
2503 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002504eval_for_line(
2505 char_u *arg,
2506 int *errp,
2507 char_u **nextcmdp,
2508 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509{
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002511 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002512 typval_T tv;
2513 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514
2515 *errp = TRUE; /* default: there is an error */
2516
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002518 if (fi == NULL)
2519 return NULL;
2520
2521 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2522 if (expr == NULL)
2523 return fi;
2524
2525 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002526 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002528 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529 return fi;
2530 }
2531
2532 if (skip)
2533 ++emsg_skip;
2534 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2535 {
2536 *errp = FALSE;
2537 if (!skip)
2538 {
2539 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002540 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002541 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002542 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002543 clear_tv(&tv);
2544 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002545 else if (l == NULL)
2546 {
2547 /* a null list is like an empty list: do nothing */
2548 clear_tv(&tv);
2549 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002550 else
2551 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002552 /* No need to increment the refcount, it's already set for the
2553 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 fi->fi_list = l;
2555 list_add_watch(l, &fi->fi_lw);
2556 fi->fi_lw.lw_item = l->lv_first;
2557 }
2558 }
2559 }
2560 if (skip)
2561 --emsg_skip;
2562
2563 return fi;
2564}
2565
2566/*
2567 * Use the first item in a ":for" list. Advance to the next.
2568 * Assign the values to the variable (list). "arg" points to the first one.
2569 * Return TRUE when a valid item was found, FALSE when at end of list or
2570 * something wrong.
2571 */
2572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002573next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002574{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002575 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002576 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002578
2579 item = fi->fi_lw.lw_item;
2580 if (item == NULL)
2581 result = FALSE;
2582 else
2583 {
2584 fi->fi_lw.lw_item = item->li_next;
2585 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2586 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2587 }
2588 return result;
2589}
2590
2591/*
2592 * Free the structure used to store info used by ":for".
2593 */
2594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002595free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002596{
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002598
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002599 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002600 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002601 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002602 list_unref(fi->fi_list);
2603 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002604 vim_free(fi);
2605}
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2608
2609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610set_context_for_expression(
2611 expand_T *xp,
2612 char_u *arg,
2613 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 int got_eq = FALSE;
2616 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002617 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002619 if (cmdidx == CMD_let)
2620 {
2621 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002622 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002623 {
2624 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002625 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002626 {
2627 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002628 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002629 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002630 break;
2631 }
2632 return;
2633 }
2634 }
2635 else
2636 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2637 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 while ((xp->xp_pattern = vim_strpbrk(arg,
2639 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2640 {
2641 c = *xp->xp_pattern;
2642 if (c == '&')
2643 {
2644 c = xp->xp_pattern[1];
2645 if (c == '&')
2646 {
2647 ++xp->xp_pattern;
2648 xp->xp_context = cmdidx != CMD_let || got_eq
2649 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2650 }
2651 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002654 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2655 xp->xp_pattern += 2;
2656
2657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 else if (c == '$')
2660 {
2661 /* environment variable */
2662 xp->xp_context = EXPAND_ENV_VARS;
2663 }
2664 else if (c == '=')
2665 {
2666 got_eq = TRUE;
2667 xp->xp_context = EXPAND_EXPRESSION;
2668 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002669 else if (c == '#'
2670 && xp->xp_context == EXPAND_EXPRESSION)
2671 {
2672 /* Autoload function/variable contains '#'. */
2673 break;
2674 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002675 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 && xp->xp_context == EXPAND_FUNCTIONS
2677 && vim_strchr(xp->xp_pattern, '(') == NULL)
2678 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002679 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 break;
2681 }
2682 else if (cmdidx != CMD_let || got_eq)
2683 {
2684 if (c == '"') /* string */
2685 {
2686 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2687 if (c == '\\' && xp->xp_pattern[1] != NUL)
2688 ++xp->xp_pattern;
2689 xp->xp_context = EXPAND_NOTHING;
2690 }
2691 else if (c == '\'') /* literal string */
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2695 /* skip */ ;
2696 xp->xp_context = EXPAND_NOTHING;
2697 }
2698 else if (c == '|')
2699 {
2700 if (xp->xp_pattern[1] == '|')
2701 {
2702 ++xp->xp_pattern;
2703 xp->xp_context = EXPAND_EXPRESSION;
2704 }
2705 else
2706 xp->xp_context = EXPAND_COMMANDS;
2707 }
2708 else
2709 xp->xp_context = EXPAND_EXPRESSION;
2710 }
2711 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002712 /* Doesn't look like something valid, expand as an expression
2713 * anyway. */
2714 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 arg = xp->xp_pattern;
2716 if (*arg != NUL)
2717 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2718 /* skip */ ;
2719 }
2720 xp->xp_pattern = arg;
2721}
2722
2723#endif /* FEAT_CMDL_COMPL */
2724
2725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 * ":unlet[!] var1 ... " command.
2727 */
2728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002731 ex_unletlock(eap, eap->arg, 0);
2732}
2733
2734/*
2735 * ":lockvar" and ":unlockvar" commands
2736 */
2737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002738ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002739{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002741 int deep = 2;
2742
2743 if (eap->forceit)
2744 deep = -1;
2745 else if (vim_isdigit(*arg))
2746 {
2747 deep = getdigits(&arg);
2748 arg = skipwhite(arg);
2749 }
2750
2751 ex_unletlock(eap, arg, deep);
2752}
2753
2754/*
2755 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2756 */
2757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002758ex_unletlock(
2759 exarg_T *eap,
2760 char_u *argstart,
2761 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002762{
2763 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002766 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 do
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002771 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002772 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lv.ll_name == NULL)
2774 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002775 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (name_end != NULL)
2779 {
2780 emsg_severe = TRUE;
2781 EMSG(_(e_trailing));
2782 }
2783 if (!(eap->skip || error))
2784 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 break;
2786 }
2787
2788 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002789 {
2790 if (eap->cmdidx == CMD_unlet)
2791 {
2792 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2793 error = TRUE;
2794 }
2795 else
2796 {
2797 if (do_lock_var(&lv, name_end, deep,
2798 eap->cmdidx == CMD_lockvar) == FAIL)
2799 error = TRUE;
2800 }
2801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (!eap->skip)
2804 clear_lval(&lv);
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 arg = skipwhite(name_end);
2807 } while (!ends_excmd(*arg));
2808
2809 eap->nextcmd = check_nextcmd(arg);
2810}
2811
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002813do_unlet_var(
2814 lval_T *lp,
2815 char_u *name_end,
2816 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 int ret = OK;
2819 int cc;
2820
2821 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 cc = *name_end;
2824 *name_end = NUL;
2825
2826 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002827 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002831 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002832 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002833 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002834 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002835 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 else if (lp->ll_range)
2837 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002838 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002839 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002840 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002841
2842 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2843 {
2844 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002845 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002846 return FAIL;
2847 ll_li = li;
2848 ++ll_n1;
2849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850
2851 /* Delete a range of List items. */
2852 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2853 {
2854 li = lp->ll_li->li_next;
2855 listitem_remove(lp->ll_list, lp->ll_li);
2856 lp->ll_li = li;
2857 ++lp->ll_n1;
2858 }
2859 }
2860 else
2861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 /* unlet a List item. */
2864 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002867 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 }
2869
2870 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871}
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873/*
2874 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002875 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
2877 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002878do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 hashtab_T *ht;
2881 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002882 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002883 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002884 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar33570922005-01-25 22:26:29 +00002886 ht = find_var_ht(name, &varname);
2887 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002890 if (d == NULL)
2891 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 if (ht == &globvarht)
2893 d = &globvardict;
2894 else if (ht == &compat_hashtab)
2895 d = &vimvardict;
2896 else
2897 {
2898 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2899 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2900 }
2901 if (d == NULL)
2902 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002903 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 return FAIL;
2905 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002906 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002908 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002909 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002910 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002911 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002912 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002913 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002914 || var_check_ro(di->di_flags, name, FALSE)
2915 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002916 return FAIL;
2917
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 delete_var(ht, hi);
2919 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002922 if (forceit)
2923 return OK;
2924 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 return FAIL;
2926}
2927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928/*
2929 * Lock or unlock variable indicated by "lp".
2930 * "deep" is the levels to go (-1 for unlimited);
2931 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2932 */
2933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002934do_lock_var(
2935 lval_T *lp,
2936 char_u *name_end,
2937 int deep,
2938 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002939{
2940 int ret = OK;
2941 int cc;
2942 dictitem_T *di;
2943
2944 if (deep == 0) /* nothing to do */
2945 return OK;
2946
2947 if (lp->ll_tv == NULL)
2948 {
2949 cc = *name_end;
2950 *name_end = NUL;
2951
2952 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002953 di = find_var(lp->ll_name, NULL, TRUE);
2954 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002955 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002956 else if ((di->di_flags & DI_FLAGS_FIX)
2957 && di->di_tv.v_type != VAR_DICT
2958 && di->di_tv.v_type != VAR_LIST)
2959 /* For historic reasons this error is not given for a list or dict.
2960 * E.g., the b: dict could be locked/unlocked. */
2961 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 else
2963 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002964 if (lock)
2965 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002967 di->di_flags &= ~DI_FLAGS_LOCK;
2968 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 }
2970 *name_end = cc;
2971 }
2972 else if (lp->ll_range)
2973 {
2974 listitem_T *li = lp->ll_li;
2975
2976 /* (un)lock a range of List items. */
2977 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2978 {
2979 item_lock(&li->li_tv, deep, lock);
2980 li = li->li_next;
2981 ++lp->ll_n1;
2982 }
2983 }
2984 else if (lp->ll_list != NULL)
2985 /* (un)lock a List item. */
2986 item_lock(&lp->ll_li->li_tv, deep, lock);
2987 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002988 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002989 item_lock(&lp->ll_di->di_tv, deep, lock);
2990
2991 return ret;
2992}
2993
2994/*
2995 * Lock or unlock an item. "deep" is nr of levels to go.
2996 */
2997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002998item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002999{
3000 static int recurse = 0;
3001 list_T *l;
3002 listitem_T *li;
3003 dict_T *d;
3004 hashitem_T *hi;
3005 int todo;
3006
3007 if (recurse >= DICT_MAXNEST)
3008 {
3009 EMSG(_("E743: variable nested too deep for (un)lock"));
3010 return;
3011 }
3012 if (deep == 0)
3013 return;
3014 ++recurse;
3015
3016 /* lock/unlock the item itself */
3017 if (lock)
3018 tv->v_lock |= VAR_LOCKED;
3019 else
3020 tv->v_lock &= ~VAR_LOCKED;
3021
3022 switch (tv->v_type)
3023 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003024 case VAR_UNKNOWN:
3025 case VAR_NUMBER:
3026 case VAR_STRING:
3027 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003028 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003029 case VAR_FLOAT:
3030 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003031 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003032 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003033 break;
3034
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003035 case VAR_LIST:
3036 if ((l = tv->vval.v_list) != NULL)
3037 {
3038 if (lock)
3039 l->lv_lock |= VAR_LOCKED;
3040 else
3041 l->lv_lock &= ~VAR_LOCKED;
3042 if (deep < 0 || deep > 1)
3043 /* recursive: lock/unlock the items the List contains */
3044 for (li = l->lv_first; li != NULL; li = li->li_next)
3045 item_lock(&li->li_tv, deep - 1, lock);
3046 }
3047 break;
3048 case VAR_DICT:
3049 if ((d = tv->vval.v_dict) != NULL)
3050 {
3051 if (lock)
3052 d->dv_lock |= VAR_LOCKED;
3053 else
3054 d->dv_lock &= ~VAR_LOCKED;
3055 if (deep < 0 || deep > 1)
3056 {
3057 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003058 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003059 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3060 {
3061 if (!HASHITEM_EMPTY(hi))
3062 {
3063 --todo;
3064 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3065 }
3066 }
3067 }
3068 }
3069 }
3070 --recurse;
3071}
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3074/*
3075 * Delete all "menutrans_" variables.
3076 */
3077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003081 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003084 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003086 {
3087 if (!HASHITEM_EMPTY(hi))
3088 {
3089 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3091 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003092 }
3093 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003094 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096#endif
3097
3098#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3099
3100/*
3101 * Local string buffer for the next two functions to store a variable name
3102 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3103 * get_user_var_name().
3104 */
3105
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003106static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108static char_u *varnamebuf = NULL;
3109static int varnamebuflen = 0;
3110
3111/*
3112 * Function to concatenate a prefix and a variable name.
3113 */
3114 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003115cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 int len;
3118
3119 len = (int)STRLEN(name) + 3;
3120 if (len > varnamebuflen)
3121 {
3122 vim_free(varnamebuf);
3123 len += 10; /* some additional space */
3124 varnamebuf = alloc(len);
3125 if (varnamebuf == NULL)
3126 {
3127 varnamebuflen = 0;
3128 return NULL;
3129 }
3130 varnamebuflen = len;
3131 }
3132 *varnamebuf = prefix;
3133 varnamebuf[1] = ':';
3134 STRCPY(varnamebuf + 2, name);
3135 return varnamebuf;
3136}
3137
3138/*
3139 * Function given to ExpandGeneric() to obtain the list of user defined
3140 * (global/buffer/window/built-in) variable names.
3141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003143get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003145 static long_u gdone;
3146 static long_u bdone;
3147 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003148 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003149 static int vidx;
3150 static hashitem_T *hi;
3151 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003154 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003155 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003156 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003157 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003158
3159 /* Global variables */
3160 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003162 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003164 else
3165 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003166 while (HASHITEM_EMPTY(hi))
3167 ++hi;
3168 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3169 return cat_prefix_varname('g', hi->hi_key);
3170 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003172
3173 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003174 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003177 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003179 else
3180 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003181 while (HASHITEM_EMPTY(hi))
3182 ++hi;
3183 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003185
3186 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003187 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003190 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003192 else
3193 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003194 while (HASHITEM_EMPTY(hi))
3195 ++hi;
3196 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003198
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003199 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003200 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003201 if (tdone < ht->ht_used)
3202 {
3203 if (tdone++ == 0)
3204 hi = ht->ht_array;
3205 else
3206 ++hi;
3207 while (HASHITEM_EMPTY(hi))
3208 ++hi;
3209 return cat_prefix_varname('t', hi->hi_key);
3210 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003211
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 /* v: variables */
3213 if (vidx < VV_LEN)
3214 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 vim_free(varnamebuf);
3217 varnamebuf = NULL;
3218 varnamebuflen = 0;
3219 return NULL;
3220}
3221
3222#endif /* FEAT_CMDL_COMPL */
3223
3224/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003225 * Return TRUE if "pat" matches "text".
3226 * Does not use 'cpo' and always uses 'magic'.
3227 */
3228 static int
3229pattern_match(char_u *pat, char_u *text, int ic)
3230{
3231 int matches = FALSE;
3232 char_u *save_cpo;
3233 regmatch_T regmatch;
3234
3235 /* avoid 'l' flag in 'cpoptions' */
3236 save_cpo = p_cpo;
3237 p_cpo = (char_u *)"";
3238 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3239 if (regmatch.regprog != NULL)
3240 {
3241 regmatch.rm_ic = ic;
3242 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3243 vim_regfree(regmatch.regprog);
3244 }
3245 p_cpo = save_cpo;
3246 return matches;
3247}
3248
3249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * types for expressions.
3251 */
3252typedef enum
3253{
3254 TYPE_UNKNOWN = 0
3255 , TYPE_EQUAL /* == */
3256 , TYPE_NEQUAL /* != */
3257 , TYPE_GREATER /* > */
3258 , TYPE_GEQUAL /* >= */
3259 , TYPE_SMALLER /* < */
3260 , TYPE_SEQUAL /* <= */
3261 , TYPE_MATCH /* =~ */
3262 , TYPE_NOMATCH /* !~ */
3263} exptype_T;
3264
3265/*
3266 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003267 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3269 */
3270
3271/*
3272 * Handle zero level expression.
3273 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003274 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003275 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 * Return OK or FAIL.
3277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279eval0(
3280 char_u *arg,
3281 typval_T *rettv,
3282 char_u **nextcmd,
3283 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 int ret;
3286 char_u *p;
3287
3288 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003289 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (ret == FAIL || !ends_excmd(*p))
3291 {
3292 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /*
3295 * Report the invalid expression unless the expression evaluation has
3296 * been cancelled due to an aborting error, an interrupt, or an
3297 * exception.
3298 */
3299 if (!aborting())
3300 EMSG2(_(e_invexpr2), arg);
3301 ret = FAIL;
3302 }
3303 if (nextcmd != NULL)
3304 *nextcmd = check_nextcmd(p);
3305
3306 return ret;
3307}
3308
3309/*
3310 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003311 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 *
3313 * "arg" must point to the first non-white of the expression.
3314 * "arg" is advanced to the next non-white after the recognized expression.
3315 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003316 * Note: "rettv.v_lock" is not set.
3317 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 * Return OK or FAIL.
3319 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
3326 /*
3327 * Get the first variable.
3328 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003329 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return FAIL;
3331
3332 if ((*arg)[0] == '?')
3333 {
3334 result = FALSE;
3335 if (evaluate)
3336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 int error = FALSE;
3338
3339 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 if (error)
3343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345
3346 /*
3347 * Get the second variable.
3348 */
3349 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 return FAIL;
3352
3353 /*
3354 * Check for the ":".
3355 */
3356 if ((*arg)[0] != ':')
3357 {
3358 EMSG(_("E109: Missing ':' after '?'"));
3359 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 return FAIL;
3362 }
3363
3364 /*
3365 * Get the third variable.
3366 */
3367 *arg = skipwhite(*arg + 1);
3368 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3369 {
3370 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003371 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return FAIL;
3373 }
3374 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003375 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377
3378 return OK;
3379}
3380
3381/*
3382 * Handle first level expression:
3383 * expr2 || expr2 || expr2 logical OR
3384 *
3385 * "arg" must point to the first non-white of the expression.
3386 * "arg" is advanced to the next non-white after the recognized expression.
3387 *
3388 * Return OK or FAIL.
3389 */
3390 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 long result;
3395 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003396 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 /*
3399 * Get the first variable.
3400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return FAIL;
3403
3404 /*
3405 * Repeat until there is no following "||".
3406 */
3407 first = TRUE;
3408 result = FALSE;
3409 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3410 {
3411 if (evaluate && first)
3412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 if (error)
3417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 first = FALSE;
3419 }
3420
3421 /*
3422 * Get the second variable.
3423 */
3424 *arg = skipwhite(*arg + 2);
3425 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3426 return FAIL;
3427
3428 /*
3429 * Compute the result.
3430 */
3431 if (evaluate && !result)
3432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 if (error)
3437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 if (evaluate)
3440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 rettv->v_type = VAR_NUMBER;
3442 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 }
3445
3446 return OK;
3447}
3448
3449/*
3450 * Handle second level expression:
3451 * expr3 && expr3 && expr3 logical AND
3452 *
3453 * "arg" must point to the first non-white of the expression.
3454 * "arg" is advanced to the next non-white after the recognized expression.
3455 *
3456 * Return OK or FAIL.
3457 */
3458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 long result;
3463 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003464 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466 /*
3467 * Get the first variable.
3468 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003469 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 return FAIL;
3471
3472 /*
3473 * Repeat until there is no following "&&".
3474 */
3475 first = TRUE;
3476 result = TRUE;
3477 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3478 {
3479 if (evaluate && first)
3480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003481 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003484 if (error)
3485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 first = FALSE;
3487 }
3488
3489 /*
3490 * Get the second variable.
3491 */
3492 *arg = skipwhite(*arg + 2);
3493 if (eval4(arg, &var2, evaluate && result) == FAIL)
3494 return FAIL;
3495
3496 /*
3497 * Compute the result.
3498 */
3499 if (evaluate && result)
3500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003501 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003504 if (error)
3505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507 if (evaluate)
3508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 rettv->v_type = VAR_NUMBER;
3510 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 }
3513
3514 return OK;
3515}
3516
3517/*
3518 * Handle third level expression:
3519 * var1 == var2
3520 * var1 =~ var2
3521 * var1 != var2
3522 * var1 !~ var2
3523 * var1 > var2
3524 * var1 >= var2
3525 * var1 < var2
3526 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003527 * var1 is var2
3528 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 *
3530 * "arg" must point to the first non-white of the expression.
3531 * "arg" is advanced to the next non-white after the recognized expression.
3532 *
3533 * Return OK or FAIL.
3534 */
3535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 char_u *p;
3540 int i;
3541 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003542 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003544 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *s1, *s2;
3546 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 /*
3550 * Get the first variable.
3551 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003552 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 return FAIL;
3554
3555 p = *arg;
3556 switch (p[0])
3557 {
3558 case '=': if (p[1] == '=')
3559 type = TYPE_EQUAL;
3560 else if (p[1] == '~')
3561 type = TYPE_MATCH;
3562 break;
3563 case '!': if (p[1] == '=')
3564 type = TYPE_NEQUAL;
3565 else if (p[1] == '~')
3566 type = TYPE_NOMATCH;
3567 break;
3568 case '>': if (p[1] != '=')
3569 {
3570 type = TYPE_GREATER;
3571 len = 1;
3572 }
3573 else
3574 type = TYPE_GEQUAL;
3575 break;
3576 case '<': if (p[1] != '=')
3577 {
3578 type = TYPE_SMALLER;
3579 len = 1;
3580 }
3581 else
3582 type = TYPE_SEQUAL;
3583 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003584 case 'i': if (p[1] == 's')
3585 {
3586 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3587 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003588 i = p[len];
3589 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003590 {
3591 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3592 type_is = TRUE;
3593 }
3594 }
3595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597
3598 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003599 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
3601 if (type != TYPE_UNKNOWN)
3602 {
3603 /* extra question mark appended: ignore case */
3604 if (p[len] == '?')
3605 {
3606 ic = TRUE;
3607 ++len;
3608 }
3609 /* extra '#' appended: match case */
3610 else if (p[len] == '#')
3611 {
3612 ic = FALSE;
3613 ++len;
3614 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003615 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 else
3617 ic = p_ic;
3618
3619 /*
3620 * Get the second variable.
3621 */
3622 *arg = skipwhite(p + len);
3623 if (eval5(arg, &var2, evaluate) == FAIL)
3624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003625 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 return FAIL;
3627 }
3628
3629 if (evaluate)
3630 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003631 if (type_is && rettv->v_type != var2.v_type)
3632 {
3633 /* For "is" a different type always means FALSE, for "notis"
3634 * it means TRUE. */
3635 n1 = (type == TYPE_NEQUAL);
3636 }
3637 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3638 {
3639 if (type_is)
3640 {
3641 n1 = (rettv->v_type == var2.v_type
3642 && rettv->vval.v_list == var2.vval.v_list);
3643 if (type == TYPE_NEQUAL)
3644 n1 = !n1;
3645 }
3646 else if (rettv->v_type != var2.v_type
3647 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3648 {
3649 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003650 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003651 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003652 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003653 clear_tv(rettv);
3654 clear_tv(&var2);
3655 return FAIL;
3656 }
3657 else
3658 {
3659 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003660 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3661 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003662 if (type == TYPE_NEQUAL)
3663 n1 = !n1;
3664 }
3665 }
3666
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003667 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3668 {
3669 if (type_is)
3670 {
3671 n1 = (rettv->v_type == var2.v_type
3672 && rettv->vval.v_dict == var2.vval.v_dict);
3673 if (type == TYPE_NEQUAL)
3674 n1 = !n1;
3675 }
3676 else if (rettv->v_type != var2.v_type
3677 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3678 {
3679 if (rettv->v_type != var2.v_type)
3680 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3681 else
3682 EMSG(_("E736: Invalid operation for Dictionary"));
3683 clear_tv(rettv);
3684 clear_tv(&var2);
3685 return FAIL;
3686 }
3687 else
3688 {
3689 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003690 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3691 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003692 if (type == TYPE_NEQUAL)
3693 n1 = !n1;
3694 }
3695 }
3696
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003697 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3698 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003699 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003700 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003701 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003702 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003703 clear_tv(rettv);
3704 clear_tv(&var2);
3705 return FAIL;
3706 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003707 if ((rettv->v_type == VAR_PARTIAL
3708 && rettv->vval.v_partial == NULL)
3709 || (var2.v_type == VAR_PARTIAL
3710 && var2.vval.v_partial == NULL))
3711 /* when a partial is NULL assume not equal */
3712 n1 = FALSE;
3713 else if (type_is)
3714 {
3715 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3716 /* strings are considered the same if their value is
3717 * the same */
3718 n1 = tv_equal(rettv, &var2, ic, FALSE);
3719 else if (rettv->v_type == VAR_PARTIAL
3720 && var2.v_type == VAR_PARTIAL)
3721 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3722 else
3723 n1 = FALSE;
3724 }
3725 else
3726 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003727 if (type == TYPE_NEQUAL)
3728 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003729 }
3730
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003731#ifdef FEAT_FLOAT
3732 /*
3733 * If one of the two variables is a float, compare as a float.
3734 * When using "=~" or "!~", always compare as string.
3735 */
3736 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3737 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3738 {
3739 float_T f1, f2;
3740
3741 if (rettv->v_type == VAR_FLOAT)
3742 f1 = rettv->vval.v_float;
3743 else
3744 f1 = get_tv_number(rettv);
3745 if (var2.v_type == VAR_FLOAT)
3746 f2 = var2.vval.v_float;
3747 else
3748 f2 = get_tv_number(&var2);
3749 n1 = FALSE;
3750 switch (type)
3751 {
3752 case TYPE_EQUAL: n1 = (f1 == f2); break;
3753 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3754 case TYPE_GREATER: n1 = (f1 > f2); break;
3755 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3756 case TYPE_SMALLER: n1 = (f1 < f2); break;
3757 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3758 case TYPE_UNKNOWN:
3759 case TYPE_MATCH:
3760 case TYPE_NOMATCH: break; /* avoid gcc warning */
3761 }
3762 }
3763#endif
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 /*
3766 * If one of the two variables is a number, compare as a number.
3767 * When using "=~" or "!~", always compare as string.
3768 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003769 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 n1 = get_tv_number(rettv);
3773 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 switch (type)
3775 {
3776 case TYPE_EQUAL: n1 = (n1 == n2); break;
3777 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3778 case TYPE_GREATER: n1 = (n1 > n2); break;
3779 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3780 case TYPE_SMALLER: n1 = (n1 < n2); break;
3781 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3782 case TYPE_UNKNOWN:
3783 case TYPE_MATCH:
3784 case TYPE_NOMATCH: break; /* avoid gcc warning */
3785 }
3786 }
3787 else
3788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789 s1 = get_tv_string_buf(rettv, buf1);
3790 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3792 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3793 else
3794 i = 0;
3795 n1 = FALSE;
3796 switch (type)
3797 {
3798 case TYPE_EQUAL: n1 = (i == 0); break;
3799 case TYPE_NEQUAL: n1 = (i != 0); break;
3800 case TYPE_GREATER: n1 = (i > 0); break;
3801 case TYPE_GEQUAL: n1 = (i >= 0); break;
3802 case TYPE_SMALLER: n1 = (i < 0); break;
3803 case TYPE_SEQUAL: n1 = (i <= 0); break;
3804
3805 case TYPE_MATCH:
3806 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003807 n1 = pattern_match(s2, s1, ic);
3808 if (type == TYPE_NOMATCH)
3809 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 break;
3811
3812 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3813 }
3814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 clear_tv(rettv);
3816 clear_tv(&var2);
3817 rettv->v_type = VAR_NUMBER;
3818 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 }
3821
3822 return OK;
3823}
3824
3825/*
3826 * Handle fourth level expression:
3827 * + number addition
3828 * - number subtraction
3829 * . string concatenation
3830 *
3831 * "arg" must point to the first non-white of the expression.
3832 * "arg" is advanced to the next non-white after the recognized expression.
3833 *
3834 * Return OK or FAIL.
3835 */
3836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003837eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 typval_T var2;
3840 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003842 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003843#ifdef FEAT_FLOAT
3844 float_T f1 = 0, f2 = 0;
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 char_u *s1, *s2;
3847 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3848 char_u *p;
3849
3850 /*
3851 * Get the first variable.
3852 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003853 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return FAIL;
3855
3856 /*
3857 * Repeat computing, until no '+', '-' or '.' is following.
3858 */
3859 for (;;)
3860 {
3861 op = **arg;
3862 if (op != '+' && op != '-' && op != '.')
3863 break;
3864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 if ((op != '+' || rettv->v_type != VAR_LIST)
3866#ifdef FEAT_FLOAT
3867 && (op == '.' || rettv->v_type != VAR_FLOAT)
3868#endif
3869 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870 {
3871 /* For "list + ...", an illegal use of the first operand as
3872 * a number cannot be determined before evaluating the 2nd
3873 * operand: if this is also a list, all is ok.
3874 * For "something . ...", "something - ..." or "non-list + ...",
3875 * we know that the first operand needs to be a string or number
3876 * without evaluating the 2nd operand. So check before to avoid
3877 * side effects after an error. */
3878 if (evaluate && get_tv_string_chk(rettv) == NULL)
3879 {
3880 clear_tv(rettv);
3881 return FAIL;
3882 }
3883 }
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 /*
3886 * Get the second variable.
3887 */
3888 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003889 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return FAIL;
3893 }
3894
3895 if (evaluate)
3896 {
3897 /*
3898 * Compute the result.
3899 */
3900 if (op == '.')
3901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3903 s2 = get_tv_string_buf_chk(&var2, buf2);
3904 if (s2 == NULL) /* type error ? */
3905 {
3906 clear_tv(rettv);
3907 clear_tv(&var2);
3908 return FAIL;
3909 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003910 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911 clear_tv(rettv);
3912 rettv->v_type = VAR_STRING;
3913 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003915 else if (op == '+' && rettv->v_type == VAR_LIST
3916 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003917 {
3918 /* concatenate Lists */
3919 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3920 &var3) == FAIL)
3921 {
3922 clear_tv(rettv);
3923 clear_tv(&var2);
3924 return FAIL;
3925 }
3926 clear_tv(rettv);
3927 *rettv = var3;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 else
3930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003931 int error = FALSE;
3932
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003933#ifdef FEAT_FLOAT
3934 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003935 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003936 f1 = rettv->vval.v_float;
3937 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003938 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003939 else
3940#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003942 n1 = get_tv_number_chk(rettv, &error);
3943 if (error)
3944 {
3945 /* This can only happen for "list + non-list". For
3946 * "non-list + ..." or "something - ...", we returned
3947 * before evaluating the 2nd operand. */
3948 clear_tv(rettv);
3949 return FAIL;
3950 }
3951#ifdef FEAT_FLOAT
3952 if (var2.v_type == VAR_FLOAT)
3953 f1 = n1;
3954#endif
3955 }
3956#ifdef FEAT_FLOAT
3957 if (var2.v_type == VAR_FLOAT)
3958 {
3959 f2 = var2.vval.v_float;
3960 n2 = 0;
3961 }
3962 else
3963#endif
3964 {
3965 n2 = get_tv_number_chk(&var2, &error);
3966 if (error)
3967 {
3968 clear_tv(rettv);
3969 clear_tv(&var2);
3970 return FAIL;
3971 }
3972#ifdef FEAT_FLOAT
3973 if (rettv->v_type == VAR_FLOAT)
3974 f2 = n2;
3975#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003977 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003978
3979#ifdef FEAT_FLOAT
3980 /* If there is a float on either side the result is a float. */
3981 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3982 {
3983 if (op == '+')
3984 f1 = f1 + f2;
3985 else
3986 f1 = f1 - f2;
3987 rettv->v_type = VAR_FLOAT;
3988 rettv->vval.v_float = f1;
3989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003991#endif
3992 {
3993 if (op == '+')
3994 n1 = n1 + n2;
3995 else
3996 n1 = n1 - n2;
3997 rettv->v_type = VAR_NUMBER;
3998 rettv->vval.v_number = n1;
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 }
4004 return OK;
4005}
4006
4007/*
4008 * Handle fifth level expression:
4009 * * number multiplication
4010 * / number division
4011 * % number modulo
4012 *
4013 * "arg" must point to the first non-white of the expression.
4014 * "arg" is advanced to the next non-white after the recognized expression.
4015 *
4016 * Return OK or FAIL.
4017 */
4018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004019eval6(
4020 char_u **arg,
4021 typval_T *rettv,
4022 int evaluate,
4023 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024{
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004027 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004028#ifdef FEAT_FLOAT
4029 int use_float = FALSE;
4030 float_T f1 = 0, f2;
4031#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034 /*
4035 * Get the first variable.
4036 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004037 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return FAIL;
4039
4040 /*
4041 * Repeat computing, until no '*', '/' or '%' is following.
4042 */
4043 for (;;)
4044 {
4045 op = **arg;
4046 if (op != '*' && op != '/' && op != '%')
4047 break;
4048
4049 if (evaluate)
4050 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004051#ifdef FEAT_FLOAT
4052 if (rettv->v_type == VAR_FLOAT)
4053 {
4054 f1 = rettv->vval.v_float;
4055 use_float = TRUE;
4056 n1 = 0;
4057 }
4058 else
4059#endif
4060 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004062 if (error)
4063 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 else
4066 n1 = 0;
4067
4068 /*
4069 * Get the second variable.
4070 */
4071 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004072 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return FAIL;
4074
4075 if (evaluate)
4076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004077#ifdef FEAT_FLOAT
4078 if (var2.v_type == VAR_FLOAT)
4079 {
4080 if (!use_float)
4081 {
4082 f1 = n1;
4083 use_float = TRUE;
4084 }
4085 f2 = var2.vval.v_float;
4086 n2 = 0;
4087 }
4088 else
4089#endif
4090 {
4091 n2 = get_tv_number_chk(&var2, &error);
4092 clear_tv(&var2);
4093 if (error)
4094 return FAIL;
4095#ifdef FEAT_FLOAT
4096 if (use_float)
4097 f2 = n2;
4098#endif
4099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 /*
4102 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004105#ifdef FEAT_FLOAT
4106 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004108 if (op == '*')
4109 f1 = f1 * f2;
4110 else if (op == '/')
4111 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004112# ifdef VMS
4113 /* VMS crashes on divide by zero, work around it */
4114 if (f2 == 0.0)
4115 {
4116 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004117 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004118 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004119 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004120 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004121 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004122 }
4123 else
4124 f1 = f1 / f2;
4125# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004126 /* We rely on the floating point library to handle divide
4127 * by zero to result in "inf" and not a crash. */
4128 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004129# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004132 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004133 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004134 return FAIL;
4135 }
4136 rettv->v_type = VAR_FLOAT;
4137 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004142 if (op == '*')
4143 n1 = n1 * n2;
4144 else if (op == '/')
4145 {
4146 if (n2 == 0) /* give an error message? */
4147 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004148 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004149 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004150 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004151 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004152 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004153 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154 }
4155 else
4156 n1 = n1 / n2;
4157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004159 {
4160 if (n2 == 0) /* give an error message? */
4161 n1 = 0;
4162 else
4163 n1 = n1 % n2;
4164 }
4165 rettv->v_type = VAR_NUMBER;
4166 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169 }
4170
4171 return OK;
4172}
4173
4174/*
4175 * Handle sixth level expression:
4176 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004177 * "string" string constant
4178 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 * &option-name option value
4180 * @r register contents
4181 * identifier variable value
4182 * function() function call
4183 * $VAR environment variable
4184 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004185 * [expr, expr] List
4186 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 *
4188 * Also handle:
4189 * ! in front logical NOT
4190 * - in front unary minus
4191 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004192 * trailing [] subscript in String or List
4193 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 *
4195 * "arg" must point to the first non-white of the expression.
4196 * "arg" is advanced to the next non-white after the recognized expression.
4197 *
4198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval7(
4202 char_u **arg,
4203 typval_T *rettv,
4204 int evaluate,
4205 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004207 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 int len;
4209 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 char_u *start_leader, *end_leader;
4211 int ret = OK;
4212 char_u *alias;
4213
4214 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004216 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004221 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 */
4223 start_leader = *arg;
4224 while (**arg == '!' || **arg == '-' || **arg == '+')
4225 *arg = skipwhite(*arg + 1);
4226 end_leader = *arg;
4227
4228 switch (**arg)
4229 {
4230 /*
4231 * Number constant.
4232 */
4233 case '0':
4234 case '1':
4235 case '2':
4236 case '3':
4237 case '4':
4238 case '5':
4239 case '6':
4240 case '7':
4241 case '8':
4242 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004243 {
4244#ifdef FEAT_FLOAT
4245 char_u *p = skipdigits(*arg + 1);
4246 int get_float = FALSE;
4247
4248 /* We accept a float when the format matches
4249 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004250 * strict to avoid backwards compatibility problems.
4251 * Don't look for a float after the "." operator, so that
4252 * ":let vers = 1.2.3" doesn't fail. */
4253 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004255 get_float = TRUE;
4256 p = skipdigits(p + 2);
4257 if (*p == 'e' || *p == 'E')
4258 {
4259 ++p;
4260 if (*p == '-' || *p == '+')
4261 ++p;
4262 if (!vim_isdigit(*p))
4263 get_float = FALSE;
4264 else
4265 p = skipdigits(p + 1);
4266 }
4267 if (ASCII_ISALPHA(*p) || *p == '.')
4268 get_float = FALSE;
4269 }
4270 if (get_float)
4271 {
4272 float_T f;
4273
4274 *arg += string2float(*arg, &f);
4275 if (evaluate)
4276 {
4277 rettv->v_type = VAR_FLOAT;
4278 rettv->vval.v_float = f;
4279 }
4280 }
4281 else
4282#endif
4283 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004284 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004285 *arg += len;
4286 if (evaluate)
4287 {
4288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = n;
4290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /*
4296 * String constant: "string".
4297 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 break;
4300
4301 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004305 break;
4306
4307 /*
4308 * List: [expr, expr]
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 break;
4312
4313 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004314 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 * Dictionary: {key: val, key: val}
4316 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004317 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4318 if (ret == NOTDONE)
4319 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004320 break;
4321
4322 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004323 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004325 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 break;
4327
4328 /*
4329 * Environment variable: $VAR.
4330 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 break;
4333
4334 /*
4335 * Register contents: @r.
4336 */
4337 case '@': ++*arg;
4338 if (evaluate)
4339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004341 rettv->vval.v_string = get_reg_contents(**arg,
4342 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 if (**arg != NUL)
4345 ++*arg;
4346 break;
4347
4348 /*
4349 * nested expression: (expression).
4350 */
4351 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004352 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (**arg == ')')
4354 ++*arg;
4355 else if (ret == OK)
4356 {
4357 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 ret = FAIL;
4360 }
4361 break;
4362
Bram Moolenaar8c711452005-01-14 21:53:12 +00004363 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 break;
4365 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004366
4367 if (ret == NOTDONE)
4368 {
4369 /*
4370 * Must be a variable or function name.
4371 * Can also be a curly-braces kind of name: {expr}.
4372 */
4373 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004374 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004375 if (alias != NULL)
4376 s = alias;
4377
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004378 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004379 ret = FAIL;
4380 else
4381 {
4382 if (**arg == '(') /* recursive! */
4383 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004384 partial_T *partial;
4385
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004386 if (!evaluate)
4387 check_vars(s, len);
4388
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 /* If "s" is the name of a variable of type VAR_FUNC
4390 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004391 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004392
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004393 /* Need to make a copy, in case evaluating the arguments makes
4394 * the name invalid. */
4395 s = vim_strsave(s);
4396 if (s == NULL)
4397 ret = FAIL;
4398 else
4399 /* Invoke the function. */
4400 ret = get_func_tv(s, len, rettv, arg,
4401 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4402 &len, evaluate, partial, NULL);
4403 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004404
4405 /* If evaluate is FALSE rettv->v_type was not set in
4406 * get_func_tv, but it's needed in handle_subscript() to parse
4407 * what follows. So set it here. */
4408 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4409 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004410 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004411 rettv->v_type = VAR_FUNC;
4412 }
4413
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414 /* Stop the expression evaluation when immediately
4415 * aborting on error, or when an interrupt occurred or
4416 * an exception was thrown but not caught. */
4417 if (aborting())
4418 {
4419 if (ret == OK)
4420 clear_tv(rettv);
4421 ret = FAIL;
4422 }
4423 }
4424 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004425 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004426 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004427 {
4428 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004429 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004432 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004433 }
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 *arg = skipwhite(*arg);
4436
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004437 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4438 * expr(expr). */
4439 if (ret == OK)
4440 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 /*
4443 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4444 */
4445 if (ret == OK && evaluate && end_leader > start_leader)
4446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004447 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004448 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449#ifdef FEAT_FLOAT
4450 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004451
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 if (rettv->v_type == VAR_FLOAT)
4453 f = rettv->vval.v_float;
4454 else
4455#endif
4456 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 clear_tv(rettv);
4460 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004462 else
4463 {
4464 while (end_leader > start_leader)
4465 {
4466 --end_leader;
4467 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004468 {
4469#ifdef FEAT_FLOAT
4470 if (rettv->v_type == VAR_FLOAT)
4471 f = !f;
4472 else
4473#endif
4474 val = !val;
4475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004476 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004477 {
4478#ifdef FEAT_FLOAT
4479 if (rettv->v_type == VAR_FLOAT)
4480 f = -f;
4481 else
4482#endif
4483 val = -val;
4484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004486#ifdef FEAT_FLOAT
4487 if (rettv->v_type == VAR_FLOAT)
4488 {
4489 clear_tv(rettv);
4490 rettv->vval.v_float = f;
4491 }
4492 else
4493#endif
4494 {
4495 clear_tv(rettv);
4496 rettv->v_type = VAR_NUMBER;
4497 rettv->vval.v_number = val;
4498 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501
4502 return ret;
4503}
4504
4505/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004506 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4507 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4509 */
4510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004511eval_index(
4512 char_u **arg,
4513 typval_T *rettv,
4514 int evaluate,
4515 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516{
4517 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004518 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004519 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 long len = -1;
4521 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524
Bram Moolenaara03f2332016-02-06 18:09:59 +01004525 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004526 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004527 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004528 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004529 if (verbose)
4530 EMSG(_("E695: Cannot index a Funcref"));
4531 return FAIL;
4532 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004533#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004534 if (verbose)
4535 EMSG(_(e_float_as_string));
4536 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004537#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004538 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004539 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004540 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004541 if (verbose)
4542 EMSG(_("E909: Cannot index a special variable"));
4543 return FAIL;
4544 case VAR_UNKNOWN:
4545 if (evaluate)
4546 return FAIL;
4547 /* FALLTHROUGH */
4548
4549 case VAR_STRING:
4550 case VAR_NUMBER:
4551 case VAR_LIST:
4552 case VAR_DICT:
4553 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004554 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004555
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004556 init_tv(&var1);
4557 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 /*
4561 * dict.name
4562 */
4563 key = *arg + 1;
4564 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4565 ;
4566 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 }
4570 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 /*
4573 * something[idx]
4574 *
4575 * Get the (first) variable from inside the [].
4576 */
4577 *arg = skipwhite(*arg + 1);
4578 if (**arg == ':')
4579 empty1 = TRUE;
4580 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4581 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004582 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4583 {
4584 /* not a number or string */
4585 clear_tv(&var1);
4586 return FAIL;
4587 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588
4589 /*
4590 * Get the second variable from inside the [:].
4591 */
4592 if (**arg == ':')
4593 {
4594 range = TRUE;
4595 *arg = skipwhite(*arg + 1);
4596 if (**arg == ']')
4597 empty2 = TRUE;
4598 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 if (!empty1)
4601 clear_tv(&var1);
4602 return FAIL;
4603 }
4604 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4605 {
4606 /* not a number or string */
4607 if (!empty1)
4608 clear_tv(&var1);
4609 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 return FAIL;
4611 }
4612 }
4613
4614 /* Check for the ']'. */
4615 if (**arg != ']')
4616 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004617 if (verbose)
4618 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 clear_tv(&var1);
4620 if (range)
4621 clear_tv(&var2);
4622 return FAIL;
4623 }
4624 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 }
4626
4627 if (evaluate)
4628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 n1 = 0;
4630 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 n1 = get_tv_number(&var1);
4633 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 }
4635 if (range)
4636 {
4637 if (empty2)
4638 n2 = -1;
4639 else
4640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 n2 = get_tv_number(&var2);
4642 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 }
4644 }
4645
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004646 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004648 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004649 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004650 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004651 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004652 case VAR_SPECIAL:
4653 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004654 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004655 break; /* not evaluating, skipping over subscript */
4656
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 case VAR_NUMBER:
4658 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 len = (long)STRLEN(s);
4661 if (range)
4662 {
4663 /* The resulting variable is a substring. If the indexes
4664 * are out of range the result is empty. */
4665 if (n1 < 0)
4666 {
4667 n1 = len + n1;
4668 if (n1 < 0)
4669 n1 = 0;
4670 }
4671 if (n2 < 0)
4672 n2 = len + n2;
4673 else if (n2 >= len)
4674 n2 = len;
4675 if (n1 >= len || n2 < 0 || n1 > n2)
4676 s = NULL;
4677 else
4678 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4679 }
4680 else
4681 {
4682 /* The resulting variable is a string of a single
4683 * character. If the index is too big or negative the
4684 * result is empty. */
4685 if (n1 >= len || n1 < 0)
4686 s = NULL;
4687 else
4688 s = vim_strnsave(s + n1, 1);
4689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 rettv->v_type = VAR_STRING;
4692 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 break;
4694
4695 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 if (n1 < 0)
4698 n1 = len + n1;
4699 if (!empty1 && (n1 < 0 || n1 >= len))
4700 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004701 /* For a range we allow invalid values and return an empty
4702 * list. A list index out of range is an error. */
4703 if (!range)
4704 {
4705 if (verbose)
4706 EMSGN(_(e_listidx), n1);
4707 return FAIL;
4708 }
4709 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 }
4711 if (range)
4712 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 list_T *l;
4714 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715
4716 if (n2 < 0)
4717 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004718 else if (n2 >= len)
4719 n2 = len - 1;
4720 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004721 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 l = list_alloc();
4723 if (l == NULL)
4724 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004725 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 n1 <= n2; ++n1)
4727 {
4728 if (list_append_tv(l, &item->li_tv) == FAIL)
4729 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004730 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 return FAIL;
4732 }
4733 item = item->li_next;
4734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004736 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 }
4738 else
4739 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004740 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
4742 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 }
4744 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745
4746 case VAR_DICT:
4747 if (range)
4748 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004749 if (verbose)
4750 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 if (len == -1)
4752 clear_tv(&var1);
4753 return FAIL;
4754 }
4755 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004756 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004757
4758 if (len == -1)
4759 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004760 key = get_tv_string_chk(&var1);
4761 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 clear_tv(&var1);
4764 return FAIL;
4765 }
4766 }
4767
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004768 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004770 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004771 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772 if (len == -1)
4773 clear_tv(&var1);
4774 if (item == NULL)
4775 return FAIL;
4776
4777 copy_tv(&item->di_tv, &var1);
4778 clear_tv(rettv);
4779 *rettv = var1;
4780 }
4781 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 }
4783 }
4784
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 return OK;
4786}
4787
4788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 * Get an option value.
4790 * "arg" points to the '&' or '+' before the option name.
4791 * "arg" is advanced to character after the option name.
4792 * Return OK or FAIL.
4793 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004795get_option_tv(
4796 char_u **arg,
4797 typval_T *rettv, /* when NULL, only check if option exists */
4798 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 char_u *option_end;
4801 long numval;
4802 char_u *stringval;
4803 int opt_type;
4804 int c;
4805 int working = (**arg == '+'); /* has("+option") */
4806 int ret = OK;
4807 int opt_flags;
4808
4809 /*
4810 * Isolate the option name and find its value.
4811 */
4812 option_end = find_option_end(arg, &opt_flags);
4813 if (option_end == NULL)
4814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 EMSG2(_("E112: Option name missing: %s"), *arg);
4817 return FAIL;
4818 }
4819
4820 if (!evaluate)
4821 {
4822 *arg = option_end;
4823 return OK;
4824 }
4825
4826 c = *option_end;
4827 *option_end = NUL;
4828 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 if (opt_type == -3) /* invalid name */
4832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 EMSG2(_("E113: Unknown option: %s"), *arg);
4835 ret = FAIL;
4836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 if (opt_type == -2) /* hidden string option */
4840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else if (opt_type == -1) /* hidden number option */
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 rettv->v_type = VAR_NUMBER;
4847 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 else if (opt_type == 1) /* number option */
4850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 rettv->v_type = VAR_NUMBER;
4852 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else /* string option */
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 rettv->v_type = VAR_STRING;
4857 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 }
4860 else if (working && (opt_type == -2 || opt_type == -1))
4861 ret = FAIL;
4862
4863 *option_end = c; /* put back for error messages */
4864 *arg = option_end;
4865
4866 return ret;
4867}
4868
4869/*
4870 * Allocate a variable for a string constant.
4871 * Return OK or FAIL.
4872 */
4873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004874get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
4876 char_u *p;
4877 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 int extra = 0;
4879
4880 /*
4881 * Find the end of the string, skipping backslashed characters.
4882 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004883 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
4885 if (*p == '\\' && p[1] != NUL)
4886 {
4887 ++p;
4888 /* A "\<x>" form occupies at least 4 characters, and produces up
4889 * to 6 characters: reserve space for 2 extra */
4890 if (*p == '<')
4891 extra += 2;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894
4895 if (*p != '"')
4896 {
4897 EMSG2(_("E114: Missing quote: %s"), *arg);
4898 return FAIL;
4899 }
4900
4901 /* If only parsing, set *arg and return here */
4902 if (!evaluate)
4903 {
4904 *arg = p + 1;
4905 return OK;
4906 }
4907
4908 /*
4909 * Copy the string into allocated memory, handling backslashed
4910 * characters.
4911 */
4912 name = alloc((unsigned)(p - *arg + extra));
4913 if (name == NULL)
4914 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 rettv->v_type = VAR_STRING;
4916 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
4920 if (*p == '\\')
4921 {
4922 switch (*++p)
4923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 case 'b': *name++ = BS; ++p; break;
4925 case 'e': *name++ = ESC; ++p; break;
4926 case 'f': *name++ = FF; ++p; break;
4927 case 'n': *name++ = NL; ++p; break;
4928 case 'r': *name++ = CAR; ++p; break;
4929 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 case 'X': /* hex: "\x1", "\x12" */
4932 case 'x':
4933 case 'u': /* Unicode: "\u0023" */
4934 case 'U':
4935 if (vim_isxdigit(p[1]))
4936 {
4937 int n, nr;
4938 int c = toupper(*p);
4939
4940 if (c == 'X')
4941 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004942 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004944 else
4945 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 nr = 0;
4947 while (--n >= 0 && vim_isxdigit(p[1]))
4948 {
4949 ++p;
4950 nr = (nr << 4) + hex2nr(*p);
4951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953#ifdef FEAT_MBYTE
4954 /* For "\u" store the number according to
4955 * 'encoding'. */
4956 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 else
4959#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 break;
4963
4964 /* octal: "\1", "\12", "\123" */
4965 case '0':
4966 case '1':
4967 case '2':
4968 case '3':
4969 case '4':
4970 case '5':
4971 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 case '7': *name = *p++ - '0';
4973 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 *name = (*name << 3) + *p++ - '0';
4976 if (*p >= '0' && *p <= '7')
4977 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981
4982 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004983 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (extra != 0)
4985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988 }
4989 /* FALLTHROUGH */
4990
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 break;
4993 }
4994 }
4995 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005000 if (*p != NUL) /* just in case */
5001 ++p;
5002 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 return OK;
5005}
5006
5007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 * Return OK or FAIL.
5010 */
5011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005012get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005015 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005016 int reduce = 0;
5017
5018 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005020 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005021 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005024 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005026 break;
5027 ++reduce;
5028 ++p;
5029 }
5030 }
5031
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035 return FAIL;
5036 }
5037
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 if (!evaluate)
5040 {
5041 *arg = p + 1;
5042 return OK;
5043 }
5044
5045 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 */
5048 str = alloc((unsigned)((p - *arg) - reduce));
5049 if (str == NULL)
5050 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 rettv->v_type = VAR_STRING;
5052 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005053
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 break;
5060 ++p;
5061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 *arg = p + 1;
5066
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 return OK;
5068}
5069
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005070/*
5071 * Return the function name of the partial.
5072 */
5073 char_u *
5074partial_name(partial_T *pt)
5075{
5076 if (pt->pt_name != NULL)
5077 return pt->pt_name;
5078 return pt->pt_func->uf_name;
5079}
5080
Bram Moolenaarddecc252016-04-06 22:59:37 +02005081 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005083{
5084 int i;
5085
5086 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005087 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005088 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005089 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005090 if (pt->pt_name != NULL)
5091 {
5092 func_unref(pt->pt_name);
5093 vim_free(pt->pt_name);
5094 }
5095 else
5096 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005097 vim_free(pt);
5098}
5099
5100/*
5101 * Unreference a closure: decrement the reference count and free it when it
5102 * becomes zero.
5103 */
5104 void
5105partial_unref(partial_T *pt)
5106{
5107 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005108 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005109}
5110
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005111static int tv_equal_recurse_limit;
5112
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005113 static int
5114func_equal(
5115 typval_T *tv1,
5116 typval_T *tv2,
5117 int ic) /* ignore case */
5118{
5119 char_u *s1, *s2;
5120 dict_T *d1, *d2;
5121 int a1, a2;
5122 int i;
5123
5124 /* empty and NULL function name considered the same */
5125 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005126 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005127 if (s1 != NULL && *s1 == NUL)
5128 s1 = NULL;
5129 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005130 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005131 if (s2 != NULL && *s2 == NUL)
5132 s2 = NULL;
5133 if (s1 == NULL || s2 == NULL)
5134 {
5135 if (s1 != s2)
5136 return FALSE;
5137 }
5138 else if (STRCMP(s1, s2) != 0)
5139 return FALSE;
5140
5141 /* empty dict and NULL dict is different */
5142 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5143 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5144 if (d1 == NULL || d2 == NULL)
5145 {
5146 if (d1 != d2)
5147 return FALSE;
5148 }
5149 else if (!dict_equal(d1, d2, ic, TRUE))
5150 return FALSE;
5151
5152 /* empty list and no list considered the same */
5153 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5154 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5155 if (a1 != a2)
5156 return FALSE;
5157 for (i = 0; i < a1; ++i)
5158 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5159 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5160 return FALSE;
5161
5162 return TRUE;
5163}
5164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005165/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005166 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005167 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005169 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005171tv_equal(
5172 typval_T *tv1,
5173 typval_T *tv2,
5174 int ic, /* ignore case */
5175 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005176{
5177 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005178 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005179 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005180 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005182 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005183 * recursiveness to a limit. We guess they are equal then.
5184 * A fixed limit has the problem of still taking an awful long time.
5185 * Reduce the limit every time running into it. That should work fine for
5186 * deeply linked structures that are not recursively linked and catch
5187 * recursiveness quickly. */
5188 if (!recursive)
5189 tv_equal_recurse_limit = 1000;
5190 if (recursive_cnt >= tv_equal_recurse_limit)
5191 {
5192 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005193 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005194 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005195
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005196 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5197 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005198 if ((tv1->v_type == VAR_FUNC
5199 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5200 && (tv2->v_type == VAR_FUNC
5201 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5202 {
5203 ++recursive_cnt;
5204 r = func_equal(tv1, tv2, ic);
5205 --recursive_cnt;
5206 return r;
5207 }
5208
5209 if (tv1->v_type != tv2->v_type)
5210 return FALSE;
5211
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005212 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005213 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005214 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005215 ++recursive_cnt;
5216 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5217 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005218 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005219
5220 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005221 ++recursive_cnt;
5222 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5223 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005224 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005225
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005226 case VAR_NUMBER:
5227 return tv1->vval.v_number == tv2->vval.v_number;
5228
5229 case VAR_STRING:
5230 s1 = get_tv_string_buf(tv1, buf1);
5231 s2 = get_tv_string_buf(tv2, buf2);
5232 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005233
5234 case VAR_SPECIAL:
5235 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005236
5237 case VAR_FLOAT:
5238#ifdef FEAT_FLOAT
5239 return tv1->vval.v_float == tv2->vval.v_float;
5240#endif
5241 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005242#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005243 return tv1->vval.v_job == tv2->vval.v_job;
5244#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005245 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005246#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005247 return tv1->vval.v_channel == tv2->vval.v_channel;
5248#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005249 case VAR_FUNC:
5250 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005251 case VAR_UNKNOWN:
5252 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005253 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005254
Bram Moolenaara03f2332016-02-06 18:09:59 +01005255 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5256 * does not equal anything, not even itself. */
5257 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005258}
5259
5260/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005261 * Return the next (unique) copy ID.
5262 * Used for serializing nested structures.
5263 */
5264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005265get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005266{
5267 current_copyID += COPYID_INC;
5268 return current_copyID;
5269}
5270
5271/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 * Garbage collection for lists and dictionaries.
5273 *
5274 * We use reference counts to be able to free most items right away when they
5275 * are no longer used. But for composite items it's possible that it becomes
5276 * unused while the reference count is > 0: When there is a recursive
5277 * reference. Example:
5278 * :let l = [1, 2, 3]
5279 * :let d = {9: l}
5280 * :let l[1] = d
5281 *
5282 * Since this is quite unusual we handle this with garbage collection: every
5283 * once in a while find out which lists and dicts are not referenced from any
5284 * variable.
5285 *
5286 * Here is a good reference text about garbage collection (refers to Python
5287 * but it applies to all reference-counting mechanisms):
5288 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005289 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005290
5291/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005292 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005293 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005294 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005295 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005296 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005297garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005298{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005299 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005300 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005301 buf_T *buf;
5302 win_T *wp;
5303 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005304 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005305 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005306
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005307 if (!testing)
5308 {
5309 /* Only do this once. */
5310 want_garbage_collect = FALSE;
5311 may_garbage_collect = FALSE;
5312 garbage_collect_at_exit = FALSE;
5313 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005314
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005315 /* We advance by two because we add one for items referenced through
5316 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005317 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005319 /*
5320 * 1. Go through all accessible variables and mark all lists and dicts
5321 * with copyID.
5322 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005323
5324 /* Don't free variables in the previous_funccal list unless they are only
5325 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005326 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005328
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005329 /* script-local variables */
5330 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005331 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005332
5333 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005334 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5336 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005337
5338 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005339 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005340 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5341 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005342#ifdef FEAT_AUTOCMD
5343 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005344 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5345 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005346#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005348 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005349 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005350 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5351 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005352 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354
5355 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005356 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005357
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005358 /* named functions (matters for closures) */
5359 abort = abort || set_ref_in_functions(copyID);
5360
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005361 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005362 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005363
Bram Moolenaard812df62008-11-09 12:46:09 +00005364 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005365 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005366
Bram Moolenaar1dced572012-04-05 16:54:08 +02005367#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005368 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005369#endif
5370
Bram Moolenaardb913952012-06-29 12:54:53 +02005371#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005372 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005373#endif
5374
5375#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005376 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005377#endif
5378
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005379#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005380 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005381 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005382#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005383#ifdef FEAT_NETBEANS_INTG
5384 abort = abort || set_ref_in_nb_channel(copyID);
5385#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005386
Bram Moolenaare3188e22016-05-31 21:13:04 +02005387#ifdef FEAT_TIMERS
5388 abort = abort || set_ref_in_timer(copyID);
5389#endif
5390
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005391#ifdef FEAT_QUICKFIX
5392 abort = abort || set_ref_in_quickfix(copyID);
5393#endif
5394
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005395#ifdef FEAT_TERMINAL
5396 abort = abort || set_ref_in_term(copyID);
5397#endif
5398
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005400 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 /*
5402 * 2. Free lists and dictionaries that are not referenced.
5403 */
5404 did_free = free_unref_items(copyID);
5405
5406 /*
5407 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005409 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005411 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005412 else if (p_verbose > 0)
5413 {
5414 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5415 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005416
5417 return did_free;
5418}
5419
5420/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005421 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005422 */
5423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005425{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005426 int did_free = FALSE;
5427
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005428 /* Let all "free" functions know that we are here. This means no
5429 * dictionaries, lists, channels or jobs are to be freed, because we will
5430 * do that here. */
5431 in_free_unref_items = TRUE;
5432
5433 /*
5434 * PASS 1: free the contents of the items. We don't free the items
5435 * themselves yet, so that it is possible to decrement refcount counters
5436 */
5437
Bram Moolenaarcd524592016-07-17 14:57:05 +02005438 /* Go through the list of dicts and free items without the copyID. */
5439 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005440
Bram Moolenaarda861d62016-07-17 15:46:27 +02005441 /* Go through the list of lists and free items without the copyID. */
5442 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005443
5444#ifdef FEAT_JOB_CHANNEL
5445 /* Go through the list of jobs and free items without the copyID. This
5446 * must happen before doing channels, because jobs refer to channels, but
5447 * the reference from the channel to the job isn't tracked. */
5448 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5449
5450 /* Go through the list of channels and free items without the copyID. */
5451 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5452#endif
5453
5454 /*
5455 * PASS 2: free the items themselves.
5456 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005457 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005458 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005459
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005460#ifdef FEAT_JOB_CHANNEL
5461 /* Go through the list of jobs and free items without the copyID. This
5462 * must happen before doing channels, because jobs refer to channels, but
5463 * the reference from the channel to the job isn't tracked. */
5464 free_unused_jobs(copyID, COPYID_MASK);
5465
5466 /* Go through the list of channels and free items without the copyID. */
5467 free_unused_channels(copyID, COPYID_MASK);
5468#endif
5469
5470 in_free_unref_items = FALSE;
5471
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005472 return did_free;
5473}
5474
5475/*
5476 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005477 * "list_stack" is used to add lists to be marked. Can be NULL.
5478 *
5479 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005482set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005483{
5484 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005486 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 hashtab_T *cur_ht;
5488 ht_stack_T *ht_stack = NULL;
5489 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005490
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005491 cur_ht = ht;
5492 for (;;)
5493 {
5494 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005495 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005496 /* Mark each item in the hashtab. If the item contains a hashtab
5497 * it is added to ht_stack, if it contains a list it is added to
5498 * list_stack. */
5499 todo = (int)cur_ht->ht_used;
5500 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5501 if (!HASHITEM_EMPTY(hi))
5502 {
5503 --todo;
5504 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5505 &ht_stack, list_stack);
5506 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005507 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005508
5509 if (ht_stack == NULL)
5510 break;
5511
5512 /* take an item from the stack */
5513 cur_ht = ht_stack->ht;
5514 tempitem = ht_stack;
5515 ht_stack = ht_stack->prev;
5516 free(tempitem);
5517 }
5518
5519 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005520}
5521
5522/*
5523 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005524 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5525 *
5526 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005527 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005529set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005530{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 listitem_T *li;
5532 int abort = FALSE;
5533 list_T *cur_l;
5534 list_stack_T *list_stack = NULL;
5535 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005536
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 cur_l = l;
5538 for (;;)
5539 {
5540 if (!abort)
5541 /* Mark each item in the list. If the item contains a hashtab
5542 * it is added to ht_stack, if it contains a list it is added to
5543 * list_stack. */
5544 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5545 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5546 ht_stack, &list_stack);
5547 if (list_stack == NULL)
5548 break;
5549
5550 /* take an item from the stack */
5551 cur_l = list_stack->list;
5552 tempitem = list_stack;
5553 list_stack = list_stack->prev;
5554 free(tempitem);
5555 }
5556
5557 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005558}
5559
5560/*
5561 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005562 * "list_stack" is used to add lists to be marked. Can be NULL.
5563 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5564 *
5565 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005566 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005568set_ref_in_item(
5569 typval_T *tv,
5570 int copyID,
5571 ht_stack_T **ht_stack,
5572 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005573{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005574 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005575
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005576 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005577 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005578 dict_T *dd = tv->vval.v_dict;
5579
Bram Moolenaara03f2332016-02-06 18:09:59 +01005580 if (dd != NULL && dd->dv_copyID != copyID)
5581 {
5582 /* Didn't see this dict yet. */
5583 dd->dv_copyID = copyID;
5584 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005585 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005586 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5587 }
5588 else
5589 {
5590 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5591 if (newitem == NULL)
5592 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005593 else
5594 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005595 newitem->ht = &dd->dv_hashtab;
5596 newitem->prev = *ht_stack;
5597 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005598 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005599 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005600 }
5601 }
5602 else if (tv->v_type == VAR_LIST)
5603 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005604 list_T *ll = tv->vval.v_list;
5605
Bram Moolenaara03f2332016-02-06 18:09:59 +01005606 if (ll != NULL && ll->lv_copyID != copyID)
5607 {
5608 /* Didn't see this list yet. */
5609 ll->lv_copyID = copyID;
5610 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005611 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005612 abort = set_ref_in_list(ll, copyID, ht_stack);
5613 }
5614 else
5615 {
5616 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005618 if (newitem == NULL)
5619 abort = TRUE;
5620 else
5621 {
5622 newitem->list = ll;
5623 newitem->prev = *list_stack;
5624 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005625 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005626 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005627 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005628 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005629 else if (tv->v_type == VAR_FUNC)
5630 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005631 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005632 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005633 else if (tv->v_type == VAR_PARTIAL)
5634 {
5635 partial_T *pt = tv->vval.v_partial;
5636 int i;
5637
5638 /* A partial does not have a copyID, because it cannot contain itself.
5639 */
5640 if (pt != NULL)
5641 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005642 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005643
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005644 if (pt->pt_dict != NULL)
5645 {
5646 typval_T dtv;
5647
5648 dtv.v_type = VAR_DICT;
5649 dtv.vval.v_dict = pt->pt_dict;
5650 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5651 }
5652
5653 for (i = 0; i < pt->pt_argc; ++i)
5654 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5655 ht_stack, list_stack);
5656 }
5657 }
5658#ifdef FEAT_JOB_CHANNEL
5659 else if (tv->v_type == VAR_JOB)
5660 {
5661 job_T *job = tv->vval.v_job;
5662 typval_T dtv;
5663
5664 if (job != NULL && job->jv_copyID != copyID)
5665 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005666 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005667 if (job->jv_channel != NULL)
5668 {
5669 dtv.v_type = VAR_CHANNEL;
5670 dtv.vval.v_channel = job->jv_channel;
5671 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5672 }
5673 if (job->jv_exit_partial != NULL)
5674 {
5675 dtv.v_type = VAR_PARTIAL;
5676 dtv.vval.v_partial = job->jv_exit_partial;
5677 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5678 }
5679 }
5680 }
5681 else if (tv->v_type == VAR_CHANNEL)
5682 {
5683 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005684 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005685 typval_T dtv;
5686 jsonq_T *jq;
5687 cbq_T *cq;
5688
5689 if (ch != NULL && ch->ch_copyID != copyID)
5690 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005691 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005692 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005693 {
5694 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5695 jq = jq->jq_next)
5696 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5697 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5698 cq = cq->cq_next)
5699 if (cq->cq_partial != NULL)
5700 {
5701 dtv.v_type = VAR_PARTIAL;
5702 dtv.vval.v_partial = cq->cq_partial;
5703 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5704 }
5705 if (ch->ch_part[part].ch_partial != NULL)
5706 {
5707 dtv.v_type = VAR_PARTIAL;
5708 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5709 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5710 }
5711 }
5712 if (ch->ch_partial != NULL)
5713 {
5714 dtv.v_type = VAR_PARTIAL;
5715 dtv.vval.v_partial = ch->ch_partial;
5716 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5717 }
5718 if (ch->ch_close_partial != NULL)
5719 {
5720 dtv.v_type = VAR_PARTIAL;
5721 dtv.vval.v_partial = ch->ch_close_partial;
5722 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5723 }
5724 }
5725 }
5726#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005727 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005728}
5729
Bram Moolenaar17a13432016-01-24 14:22:10 +01005730 static char *
5731get_var_special_name(int nr)
5732{
5733 switch (nr)
5734 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005735 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005736 case VVAL_TRUE: return "v:true";
5737 case VVAL_NONE: return "v:none";
5738 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005739 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005740 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005741 return "42";
5742}
5743
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 * Return a string with the string representation of a variable.
5746 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005747 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005748 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005749 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5750 * stings as "string()", otherwise does not put quotes around strings, as
5751 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005752 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5753 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005754 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005756 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005757echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005758 typval_T *tv,
5759 char_u **tofree,
5760 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005761 int copyID,
5762 int echo_style,
5763 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005764 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005766 static int recurse = 0;
5767 char_u *r = NULL;
5768
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005770 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005771 if (!did_echo_string_emsg)
5772 {
5773 /* Only give this message once for a recursive call to avoid
5774 * flooding the user with errors. And stop iterating over lists
5775 * and dicts. */
5776 did_echo_string_emsg = TRUE;
5777 EMSG(_("E724: variable nested too deep for displaying"));
5778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005779 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005780 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005781 }
5782 ++recurse;
5783
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784 switch (tv->v_type)
5785 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005786 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005787 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005788 {
5789 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005790 r = tv->vval.v_string;
5791 if (r == NULL)
5792 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005793 }
5794 else
5795 {
5796 *tofree = string_quote(tv->vval.v_string, FALSE);
5797 r = *tofree;
5798 }
5799 break;
5800
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005802 if (echo_style)
5803 {
5804 *tofree = NULL;
5805 r = tv->vval.v_string;
5806 }
5807 else
5808 {
5809 *tofree = string_quote(tv->vval.v_string, TRUE);
5810 r = *tofree;
5811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005813
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005814 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005815 {
5816 partial_T *pt = tv->vval.v_partial;
5817 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005818 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005819 garray_T ga;
5820 int i;
5821 char_u *tf;
5822
5823 ga_init2(&ga, 1, 100);
5824 ga_concat(&ga, (char_u *)"function(");
5825 if (fname != NULL)
5826 {
5827 ga_concat(&ga, fname);
5828 vim_free(fname);
5829 }
5830 if (pt != NULL && pt->pt_argc > 0)
5831 {
5832 ga_concat(&ga, (char_u *)", [");
5833 for (i = 0; i < pt->pt_argc; ++i)
5834 {
5835 if (i > 0)
5836 ga_concat(&ga, (char_u *)", ");
5837 ga_concat(&ga,
5838 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5839 vim_free(tf);
5840 }
5841 ga_concat(&ga, (char_u *)"]");
5842 }
5843 if (pt != NULL && pt->pt_dict != NULL)
5844 {
5845 typval_T dtv;
5846
5847 ga_concat(&ga, (char_u *)", ");
5848 dtv.v_type = VAR_DICT;
5849 dtv.vval.v_dict = pt->pt_dict;
5850 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5851 vim_free(tf);
5852 }
5853 ga_concat(&ga, (char_u *)")");
5854
5855 *tofree = ga.ga_data;
5856 r = *tofree;
5857 break;
5858 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005859
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005861 if (tv->vval.v_list == NULL)
5862 {
5863 *tofree = NULL;
5864 r = NULL;
5865 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005866 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5867 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005868 {
5869 *tofree = NULL;
5870 r = (char_u *)"[...]";
5871 }
5872 else
5873 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005874 int old_copyID = tv->vval.v_list->lv_copyID;
5875
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005876 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005877 *tofree = list2string(tv, copyID, restore_copyID);
5878 if (restore_copyID)
5879 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005880 r = *tofree;
5881 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005882 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005883
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005885 if (tv->vval.v_dict == NULL)
5886 {
5887 *tofree = NULL;
5888 r = NULL;
5889 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005890 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5891 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005892 {
5893 *tofree = NULL;
5894 r = (char_u *)"{...}";
5895 }
5896 else
5897 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005899 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005900 *tofree = dict2string(tv, copyID, restore_copyID);
5901 if (restore_copyID)
5902 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005903 r = *tofree;
5904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005906
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005908 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005909 *tofree = NULL;
5910 r = get_tv_string_buf(tv, numbuf);
5911 break;
5912
Bram Moolenaar835dc632016-02-07 14:27:38 +01005913 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005914 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915 *tofree = NULL;
5916 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005917 if (composite_val)
5918 {
5919 *tofree = string_quote(r, FALSE);
5920 r = *tofree;
5921 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005924 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005925#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005926 *tofree = NULL;
5927 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5928 r = numbuf;
5929 break;
5930#endif
5931
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005932 case VAR_SPECIAL:
5933 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005934 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005935 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005937
Bram Moolenaar8502c702014-06-17 12:51:16 +02005938 if (--recurse == 0)
5939 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941}
5942
5943/*
5944 * Return a string with the string representation of a variable.
5945 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5946 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005947 * Does not put quotes around strings, as ":echo" displays values.
5948 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5949 * May return NULL.
5950 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005951 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005952echo_string(
5953 typval_T *tv,
5954 char_u **tofree,
5955 char_u *numbuf,
5956 int copyID)
5957{
5958 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5959}
5960
5961/*
5962 * Return a string with the string representation of a variable.
5963 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5964 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005966 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005968 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969tv2string(
5970 typval_T *tv,
5971 char_u **tofree,
5972 char_u *numbuf,
5973 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005974{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005975 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976}
5977
5978/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 * Return string "str" in ' quotes, doubling ' characters.
5980 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005983 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 char_u *p, *r, *s;
5988
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 len = (function ? 13 : 3);
5990 if (str != NULL)
5991 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005992 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005993 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 if (*p == '\'')
5995 ++len;
5996 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 s = r = alloc(len);
5998 if (r != NULL)
5999 {
6000 if (function)
6001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006002 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006003 r += 10;
6004 }
6005 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006006 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 if (str != NULL)
6008 for (p = str; *p != NUL; )
6009 {
6010 if (*p == '\'')
6011 *r++ = '\'';
6012 MB_COPY_CHAR(p, r);
6013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006014 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 if (function)
6016 *r++ = ')';
6017 *r++ = NUL;
6018 }
6019 return s;
6020}
6021
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006022#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006023/*
6024 * Convert the string "text" to a floating point number.
6025 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6026 * this always uses a decimal point.
6027 * Returns the length of the text that was consumed.
6028 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030string2float(
6031 char_u *text,
6032 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006033{
6034 char *s = (char *)text;
6035 float_T f;
6036
Bram Moolenaar62473612017-01-08 19:25:40 +01006037 /* MS-Windows does not deal with "inf" and "nan" properly. */
6038 if (STRNICMP(text, "inf", 3) == 0)
6039 {
6040 *value = INFINITY;
6041 return 3;
6042 }
6043 if (STRNICMP(text, "-inf", 3) == 0)
6044 {
6045 *value = -INFINITY;
6046 return 4;
6047 }
6048 if (STRNICMP(text, "nan", 3) == 0)
6049 {
6050 *value = NAN;
6051 return 3;
6052 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006053 f = strtod(s, &s);
6054 *value = f;
6055 return (int)((char_u *)s - text);
6056}
6057#endif
6058
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 * Get the value of an environment variable.
6061 * "arg" is pointing to the '$'. It is advanced to after the name.
6062 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006063 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 */
6065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068 char_u *string = NULL;
6069 int len;
6070 int cc;
6071 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006072 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
6074 ++*arg;
6075 name = *arg;
6076 len = get_env_len(arg);
6077 if (evaluate)
6078 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006079 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006080 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006081
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006082 cc = name[len];
6083 name[len] = NUL;
6084 /* first try vim_getenv(), fast for normal environment vars */
6085 string = vim_getenv(name, &mustfree);
6086 if (string != NULL && *string != NUL)
6087 {
6088 if (!mustfree)
6089 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006091 else
6092 {
6093 if (mustfree)
6094 vim_free(string);
6095
6096 /* next try expanding things like $VIM and ${HOME} */
6097 string = expand_env_save(name - 1);
6098 if (string != NULL && *string == '$')
6099 {
6100 vim_free(string);
6101 string = NULL;
6102 }
6103 }
6104 name[len] = cc;
6105
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006106 rettv->v_type = VAR_STRING;
6107 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 }
6109
6110 return OK;
6111}
6112
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006113
6114
6115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006117 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120var2fpos(
6121 typval_T *varp,
6122 int dollar_lnum, /* TRUE when $ is last line */
6123 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006125 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006127 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
Bram Moolenaara5525202006-03-02 22:52:09 +00006129 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006130 if (varp->v_type == VAR_LIST)
6131 {
6132 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006133 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006134 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006135 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006136
6137 l = varp->vval.v_list;
6138 if (l == NULL)
6139 return NULL;
6140
6141 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006142 pos.lnum = list_find_nr(l, 0L, &error);
6143 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006144 return NULL; /* invalid line number */
6145
6146 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006147 pos.col = list_find_nr(l, 1L, &error);
6148 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006149 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006150 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006151
6152 /* We accept "$" for the column number: last column. */
6153 li = list_find(l, 1L);
6154 if (li != NULL && li->li_tv.v_type == VAR_STRING
6155 && li->li_tv.vval.v_string != NULL
6156 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6157 pos.col = len + 1;
6158
Bram Moolenaara5525202006-03-02 22:52:09 +00006159 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006160 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006161 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006162 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006163
Bram Moolenaara5525202006-03-02 22:52:09 +00006164#ifdef FEAT_VIRTUALEDIT
6165 /* Get the virtual offset. Defaults to zero. */
6166 pos.coladd = list_find_nr(l, 2L, &error);
6167 if (error)
6168 pos.coladd = 0;
6169#endif
6170
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006171 return &pos;
6172 }
6173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006174 name = get_tv_string_chk(varp);
6175 if (name == NULL)
6176 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006177 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006179 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6180 {
6181 if (VIsual_active)
6182 return &VIsual;
6183 return &curwin->w_cursor;
6184 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006185 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006187 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6189 return NULL;
6190 return pp;
6191 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006192
6193#ifdef FEAT_VIRTUALEDIT
6194 pos.coladd = 0;
6195#endif
6196
Bram Moolenaar477933c2007-07-17 14:32:23 +00006197 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006198 {
6199 pos.col = 0;
6200 if (name[1] == '0') /* "w0": first visible line */
6201 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006202 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006203 /* In silent Ex mode topline is zero, but that's not a valid line
6204 * number; use one instead. */
6205 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006206 return &pos;
6207 }
6208 else if (name[1] == '$') /* "w$": last visible line */
6209 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006210 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006211 /* In silent Ex mode botline is zero, return zero then. */
6212 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006213 return &pos;
6214 }
6215 }
6216 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006218 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220 pos.lnum = curbuf->b_ml.ml_line_count;
6221 pos.col = 0;
6222 }
6223 else
6224 {
6225 pos.lnum = curwin->w_cursor.lnum;
6226 pos.col = (colnr_T)STRLEN(ml_get_curline());
6227 }
6228 return &pos;
6229 }
6230 return NULL;
6231}
6232
6233/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006234 * Convert list in "arg" into a position and optional file number.
6235 * When "fnump" is NULL there is no file number, only 3 items.
6236 * Note that the column is passed on as-is, the caller may want to decrement
6237 * it to use 1 for the first column.
6238 * Return FAIL when conversion is not possible, doesn't check the position for
6239 * validity.
6240 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006242list2fpos(
6243 typval_T *arg,
6244 pos_T *posp,
6245 int *fnump,
6246 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006247{
6248 list_T *l = arg->vval.v_list;
6249 long i = 0;
6250 long n;
6251
Bram Moolenaar493c1782014-05-28 14:34:46 +02006252 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6253 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006254 if (arg->v_type != VAR_LIST
6255 || l == NULL
6256 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006257 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006258 return FAIL;
6259
6260 if (fnump != NULL)
6261 {
6262 n = list_find_nr(l, i++, NULL); /* fnum */
6263 if (n < 0)
6264 return FAIL;
6265 if (n == 0)
6266 n = curbuf->b_fnum; /* current buffer */
6267 *fnump = n;
6268 }
6269
6270 n = list_find_nr(l, i++, NULL); /* lnum */
6271 if (n < 0)
6272 return FAIL;
6273 posp->lnum = n;
6274
6275 n = list_find_nr(l, i++, NULL); /* col */
6276 if (n < 0)
6277 return FAIL;
6278 posp->col = n;
6279
6280#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006281 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006282 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006283 posp->coladd = 0;
6284 else
6285 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006286#endif
6287
Bram Moolenaar493c1782014-05-28 14:34:46 +02006288 if (curswantp != NULL)
6289 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6290
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006291 return OK;
6292}
6293
6294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * Get the length of an environment variable name.
6296 * Advance "arg" to the first character after the name.
6297 * Return 0 for error.
6298 */
6299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006300get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 char_u *p;
6303 int len;
6304
6305 for (p = *arg; vim_isIDc(*p); ++p)
6306 ;
6307 if (p == *arg) /* no name found */
6308 return 0;
6309
6310 len = (int)(p - *arg);
6311 *arg = p;
6312 return len;
6313}
6314
6315/*
6316 * Get the length of the name of a function or internal variable.
6317 * "arg" is advanced to the first non-white character after the name.
6318 * Return 0 if something is wrong.
6319 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006320 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006321get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 char_u *p;
6324 int len;
6325
6326 /* Find the end of the name. */
6327 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006328 {
6329 if (*p == ':')
6330 {
6331 /* "s:" is start of "s:var", but "n:" is not and can be used in
6332 * slice "[n:]". Also "xx:" is not a namespace. */
6333 len = (int)(p - *arg);
6334 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6335 || len > 1)
6336 break;
6337 }
6338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (p == *arg) /* no name found */
6340 return 0;
6341
6342 len = (int)(p - *arg);
6343 *arg = skipwhite(p);
6344
6345 return len;
6346}
6347
6348/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006349 * Get the length of the name of a variable or function.
6350 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006352 * Return -1 if curly braces expansion failed.
6353 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 * If the name contains 'magic' {}'s, expand them and return the
6355 * expanded name in an allocated string via 'alias' - caller must free.
6356 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006358get_name_len(
6359 char_u **arg,
6360 char_u **alias,
6361 int evaluate,
6362 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 char_u *p;
6366 char_u *expr_start;
6367 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368
6369 *alias = NULL; /* default to no alias */
6370
6371 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6372 && (*arg)[2] == (int)KE_SNR)
6373 {
6374 /* hard coded <SNR>, already translated */
6375 *arg += 3;
6376 return get_id_len(arg) + 3;
6377 }
6378 len = eval_fname_script(*arg);
6379 if (len > 0)
6380 {
6381 /* literal "<SID>", "s:" or "<SNR>" */
6382 *arg += len;
6383 }
6384
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006386 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006388 p = find_name_end(*arg, &expr_start, &expr_end,
6389 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (expr_start != NULL)
6391 {
6392 char_u *temp_string;
6393
6394 if (!evaluate)
6395 {
6396 len += (int)(p - *arg);
6397 *arg = skipwhite(p);
6398 return len;
6399 }
6400
6401 /*
6402 * Include any <SID> etc in the expanded string:
6403 * Thus the -len here.
6404 */
6405 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6406 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006407 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 *alias = temp_string;
6409 *arg = skipwhite(p);
6410 return (int)STRLEN(temp_string);
6411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006414 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 EMSG2(_(e_invexpr2), *arg);
6416
6417 return len;
6418}
6419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420/*
6421 * Find the end of a variable or function name, taking care of magic braces.
6422 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6423 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006424 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 * Return a pointer to just after the name. Equal to "arg" if there is no
6426 * valid name.
6427 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006428 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429find_name_end(
6430 char_u *arg,
6431 char_u **expr_start,
6432 char_u **expr_end,
6433 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 int mb_nest = 0;
6436 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006438 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006440 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006442 *expr_start = NULL;
6443 *expr_end = NULL;
6444 }
6445
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006446 /* Quick check for valid starting character. */
6447 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6448 return arg;
6449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006450 for (p = arg; *p != NUL
6451 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006452 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006453 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006454 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006455 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006457 if (*p == '\'')
6458 {
6459 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006460 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006461 ;
6462 if (*p == NUL)
6463 break;
6464 }
6465 else if (*p == '"')
6466 {
6467 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006468 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006469 if (*p == '\\' && p[1] != NUL)
6470 ++p;
6471 if (*p == NUL)
6472 break;
6473 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006474 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6475 {
6476 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006477 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006478 len = (int)(p - arg);
6479 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006480 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006481 break;
6482 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006484 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486 if (*p == '[')
6487 ++br_nest;
6488 else if (*p == ']')
6489 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006494 if (*p == '{')
6495 {
6496 mb_nest++;
6497 if (expr_start != NULL && *expr_start == NULL)
6498 *expr_start = p;
6499 }
6500 else if (*p == '}')
6501 {
6502 mb_nest--;
6503 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6504 *expr_end = p;
6505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508
6509 return p;
6510}
6511
6512/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006513 * Expands out the 'magic' {}'s in a variable/function name.
6514 * Note that this can call itself recursively, to deal with
6515 * constructs like foo{bar}{baz}{bam}
6516 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6517 * "in_start" ^
6518 * "expr_start" ^
6519 * "expr_end" ^
6520 * "in_end" ^
6521 *
6522 * Returns a new allocated string, which the caller must free.
6523 * Returns NULL for failure.
6524 */
6525 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006526make_expanded_name(
6527 char_u *in_start,
6528 char_u *expr_start,
6529 char_u *expr_end,
6530 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006531{
6532 char_u c1;
6533 char_u *retval = NULL;
6534 char_u *temp_result;
6535 char_u *nextcmd = NULL;
6536
6537 if (expr_end == NULL || in_end == NULL)
6538 return NULL;
6539 *expr_start = NUL;
6540 *expr_end = NUL;
6541 c1 = *in_end;
6542 *in_end = NUL;
6543
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006544 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006545 if (temp_result != NULL && nextcmd == NULL)
6546 {
6547 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6548 + (in_end - expr_end) + 1));
6549 if (retval != NULL)
6550 {
6551 STRCPY(retval, in_start);
6552 STRCAT(retval, temp_result);
6553 STRCAT(retval, expr_end + 1);
6554 }
6555 }
6556 vim_free(temp_result);
6557
6558 *in_end = c1; /* put char back for error messages */
6559 *expr_start = '{';
6560 *expr_end = '}';
6561
6562 if (retval != NULL)
6563 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006564 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006565 if (expr_start != NULL)
6566 {
6567 /* Further expansion! */
6568 temp_result = make_expanded_name(retval, expr_start,
6569 expr_end, temp_result);
6570 vim_free(retval);
6571 retval = temp_result;
6572 }
6573 }
6574
6575 return retval;
6576}
6577
6578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006585 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6586}
6587
6588/*
6589 * Return TRUE if character "c" can be used as the first character in a
6590 * variable or function name (excluding '{' and '}').
6591 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006593eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006594{
6595 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596}
6597
6598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 * Set number v: variable to "val".
6600 */
6601 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006602set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605}
6606
6607/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006608 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006610 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006611get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006613 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
6615
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006616/*
6617 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006618 * If the String variable has never been set, return an empty string.
6619 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006620 */
6621 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006623{
6624 return get_tv_string(&vimvars[idx].vv_tv);
6625}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006628 * Get List v: variable value. Caller must take care of reference count when
6629 * needed.
6630 */
6631 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006633{
6634 return vimvars[idx].vv_list;
6635}
6636
6637/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006638 * Get Dict v: variable value. Caller must take care of reference count when
6639 * needed.
6640 */
6641 dict_T *
6642get_vim_var_dict(int idx)
6643{
6644 return vimvars[idx].vv_dict;
6645}
6646
6647/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006648 * Set v:char to character "c".
6649 */
6650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006651set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006652{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006653 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006654
6655#ifdef FEAT_MBYTE
6656 if (has_mbyte)
6657 buf[(*mb_char2bytes)(c, buf)] = NUL;
6658 else
6659#endif
6660 {
6661 buf[0] = c;
6662 buf[1] = NUL;
6663 }
6664 set_vim_var_string(VV_CHAR, buf, -1);
6665}
6666
6667/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006668 * Set v:count to "count" and v:count1 to "count1".
6669 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 */
6671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006672set_vcount(
6673 long count,
6674 long count1,
6675 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006677 if (set_prevcount)
6678 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 vimvars[VV_COUNT].vv_nr = count;
6680 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681}
6682
6683/*
6684 * Set string v: variable to a copy of "val".
6685 */
6686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006687set_vim_var_string(
6688 int idx,
6689 char_u *val,
6690 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
Bram Moolenaara542c682016-01-31 16:28:04 +01006692 clear_tv(&vimvars[idx].vv_di.di_tv);
6693 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006695 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006697 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700}
6701
6702/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006703 * Set List v: variable to "val".
6704 */
6705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006707{
Bram Moolenaara542c682016-01-31 16:28:04 +01006708 clear_tv(&vimvars[idx].vv_di.di_tv);
6709 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006710 vimvars[idx].vv_list = val;
6711 if (val != NULL)
6712 ++val->lv_refcount;
6713}
6714
6715/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006716 * Set Dictionary v: variable to "val".
6717 */
6718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006719set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006720{
Bram Moolenaara542c682016-01-31 16:28:04 +01006721 clear_tv(&vimvars[idx].vv_di.di_tv);
6722 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006723 vimvars[idx].vv_dict = val;
6724 if (val != NULL)
6725 {
6726 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006727 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006728 }
6729}
6730
6731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Set v:register if needed.
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 char_u regname;
6738
6739 if (c == 0 || c == ' ')
6740 regname = '"';
6741 else
6742 regname = c;
6743 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006744 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 set_vim_var_string(VV_REG, &regname, 1);
6746}
6747
6748/*
6749 * Get or set v:exception. If "oldval" == NULL, return the current value.
6750 * Otherwise, restore the value to "oldval" and return NULL.
6751 * Must always be called in pairs to save and restore v:exception! Does not
6752 * take care of memory allocations.
6753 */
6754 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 return NULL;
6762}
6763
6764/*
6765 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6766 * Otherwise, restore the value to "oldval" and return NULL.
6767 * Must always be called in pairs to save and restore v:throwpoint! Does not
6768 * take care of memory allocations.
6769 */
6770 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 return NULL;
6778}
6779
6780#if defined(FEAT_AUTOCMD) || defined(PROTO)
6781/*
6782 * Set v:cmdarg.
6783 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6784 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6785 * Must always be called in pairs!
6786 */
6787 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006788set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 char_u *oldval;
6791 char_u *newval;
6792 unsigned len;
6793
Bram Moolenaare9a41262005-01-15 22:18:47 +00006794 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006795 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006797 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006799 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 }
6801
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006802 if (eap->force_bin == FORCE_BIN)
6803 len = 6;
6804 else if (eap->force_bin == FORCE_NOBIN)
6805 len = 8;
6806 else
6807 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006808
6809 if (eap->read_edit)
6810 len += 7;
6811
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006812 if (eap->force_ff != 0)
6813 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6814# ifdef FEAT_MBYTE
6815 if (eap->force_enc != 0)
6816 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006817 if (eap->bad_char != 0)
6818 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006819# endif
6820
6821 newval = alloc(len + 1);
6822 if (newval == NULL)
6823 return NULL;
6824
6825 if (eap->force_bin == FORCE_BIN)
6826 sprintf((char *)newval, " ++bin");
6827 else if (eap->force_bin == FORCE_NOBIN)
6828 sprintf((char *)newval, " ++nobin");
6829 else
6830 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831
6832 if (eap->read_edit)
6833 STRCAT(newval, " ++edit");
6834
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006835 if (eap->force_ff != 0)
6836 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6837 eap->cmd + eap->force_ff);
6838# ifdef FEAT_MBYTE
6839 if (eap->force_enc != 0)
6840 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6841 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006842 if (eap->bad_char == BAD_KEEP)
6843 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6844 else if (eap->bad_char == BAD_DROP)
6845 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6846 else if (eap->bad_char != 0)
6847 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006848# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006850 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851}
6852#endif
6853
6854/*
6855 * Get the value of internal variable "name".
6856 * Return OK or FAIL.
6857 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006858 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006859get_var_tv(
6860 char_u *name,
6861 int len, /* length of "name" */
6862 typval_T *rettv, /* NULL when only checking existence */
6863 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6864 int verbose, /* may give error message */
6865 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006869 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
6872 /* truncate the name, so that we can use strcmp() */
6873 cc = name[len];
6874 name[len] = NUL;
6875
6876 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 * Check for user-defined variables.
6878 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006879 v = find_var(name, NULL, no_autoload);
6880 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006882 tv = &v->di_tv;
6883 if (dip != NULL)
6884 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006889 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006890 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 ret = FAIL;
6892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006894 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 name[len] = cc;
6897
6898 return ret;
6899}
6900
6901/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006902 * Check if variable "name[len]" is a local variable or an argument.
6903 * If so, "*eval_lavars_used" is set to TRUE.
6904 */
6905 static void
6906check_vars(char_u *name, int len)
6907{
6908 int cc;
6909 char_u *varname;
6910 hashtab_T *ht;
6911
6912 if (eval_lavars_used == NULL)
6913 return;
6914
6915 /* truncate the name, so that we can use strcmp() */
6916 cc = name[len];
6917 name[len] = NUL;
6918
6919 ht = find_var_ht(name, &varname);
6920 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6921 {
6922 if (find_var(name, NULL, TRUE) != NULL)
6923 *eval_lavars_used = TRUE;
6924 }
6925
6926 name[len] = cc;
6927}
6928
6929/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006930 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6931 * Also handle function call with Funcref variable: func(expr)
6932 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6933 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006935handle_subscript(
6936 char_u **arg,
6937 typval_T *rettv,
6938 int evaluate, /* do more than finding the end */
6939 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006940{
6941 int ret = OK;
6942 dict_T *selfdict = NULL;
6943 char_u *s;
6944 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006946
6947 while (ret == OK
6948 && (**arg == '['
6949 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006950 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6951 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006952 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006953 {
6954 if (**arg == '(')
6955 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006956 partial_T *pt = NULL;
6957
Bram Moolenaard9fba312005-06-26 22:34:35 +00006958 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006959 if (evaluate)
6960 {
6961 functv = *rettv;
6962 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006963
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006964 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006965 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006966 {
6967 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006968 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006969 }
6970 else
6971 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006972 }
6973 else
6974 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006975 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006976 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006977 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006978
6979 /* Clear the funcref afterwards, so that deleting it while
6980 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006981 if (evaluate)
6982 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006983
6984 /* Stop the expression evaluation when immediately aborting on
6985 * error, or when an interrupt occurred or an exception was thrown
6986 * but not caught. */
6987 if (aborting())
6988 {
6989 if (ret == OK)
6990 clear_tv(rettv);
6991 ret = FAIL;
6992 }
6993 dict_unref(selfdict);
6994 selfdict = NULL;
6995 }
6996 else /* **arg == '[' || **arg == '.' */
6997 {
6998 dict_unref(selfdict);
6999 if (rettv->v_type == VAR_DICT)
7000 {
7001 selfdict = rettv->vval.v_dict;
7002 if (selfdict != NULL)
7003 ++selfdict->dv_refcount;
7004 }
7005 else
7006 selfdict = NULL;
7007 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7008 {
7009 clear_tv(rettv);
7010 ret = FAIL;
7011 }
7012 }
7013 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007014
Bram Moolenaar1d429612016-05-24 15:44:17 +02007015 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7016 * Don't do this when "Func" is already a partial that was bound
7017 * explicitly (pt_auto is FALSE). */
7018 if (selfdict != NULL
7019 && (rettv->v_type == VAR_FUNC
7020 || (rettv->v_type == VAR_PARTIAL
7021 && (rettv->vval.v_partial->pt_auto
7022 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007023 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007024
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007025 dict_unref(selfdict);
7026 return ret;
7027}
7028
7029/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007030 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007031 * value).
7032 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007033 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035{
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037}
7038
7039/*
7040 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 * The string "s" must have been allocated, it is consumed.
7042 * Return NULL for out of memory, the variable otherwise.
7043 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007044 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007049 rettv = alloc_tv();
7050 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007052 rettv->v_type = VAR_STRING;
7053 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055 else
7056 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007057 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058}
7059
7060/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007061 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007064free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 if (varp != NULL)
7067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007068 switch (varp->v_type)
7069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007070 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007071 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007072 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007073 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007074 vim_free(varp->vval.v_string);
7075 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007076 case VAR_PARTIAL:
7077 partial_unref(varp->vval.v_partial);
7078 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007079 case VAR_LIST:
7080 list_unref(varp->vval.v_list);
7081 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007082 case VAR_DICT:
7083 dict_unref(varp->vval.v_dict);
7084 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007085 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007086#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007087 job_unref(varp->vval.v_job);
7088 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007089#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007090 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007091#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007092 channel_unref(varp->vval.v_channel);
7093 break;
7094#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007095 case VAR_NUMBER:
7096 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007097 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007098 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007099 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 vim_free(varp);
7102 }
7103}
7104
7105/*
7106 * Free the memory for a variable value and set the value to NULL or 0.
7107 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
7111 if (varp != NULL)
7112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007116 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007117 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007118 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 vim_free(varp->vval.v_string);
7120 varp->vval.v_string = NULL;
7121 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007122 case VAR_PARTIAL:
7123 partial_unref(varp->vval.v_partial);
7124 varp->vval.v_partial = NULL;
7125 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 case VAR_LIST:
7127 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007129 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 case VAR_DICT:
7131 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007134 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007135 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007136 varp->vval.v_number = 0;
7137 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007138 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007139#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007140 varp->vval.v_float = 0.0;
7141 break;
7142#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007143 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007145 job_unref(varp->vval.v_job);
7146 varp->vval.v_job = NULL;
7147#endif
7148 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007149 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007150#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007151 channel_unref(varp->vval.v_channel);
7152 varp->vval.v_channel = NULL;
7153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 case VAR_UNKNOWN:
7155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007157 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159}
7160
7161/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007162 * Set the value of a variable to NULL without freeing items.
7163 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166{
7167 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007169}
7170
7171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 * Get the number value of a variable.
7173 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 * For incompatible types, return 0.
7175 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7176 * caller of incompatible types: it sets *denote to TRUE if "denote"
7177 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007179 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007182 int error = FALSE;
7183
7184 return get_tv_number_chk(varp, &error); /* return 0L on error */
7185}
7186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007187 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007188get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007189{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007190 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007194 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007195 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007196 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007197#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007198 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007199 break;
7200#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007201 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007202 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007203 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007204 break;
7205 case VAR_STRING:
7206 if (varp->vval.v_string != NULL)
7207 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007208 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007209 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007210 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007211 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007212 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007213 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007214 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007215 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007216 case VAR_SPECIAL:
7217 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7218 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007219 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007220#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007221 EMSG(_("E910: Using a Job as a Number"));
7222 break;
7223#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007224 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007225#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007226 EMSG(_("E913: Using a Channel as a Number"));
7227 break;
7228#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007229 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007230 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007233 if (denote == NULL) /* useful for values that must be unsigned */
7234 n = -1;
7235 else
7236 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007237 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238}
7239
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007240#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007242get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007243{
7244 switch (varp->v_type)
7245 {
7246 case VAR_NUMBER:
7247 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007248 case VAR_FLOAT:
7249 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007250 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007251 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007252 EMSG(_("E891: Using a Funcref as a Float"));
7253 break;
7254 case VAR_STRING:
7255 EMSG(_("E892: Using a String as a Float"));
7256 break;
7257 case VAR_LIST:
7258 EMSG(_("E893: Using a List as a Float"));
7259 break;
7260 case VAR_DICT:
7261 EMSG(_("E894: Using a Dictionary as a Float"));
7262 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007263 case VAR_SPECIAL:
7264 EMSG(_("E907: Using a special value as a Float"));
7265 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007266 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007267# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007268 EMSG(_("E911: Using a Job as a Float"));
7269 break;
7270# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007272# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007273 EMSG(_("E914: Using a Channel as a Float"));
7274 break;
7275# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007276 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007277 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007278 break;
7279 }
7280 return 0;
7281}
7282#endif
7283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 * Get the string value of a variable.
7286 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007287 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7288 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 * If the String variable has never been set, return an empty string.
7290 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007291 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7292 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007295get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297 static char_u mybuf[NUMBUFLEN];
7298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007299 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300}
7301
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007302 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007303get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007305 char_u *res = get_tv_string_buf_chk(varp, buf);
7306
7307 return res != NULL ? res : (char_u *)"";
7308}
7309
Bram Moolenaar7d647822014-04-05 21:28:56 +02007310/*
7311 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7312 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007313 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007314get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315{
7316 static char_u mybuf[NUMBUFLEN];
7317
7318 return get_tv_string_buf_chk(varp, mybuf);
7319}
7320
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007321 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007322get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007323{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007324 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007326 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007327 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7328 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329 return buf;
7330 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007331 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007332 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007333 break;
7334 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007335 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 break;
7337 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007338 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007339 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007340 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007341#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007342 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007343 break;
7344#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 case VAR_STRING:
7346 if (varp->vval.v_string != NULL)
7347 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007348 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007349 case VAR_SPECIAL:
7350 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7351 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007352 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007354 {
7355 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007356 char *status;
7357
7358 if (job == NULL)
7359 return (char_u *)"no process";
7360 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007361 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007362 : "run";
7363# ifdef UNIX
7364 vim_snprintf((char *)buf, NUMBUFLEN,
7365 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007366# elif defined(WIN32)
7367 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007368 "process %ld %s",
7369 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007370 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007371# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007372 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007373 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7374# endif
7375 return buf;
7376 }
7377#endif
7378 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007379 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007380#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007381 {
7382 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007383 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007384
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007385 if (channel == NULL)
7386 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7387 else
7388 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007389 "channel %d %s", channel->ch_id, status);
7390 return buf;
7391 }
7392#endif
7393 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007394 case VAR_UNKNOWN:
7395 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007398 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401/*
7402 * Find variable "name" in the list of variables.
7403 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007404 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007405 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007408 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007413 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaara7043832005-01-21 11:56:39 +00007415 ht = find_var_ht(name, &varname);
7416 if (htp != NULL)
7417 *htp = ht;
7418 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007420 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7421 if (ret != NULL)
7422 return ret;
7423
7424 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007425 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426}
7427
7428/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007429 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007430 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007432 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433find_var_in_ht(
7434 hashtab_T *ht,
7435 int htname,
7436 char_u *varname,
7437 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007438{
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 hashitem_T *hi;
7440
7441 if (*varname == NUL)
7442 {
7443 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007444 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007446 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 case 'g': return &globvars_var;
7448 case 'v': return &vimvars_var;
7449 case 'b': return &curbuf->b_bufvar;
7450 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007451 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007452 case 'l': return get_funccal_local_var();
7453 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 }
7455 return NULL;
7456 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007457
7458 hi = hash_find(ht, varname);
7459 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007460 {
7461 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007462 * worked find the variable again. Don't auto-load a script if it was
7463 * loaded already, otherwise it would be loaded every time when
7464 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007465 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007466 {
7467 /* Note: script_autoload() may make "hi" invalid. It must either
7468 * be obtained again or not used. */
7469 if (!script_autoload(varname, FALSE) || aborting())
7470 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007471 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007472 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007473 if (HASHITEM_EMPTY(hi))
7474 return NULL;
7475 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007477}
7478
7479/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007481 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007482 * Set "varname" to the start of name without ':'.
7483 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007484 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007485find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007487 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007488 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007489
Bram Moolenaar73627d02015-08-11 15:46:09 +02007490 if (name[0] == NUL)
7491 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (name[1] != ':')
7493 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007494 /* The name must not start with a colon or #. */
7495 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 return NULL;
7497 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007498
7499 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007500 hi = hash_find(&compat_hashtab, name);
7501 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007502 return &compat_hashtab;
7503
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007504 ht = get_funccal_local_ht();
7505 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007507 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007510 if (*name == 'g') /* global variable */
7511 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007512 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7513 */
7514 if (vim_strchr(name + 2, ':') != NULL
7515 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007516 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007518 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007520 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007521 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007522 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 if (*name == 'v') /* v: variable */
7524 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007525 if (*name == 'a') /* a: function argument */
7526 return get_funccal_args_ht();
7527 if (*name == 'l') /* l: local function variable */
7528 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (*name == 's' /* script variable */
7530 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7531 return &SCRIPT_VARS(current_SID);
7532 return NULL;
7533}
7534
7535/*
7536 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007537 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 * Returns NULL when it doesn't exist.
7539 */
7540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007545 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (v == NULL)
7547 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549}
7550
7551/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 * sourcing this script and when executing functions defined in the script.
7554 */
7555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaara7043832005-01-21 11:56:39 +00007558 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 hashtab_T *ht;
7560 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7563 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007564 /* Re-allocating ga_data means that an ht_array pointing to
7565 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007567 for (i = 1; i <= ga_scripts.ga_len; ++i)
7568 {
7569 ht = &SCRIPT_VARS(i);
7570 if (ht->ht_mask == HT_INIT_SIZE - 1)
7571 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007572 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007574 }
7575
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 while (ga_scripts.ga_len < id)
7577 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007578 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007579 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007580 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 }
7583 }
7584}
7585
7586/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7588 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 */
7590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007591init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007594 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007595 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007596 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007597 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 dict_var->di_tv.vval.v_dict = dict;
7599 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007600 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7602 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
7605/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007606 * Unreference a dictionary initialized by init_var_dict().
7607 */
7608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007610{
7611 /* Now the dict needs to be freed if no one else is using it, go back to
7612 * normal reference counting. */
7613 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7614 dict_unref(dict);
7615}
7616
7617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 * Frees all allocated variables and the value they contain.
7620 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 */
7622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007623vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007624{
7625 vars_clear_ext(ht, TRUE);
7626}
7627
7628/*
7629 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7630 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaara7043832005-01-21 11:56:39 +00007634 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007635 hashitem_T *hi;
7636 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007639 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007640 for (hi = ht->ht_array; todo > 0; ++hi)
7641 {
7642 if (!HASHITEM_EMPTY(hi))
7643 {
7644 --todo;
7645
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007647 * ht_array might change then. hash_clear() takes care of it
7648 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 v = HI2DI(hi);
7650 if (free_val)
7651 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007652 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007654 }
7655 }
7656 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007657 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
Bram Moolenaara7043832005-01-21 11:56:39 +00007660/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 * Delete a variable from hashtab "ht" at item "hi".
7662 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007668
7669 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 clear_tv(&di->di_tv);
7671 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672}
7673
7674/*
7675 * List the value of one internal variable.
7676 */
7677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 char_u *tofree;
7681 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007682 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007684 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007686 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007691list_one_var_a(
7692 char_u *prefix,
7693 char_u *name,
7694 int type,
7695 char_u *string,
7696 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
Bram Moolenaar31859182007-08-14 20:41:13 +00007698 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7699 msg_start();
7700 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (name != NULL) /* "a:" vars don't have a name stored */
7702 msg_puts(name);
7703 msg_putchar(' ');
7704 msg_advance(22);
7705 if (type == VAR_NUMBER)
7706 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007707 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007708 msg_putchar('*');
7709 else if (type == VAR_LIST)
7710 {
7711 msg_putchar('[');
7712 if (*string == '[')
7713 ++string;
7714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 else if (type == VAR_DICT)
7716 {
7717 msg_putchar('{');
7718 if (*string == '{')
7719 ++string;
7720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 else
7722 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007726 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007727 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007728 if (*first)
7729 {
7730 msg_clr_eos();
7731 *first = FALSE;
7732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733}
7734
7735/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 * If the variable already exists, the value is updated.
7738 * Otherwise the variable is created.
7739 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007741set_var(
7742 char_u *name,
7743 typval_T *tv,
7744 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007750 ht = find_var_ht(name, &varname);
7751 if (ht == NULL || *varname == NUL)
7752 {
7753 EMSG2(_(e_illvar), name);
7754 return;
7755 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007756 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007757
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007758 /* Search in parent scope which is possible to reference from lambda */
7759 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007760 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007761
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007762 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7763 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007764 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007765
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007769 if (var_check_ro(v->di_flags, name, FALSE)
7770 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007772
7773 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007774 * Handle setting internal v: variables separately where needed to
7775 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 */
7777 if (ht == &vimvarht)
7778 {
7779 if (v->di_tv.v_type == VAR_STRING)
7780 {
7781 vim_free(v->di_tv.vval.v_string);
7782 if (copy || tv->v_type != VAR_STRING)
7783 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7784 else
7785 {
7786 /* Take over the string to avoid an extra alloc/free. */
7787 v->di_tv.vval.v_string = tv->vval.v_string;
7788 tv->vval.v_string = NULL;
7789 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007790 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007792 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007793 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795 if (STRCMP(varname, "searchforward") == 0)
7796 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007797#ifdef FEAT_SEARCH_EXTRA
7798 else if (STRCMP(varname, "hlsearch") == 0)
7799 {
7800 no_hlsearch = !v->di_tv.vval.v_number;
7801 redraw_all_later(SOME_VALID);
7802 }
7803#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007804 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007805 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007806 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007807 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 }
7809
7810 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
7812 else /* add a new variable */
7813 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007814 /* Can't add "v:" variable. */
7815 if (ht == &vimvarht)
7816 {
7817 EMSG2(_(e_illvar), name);
7818 return;
7819 }
7820
Bram Moolenaar92124a32005-06-17 22:03:40 +00007821 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007822 if (!valid_varname(varname))
7823 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007825 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7826 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007827 if (v == NULL)
7828 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007830 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007832 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 return;
7834 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007835 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007837
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007840 else
7841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007843 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007849 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 * Also give an error message.
7851 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007854{
7855 if (flags & DI_FLAGS_RO)
7856 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007857 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 return TRUE;
7859 }
7860 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7861 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007862 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 return TRUE;
7864 }
7865 return FALSE;
7866}
7867
7868/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007869 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7870 * Also give an error message.
7871 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007874{
7875 if (flags & DI_FLAGS_FIX)
7876 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007877 EMSG2(_("E795: Cannot delete variable %s"),
7878 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007879 return TRUE;
7880 }
7881 return FALSE;
7882}
7883
7884/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007885 * Check if a funcref is assigned to a valid variable name.
7886 * Return TRUE and give an error if not.
7887 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889var_check_func_name(
7890 char_u *name, /* points to start of variable name */
7891 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007892{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007893 /* Allow for w: b: s: and t:. */
7894 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007895 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7896 ? name[2] : name[0]))
7897 {
7898 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7899 name);
7900 return TRUE;
7901 }
7902 /* Don't allow hiding a function. When "v" is not NULL we might be
7903 * assigning another function to the same var, the type is checked
7904 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007905 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007906 {
7907 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7908 name);
7909 return TRUE;
7910 }
7911 return FALSE;
7912}
7913
7914/*
7915 * Check if a variable name is valid.
7916 * Return FALSE and give an error if not.
7917 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007919valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007920{
7921 char_u *p;
7922
7923 for (p = varname; *p != NUL; ++p)
7924 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7925 && *p != AUTOLOAD_CHAR)
7926 {
7927 EMSG2(_(e_illvar), varname);
7928 return FALSE;
7929 }
7930 return TRUE;
7931}
7932
7933/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007934 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007935 * Also give an error message, using "name" or _("name") when use_gettext is
7936 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007937 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007939tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007940{
7941 if (lock & VAR_LOCKED)
7942 {
7943 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007944 name == NULL ? (char_u *)_("Unknown")
7945 : use_gettext ? (char_u *)_(name)
7946 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007947 return TRUE;
7948 }
7949 if (lock & VAR_FIXED)
7950 {
7951 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007952 name == NULL ? (char_u *)_("Unknown")
7953 : use_gettext ? (char_u *)_(name)
7954 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007955 return TRUE;
7956 }
7957 return FALSE;
7958}
7959
7960/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007964 * It is OK for "from" and "to" to point to the same item. This is used to
7965 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007966 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007968copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007971 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 switch (from->v_type)
7973 {
7974 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007975 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 to->vval.v_number = from->vval.v_number;
7977 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007978 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007979#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007980 to->vval.v_float = from->vval.v_float;
7981 break;
7982#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007983 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007985 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007986 if (to->vval.v_job != NULL)
7987 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007988 break;
7989#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007990 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007991#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007992 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007993 if (to->vval.v_channel != NULL)
7994 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007995 break;
7996#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 case VAR_STRING:
7998 case VAR_FUNC:
7999 if (from->vval.v_string == NULL)
8000 to->vval.v_string = NULL;
8001 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008004 if (from->v_type == VAR_FUNC)
8005 func_ref(to->vval.v_string);
8006 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008008 case VAR_PARTIAL:
8009 if (from->vval.v_partial == NULL)
8010 to->vval.v_partial = NULL;
8011 else
8012 {
8013 to->vval.v_partial = from->vval.v_partial;
8014 ++to->vval.v_partial->pt_refcount;
8015 }
8016 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 case VAR_LIST:
8018 if (from->vval.v_list == NULL)
8019 to->vval.v_list = NULL;
8020 else
8021 {
8022 to->vval.v_list = from->vval.v_list;
8023 ++to->vval.v_list->lv_refcount;
8024 }
8025 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008026 case VAR_DICT:
8027 if (from->vval.v_dict == NULL)
8028 to->vval.v_dict = NULL;
8029 else
8030 {
8031 to->vval.v_dict = from->vval.v_dict;
8032 ++to->vval.v_dict->dv_refcount;
8033 }
8034 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008035 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008036 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008037 break;
8038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039}
8040
8041/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042 * Make a copy of an item.
8043 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008044 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8045 * reference to an already copied list/dict can be used.
8046 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008049item_copy(
8050 typval_T *from,
8051 typval_T *to,
8052 int deep,
8053 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054{
8055 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008057
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008059 {
8060 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008061 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 }
8063 ++recurse;
8064
8065 switch (from->v_type)
8066 {
8067 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 case VAR_STRING:
8070 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008071 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008072 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008073 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008074 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008075 copy_tv(from, to);
8076 break;
8077 case VAR_LIST:
8078 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008079 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008080 if (from->vval.v_list == NULL)
8081 to->vval.v_list = NULL;
8082 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8083 {
8084 /* use the copy made earlier */
8085 to->vval.v_list = from->vval.v_list->lv_copylist;
8086 ++to->vval.v_list->lv_refcount;
8087 }
8088 else
8089 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8090 if (to->vval.v_list == NULL)
8091 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008092 break;
8093 case VAR_DICT:
8094 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008095 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008096 if (from->vval.v_dict == NULL)
8097 to->vval.v_dict = NULL;
8098 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8099 {
8100 /* use the copy made earlier */
8101 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8102 ++to->vval.v_dict->dv_refcount;
8103 }
8104 else
8105 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8106 if (to->vval.v_dict == NULL)
8107 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008109 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008110 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008111 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008112 }
8113 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008114 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115}
8116
8117/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 * This function is used by f_input() and f_inputdialog() functions. The third
8119 * argument to f_input() specifies the type of completion to use at the
8120 * prompt. The third argument to f_inputdialog() specifies the value to return
8121 * when the user cancels the prompt.
8122 */
8123 void
8124get_user_input(
8125 typval_T *argvars,
8126 typval_T *rettv,
8127 int inputdialog,
8128 int secret)
8129{
8130 char_u *prompt = get_tv_string_chk(&argvars[0]);
8131 char_u *p = NULL;
8132 int c;
8133 char_u buf[NUMBUFLEN];
8134 int cmd_silent_save = cmd_silent;
8135 char_u *defstr = (char_u *)"";
8136 int xp_type = EXPAND_NOTHING;
8137 char_u *xp_arg = NULL;
8138
8139 rettv->v_type = VAR_STRING;
8140 rettv->vval.v_string = NULL;
8141
8142#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008143 /* While starting up, there is no place to enter text. When running tests
8144 * with --not-a-term we assume feedkeys() will be used. */
8145 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 return;
8147#endif
8148
8149 cmd_silent = FALSE; /* Want to see the prompt. */
8150 if (prompt != NULL)
8151 {
8152 /* Only the part of the message after the last NL is considered as
8153 * prompt for the command line */
8154 p = vim_strrchr(prompt, '\n');
8155 if (p == NULL)
8156 p = prompt;
8157 else
8158 {
8159 ++p;
8160 c = *p;
8161 *p = NUL;
8162 msg_start();
8163 msg_clr_eos();
8164 msg_puts_attr(prompt, echo_attr);
8165 msg_didout = FALSE;
8166 msg_starthere();
8167 *p = c;
8168 }
8169 cmdline_row = msg_row;
8170
8171 if (argvars[1].v_type != VAR_UNKNOWN)
8172 {
8173 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8174 if (defstr != NULL)
8175 stuffReadbuffSpec(defstr);
8176
8177 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8178 {
8179 char_u *xp_name;
8180 int xp_namelen;
8181 long argt;
8182
8183 /* input() with a third argument: completion */
8184 rettv->vval.v_string = NULL;
8185
8186 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8187 if (xp_name == NULL)
8188 return;
8189
8190 xp_namelen = (int)STRLEN(xp_name);
8191
8192 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8193 &xp_arg) == FAIL)
8194 return;
8195 }
8196 }
8197
8198 if (defstr != NULL)
8199 {
8200 int save_ex_normal_busy = ex_normal_busy;
8201 ex_normal_busy = 0;
8202 rettv->vval.v_string =
8203 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8204 xp_type, xp_arg);
8205 ex_normal_busy = save_ex_normal_busy;
8206 }
8207 if (inputdialog && rettv->vval.v_string == NULL
8208 && argvars[1].v_type != VAR_UNKNOWN
8209 && argvars[2].v_type != VAR_UNKNOWN)
8210 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8211 &argvars[2], buf));
8212
8213 vim_free(xp_arg);
8214
8215 /* since the user typed this, no need to wait for return */
8216 need_wait_return = FALSE;
8217 msg_didout = FALSE;
8218 }
8219 cmd_silent = cmd_silent_save;
8220}
8221
8222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 * ":echo expr1 ..." print each argument separated with a space, add a
8224 * newline at the end.
8225 * ":echon expr1 ..." print each argument plain.
8226 */
8227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008228ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 char_u *p;
8234 int needclr = TRUE;
8235 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008236 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237
8238 if (eap->skip)
8239 ++emsg_skip;
8240 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8241 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242 /* If eval1() causes an error message the text from the command may
8243 * still need to be cleared. E.g., "echo 22,44". */
8244 need_clr_eos = needclr;
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 /*
8250 * Report the invalid expression unless the expression evaluation
8251 * has been cancelled due to an aborting error, an interrupt, or an
8252 * exception.
8253 */
8254 if (!aborting())
8255 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 break;
8258 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259 need_clr_eos = FALSE;
8260
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 if (!eap->skip)
8262 {
8263 if (atstart)
8264 {
8265 atstart = FALSE;
8266 /* Call msg_start() after eval1(), evaluating the expression
8267 * may cause a message to appear. */
8268 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008269 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008270 /* Mark the saved text as finishing the line, so that what
8271 * follows is displayed on a new line when scrolling back
8272 * at the more prompt. */
8273 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 }
8277 else if (eap->cmdidx == CMD_echo)
8278 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008279 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008280 if (p != NULL)
8281 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008283 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008285 if (*p != TAB && needclr)
8286 {
8287 /* remove any text still there from the command */
8288 msg_clr_eos();
8289 needclr = FALSE;
8290 }
8291 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008294 {
8295#ifdef FEAT_MBYTE
8296 if (has_mbyte)
8297 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008298 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008299
8300 (void)msg_outtrans_len_attr(p, i, echo_attr);
8301 p += i - 1;
8302 }
8303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008305 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 arg = skipwhite(arg);
8312 }
8313 eap->nextcmd = check_nextcmd(arg);
8314
8315 if (eap->skip)
8316 --emsg_skip;
8317 else
8318 {
8319 /* remove text that may still be there from the command */
8320 if (needclr)
8321 msg_clr_eos();
8322 if (eap->cmdidx == CMD_echo)
8323 msg_end();
8324 }
8325}
8326
8327/*
8328 * ":echohl {name}".
8329 */
8330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008331ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008333 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334}
8335
8336/*
8337 * ":execute expr1 ..." execute the result of an expression.
8338 * ":echomsg expr1 ..." Print a message
8339 * ":echoerr expr1 ..." Print an error
8340 * Each gets spaces around each argument and a newline at the end for
8341 * echo commands
8342 */
8343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008344ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 int ret = OK;
8349 char_u *p;
8350 garray_T ga;
8351 int len;
8352 int save_did_emsg;
8353
8354 ga_init2(&ga, 1, 80);
8355
8356 if (eap->skip)
8357 ++emsg_skip;
8358 while (*arg != NUL && *arg != '|' && *arg != '\n')
8359 {
8360 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
8363 /*
8364 * Report the invalid expression unless the expression evaluation
8365 * has been cancelled due to an aborting error, an interrupt, or an
8366 * exception.
8367 */
8368 if (!aborting())
8369 EMSG2(_(e_invexpr2), p);
8370 ret = FAIL;
8371 break;
8372 }
8373
8374 if (!eap->skip)
8375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 len = (int)STRLEN(p);
8378 if (ga_grow(&ga, len + 2) == FAIL)
8379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 ret = FAIL;
8382 break;
8383 }
8384 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 ga.ga_len += len;
8388 }
8389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 arg = skipwhite(arg);
8392 }
8393
8394 if (ret != FAIL && ga.ga_data != NULL)
8395 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008396 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8397 {
8398 /* Mark the already saved text as finishing the line, so that what
8399 * follows is displayed on a new line when scrolling back at the
8400 * more prompt. */
8401 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008402 }
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008407 out_flush();
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 else if (eap->cmdidx == CMD_echoerr)
8410 {
8411 /* We don't want to abort following commands, restore did_emsg. */
8412 save_did_emsg = did_emsg;
8413 EMSG((char_u *)ga.ga_data);
8414 if (!force_abort)
8415 did_emsg = save_did_emsg;
8416 }
8417 else if (eap->cmdidx == CMD_execute)
8418 do_cmdline((char_u *)ga.ga_data,
8419 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8420 }
8421
8422 ga_clear(&ga);
8423
8424 if (eap->skip)
8425 --emsg_skip;
8426
8427 eap->nextcmd = check_nextcmd(arg);
8428}
8429
8430/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431 * Find window specified by "vp" in tabpage "tp".
8432 */
8433 win_T *
8434find_win_by_nr(
8435 typval_T *vp,
8436 tabpage_T *tp UNUSED) /* NULL for current tab page */
8437{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439 int nr;
8440
8441 nr = (int)get_tv_number_chk(vp, NULL);
8442
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 if (nr < 0)
8444 return NULL;
8445 if (nr == 0)
8446 return curwin;
8447
Bram Moolenaar29323592016-07-24 22:04:11 +02008448 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8449 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450 if (nr >= LOWEST_WIN_ID)
8451 {
8452 if (wp->w_id == nr)
8453 return wp;
8454 }
8455 else if (--nr <= 0)
8456 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 if (nr >= LOWEST_WIN_ID)
8459 return NULL;
8460 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461}
8462
8463/*
8464 * Find window specified by "wvp" in tabpage "tvp".
8465 */
8466 win_T *
8467find_tabwin(
8468 typval_T *wvp, /* VAR_UNKNOWN for current window */
8469 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8470{
8471 win_T *wp = NULL;
8472 tabpage_T *tp = NULL;
8473 long n;
8474
8475 if (wvp->v_type != VAR_UNKNOWN)
8476 {
8477 if (tvp->v_type != VAR_UNKNOWN)
8478 {
8479 n = (long)get_tv_number(tvp);
8480 if (n >= 0)
8481 tp = find_tabpage(n);
8482 }
8483 else
8484 tp = curtab;
8485
8486 if (tp != NULL)
8487 wp = find_win_by_nr(wvp, tp);
8488 }
8489 else
8490 wp = curwin;
8491
8492 return wp;
8493}
8494
8495/*
8496 * getwinvar() and gettabwinvar()
8497 */
8498 void
8499getwinvar(
8500 typval_T *argvars,
8501 typval_T *rettv,
8502 int off) /* 1 for gettabwinvar() */
8503{
8504 win_T *win;
8505 char_u *varname;
8506 dictitem_T *v;
8507 tabpage_T *tp = NULL;
8508 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 win_T *oldcurwin;
8510 tabpage_T *oldtabpage;
8511 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 if (off == 1)
8514 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8515 else
8516 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 win = find_win_by_nr(&argvars[off], tp);
8518 varname = get_tv_string_chk(&argvars[off + 1]);
8519 ++emsg_off;
8520
8521 rettv->v_type = VAR_STRING;
8522 rettv->vval.v_string = NULL;
8523
8524 if (win != NULL && varname != NULL)
8525 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 /* Set curwin to be our win, temporarily. Also set the tabpage,
8527 * otherwise the window is not valid. Only do this when needed,
8528 * autocommands get blocked. */
8529 need_switch_win = !(tp == curtab && win == curwin);
8530 if (!need_switch_win
8531 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008533 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008535 if (varname[1] == NUL)
8536 {
8537 /* get all window-local options in a dict */
8538 dict_T *opts = get_winbuf_options(FALSE);
8539
8540 if (opts != NULL)
8541 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008542 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008543 done = TRUE;
8544 }
8545 }
8546 else if (get_option_tv(&varname, rettv, 1) == OK)
8547 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 done = TRUE;
8549 }
8550 else
8551 {
8552 /* Look up the variable. */
8553 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8554 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8555 varname, FALSE);
8556 if (v != NULL)
8557 {
8558 copy_tv(&v->di_tv, rettv);
8559 done = TRUE;
8560 }
8561 }
8562 }
8563
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 if (need_switch_win)
8565 /* restore previous notion of curwin */
8566 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 }
8568
8569 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8570 /* use the default return value */
8571 copy_tv(&argvars[off + 2], rettv);
8572
8573 --emsg_off;
8574}
8575
8576/*
8577 * "setwinvar()" and "settabwinvar()" functions
8578 */
8579 void
8580setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8581{
8582 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 win_T *save_curwin;
8584 tabpage_T *save_curtab;
8585 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 char_u *varname, *winvarname;
8587 typval_T *varp;
8588 char_u nbuf[NUMBUFLEN];
8589 tabpage_T *tp = NULL;
8590
8591 if (check_restricted() || check_secure())
8592 return;
8593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 if (off == 1)
8595 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8596 else
8597 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 win = find_win_by_nr(&argvars[off], tp);
8599 varname = get_tv_string_chk(&argvars[off + 1]);
8600 varp = &argvars[off + 2];
8601
8602 if (win != NULL && varname != NULL && varp != NULL)
8603 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 need_switch_win = !(tp == curtab && win == curwin);
8605 if (!need_switch_win
8606 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 {
8608 if (*varname == '&')
8609 {
8610 long numval;
8611 char_u *strval;
8612 int error = FALSE;
8613
8614 ++varname;
8615 numval = (long)get_tv_number_chk(varp, &error);
8616 strval = get_tv_string_buf_chk(varp, nbuf);
8617 if (!error && strval != NULL)
8618 set_option_value(varname, numval, strval, OPT_LOCAL);
8619 }
8620 else
8621 {
8622 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8623 if (winvarname != NULL)
8624 {
8625 STRCPY(winvarname, "w:");
8626 STRCPY(winvarname + 2, varname);
8627 set_var(winvarname, varp, TRUE);
8628 vim_free(winvarname);
8629 }
8630 }
8631 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (need_switch_win)
8633 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 }
8635}
8636
8637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8639 * "arg" points to the "&" or '+' when called, to "option" when returning.
8640 * Returns NULL when no option name found. Otherwise pointer to the char
8641 * after the option name.
8642 */
8643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008644find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 char_u *p = *arg;
8647
8648 ++p;
8649 if (*p == 'g' && p[1] == ':')
8650 {
8651 *opt_flags = OPT_GLOBAL;
8652 p += 2;
8653 }
8654 else if (*p == 'l' && p[1] == ':')
8655 {
8656 *opt_flags = OPT_LOCAL;
8657 p += 2;
8658 }
8659 else
8660 *opt_flags = 0;
8661
8662 if (!ASCII_ISALPHA(*p))
8663 return NULL;
8664 *arg = p;
8665
8666 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8667 p += 4; /* termcap option */
8668 else
8669 while (ASCII_ISALPHA(*p))
8670 ++p;
8671 return p;
8672}
8673
8674/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008675 * Return the autoload script name for a function or variable name.
8676 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008678 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008679autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008680{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008681 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008682 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008683
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008684 /* Get the script file name: replace '#' with '/', append ".vim". */
8685 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8686 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008687 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008688 STRCPY(scriptname, "autoload/");
8689 STRCAT(scriptname, name);
8690 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8691 STRCAT(scriptname, ".vim");
8692 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8693 *p = '/';
8694 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008695}
8696
8697/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008698 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008699 * Return TRUE if a package was loaded.
8700 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008701 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008702script_autoload(
8703 char_u *name,
8704 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008705{
8706 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008707 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008709 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008711 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008712 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008713 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 return FALSE;
8715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008716 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008717
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008718 /* Find the name in the list of previously loaded package names. Skip
8719 * "autoload/", it's always the same. */
8720 for (i = 0; i < ga_loaded.ga_len; ++i)
8721 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8722 break;
8723 if (!reload && i < ga_loaded.ga_len)
8724 ret = FALSE; /* was loaded already */
8725 else
8726 {
8727 /* Remember the name if it wasn't loaded already. */
8728 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8729 {
8730 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8731 tofree = NULL;
8732 }
8733
8734 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008735 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008736 ret = TRUE;
8737 }
8738
8739 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 return ret;
8741}
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8744typedef enum
8745{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008746 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8747 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8748 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749} var_flavour_T;
8750
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008751static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008754var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
8756 char_u *p = varname;
8757
8758 if (ASCII_ISUPPER(*p))
8759 {
8760 while (*(++p))
8761 if (ASCII_ISLOWER(*p))
8762 return VAR_FLAVOUR_SESSION;
8763 return VAR_FLAVOUR_VIMINFO;
8764 }
8765 else
8766 return VAR_FLAVOUR_DEFAULT;
8767}
8768#endif
8769
8770#if defined(FEAT_VIMINFO) || defined(PROTO)
8771/*
8772 * Restore global vars that start with a capital from the viminfo file
8773 */
8774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008775read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008778 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008780 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
8782 if (!writing && (find_viminfo_parameter('!') != NULL))
8783 {
8784 tab = vim_strchr(virp->vir_line + 1, '\t');
8785 if (tab != NULL)
8786 {
8787 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008788 switch (*tab)
8789 {
8790 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008792 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008793#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008794 case 'D': type = VAR_DICT; break;
8795 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008796 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
8799 tab = vim_strchr(tab, '\t');
8800 if (tab != NULL)
8801 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008802 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008803 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008804 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008806#ifdef FEAT_FLOAT
8807 else if (type == VAR_FLOAT)
8808 (void)string2float(tab + 1, &tv.vval.v_float);
8809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008811 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008812 if (type == VAR_DICT || type == VAR_LIST)
8813 {
8814 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8815
8816 if (etv == NULL)
8817 /* Failed to parse back the dict or list, use it as a
8818 * string. */
8819 tv.v_type = VAR_STRING;
8820 else
8821 {
8822 vim_free(tv.vval.v_string);
8823 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008824 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008825 }
8826 }
8827
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008828 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008829 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008830 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008831 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008832
8833 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008834 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008835 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8836 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 }
8838 }
8839 }
8840
8841 return viminfo_readline(virp);
8842}
8843
8844/*
8845 * Write global vars that start with a capital to the viminfo file
8846 */
8847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008848write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 hashitem_T *hi;
8851 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008852 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008853 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008854 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008856 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857
8858 if (find_viminfo_parameter('!') == NULL)
8859 return;
8860
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008861 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008862
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 this_var = HI2DI(hi);
8870 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008873 {
8874 case VAR_STRING: s = "STR"; break;
8875 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008876 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008877 case VAR_DICT: s = "DIC"; break;
8878 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008879 case VAR_SPECIAL: s = "XPL"; break;
8880
8881 case VAR_UNKNOWN:
8882 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008883 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008884 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008885 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008886 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008888 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008889 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008890 if (p != NULL)
8891 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008892 vim_free(tofree);
8893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895 }
8896}
8897#endif
8898
8899#if defined(FEAT_SESSION) || defined(PROTO)
8900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008901store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 hashitem_T *hi;
8904 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008905 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 char_u *p, *t;
8907
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008908 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008909 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008911 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008913 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 this_var = HI2DI(hi);
8915 if ((this_var->di_tv.v_type == VAR_NUMBER
8916 || this_var->di_tv.v_type == VAR_STRING)
8917 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008918 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008919 /* Escape special characters with a backslash. Turn a LF and
8920 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008921 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008922 (char_u *)"\\\"\n\r");
8923 if (p == NULL) /* out of memory */
8924 break;
8925 for (t = p; *t != NUL; ++t)
8926 if (*t == '\n')
8927 *t = 'n';
8928 else if (*t == '\r')
8929 *t = 'r';
8930 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008931 this_var->di_key,
8932 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8933 : ' ',
8934 p,
8935 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8936 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008937 || put_eol(fd) == FAIL)
8938 {
8939 vim_free(p);
8940 return FAIL;
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 vim_free(p);
8943 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#ifdef FEAT_FLOAT
8945 else if (this_var->di_tv.v_type == VAR_FLOAT
8946 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8947 {
8948 float_T f = this_var->di_tv.vval.v_float;
8949 int sign = ' ';
8950
8951 if (f < 0)
8952 {
8953 f = -f;
8954 sign = '-';
8955 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008956 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008957 this_var->di_key, sign, f) < 0)
8958 || put_eol(fd) == FAIL)
8959 return FAIL;
8960 }
8961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 }
8964 return OK;
8965}
8966#endif
8967
Bram Moolenaar661b1822005-07-28 22:36:45 +00008968/*
8969 * Display script name where an item was last set.
8970 * Should only be invoked when 'verbose' is non-zero.
8971 */
8972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008974{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008975 char_u *p;
8976
Bram Moolenaar661b1822005-07-28 22:36:45 +00008977 if (scriptID != 0)
8978 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008979 p = home_replace_save(NULL, get_scriptname(scriptID));
8980 if (p != NULL)
8981 {
8982 verbose_enter();
8983 MSG_PUTS(_("\n\tLast set from "));
8984 MSG_PUTS(p);
8985 vim_free(p);
8986 verbose_leave();
8987 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008988 }
8989}
8990
Bram Moolenaar53744302015-07-17 17:38:22 +02008991/* reset v:option_new, v:option_old and v:option_type */
8992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008994{
8995 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8996 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8997 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8998}
8999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000/*
9001 * Prepare "gap" for an assert error and add the sourcing position.
9002 */
9003 void
9004prepare_assert_error(garray_T *gap)
9005{
9006 char buf[NUMBUFLEN];
9007
9008 ga_init2(gap, 1, 100);
9009 if (sourcing_name != NULL)
9010 {
9011 ga_concat(gap, sourcing_name);
9012 if (sourcing_lnum > 0)
9013 ga_concat(gap, (char_u *)" ");
9014 }
9015 if (sourcing_lnum > 0)
9016 {
9017 sprintf(buf, "line %ld", (long)sourcing_lnum);
9018 ga_concat(gap, (char_u *)buf);
9019 }
9020 if (sourcing_name != NULL || sourcing_lnum > 0)
9021 ga_concat(gap, (char_u *)": ");
9022}
9023
9024/*
9025 * Add an assert error to v:errors.
9026 */
9027 void
9028assert_error(garray_T *gap)
9029{
9030 struct vimvar *vp = &vimvars[VV_ERRORS];
9031
9032 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9033 /* Make sure v:errors is a list. */
9034 set_vim_var_list(VV_ERRORS, list_alloc());
9035 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9036}
9037
9038 void
9039assert_equal_common(typval_T *argvars, assert_type_T atype)
9040{
9041 garray_T ga;
9042
9043 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9044 != (atype == ASSERT_EQUAL))
9045 {
9046 prepare_assert_error(&ga);
9047 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9048 atype);
9049 assert_error(&ga);
9050 ga_clear(&ga);
9051 }
9052}
9053
9054 void
9055assert_match_common(typval_T *argvars, assert_type_T atype)
9056{
9057 garray_T ga;
9058 char_u buf1[NUMBUFLEN];
9059 char_u buf2[NUMBUFLEN];
9060 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9061 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9062
9063 if (pat == NULL || text == NULL)
9064 EMSG(_(e_invarg));
9065 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9066 {
9067 prepare_assert_error(&ga);
9068 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9069 atype);
9070 assert_error(&ga);
9071 ga_clear(&ga);
9072 }
9073}
9074
Bram Moolenaar61c04492016-07-23 15:35:35 +02009075 void
9076assert_inrange(typval_T *argvars)
9077{
9078 garray_T ga;
9079 int error = FALSE;
9080 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9081 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9082 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9083 char_u *tofree;
9084 char msg[200];
9085 char_u numbuf[NUMBUFLEN];
9086
9087 if (error)
9088 return;
9089 if (actual < lower || actual > upper)
9090 {
9091 prepare_assert_error(&ga);
9092 if (argvars[3].v_type != VAR_UNKNOWN)
9093 {
9094 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9095 vim_free(tofree);
9096 }
9097 else
9098 {
9099 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9100 (long)lower, (long)upper, (long)actual);
9101 ga_concat(&ga, (char_u *)msg);
9102 }
9103 assert_error(&ga);
9104 ga_clear(&ga);
9105 }
9106}
9107
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108/*
9109 * Common for assert_true() and assert_false().
9110 */
9111 void
9112assert_bool(typval_T *argvars, int isTrue)
9113{
9114 int error = FALSE;
9115 garray_T ga;
9116
9117 if (argvars[0].v_type == VAR_SPECIAL
9118 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9119 return;
9120 if (argvars[0].v_type != VAR_NUMBER
9121 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9122 || error)
9123 {
9124 prepare_assert_error(&ga);
9125 fill_assert_error(&ga, &argvars[1],
9126 (char_u *)(isTrue ? "True" : "False"),
9127 NULL, &argvars[0], ASSERT_OTHER);
9128 assert_error(&ga);
9129 ga_clear(&ga);
9130 }
9131}
9132
9133 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009134assert_report(typval_T *argvars)
9135{
9136 garray_T ga;
9137
9138 prepare_assert_error(&ga);
9139 ga_concat(&ga, get_tv_string(&argvars[0]));
9140 assert_error(&ga);
9141 ga_clear(&ga);
9142}
9143
9144 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145assert_exception(typval_T *argvars)
9146{
9147 garray_T ga;
9148 char_u *error = get_tv_string_chk(&argvars[0]);
9149
9150 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9151 {
9152 prepare_assert_error(&ga);
9153 ga_concat(&ga, (char_u *)"v:exception is not set");
9154 assert_error(&ga);
9155 ga_clear(&ga);
9156 }
9157 else if (error != NULL
9158 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9159 {
9160 prepare_assert_error(&ga);
9161 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9162 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9163 assert_error(&ga);
9164 ga_clear(&ga);
9165 }
9166}
9167
9168 void
9169assert_fails(typval_T *argvars)
9170{
9171 char_u *cmd = get_tv_string_chk(&argvars[0]);
9172 garray_T ga;
9173
9174 called_emsg = FALSE;
9175 suppress_errthrow = TRUE;
9176 emsg_silent = TRUE;
9177 do_cmdline_cmd(cmd);
9178 if (!called_emsg)
9179 {
9180 prepare_assert_error(&ga);
9181 ga_concat(&ga, (char_u *)"command did not fail: ");
9182 ga_concat(&ga, cmd);
9183 assert_error(&ga);
9184 ga_clear(&ga);
9185 }
9186 else if (argvars[1].v_type != VAR_UNKNOWN)
9187 {
9188 char_u buf[NUMBUFLEN];
9189 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9190
9191 if (error == NULL
9192 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9193 {
9194 prepare_assert_error(&ga);
9195 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9196 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9197 assert_error(&ga);
9198 ga_clear(&ga);
9199 }
9200 }
9201
9202 called_emsg = FALSE;
9203 suppress_errthrow = FALSE;
9204 emsg_silent = FALSE;
9205 emsg_on_display = FALSE;
9206 set_vim_var_string(VV_ERRMSG, NULL, 0);
9207}
9208
9209/*
9210 * Append "str" to "gap", escaping unprintable characters.
9211 * Changes NL to \n, CR to \r, etc.
9212 */
9213 static void
9214ga_concat_esc(garray_T *gap, char_u *str)
9215{
9216 char_u *p;
9217 char_u buf[NUMBUFLEN];
9218
9219 if (str == NULL)
9220 {
9221 ga_concat(gap, (char_u *)"NULL");
9222 return;
9223 }
9224
9225 for (p = str; *p != NUL; ++p)
9226 switch (*p)
9227 {
9228 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9229 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9230 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9231 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9232 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9233 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9234 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9235 default:
9236 if (*p < ' ')
9237 {
9238 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9239 ga_concat(gap, buf);
9240 }
9241 else
9242 ga_append(gap, *p);
9243 break;
9244 }
9245}
9246
9247/*
9248 * Fill "gap" with information about an assert error.
9249 */
9250 void
9251fill_assert_error(
9252 garray_T *gap,
9253 typval_T *opt_msg_tv,
9254 char_u *exp_str,
9255 typval_T *exp_tv,
9256 typval_T *got_tv,
9257 assert_type_T atype)
9258{
9259 char_u numbuf[NUMBUFLEN];
9260 char_u *tofree;
9261
9262 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9263 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009264 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9265 vim_free(tofree);
9266 ga_concat(gap, (char_u *)": ");
9267 }
9268
9269 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9270 ga_concat(gap, (char_u *)"Pattern ");
9271 else if (atype == ASSERT_NOTEQUAL)
9272 ga_concat(gap, (char_u *)"Expected not equal to ");
9273 else
9274 ga_concat(gap, (char_u *)"Expected ");
9275 if (exp_str == NULL)
9276 {
9277 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 vim_free(tofree);
9279 }
9280 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009281 ga_concat_esc(gap, exp_str);
9282 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009284 if (atype == ASSERT_MATCH)
9285 ga_concat(gap, (char_u *)" does not match ");
9286 else if (atype == ASSERT_NOTMATCH)
9287 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009289 ga_concat(gap, (char_u *)" but got ");
9290 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9291 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009292 }
9293}
9294
Bram Moolenaar53744302015-07-17 17:38:22 +02009295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296#endif /* FEAT_EVAL */
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009299#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
9301#ifdef WIN3264
9302/*
9303 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9304 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009305static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9306static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9307static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
9309/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009310 * Get the short path (8.3) for the filename in "fnamep".
9311 * Only works for a valid file name.
9312 * When the path gets longer "fnamep" is changed and the allocated buffer
9313 * is put in "bufp".
9314 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9315 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 */
9317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009320 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 char_u *newbuf;
9322
9323 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009324 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 if (l > len - 1)
9326 {
9327 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009328 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 newbuf = vim_strnsave(*fnamep, l);
9330 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
9333 vim_free(*bufp);
9334 *fnamep = *bufp = newbuf;
9335
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009336 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009337 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 }
9339
9340 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009341 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342}
9343
9344/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009345 * Get the short path (8.3) for the filename in "fname". The converted
9346 * path is returned in "bufp".
9347 *
9348 * Some of the directories specified in "fname" may not exist. This function
9349 * will shorten the existing directories at the beginning of the path and then
9350 * append the remaining non-existing path.
9351 *
9352 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009353 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009354 * bufp - Pointer to an allocated buffer for the filename.
9355 * fnamelen - Length of the filename pointed to by fname
9356 *
9357 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 */
9359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360shortpath_for_invalid_fname(
9361 char_u **fname,
9362 char_u **bufp,
9363 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009365 char_u *short_fname, *save_fname, *pbuf_unused;
9366 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009368 int old_len, len;
9369 int new_len, sfx_len;
9370 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
9372 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009373 old_len = *fnamelen;
9374 save_fname = vim_strnsave(*fname, old_len);
9375 pbuf_unused = NULL;
9376 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009378 endp = save_fname + old_len - 1; /* Find the end of the copy */
9379 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009381 /*
9382 * Try shortening the supplied path till it succeeds by removing one
9383 * directory at a time from the tail of the path.
9384 */
9385 len = 0;
9386 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009388 /* go back one path-separator */
9389 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9390 --endp;
9391 if (endp <= save_fname)
9392 break; /* processed the complete path */
9393
9394 /*
9395 * Replace the path separator with a NUL and try to shorten the
9396 * resulting path.
9397 */
9398 ch = *endp;
9399 *endp = 0;
9400 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009401 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009402 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9403 {
9404 retval = FAIL;
9405 goto theend;
9406 }
9407 *endp = ch; /* preserve the string */
9408
9409 if (len > 0)
9410 break; /* successfully shortened the path */
9411
9412 /* failed to shorten the path. Skip the path separator */
9413 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 }
9415
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009416 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009418 /*
9419 * Succeeded in shortening the path. Now concatenate the shortened
9420 * path with the remaining path at the tail.
9421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009423 /* Compute the length of the new path. */
9424 sfx_len = (int)(save_endp - endp) + 1;
9425 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009427 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009429 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009431 /* There is not enough space in the currently allocated string,
9432 * copy it to a buffer big enough. */
9433 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009435 {
9436 retval = FAIL;
9437 goto theend;
9438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 }
9440 else
9441 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009442 /* Transfer short_fname to the main buffer (it's big enough),
9443 * unless get_short_pathname() did its work in-place. */
9444 *fname = *bufp = save_fname;
9445 if (short_fname != save_fname)
9446 vim_strncpy(save_fname, short_fname, len);
9447 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009449
9450 /* concat the not-shortened part of the path */
9451 vim_strncpy(*fname + len, endp, sfx_len);
9452 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009454
9455theend:
9456 vim_free(pbuf_unused);
9457 vim_free(save_fname);
9458
9459 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462/*
9463 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009464 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 */
9466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467shortpath_for_partial(
9468 char_u **fnamep,
9469 char_u **bufp,
9470 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471{
9472 int sepcount, len, tflen;
9473 char_u *p;
9474 char_u *pbuf, *tfname;
9475 int hasTilde;
9476
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009477 /* Count up the path separators from the RHS.. so we know which part
9478 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009480 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (vim_ispathsep(*p))
9482 ++sepcount;
9483
9484 /* Need full path first (use expand_env() to remove a "~/") */
9485 hasTilde = (**fnamep == '~');
9486 if (hasTilde)
9487 pbuf = tfname = expand_env_save(*fnamep);
9488 else
9489 pbuf = tfname = FullName_save(*fnamep, FALSE);
9490
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009491 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009493 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9494 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
9496 if (len == 0)
9497 {
9498 /* Don't have a valid filename, so shorten the rest of the
9499 * path if we can. This CAN give us invalid 8.3 filenames, but
9500 * there's not a lot of point in guessing what it might be.
9501 */
9502 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009503 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9504 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 }
9506
9507 /* Count the paths backward to find the beginning of the desired string. */
9508 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009509 {
9510#ifdef FEAT_MBYTE
9511 if (has_mbyte)
9512 p -= mb_head_off(tfname, p);
9513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (vim_ispathsep(*p))
9515 {
9516 if (sepcount == 0 || (hasTilde && sepcount == 1))
9517 break;
9518 else
9519 sepcount --;
9520 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (hasTilde)
9523 {
9524 --p;
9525 if (p >= tfname)
9526 *p = '~';
9527 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009528 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 }
9530 else
9531 ++p;
9532
9533 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9534 vim_free(*bufp);
9535 *fnamelen = (int)STRLEN(p);
9536 *bufp = pbuf;
9537 *fnamep = p;
9538
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009539 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541#endif /* WIN3264 */
9542
9543/*
9544 * Adjust a filename, according to a string of modifiers.
9545 * *fnamep must be NUL terminated when called. When returning, the length is
9546 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009547 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 * When there is an error, *fnamep is set to NULL.
9549 */
9550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009551modify_fname(
9552 char_u *src, /* string with modifiers */
9553 int *usedlen, /* characters after src that are used */
9554 char_u **fnamep, /* file name so far */
9555 char_u **bufp, /* buffer for allocated file name or NULL */
9556 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 int valid = 0;
9559 char_u *tail;
9560 char_u *s, *p, *pbuf;
9561 char_u dirname[MAXPATHL];
9562 int c;
9563 int has_fullname = 0;
9564#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009565 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 int has_shortname = 0;
9567#endif
9568
9569repeat:
9570 /* ":p" - full path/file_name */
9571 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9572 {
9573 has_fullname = 1;
9574
9575 valid |= VALID_PATH;
9576 *usedlen += 2;
9577
9578 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9579 if ((*fnamep)[0] == '~'
9580#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9581 && ((*fnamep)[1] == '/'
9582# ifdef BACKSLASH_IN_FILENAME
9583 || (*fnamep)[1] == '\\'
9584# endif
9585 || (*fnamep)[1] == NUL)
9586
9587#endif
9588 )
9589 {
9590 *fnamep = expand_env_save(*fnamep);
9591 vim_free(*bufp); /* free any allocated file name */
9592 *bufp = *fnamep;
9593 if (*fnamep == NULL)
9594 return -1;
9595 }
9596
9597 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009598 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
9600 if (vim_ispathsep(*p)
9601 && p[1] == '.'
9602 && (p[2] == NUL
9603 || vim_ispathsep(p[2])
9604 || (p[2] == '.'
9605 && (p[3] == NUL || vim_ispathsep(p[3])))))
9606 break;
9607 }
9608
9609 /* FullName_save() is slow, don't use it when not needed. */
9610 if (*p != NUL || !vim_isAbsName(*fnamep))
9611 {
9612 *fnamep = FullName_save(*fnamep, *p != NUL);
9613 vim_free(*bufp); /* free any allocated file name */
9614 *bufp = *fnamep;
9615 if (*fnamep == NULL)
9616 return -1;
9617 }
9618
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009619#ifdef WIN3264
9620# if _WIN32_WINNT >= 0x0500
9621 if (vim_strchr(*fnamep, '~') != NULL)
9622 {
9623 /* Expand 8.3 filename to full path. Needed to make sure the same
9624 * file does not have two different names.
9625 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9626 p = alloc(_MAX_PATH + 1);
9627 if (p != NULL)
9628 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009629 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009630 {
9631 vim_free(*bufp);
9632 *bufp = *fnamep = p;
9633 }
9634 else
9635 vim_free(p);
9636 }
9637 }
9638# endif
9639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 /* Append a path separator to a directory. */
9641 if (mch_isdir(*fnamep))
9642 {
9643 /* Make room for one or two extra characters. */
9644 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9645 vim_free(*bufp); /* free any allocated file name */
9646 *bufp = *fnamep;
9647 if (*fnamep == NULL)
9648 return -1;
9649 add_pathsep(*fnamep);
9650 }
9651 }
9652
9653 /* ":." - path relative to the current directory */
9654 /* ":~" - path relative to the home directory */
9655 /* ":8" - shortname path - postponed till after */
9656 while (src[*usedlen] == ':'
9657 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9658 {
9659 *usedlen += 2;
9660 if (c == '8')
9661 {
9662#ifdef WIN3264
9663 has_shortname = 1; /* Postpone this. */
9664#endif
9665 continue;
9666 }
9667 pbuf = NULL;
9668 /* Need full path first (use expand_env() to remove a "~/") */
9669 if (!has_fullname)
9670 {
9671 if (c == '.' && **fnamep == '~')
9672 p = pbuf = expand_env_save(*fnamep);
9673 else
9674 p = pbuf = FullName_save(*fnamep, FALSE);
9675 }
9676 else
9677 p = *fnamep;
9678
9679 has_fullname = 0;
9680
9681 if (p != NULL)
9682 {
9683 if (c == '.')
9684 {
9685 mch_dirname(dirname, MAXPATHL);
9686 s = shorten_fname(p, dirname);
9687 if (s != NULL)
9688 {
9689 *fnamep = s;
9690 if (pbuf != NULL)
9691 {
9692 vim_free(*bufp); /* free any allocated file name */
9693 *bufp = pbuf;
9694 pbuf = NULL;
9695 }
9696 }
9697 }
9698 else
9699 {
9700 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9701 /* Only replace it when it starts with '~' */
9702 if (*dirname == '~')
9703 {
9704 s = vim_strsave(dirname);
9705 if (s != NULL)
9706 {
9707 *fnamep = s;
9708 vim_free(*bufp);
9709 *bufp = s;
9710 }
9711 }
9712 }
9713 vim_free(pbuf);
9714 }
9715 }
9716
9717 tail = gettail(*fnamep);
9718 *fnamelen = (int)STRLEN(*fnamep);
9719
9720 /* ":h" - head, remove "/file_name", can be repeated */
9721 /* Don't remove the first "/" or "c:\" */
9722 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9723 {
9724 valid |= VALID_HEAD;
9725 *usedlen += 2;
9726 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009727 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009728 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 *fnamelen = (int)(tail - *fnamep);
9730#ifdef VMS
9731 if (*fnamelen > 0)
9732 *fnamelen += 1; /* the path separator is part of the path */
9733#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009734 if (*fnamelen == 0)
9735 {
9736 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9737 p = vim_strsave((char_u *)".");
9738 if (p == NULL)
9739 return -1;
9740 vim_free(*bufp);
9741 *bufp = *fnamep = tail = p;
9742 *fnamelen = 1;
9743 }
9744 else
9745 {
9746 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009747 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 }
9750
9751 /* ":8" - shortname */
9752 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9753 {
9754 *usedlen += 2;
9755#ifdef WIN3264
9756 has_shortname = 1;
9757#endif
9758 }
9759
9760#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009761 /*
9762 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 */
9764 if (has_shortname)
9765 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009766 /* Copy the string if it is shortened by :h and when it wasn't copied
9767 * yet, because we are going to change it in place. Avoids changing
9768 * the buffer name for "%:8". */
9769 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 {
9771 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009772 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 return -1;
9774 vim_free(*bufp);
9775 *bufp = *fnamep = p;
9776 }
9777
9778 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009779 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (!has_fullname && !vim_isAbsName(*fnamep))
9781 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009782 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 return -1;
9784 }
9785 else
9786 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009787 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaardc935552011-08-17 15:23:23 +02009789 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009791 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 return -1;
9793
9794 if (l == 0)
9795 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009796 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009798 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 return -1;
9800 }
9801 *fnamelen = l;
9802 }
9803 }
9804#endif /* WIN3264 */
9805
9806 /* ":t" - tail, just the basename */
9807 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9808 {
9809 *usedlen += 2;
9810 *fnamelen -= (int)(tail - *fnamep);
9811 *fnamep = tail;
9812 }
9813
9814 /* ":e" - extension, can be repeated */
9815 /* ":r" - root, without extension, can be repeated */
9816 while (src[*usedlen] == ':'
9817 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9818 {
9819 /* find a '.' in the tail:
9820 * - for second :e: before the current fname
9821 * - otherwise: The last '.'
9822 */
9823 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9824 s = *fnamep - 2;
9825 else
9826 s = *fnamep + *fnamelen - 1;
9827 for ( ; s > tail; --s)
9828 if (s[0] == '.')
9829 break;
9830 if (src[*usedlen + 1] == 'e') /* :e */
9831 {
9832 if (s > tail)
9833 {
9834 *fnamelen += (int)(*fnamep - (s + 1));
9835 *fnamep = s + 1;
9836#ifdef VMS
9837 /* cut version from the extension */
9838 s = *fnamep + *fnamelen - 1;
9839 for ( ; s > *fnamep; --s)
9840 if (s[0] == ';')
9841 break;
9842 if (s > *fnamep)
9843 *fnamelen = s - *fnamep;
9844#endif
9845 }
9846 else if (*fnamep <= tail)
9847 *fnamelen = 0;
9848 }
9849 else /* :r */
9850 {
9851 if (s > tail) /* remove one extension */
9852 *fnamelen = (int)(s - *fnamep);
9853 }
9854 *usedlen += 2;
9855 }
9856
9857 /* ":s?pat?foo?" - substitute */
9858 /* ":gs?pat?foo?" - global substitute */
9859 if (src[*usedlen] == ':'
9860 && (src[*usedlen + 1] == 's'
9861 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9862 {
9863 char_u *str;
9864 char_u *pat;
9865 char_u *sub;
9866 int sep;
9867 char_u *flags;
9868 int didit = FALSE;
9869
9870 flags = (char_u *)"";
9871 s = src + *usedlen + 2;
9872 if (src[*usedlen + 1] == 'g')
9873 {
9874 flags = (char_u *)"g";
9875 ++s;
9876 }
9877
9878 sep = *s++;
9879 if (sep)
9880 {
9881 /* find end of pattern */
9882 p = vim_strchr(s, sep);
9883 if (p != NULL)
9884 {
9885 pat = vim_strnsave(s, (int)(p - s));
9886 if (pat != NULL)
9887 {
9888 s = p + 1;
9889 /* find end of substitution */
9890 p = vim_strchr(s, sep);
9891 if (p != NULL)
9892 {
9893 sub = vim_strnsave(s, (int)(p - s));
9894 str = vim_strnsave(*fnamep, *fnamelen);
9895 if (sub != NULL && str != NULL)
9896 {
9897 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009898 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 if (s != NULL)
9900 {
9901 *fnamep = s;
9902 *fnamelen = (int)STRLEN(s);
9903 vim_free(*bufp);
9904 *bufp = s;
9905 didit = TRUE;
9906 }
9907 }
9908 vim_free(sub);
9909 vim_free(str);
9910 }
9911 vim_free(pat);
9912 }
9913 }
9914 /* after using ":s", repeat all the modifiers */
9915 if (didit)
9916 goto repeat;
9917 }
9918 }
9919
Bram Moolenaar26df0922014-02-23 23:39:13 +01009920 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9921 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009922 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009923 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009924 if (c != NUL)
9925 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009926 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009927 if (c != NUL)
9928 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009929 if (p == NULL)
9930 return -1;
9931 vim_free(*bufp);
9932 *bufp = *fnamep = p;
9933 *fnamelen = (int)STRLEN(p);
9934 *usedlen += 2;
9935 }
9936
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 return valid;
9938}
9939
9940/*
9941 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009942 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 * "flags" can be "g" to do a global substitute.
9944 * Returns an allocated string, NULL for error.
9945 */
9946 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947do_string_sub(
9948 char_u *str,
9949 char_u *pat,
9950 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009951 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954 int sublen;
9955 regmatch_T regmatch;
9956 int i;
9957 int do_all;
9958 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009959 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 garray_T ga;
9961 char_u *ret;
9962 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009963 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
9965 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9966 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009967 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
9969 ga_init2(&ga, 1, 200);
9970
9971 do_all = (flags[0] == 'g');
9972
9973 regmatch.rm_ic = p_ic;
9974 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9975 if (regmatch.regprog != NULL)
9976 {
9977 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009978 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9980 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009981 /* Skip empty match except for first match. */
9982 if (regmatch.startp[0] == regmatch.endp[0])
9983 {
9984 if (zero_width == regmatch.startp[0])
9985 {
9986 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009987 i = MB_PTR2LEN(tail);
9988 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9989 (size_t)i);
9990 ga.ga_len += i;
9991 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009992 continue;
9993 }
9994 zero_width = regmatch.startp[0];
9995 }
9996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 /*
9998 * Get some space for a temporary buffer to do the substitution
9999 * into. It will contain:
10000 * - The text up to where the match is.
10001 * - The substituted text.
10002 * - The text after the match.
10003 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010004 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010005 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10007 {
10008 ga_clear(&ga);
10009 break;
10010 }
10011
10012 /* copy the text up to where the match is */
10013 i = (int)(regmatch.startp[0] - tail);
10014 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10015 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010016 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 + ga.ga_len + i, TRUE, TRUE, FALSE);
10018 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010019 tail = regmatch.endp[0];
10020 if (*tail == NUL)
10021 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 if (!do_all)
10023 break;
10024 }
10025
10026 if (ga.ga_data != NULL)
10027 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10028
Bram Moolenaar473de612013-06-08 18:19:48 +020010029 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031
10032 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10033 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010034 if (p_cpo == empty_option)
10035 p_cpo = save_cpo;
10036 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010037 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010038 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039
10040 return ret;
10041}
10042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 static int
10044filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10045{
10046 typval_T rettv;
10047 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010048 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049
10050 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10051 argv[0] = vimvars[VV_KEY].vv_tv;
10052 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010053 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10054 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010055 if (map)
10056 {
10057 /* map(): replace the list item value */
10058 clear_tv(tv);
10059 rettv.v_lock = 0;
10060 *tv = rettv;
10061 }
10062 else
10063 {
10064 int error = FALSE;
10065
10066 /* filter(): when expr is zero remove the item */
10067 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10068 clear_tv(&rettv);
10069 /* On type error, nothing has been removed; return FAIL to stop the
10070 * loop. The error message was given by get_tv_number_chk(). */
10071 if (error)
10072 goto theend;
10073 }
10074 retval = OK;
10075theend:
10076 clear_tv(&vimvars[VV_VAL].vv_tv);
10077 return retval;
10078}
10079
10080
10081/*
10082 * Implementation of map() and filter().
10083 */
10084 void
10085filter_map(typval_T *argvars, typval_T *rettv, int map)
10086{
10087 typval_T *expr;
10088 listitem_T *li, *nli;
10089 list_T *l = NULL;
10090 dictitem_T *di;
10091 hashtab_T *ht;
10092 hashitem_T *hi;
10093 dict_T *d = NULL;
10094 typval_T save_val;
10095 typval_T save_key;
10096 int rem;
10097 int todo;
10098 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10099 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10100 : N_("filter() argument"));
10101 int save_did_emsg;
10102 int idx = 0;
10103
10104 if (argvars[0].v_type == VAR_LIST)
10105 {
10106 if ((l = argvars[0].vval.v_list) == NULL
10107 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10108 return;
10109 }
10110 else if (argvars[0].v_type == VAR_DICT)
10111 {
10112 if ((d = argvars[0].vval.v_dict) == NULL
10113 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10114 return;
10115 }
10116 else
10117 {
10118 EMSG2(_(e_listdictarg), ermsg);
10119 return;
10120 }
10121
10122 expr = &argvars[1];
10123 /* On type errors, the preceding call has already displayed an error
10124 * message. Avoid a misleading error message for an empty string that
10125 * was not passed as argument. */
10126 if (expr->v_type != VAR_UNKNOWN)
10127 {
10128 prepare_vimvar(VV_VAL, &save_val);
10129
10130 /* We reset "did_emsg" to be able to detect whether an error
10131 * occurred during evaluation of the expression. */
10132 save_did_emsg = did_emsg;
10133 did_emsg = FALSE;
10134
10135 prepare_vimvar(VV_KEY, &save_key);
10136 if (argvars[0].v_type == VAR_DICT)
10137 {
10138 vimvars[VV_KEY].vv_type = VAR_STRING;
10139
10140 ht = &d->dv_hashtab;
10141 hash_lock(ht);
10142 todo = (int)ht->ht_used;
10143 for (hi = ht->ht_array; todo > 0; ++hi)
10144 {
10145 if (!HASHITEM_EMPTY(hi))
10146 {
10147 int r;
10148
10149 --todo;
10150 di = HI2DI(hi);
10151 if (map &&
10152 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10153 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10154 break;
10155 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10156 r = filter_map_one(&di->di_tv, expr, map, &rem);
10157 clear_tv(&vimvars[VV_KEY].vv_tv);
10158 if (r == FAIL || did_emsg)
10159 break;
10160 if (!map && rem)
10161 {
10162 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10163 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10164 break;
10165 dictitem_remove(d, di);
10166 }
10167 }
10168 }
10169 hash_unlock(ht);
10170 }
10171 else
10172 {
10173 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10174
10175 for (li = l->lv_first; li != NULL; li = nli)
10176 {
10177 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10178 break;
10179 nli = li->li_next;
10180 vimvars[VV_KEY].vv_nr = idx;
10181 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10182 || did_emsg)
10183 break;
10184 if (!map && rem)
10185 listitem_remove(l, li);
10186 ++idx;
10187 }
10188 }
10189
10190 restore_vimvar(VV_KEY, &save_key);
10191 restore_vimvar(VV_VAL, &save_val);
10192
10193 did_emsg |= save_did_emsg;
10194 }
10195
10196 copy_tv(&argvars[0], rettv);
10197}
10198
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */