blob: 3a275213cbf8653677be5061f5c03308e764c04d [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 Moolenaard23a8232018-02-10 18:45:26 +0100364 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 else if (p->vv_di.di_tv.v_type == VAR_LIST)
366 {
367 list_unref(p->vv_list);
368 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000369 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000370 }
371 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000372 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000373 hash_clear(&compat_hashtab);
374
Bram Moolenaard9fba312005-06-26 22:34:35 +0000375 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200377 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100378# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000379
380 /* global variables */
381 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000382
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000383 /* autoloaded script names */
384 ga_clear_strings(&ga_loaded);
385
Bram Moolenaarcca74132013-09-25 21:00:28 +0200386 /* Script-local variables. First clear all the variables and in a second
387 * loop free the scriptvar_T, because a variable in one script might hold
388 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 ga_clear(&ga_scripts);
394
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000395 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200396 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000397
398 /* functions */
399 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400}
401#endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 * Set an internal variable to a string value. Creates the variable if it does
406 * not already exist.
407 */
408 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100409set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000411 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000412 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 val = vim_strsave(value);
415 if (val != NULL)
416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000420 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
423 }
424}
425
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000426static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200427#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000428static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000429static char_u *redir_endp = NULL;
430static char_u *redir_varname = NULL;
431
432/*
433 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100434 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000435 * Returns OK if successfully completed the setup. FAIL otherwise.
436 */
437 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439{
440 int save_emsg;
441 int err;
442 typval_T tv;
443
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000444 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000445 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000446 {
447 EMSG(_(e_invarg));
448 return FAIL;
449 }
450
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000451 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 redir_varname = vim_strsave(name);
453 if (redir_varname == NULL)
454 return FAIL;
455
456 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
457 if (redir_lval == NULL)
458 {
459 var_redir_stop();
460 return FAIL;
461 }
462
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000463 /* The output is stored in growarray "redir_ga" until redirection ends. */
464 ga_init2(&redir_ga, (int)sizeof(char), 500);
465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000466 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100467 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000468 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
470 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200471 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 if (redir_endp != NULL && *redir_endp != NUL)
473 /* Trailing characters are present after the variable name */
474 EMSG(_(e_trailing));
475 else
476 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000477 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478 var_redir_stop();
479 return FAIL;
480 }
481
482 /* check if we can write to the variable: set it to or append an empty
483 * string */
484 save_emsg = did_emsg;
485 did_emsg = FALSE;
486 tv.v_type = VAR_STRING;
487 tv.vval.v_string = (char_u *)"";
488 if (append)
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
490 else
491 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200492 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000494 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 if (err)
496 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 var_redir_stop();
499 return FAIL;
500 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000501
502 return OK;
503}
504
505/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000506 * Append "value[value_len]" to the variable set by var_redir_start().
507 * The actual appending is postponed until redirection ends, because the value
508 * appended may in fact be the string we write to, changing it may cause freed
509 * memory to be used:
510 * :redir => foo
511 * :let foo
512 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 */
514 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100515var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000517 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518
519 if (redir_lval == NULL)
520 return;
521
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000522 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000526
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000528 {
529 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000531 }
532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534}
535
536/*
537 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000538 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 */
540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100541var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543 typval_T tv;
544
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200545 if (EVALCMD_BUSY)
546 {
547 redir_lval = NULL;
548 return;
549 }
550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 if (redir_lval != NULL)
552 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000553 /* If there was no error: assign the text to the variable. */
554 if (redir_endp != NULL)
555 {
556 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
557 tv.v_type = VAR_STRING;
558 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200559 /* Call get_lval() again, if it's inside a Dict or List it may
560 * have changed. */
561 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100562 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 if (redir_endp != NULL && redir_lval->ll_name != NULL)
564 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
565 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000568 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100569 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000570
Bram Moolenaard23a8232018-02-10 18:45:26 +0100571 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576# if defined(FEAT_MBYTE) || defined(PROTO)
577 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100578eval_charconvert(
579 char_u *enc_from,
580 char_u *enc_to,
581 char_u *fname_from,
582 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 int err = FALSE;
585
586 set_vim_var_string(VV_CC_FROM, enc_from, -1);
587 set_vim_var_string(VV_CC_TO, enc_to, -1);
588 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
589 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
590 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
591 err = TRUE;
592 set_vim_var_string(VV_CC_FROM, NULL, -1);
593 set_vim_var_string(VV_CC_TO, NULL, -1);
594 set_vim_var_string(VV_FNAME_IN, NULL, -1);
595 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
596
597 if (err)
598 return FAIL;
599 return OK;
600}
601# endif
602
603# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100605eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 int err = FALSE;
608
609 set_vim_var_string(VV_FNAME_IN, fname, -1);
610 set_vim_var_string(VV_CMDARG, args, -1);
611 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
612 err = TRUE;
613 set_vim_var_string(VV_FNAME_IN, NULL, -1);
614 set_vim_var_string(VV_CMDARG, NULL, -1);
615
616 if (err)
617 {
618 mch_remove(fname);
619 return FAIL;
620 }
621 return OK;
622}
623# endif
624
625# if defined(FEAT_DIFF) || defined(PROTO)
626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100627eval_diff(
628 char_u *origfile,
629 char_u *newfile,
630 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 int err = FALSE;
633
634 set_vim_var_string(VV_FNAME_IN, origfile, -1);
635 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
636 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
637 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
638 set_vim_var_string(VV_FNAME_IN, NULL, -1);
639 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641}
642
643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100644eval_patch(
645 char_u *origfile,
646 char_u *difffile,
647 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 int err;
650
651 set_vim_var_string(VV_FNAME_IN, origfile, -1);
652 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
653 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
654 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
655 set_vim_var_string(VV_FNAME_IN, NULL, -1);
656 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
657 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
658}
659# endif
660
661/*
662 * Top level evaluation function, returning a boolean.
663 * Sets "error" to TRUE if there was an error.
664 * Return TRUE or FALSE.
665 */
666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100667eval_to_bool(
668 char_u *arg,
669 int *error,
670 char_u **nextcmd,
671 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
Bram Moolenaar33570922005-01-25 22:26:29 +0000673 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200674 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 if (skip)
677 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000678 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
681 {
682 *error = FALSE;
683 if (!skip)
684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000685 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000686 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 }
689 if (skip)
690 --emsg_skip;
691
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200692 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
Bram Moolenaar48570482017-10-30 21:48:41 +0100695 static int
696eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
697{
698 char_u *s;
699 int dummy;
700 char_u buf[NUMBUFLEN];
701
702 if (expr->v_type == VAR_FUNC)
703 {
704 s = expr->vval.v_string;
705 if (s == NULL || *s == NUL)
706 return FAIL;
707 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
708 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
709 return FAIL;
710 }
711 else if (expr->v_type == VAR_PARTIAL)
712 {
713 partial_T *partial = expr->vval.v_partial;
714
715 s = partial_name(partial);
716 if (s == NULL || *s == NUL)
717 return FAIL;
718 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
719 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
720 return FAIL;
721 }
722 else
723 {
724 s = get_tv_string_buf_chk(expr, buf);
725 if (s == NULL)
726 return FAIL;
727 s = skipwhite(s);
728 if (eval1(&s, rettv, TRUE) == FAIL)
729 return FAIL;
730 if (*s != NUL) /* check for trailing chars after expr */
731 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200732 clear_tv(rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100733 EMSG2(_(e_invexpr2), s);
734 return FAIL;
735 }
736 }
737 return OK;
738}
739
740/*
741 * Like eval_to_bool() but using a typval_T instead of a string.
742 * Works for string, funcref and partial.
743 */
744 int
745eval_expr_to_bool(typval_T *expr, int *error)
746{
747 typval_T rettv;
748 int res;
749
750 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
751 {
752 *error = TRUE;
753 return FALSE;
754 }
755 res = (get_tv_number_chk(&rettv, error) != 0);
756 clear_tv(&rettv);
757 return res;
758}
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760/*
761 * Top level evaluation function, returning a string. If "skip" is TRUE,
762 * only parsing to "nextcmd" is done, without reporting errors. Return
763 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
764 */
765 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766eval_to_string_skip(
767 char_u *arg,
768 char_u **nextcmd,
769 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar33570922005-01-25 22:26:29 +0000771 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 char_u *retval;
773
774 if (skip)
775 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 retval = NULL;
778 else
779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000780 retval = vim_strsave(get_tv_string(&tv));
781 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 if (skip)
784 --emsg_skip;
785
786 return retval;
787}
788
789/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000790 * Skip over an expression at "*pp".
791 * Return FAIL for an error, OK otherwise.
792 */
793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100794skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000795{
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000797
798 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800}
801
802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000804 * When "convert" is TRUE convert a List into a sequence of lines and convert
805 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * Return pointer to allocated memory, or NULL for failure.
807 */
808 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100809eval_to_string(
810 char_u *arg,
811 char_u **nextcmd,
812 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000816 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000817#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000818 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 retval = NULL;
823 else
824 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000825 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000826 {
827 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000828 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200829 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200830 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200831 if (tv.vval.v_list->lv_len > 0)
832 ga_append(&ga, NL);
833 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834 ga_append(&ga, NUL);
835 retval = (char_u *)ga.ga_data;
836 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000837#ifdef FEAT_FLOAT
838 else if (convert && tv.v_type == VAR_FLOAT)
839 {
840 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
841 retval = vim_strsave(numbuf);
842 }
843#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 else
845 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848
849 return retval;
850}
851
852/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000853 * Call eval_to_string() without using current local variables and using
854 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 */
856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857eval_to_string_safe(
858 char_u *arg,
859 char_u **nextcmd,
860 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 char_u *retval;
863 void *save_funccalp;
864
865 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000866 if (use_sandbox)
867 ++sandbox;
868 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000869 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000870 if (use_sandbox)
871 --sandbox;
872 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 restore_funccal(save_funccalp);
874 return retval;
875}
876
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877/*
878 * Top level evaluation function, returning a number.
879 * Evaluates "expr" silently.
880 * Returns -1 for an error.
881 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200882 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100883eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200886 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000887 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 ++emsg_off;
890
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = -1;
893 else
894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000895 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 --emsg_off;
899
900 return retval;
901}
902
Bram Moolenaara40058a2005-07-11 22:42:07 +0000903/*
904 * Prepare v: variable "idx" to be used.
905 * Save the current typeval in "save_tv".
906 * When not used yet add the variable to the v: hashtable.
907 */
908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100909prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000910{
911 *save_tv = vimvars[idx].vv_tv;
912 if (vimvars[idx].vv_type == VAR_UNKNOWN)
913 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
914}
915
916/*
917 * Restore v: variable "idx" to typeval "save_tv".
918 * When no longer defined, remove the variable from the v: hashtable.
919 */
920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100921restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000922{
923 hashitem_T *hi;
924
Bram Moolenaara40058a2005-07-11 22:42:07 +0000925 vimvars[idx].vv_tv = *save_tv;
926 if (vimvars[idx].vv_type == VAR_UNKNOWN)
927 {
928 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
929 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100930 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000931 else
932 hash_remove(&vimvarht, hi);
933 }
934}
935
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000936#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000937/*
938 * Evaluate an expression to a list with suggestions.
939 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000940 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000941 */
942 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100943eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000944{
945 typval_T save_val;
946 typval_T rettv;
947 list_T *list = NULL;
948 char_u *p = skipwhite(expr);
949
950 /* Set "v:val" to the bad word. */
951 prepare_vimvar(VV_VAL, &save_val);
952 vimvars[VV_VAL].vv_type = VAR_STRING;
953 vimvars[VV_VAL].vv_str = badword;
954 if (p_verbose == 0)
955 ++emsg_off;
956
957 if (eval1(&p, &rettv, TRUE) == OK)
958 {
959 if (rettv.v_type != VAR_LIST)
960 clear_tv(&rettv);
961 else
962 list = rettv.vval.v_list;
963 }
964
965 if (p_verbose == 0)
966 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000967 restore_vimvar(VV_VAL, &save_val);
968
969 return list;
970}
971
972/*
973 * "list" is supposed to contain two items: a word and a number. Return the
974 * word in "pp" and the number as the return value.
975 * Return -1 if anything isn't right.
976 * Used to get the good word and score from the eval_spell_expr() result.
977 */
978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100979get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000980{
981 listitem_T *li;
982
983 li = list->lv_first;
984 if (li == NULL)
985 return -1;
986 *pp = get_tv_string(&li->li_tv);
987
988 li = li->li_next;
989 if (li == NULL)
990 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200991 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000992}
993#endif
994
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000995/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000996 * Top level evaluation function.
997 * Returns an allocated typval_T with the result.
998 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000999 */
1000 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001001eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001002{
1003 typval_T *tv;
1004
1005 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001006 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001007 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001008
1009 return tv;
1010}
1011
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001014 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001015 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1016 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001017 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001020call_vim_function(
1021 char_u *func,
1022 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001023 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001024 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001027 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001029 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001030 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001032 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001033 if (ret == FAIL)
1034 clear_tv(rettv);
1035
1036 return ret;
1037}
1038
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001039/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001040 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001041 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001042 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1043 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001044 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001045 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001046call_func_retnr(
1047 char_u *func,
1048 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001049 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001050{
1051 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001052 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001053
Bram Moolenaarded27a12018-08-01 19:06:03 +02001054 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001055 return -1;
1056
1057 retval = get_tv_number_chk(&rettv, NULL);
1058 clear_tv(&rettv);
1059 return retval;
1060}
1061
1062#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1063 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1064
Bram Moolenaar4f688582007-07-24 12:34:30 +00001065# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001066/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001067 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001068 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001069 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1070 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071 */
1072 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073call_func_retstr(
1074 char_u *func,
1075 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001076 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001077{
1078 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001079 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001080
Bram Moolenaarded27a12018-08-01 19:06:03 +02001081 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001082 return NULL;
1083
1084 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001085 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 return retval;
1087}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001088# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001090/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001091 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001092 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1093 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001094 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095 */
1096 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001097call_func_retlist(
1098 char_u *func,
1099 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001100 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001101{
1102 typval_T rettv;
1103
Bram Moolenaarded27a12018-08-01 19:06:03 +02001104 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001105 return NULL;
1106
1107 if (rettv.v_type != VAR_LIST)
1108 {
1109 clear_tv(&rettv);
1110 return NULL;
1111 }
1112
1113 return rettv.vval.v_list;
1114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
1116
Bram Moolenaar05159a02005-02-26 23:04:13 +00001117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#ifdef FEAT_FOLDING
1119/*
1120 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1121 * it in "*cp". Doesn't give error messages.
1122 */
1123 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001124eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
Bram Moolenaar33570922005-01-25 22:26:29 +00001126 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001127 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001129 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1130 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001133 if (use_sandbox)
1134 ++sandbox;
1135 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 retval = 0;
1139 else
1140 {
1141 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 if (tv.v_type == VAR_NUMBER)
1143 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001144 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 retval = 0;
1146 else
1147 {
1148 /* If the result is a string, check if there is a non-digit before
1149 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (!VIM_ISDIGIT(*s) && *s != '-')
1152 *cp = *s++;
1153 retval = atol((char *)s);
1154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001158 if (use_sandbox)
1159 --sandbox;
1160 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001162 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163}
1164#endif
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 * ":let" list all variable values
1168 * ":let var1 var2" list variable values
1169 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001170 * ":let var += expr" assignment command.
1171 * ":let var -= expr" assignment command.
1172 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001173 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 */
1175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001176ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177{
1178 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182 int var_count = 0;
1183 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001184 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001185 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001186 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaardb552d602006-03-23 22:59:57 +00001188 argend = skip_var_list(arg, &var_count, &semicolon);
1189 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001190 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001191 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1192 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001193 expr = skipwhite(argend);
1194 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1195 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001197 /*
1198 * ":let" without "=": list variables
1199 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001200 if (*arg == '[')
1201 EMSG(_(e_invarg));
1202 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001204 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001208 list_glob_vars(&first);
1209 list_buf_vars(&first);
1210 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001211 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001212 list_script_vars(&first);
1213 list_func_vars(&first);
1214 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 eap->nextcmd = check_nextcmd(arg);
1217 }
1218 else
1219 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001220 op[0] = '=';
1221 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001222 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001223 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001224 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1225 op[0] = *expr; /* +=, -= or .= */
1226 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001228 else
1229 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (eap->skip)
1232 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001233 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (eap->skip)
1235 {
1236 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 --emsg_skip;
1239 }
1240 else if (i != FAIL)
1241 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001242 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001243 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246 }
1247}
1248
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001249/*
1250 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1251 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001252 * When "nextchars" is not NULL it points to a string with characters that
1253 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1254 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001255 * Returns OK or FAIL;
1256 */
1257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001258ex_let_vars(
1259 char_u *arg_start,
1260 typval_T *tv,
1261 int copy, /* copy values from "tv", don't move */
1262 int semicolon, /* from skip_var_list() */
1263 int var_count, /* from skip_var_list() */
1264 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001265{
1266 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001268 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001269 listitem_T *item;
1270 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271
1272 if (*arg != '[')
1273 {
1274 /*
1275 * ":let var = expr" or ":for var in list"
1276 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001278 return FAIL;
1279 return OK;
1280 }
1281
1282 /*
1283 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1284 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001285 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001286 {
1287 EMSG(_(e_listreq));
1288 return FAIL;
1289 }
1290
1291 i = list_len(l);
1292 if (semicolon == 0 && var_count < i)
1293 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001294 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 return FAIL;
1296 }
1297 if (var_count - semicolon > i)
1298 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001299 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001300 return FAIL;
1301 }
1302
1303 item = l->lv_first;
1304 while (*arg != ']')
1305 {
1306 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001307 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001308 item = item->li_next;
1309 if (arg == NULL)
1310 return FAIL;
1311
1312 arg = skipwhite(arg);
1313 if (*arg == ';')
1314 {
1315 /* Put the rest of the list (may be empty) in the var after ';'.
1316 * Create a new list for this. */
1317 l = list_alloc();
1318 if (l == NULL)
1319 return FAIL;
1320 while (item != NULL)
1321 {
1322 list_append_tv(l, &item->li_tv);
1323 item = item->li_next;
1324 }
1325
1326 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001327 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 ltv.vval.v_list = l;
1329 l->lv_refcount = 1;
1330
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001331 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1332 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001333 clear_tv(&ltv);
1334 if (arg == NULL)
1335 return FAIL;
1336 break;
1337 }
1338 else if (*arg != ',' && *arg != ']')
1339 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001340 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001341 return FAIL;
1342 }
1343 }
1344
1345 return OK;
1346}
1347
1348/*
1349 * Skip over assignable variable "var" or list of variables "[var, var]".
1350 * Used for ":let varvar = expr" and ":for varvar in expr".
1351 * For "[var, var]" increment "*var_count" for each variable.
1352 * for "[var, var; var]" set "semicolon".
1353 * Return NULL for an error.
1354 */
1355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001356skip_var_list(
1357 char_u *arg,
1358 int *var_count,
1359 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001360{
1361 char_u *p, *s;
1362
1363 if (*arg == '[')
1364 {
1365 /* "[var, var]": find the matching ']'. */
1366 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001368 {
1369 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1370 s = skip_var_one(p);
1371 if (s == p)
1372 {
1373 EMSG2(_(e_invarg2), p);
1374 return NULL;
1375 }
1376 ++*var_count;
1377
1378 p = skipwhite(s);
1379 if (*p == ']')
1380 break;
1381 else if (*p == ';')
1382 {
1383 if (*semicolon == 1)
1384 {
1385 EMSG(_("Double ; in list of variables"));
1386 return NULL;
1387 }
1388 *semicolon = 1;
1389 }
1390 else if (*p != ',')
1391 {
1392 EMSG2(_(e_invarg2), p);
1393 return NULL;
1394 }
1395 }
1396 return p + 1;
1397 }
1398 else
1399 return skip_var_one(arg);
1400}
1401
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001402/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001403 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001404 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001405 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001406 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001407skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001409 if (*arg == '@' && arg[1] != NUL)
1410 return arg + 2;
1411 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1412 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413}
1414
Bram Moolenaara7043832005-01-21 11:56:39 +00001415/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 * List variables for hashtab "ht" with prefix "prefix".
1417 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001418 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420list_hashtable_vars(
1421 hashtab_T *ht,
1422 char_u *prefix,
1423 int empty,
1424 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 hashitem_T *hi;
1427 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001428 int todo;
1429
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001430 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001431 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1432 {
1433 if (!HASHITEM_EMPTY(hi))
1434 {
1435 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 di = HI2DI(hi);
1437 if (empty || di->di_tv.v_type != VAR_STRING
1438 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001439 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001440 }
1441 }
1442}
1443
1444/*
1445 * List global variables.
1446 */
1447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001448list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001449{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001450 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001451}
1452
1453/*
1454 * List buffer variables.
1455 */
1456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001458{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001459 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001460 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001461}
1462
1463/*
1464 * List window variables.
1465 */
1466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001467list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001468{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001469 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001470 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001471}
1472
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001473/*
1474 * List tab page variables.
1475 */
1476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001478{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001479 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001480 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001481}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001482
Bram Moolenaara7043832005-01-21 11:56:39 +00001483/*
1484 * List Vim variables.
1485 */
1486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001489 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490}
1491
1492/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001493 * List script-local variables, if there is a script.
1494 */
1495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001497{
1498 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001499 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1500 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001501}
1502
1503/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 * List variables in "arg".
1505 */
1506 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508{
1509 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001510 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001512 char_u *name_start;
1513 char_u *arg_subsc;
1514 char_u *tofree;
1515 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516
1517 while (!ends_excmd(*arg) && !got_int)
1518 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001519 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001521 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001522 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001523 {
1524 emsg_severe = TRUE;
1525 EMSG(_(e_trailing));
1526 break;
1527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 /* get_name_len() takes care of expanding curly braces */
1532 name_start = name = arg;
1533 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1534 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001536 /* This is mainly to keep test 49 working: when expanding
1537 * curly braces fails overrule the exception error message. */
1538 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001540 emsg_severe = TRUE;
1541 EMSG2(_(e_invarg2), arg);
1542 break;
1543 }
1544 error = TRUE;
1545 }
1546 else
1547 {
1548 if (tofree != NULL)
1549 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001550 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001551 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001552 else
1553 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001554 /* handle d.key, l[idx], f(expr) */
1555 arg_subsc = arg;
1556 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001557 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001558 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001559 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001563 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001564 case 'g': list_glob_vars(first); break;
1565 case 'b': list_buf_vars(first); break;
1566 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001567 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001568 case 'v': list_vim_vars(first); break;
1569 case 's': list_script_vars(first); break;
1570 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001571 default:
1572 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001574 }
1575 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001576 {
1577 char_u numbuf[NUMBUFLEN];
1578 char_u *tf;
1579 int c;
1580 char_u *s;
1581
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001582 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583 c = *arg;
1584 *arg = NUL;
1585 list_one_var_a((char_u *)"",
1586 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001587 tv.v_type,
1588 s == NULL ? (char_u *)"" : s,
1589 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590 *arg = c;
1591 vim_free(tf);
1592 }
1593 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 }
1596 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001597
1598 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600
1601 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 }
1603
1604 return arg;
1605}
1606
1607/*
1608 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1609 * Returns a pointer to the char just after the var name.
1610 * Returns NULL if there is an error.
1611 */
1612 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001613ex_let_one(
1614 char_u *arg, /* points to variable name */
1615 typval_T *tv, /* value to assign to variable */
1616 int copy, /* copy value from "tv" */
1617 char_u *endchars, /* valid chars after variable name or NULL */
1618 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619{
1620 int c1;
1621 char_u *name;
1622 char_u *p;
1623 char_u *arg_end = NULL;
1624 int len;
1625 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001626 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001627
1628 /*
1629 * ":let $VAR = expr": Set environment variable.
1630 */
1631 if (*arg == '$')
1632 {
1633 /* Find the end of the name. */
1634 ++arg;
1635 name = arg;
1636 len = get_env_len(&arg);
1637 if (len == 0)
1638 EMSG2(_(e_invarg2), name - 1);
1639 else
1640 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 if (op != NULL && (*op == '+' || *op == '-'))
1642 EMSG2(_(e_letwrong), op);
1643 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001646 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 {
1648 c1 = name[len];
1649 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001650 p = get_tv_string_chk(tv);
1651 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001652 {
1653 int mustfree = FALSE;
1654 char_u *s = vim_getenv(name, &mustfree);
1655
1656 if (s != NULL)
1657 {
1658 p = tofree = concat_str(s, p);
1659 if (mustfree)
1660 vim_free(s);
1661 }
1662 }
1663 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001666 if (STRICMP(name, "HOME") == 0)
1667 init_homedir();
1668 else if (didset_vim && STRICMP(name, "VIM") == 0)
1669 didset_vim = FALSE;
1670 else if (didset_vimruntime
1671 && STRICMP(name, "VIMRUNTIME") == 0)
1672 didset_vimruntime = FALSE;
1673 arg_end = arg;
1674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 }
1678 }
1679 }
1680
1681 /*
1682 * ":let &option = expr": Set option value.
1683 * ":let &l:option = expr": Set local option value.
1684 * ":let &g:option = expr": Set global option value.
1685 */
1686 else if (*arg == '&')
1687 {
1688 /* Find the end of the name. */
1689 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 if (p == NULL || (endchars != NULL
1691 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692 EMSG(_(e_letunexp));
1693 else
1694 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 long n;
1696 int opt_type;
1697 long numval;
1698 char_u *stringval = NULL;
1699 char_u *s;
1700
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701 c1 = *p;
1702 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001704 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001705 s = get_tv_string_chk(tv); /* != NULL if number or string */
1706 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707 {
1708 opt_type = get_option_value(arg, &numval,
1709 &stringval, opt_flags);
1710 if ((opt_type == 1 && *op == '.')
1711 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001714 s = NULL; /* don't set the value */
1715 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 else
1717 {
1718 if (opt_type == 1) /* number */
1719 {
1720 if (*op == '+')
1721 n = numval + n;
1722 else
1723 n = numval - n;
1724 }
1725 else if (opt_type == 0 && stringval != NULL) /* string */
1726 {
1727 s = concat_str(stringval, s);
1728 vim_free(stringval);
1729 stringval = s;
1730 }
1731 }
1732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733 if (s != NULL)
1734 {
1735 set_option_value(arg, n, s, opt_flags);
1736 arg_end = p;
1737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 }
1741 }
1742
1743 /*
1744 * ":let @r = expr": Set register contents.
1745 */
1746 else if (*arg == '@')
1747 {
1748 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 if (op != NULL && (*op == '+' || *op == '-'))
1750 EMSG2(_(e_letwrong), op);
1751 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 EMSG(_(e_letunexp));
1754 else
1755 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 char_u *s;
1758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001759 p = get_tv_string_chk(tv);
1760 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001762 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 if (s != NULL)
1764 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001765 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 vim_free(s);
1767 }
1768 }
1769 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772 arg_end = arg + 1;
1773 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001774 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 }
1776 }
1777
1778 /*
1779 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001780 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001782 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001786 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001787 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001789 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1790 EMSG(_(e_letunexp));
1791 else
1792 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001794 arg_end = p;
1795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 }
1799
1800 else
1801 EMSG2(_(e_invarg2), arg);
1802
1803 return arg_end;
1804}
1805
1806/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001807 * Get an lval: variable, Dict item or List item that can be assigned a value
1808 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1809 * "name.key", "name.key[expr]" etc.
1810 * Indexing only works if "name" is an existing List or Dictionary.
1811 * "name" points to the start of the name.
1812 * If "rettv" is not NULL it points to the value to be assigned.
1813 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1814 * wrong; must end in space or cmd separator.
1815 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001816 * flags:
1817 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001818 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001819 * GLV_NO_AUTOLOAD: do not use script autoloading
1820 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001821 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001822 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001823 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826get_lval(
1827 char_u *name,
1828 typval_T *rettv,
1829 lval_T *lp,
1830 int unlet,
1831 int skip,
1832 int flags, /* GLV_ values */
1833 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001836 char_u *expr_start, *expr_end;
1837 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 dictitem_T *v;
1839 typval_T var1;
1840 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001843 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001844 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001846 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001850
1851 if (skip)
1852 {
1853 /* When skipping just find the end of the name. */
1854 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001855 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 }
1857
1858 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001859 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 if (expr_start != NULL)
1861 {
1862 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001863 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 && *p != '[' && *p != '.')
1865 {
1866 EMSG(_(e_trailing));
1867 return NULL;
1868 }
1869
1870 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1871 if (lp->ll_exp_name == NULL)
1872 {
1873 /* Report an invalid expression in braces, unless the
1874 * expression evaluation has been cancelled due to an
1875 * aborting error, an interrupt, or an exception. */
1876 if (!aborting() && !quiet)
1877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001878 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001879 EMSG2(_(e_invarg2), name);
1880 return NULL;
1881 }
1882 }
1883 lp->ll_name = lp->ll_exp_name;
1884 }
1885 else
1886 lp->ll_name = name;
1887
1888 /* Without [idx] or .key we are done. */
1889 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1890 return p;
1891
1892 cc = *p;
1893 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001894 /* Only pass &ht when we would write to the variable, it prevents autoload
1895 * as well. */
1896 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1897 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898 if (v == NULL && !quiet)
1899 EMSG2(_(e_undefvar), lp->ll_name);
1900 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 if (v == NULL)
1902 return NULL;
1903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 /*
1905 * Loop until no more [idx] or .key is following.
1906 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001908 var1.v_type = VAR_UNKNOWN;
1909 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1913 && !(lp->ll_tv->v_type == VAR_DICT
1914 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 if (!quiet)
1917 EMSG(_("E689: Can only index a List or Dictionary"));
1918 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 if (!quiet)
1923 EMSG(_("E708: [:] must come last"));
1924 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001926
Bram Moolenaar8c711452005-01-14 21:53:12 +00001927 len = -1;
1928 if (*p == '.')
1929 {
1930 key = p + 1;
1931 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1932 ;
1933 if (len == 0)
1934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 if (!quiet)
1936 EMSG(_(e_emptykey));
1937 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001938 }
1939 p = key + len;
1940 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001941 else
1942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001943 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001944 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001945 if (*p == ':')
1946 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001947 else
1948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001949 empty1 = FALSE;
1950 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001952 if (get_tv_string_chk(&var1) == NULL)
1953 {
1954 /* not a number or string */
1955 clear_tv(&var1);
1956 return NULL;
1957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001958 }
1959
1960 /* Optionally get the second index [ :expr]. */
1961 if (*p == ':')
1962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001967 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 if (rettv != NULL && (rettv->v_type != VAR_LIST
1971 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 if (!quiet)
1974 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001975 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 }
1978 p = skipwhite(p + 1);
1979 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 else
1982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1985 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001986 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 if (get_tv_string_chk(&var2) == NULL)
1990 {
1991 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001992 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001993 clear_tv(&var2);
1994 return NULL;
1995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001999 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002001
Bram Moolenaar8c711452005-01-14 21:53:12 +00002002 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 if (!quiet)
2005 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002006 clear_tv(&var1);
2007 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 }
2010
2011 /* Skip to past ']'. */
2012 ++p;
2013 }
2014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 {
2017 if (len == -1)
2018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002019 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002020 key = get_tv_string_chk(&var1); /* is number or string */
2021 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 }
2026 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 lp->ll_list = NULL;
2028 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002029 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002030
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002031 /* When assigning to a scope dictionary check that a function and
2032 * variable name is valid (only variable name unless it is l: or
2033 * g: dictionary). Disallow overwriting a builtin function. */
2034 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002035 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002036 int prevval;
2037 int wrong;
2038
2039 if (len != -1)
2040 {
2041 prevval = key[len];
2042 key[len] = NUL;
2043 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002044 else
2045 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002046 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2047 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002048 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002049 || !valid_varname(key);
2050 if (len != -1)
2051 key[len] = prevval;
2052 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002053 return NULL;
2054 }
2055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002057 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002058 /* Can't add "v:" variable. */
2059 if (lp->ll_dict == &vimvardict)
2060 {
2061 EMSG2(_(e_illvar), name);
2062 return NULL;
2063 }
2064
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002065 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002070 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 }
2073 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002075 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002077 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 p = NULL;
2080 break;
2081 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002082 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002083 else if ((flags & GLV_READ_ONLY) == 0
2084 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002085 {
2086 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002087 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002088 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002089
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002090 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 }
2093 else
2094 {
2095 /*
2096 * Get the number and item for the only or first index of the List.
2097 */
2098 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002100 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002101 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002102 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002103 clear_tv(&var1);
2104
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 lp->ll_list = lp->ll_tv->vval.v_list;
2107 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2108 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002110 if (lp->ll_n1 < 0)
2111 {
2112 lp->ll_n1 = 0;
2113 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2114 }
2115 }
2116 if (lp->ll_li == NULL)
2117 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002118 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002119 if (!quiet)
2120 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 }
2123
2124 /*
2125 * May need to find the item or absolute index for the second
2126 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 * When no index given: "lp->ll_empty2" is TRUE.
2128 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002132 lp->ll_n2 = (long)get_tv_number(&var2);
2133 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002136 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002139 {
2140 if (!quiet)
2141 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002143 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002145 }
2146
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2148 if (lp->ll_n1 < 0)
2149 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2150 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002151 {
2152 if (!quiet)
2153 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002156 }
2157
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 }
2161
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002162 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 return p;
2164}
2165
2166/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002167 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171{
2172 vim_free(lp->ll_exp_name);
2173 vim_free(lp->ll_newkey);
2174}
2175
2176/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002177 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002179 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 */
2181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182set_var_lval(
2183 lval_T *lp,
2184 char_u *endp,
2185 typval_T *rettv,
2186 int copy,
2187 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188{
2189 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002190 listitem_T *ri;
2191 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192
2193 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002195 cc = *endp;
2196 *endp = NUL;
2197 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002199 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002200
Bram Moolenaar79518e22017-02-17 16:31:35 +01002201 /* handle +=, -= and .= */
2202 di = NULL;
2203 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2204 &tv, &di, TRUE, FALSE) == OK)
2205 {
2206 if ((di == NULL
2207 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2208 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2209 FALSE)))
2210 && tv_op(&tv, rettv, op) == OK)
2211 set_var(lp->ll_name, &tv, FALSE);
2212 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002215 else
2216 set_var(lp->ll_name, rettv, copy);
2217 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002219 else if (tv_check_lock(lp->ll_newkey == NULL
2220 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002221 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002222 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 else if (lp->ll_range)
2224 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002225 listitem_T *ll_li = lp->ll_li;
2226 int ll_n1 = lp->ll_n1;
2227
2228 /*
2229 * Check whether any of the list items is locked
2230 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002231 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002232 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002233 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002234 return;
2235 ri = ri->li_next;
2236 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2237 break;
2238 ll_li = ll_li->li_next;
2239 ++ll_n1;
2240 }
2241
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 /*
2243 * Assign the List values to the list items.
2244 */
2245 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002246 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 if (op != NULL && *op != '=')
2248 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2249 else
2250 {
2251 clear_tv(&lp->ll_li->li_tv);
2252 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2253 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 ri = ri->li_next;
2255 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2256 break;
2257 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002260 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 ri = NULL;
2263 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002264 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002265 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 lp->ll_li = lp->ll_li->li_next;
2267 ++lp->ll_n1;
2268 }
2269 if (ri != NULL)
2270 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002271 else if (lp->ll_empty2
2272 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 : lp->ll_n1 != lp->ll_n2)
2274 EMSG(_("E711: List value has not enough items"));
2275 }
2276 else
2277 {
2278 /*
2279 * Assign to a List or Dictionary item.
2280 */
2281 if (lp->ll_newkey != NULL)
2282 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002283 if (op != NULL && *op != '=')
2284 {
2285 EMSG2(_(e_letwrong), op);
2286 return;
2287 }
2288
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002290 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 if (di == NULL)
2292 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002293 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2294 {
2295 vim_free(di);
2296 return;
2297 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002299 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 else if (op != NULL && *op != '=')
2301 {
2302 tv_op(lp->ll_tv, rettv, op);
2303 return;
2304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002307
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 /*
2309 * Assign the value to the variable or list item.
2310 */
2311 if (copy)
2312 copy_tv(rettv, lp->ll_tv);
2313 else
2314 {
2315 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002316 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 }
2319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320}
2321
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2324 * Returns OK or FAIL.
2325 */
2326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002327tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002329 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u numbuf[NUMBUFLEN];
2331 char_u *s;
2332
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002333 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2334 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2335 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 switch (tv1->v_type)
2338 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002339 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 case VAR_DICT:
2341 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002342 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002343 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002344 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002345 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 break;
2347
2348 case VAR_LIST:
2349 if (*op != '+' || tv2->v_type != VAR_LIST)
2350 break;
2351 /* List += List */
2352 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2353 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2354 return OK;
2355
2356 case VAR_NUMBER:
2357 case VAR_STRING:
2358 if (tv2->v_type == VAR_LIST)
2359 break;
2360 if (*op == '+' || *op == '-')
2361 {
2362 /* nr += nr or nr -= nr*/
2363 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002364#ifdef FEAT_FLOAT
2365 if (tv2->v_type == VAR_FLOAT)
2366 {
2367 float_T f = n;
2368
2369 if (*op == '+')
2370 f += tv2->vval.v_float;
2371 else
2372 f -= tv2->vval.v_float;
2373 clear_tv(tv1);
2374 tv1->v_type = VAR_FLOAT;
2375 tv1->vval.v_float = f;
2376 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002378#endif
2379 {
2380 if (*op == '+')
2381 n += get_tv_number(tv2);
2382 else
2383 n -= get_tv_number(tv2);
2384 clear_tv(tv1);
2385 tv1->v_type = VAR_NUMBER;
2386 tv1->vval.v_number = n;
2387 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 }
2389 else
2390 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002391 if (tv2->v_type == VAR_FLOAT)
2392 break;
2393
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 /* str .= str */
2395 s = get_tv_string(tv1);
2396 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2397 clear_tv(tv1);
2398 tv1->v_type = VAR_STRING;
2399 tv1->vval.v_string = s;
2400 }
2401 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002403 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002404#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002405 {
2406 float_T f;
2407
2408 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2409 && tv2->v_type != VAR_NUMBER
2410 && tv2->v_type != VAR_STRING))
2411 break;
2412 if (tv2->v_type == VAR_FLOAT)
2413 f = tv2->vval.v_float;
2414 else
2415 f = get_tv_number(tv2);
2416 if (*op == '+')
2417 tv1->vval.v_float += f;
2418 else
2419 tv1->vval.v_float -= f;
2420 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002421#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002422 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 }
2424 }
2425
2426 EMSG2(_(e_letwrong), op);
2427 return FAIL;
2428}
2429
2430/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 * Evaluate the expression used in a ":for var in expr" command.
2432 * "arg" points to "var".
2433 * Set "*errp" to TRUE for an error, FALSE otherwise;
2434 * Return a pointer that holds the info. Null when there is an error.
2435 */
2436 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002437eval_for_line(
2438 char_u *arg,
2439 int *errp,
2440 char_u **nextcmdp,
2441 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002442{
Bram Moolenaar33570922005-01-25 22:26:29 +00002443 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002445 typval_T tv;
2446 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447
2448 *errp = TRUE; /* default: there is an error */
2449
Bram Moolenaar33570922005-01-25 22:26:29 +00002450 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 if (fi == NULL)
2452 return NULL;
2453
2454 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2455 if (expr == NULL)
2456 return fi;
2457
2458 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002459 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002460 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002461 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002462 return fi;
2463 }
2464
2465 if (skip)
2466 ++emsg_skip;
2467 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2468 {
2469 *errp = FALSE;
2470 if (!skip)
2471 {
2472 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002473 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002474 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002476 clear_tv(&tv);
2477 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002478 else if (l == NULL)
2479 {
2480 /* a null list is like an empty list: do nothing */
2481 clear_tv(&tv);
2482 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 else
2484 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002485 /* No need to increment the refcount, it's already set for the
2486 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487 fi->fi_list = l;
2488 list_add_watch(l, &fi->fi_lw);
2489 fi->fi_lw.lw_item = l->lv_first;
2490 }
2491 }
2492 }
2493 if (skip)
2494 --emsg_skip;
2495
2496 return fi;
2497}
2498
2499/*
2500 * Use the first item in a ":for" list. Advance to the next.
2501 * Assign the values to the variable (list). "arg" points to the first one.
2502 * Return TRUE when a valid item was found, FALSE when at end of list or
2503 * something wrong.
2504 */
2505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002506next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002508 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002511
2512 item = fi->fi_lw.lw_item;
2513 if (item == NULL)
2514 result = FALSE;
2515 else
2516 {
2517 fi->fi_lw.lw_item = item->li_next;
2518 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2519 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2520 }
2521 return result;
2522}
2523
2524/*
2525 * Free the structure used to store info used by ":for".
2526 */
2527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002528free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529{
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002532 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002533 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002535 list_unref(fi->fi_list);
2536 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002537 vim_free(fi);
2538}
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2541
2542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543set_context_for_expression(
2544 expand_T *xp,
2545 char_u *arg,
2546 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 int got_eq = FALSE;
2549 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002550 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 if (cmdidx == CMD_let)
2553 {
2554 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002555 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 {
2557 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002558 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002559 {
2560 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002561 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002562 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002563 break;
2564 }
2565 return;
2566 }
2567 }
2568 else
2569 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2570 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 while ((xp->xp_pattern = vim_strpbrk(arg,
2572 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2573 {
2574 c = *xp->xp_pattern;
2575 if (c == '&')
2576 {
2577 c = xp->xp_pattern[1];
2578 if (c == '&')
2579 {
2580 ++xp->xp_pattern;
2581 xp->xp_context = cmdidx != CMD_let || got_eq
2582 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2583 }
2584 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002587 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2588 xp->xp_pattern += 2;
2589
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 else if (c == '$')
2593 {
2594 /* environment variable */
2595 xp->xp_context = EXPAND_ENV_VARS;
2596 }
2597 else if (c == '=')
2598 {
2599 got_eq = TRUE;
2600 xp->xp_context = EXPAND_EXPRESSION;
2601 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002602 else if (c == '#'
2603 && xp->xp_context == EXPAND_EXPRESSION)
2604 {
2605 /* Autoload function/variable contains '#'. */
2606 break;
2607 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002608 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 && xp->xp_context == EXPAND_FUNCTIONS
2610 && vim_strchr(xp->xp_pattern, '(') == NULL)
2611 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002612 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 break;
2614 }
2615 else if (cmdidx != CMD_let || got_eq)
2616 {
2617 if (c == '"') /* string */
2618 {
2619 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2620 if (c == '\\' && xp->xp_pattern[1] != NUL)
2621 ++xp->xp_pattern;
2622 xp->xp_context = EXPAND_NOTHING;
2623 }
2624 else if (c == '\'') /* literal string */
2625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2628 /* skip */ ;
2629 xp->xp_context = EXPAND_NOTHING;
2630 }
2631 else if (c == '|')
2632 {
2633 if (xp->xp_pattern[1] == '|')
2634 {
2635 ++xp->xp_pattern;
2636 xp->xp_context = EXPAND_EXPRESSION;
2637 }
2638 else
2639 xp->xp_context = EXPAND_COMMANDS;
2640 }
2641 else
2642 xp->xp_context = EXPAND_EXPRESSION;
2643 }
2644 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645 /* Doesn't look like something valid, expand as an expression
2646 * anyway. */
2647 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 arg = xp->xp_pattern;
2649 if (*arg != NUL)
2650 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2651 /* skip */ ;
2652 }
2653 xp->xp_pattern = arg;
2654}
2655
2656#endif /* FEAT_CMDL_COMPL */
2657
2658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 * ":unlet[!] var1 ... " command.
2660 */
2661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002662ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002664 ex_unletlock(eap, eap->arg, 0);
2665}
2666
2667/*
2668 * ":lockvar" and ":unlockvar" commands
2669 */
2670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002671ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002672{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002674 int deep = 2;
2675
2676 if (eap->forceit)
2677 deep = -1;
2678 else if (vim_isdigit(*arg))
2679 {
2680 deep = getdigits(&arg);
2681 arg = skipwhite(arg);
2682 }
2683
2684 ex_unletlock(eap, arg, deep);
2685}
2686
2687/*
2688 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2689 */
2690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002691ex_unletlock(
2692 exarg_T *eap,
2693 char_u *argstart,
2694 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002695{
2696 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002699 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 do
2702 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002703 if (*arg == '$')
2704 {
2705 char_u *name = ++arg;
2706
2707 if (get_env_len(&arg) == 0)
2708 {
2709 EMSG2(_(e_invarg2), name - 1);
2710 return;
2711 }
2712 vim_unsetenv(name);
2713 arg = skipwhite(arg);
2714 continue;
2715 }
2716
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002718 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002719 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lv.ll_name == NULL)
2721 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002722 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (name_end != NULL)
2726 {
2727 emsg_severe = TRUE;
2728 EMSG(_(e_trailing));
2729 }
2730 if (!(eap->skip || error))
2731 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 break;
2733 }
2734
2735 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002736 {
2737 if (eap->cmdidx == CMD_unlet)
2738 {
2739 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2740 error = TRUE;
2741 }
2742 else
2743 {
2744 if (do_lock_var(&lv, name_end, deep,
2745 eap->cmdidx == CMD_lockvar) == FAIL)
2746 error = TRUE;
2747 }
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!eap->skip)
2751 clear_lval(&lv);
2752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 arg = skipwhite(name_end);
2754 } while (!ends_excmd(*arg));
2755
2756 eap->nextcmd = check_nextcmd(arg);
2757}
2758
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002760do_unlet_var(
2761 lval_T *lp,
2762 char_u *name_end,
2763 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 int ret = OK;
2766 int cc;
2767
2768 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 cc = *name_end;
2771 *name_end = NUL;
2772
2773 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002774 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002778 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002779 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002780 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002781 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002782 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 else if (lp->ll_range)
2784 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002785 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002786 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002787 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002788
2789 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2790 {
2791 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002792 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002793 return FAIL;
2794 ll_li = li;
2795 ++ll_n1;
2796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797
2798 /* Delete a range of List items. */
2799 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2800 {
2801 li = lp->ll_li->li_next;
2802 listitem_remove(lp->ll_list, lp->ll_li);
2803 lp->ll_li = li;
2804 ++lp->ll_n1;
2805 }
2806 }
2807 else
2808 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 /* unlet a List item. */
2811 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 }
2816
2817 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818}
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820/*
2821 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002822 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002825do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 hashtab_T *ht;
2828 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002829 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002830 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002831 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 ht = find_var_ht(name, &varname);
2834 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002837 if (d == NULL)
2838 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839 if (ht == &globvarht)
2840 d = &globvardict;
2841 else if (ht == &compat_hashtab)
2842 d = &vimvardict;
2843 else
2844 {
2845 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2846 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2847 }
2848 if (d == NULL)
2849 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002850 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 return FAIL;
2852 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002853 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002854 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002855 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002856 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002857 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002858 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002859 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002860 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002861 || var_check_ro(di->di_flags, name, FALSE)
2862 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002863 return FAIL;
2864
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002865 delete_var(ht, hi);
2866 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869 if (forceit)
2870 return OK;
2871 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 return FAIL;
2873}
2874
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002875/*
2876 * Lock or unlock variable indicated by "lp".
2877 * "deep" is the levels to go (-1 for unlimited);
2878 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2879 */
2880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002881do_lock_var(
2882 lval_T *lp,
2883 char_u *name_end,
2884 int deep,
2885 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886{
2887 int ret = OK;
2888 int cc;
2889 dictitem_T *di;
2890
2891 if (deep == 0) /* nothing to do */
2892 return OK;
2893
2894 if (lp->ll_tv == NULL)
2895 {
2896 cc = *name_end;
2897 *name_end = NUL;
2898
2899 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002900 di = find_var(lp->ll_name, NULL, TRUE);
2901 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002903 else if ((di->di_flags & DI_FLAGS_FIX)
2904 && di->di_tv.v_type != VAR_DICT
2905 && di->di_tv.v_type != VAR_LIST)
2906 /* For historic reasons this error is not given for a list or dict.
2907 * E.g., the b: dict could be locked/unlocked. */
2908 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909 else
2910 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002911 if (lock)
2912 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002914 di->di_flags &= ~DI_FLAGS_LOCK;
2915 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 }
2917 *name_end = cc;
2918 }
2919 else if (lp->ll_range)
2920 {
2921 listitem_T *li = lp->ll_li;
2922
2923 /* (un)lock a range of List items. */
2924 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2925 {
2926 item_lock(&li->li_tv, deep, lock);
2927 li = li->li_next;
2928 ++lp->ll_n1;
2929 }
2930 }
2931 else if (lp->ll_list != NULL)
2932 /* (un)lock a List item. */
2933 item_lock(&lp->ll_li->li_tv, deep, lock);
2934 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002935 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 item_lock(&lp->ll_di->di_tv, deep, lock);
2937
2938 return ret;
2939}
2940
2941/*
2942 * Lock or unlock an item. "deep" is nr of levels to go.
2943 */
2944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946{
2947 static int recurse = 0;
2948 list_T *l;
2949 listitem_T *li;
2950 dict_T *d;
2951 hashitem_T *hi;
2952 int todo;
2953
2954 if (recurse >= DICT_MAXNEST)
2955 {
2956 EMSG(_("E743: variable nested too deep for (un)lock"));
2957 return;
2958 }
2959 if (deep == 0)
2960 return;
2961 ++recurse;
2962
2963 /* lock/unlock the item itself */
2964 if (lock)
2965 tv->v_lock |= VAR_LOCKED;
2966 else
2967 tv->v_lock &= ~VAR_LOCKED;
2968
2969 switch (tv->v_type)
2970 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002971 case VAR_UNKNOWN:
2972 case VAR_NUMBER:
2973 case VAR_STRING:
2974 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002975 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976 case VAR_FLOAT:
2977 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002978 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002979 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002980 break;
2981
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002982 case VAR_LIST:
2983 if ((l = tv->vval.v_list) != NULL)
2984 {
2985 if (lock)
2986 l->lv_lock |= VAR_LOCKED;
2987 else
2988 l->lv_lock &= ~VAR_LOCKED;
2989 if (deep < 0 || deep > 1)
2990 /* recursive: lock/unlock the items the List contains */
2991 for (li = l->lv_first; li != NULL; li = li->li_next)
2992 item_lock(&li->li_tv, deep - 1, lock);
2993 }
2994 break;
2995 case VAR_DICT:
2996 if ((d = tv->vval.v_dict) != NULL)
2997 {
2998 if (lock)
2999 d->dv_lock |= VAR_LOCKED;
3000 else
3001 d->dv_lock &= ~VAR_LOCKED;
3002 if (deep < 0 || deep > 1)
3003 {
3004 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003005 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003006 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3007 {
3008 if (!HASHITEM_EMPTY(hi))
3009 {
3010 --todo;
3011 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3012 }
3013 }
3014 }
3015 }
3016 }
3017 --recurse;
3018}
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3021/*
3022 * Delete all "menutrans_" variables.
3023 */
3024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003025del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003028 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
Bram Moolenaar33570922005-01-25 22:26:29 +00003030 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003031 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003033 {
3034 if (!HASHITEM_EMPTY(hi))
3035 {
3036 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3038 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003039 }
3040 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003041 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043#endif
3044
3045#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3046
3047/*
3048 * Local string buffer for the next two functions to store a variable name
3049 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3050 * get_user_var_name().
3051 */
3052
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003053static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055static char_u *varnamebuf = NULL;
3056static int varnamebuflen = 0;
3057
3058/*
3059 * Function to concatenate a prefix and a variable name.
3060 */
3061 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003062cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 int len;
3065
3066 len = (int)STRLEN(name) + 3;
3067 if (len > varnamebuflen)
3068 {
3069 vim_free(varnamebuf);
3070 len += 10; /* some additional space */
3071 varnamebuf = alloc(len);
3072 if (varnamebuf == NULL)
3073 {
3074 varnamebuflen = 0;
3075 return NULL;
3076 }
3077 varnamebuflen = len;
3078 }
3079 *varnamebuf = prefix;
3080 varnamebuf[1] = ':';
3081 STRCPY(varnamebuf + 2, name);
3082 return varnamebuf;
3083}
3084
3085/*
3086 * Function given to ExpandGeneric() to obtain the list of user defined
3087 * (global/buffer/window/built-in) variable names.
3088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003090get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003092 static long_u gdone;
3093 static long_u bdone;
3094 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003095 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003096 static int vidx;
3097 static hashitem_T *hi;
3098 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003102 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003103 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003105
3106 /* Global variables */
3107 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003109 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003111 else
3112 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003113 while (HASHITEM_EMPTY(hi))
3114 ++hi;
3115 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3116 return cat_prefix_varname('g', hi->hi_key);
3117 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003119
3120 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003121 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003124 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003126 else
3127 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003128 while (HASHITEM_EMPTY(hi))
3129 ++hi;
3130 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003132
3133 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003134 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003137 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003139 else
3140 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003141 while (HASHITEM_EMPTY(hi))
3142 ++hi;
3143 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003145
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003146 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003147 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003148 if (tdone < ht->ht_used)
3149 {
3150 if (tdone++ == 0)
3151 hi = ht->ht_array;
3152 else
3153 ++hi;
3154 while (HASHITEM_EMPTY(hi))
3155 ++hi;
3156 return cat_prefix_varname('t', hi->hi_key);
3157 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003158
Bram Moolenaar33570922005-01-25 22:26:29 +00003159 /* v: variables */
3160 if (vidx < VV_LEN)
3161 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaard23a8232018-02-10 18:45:26 +01003163 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 varnamebuflen = 0;
3165 return NULL;
3166}
3167
3168#endif /* FEAT_CMDL_COMPL */
3169
3170/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003171 * Return TRUE if "pat" matches "text".
3172 * Does not use 'cpo' and always uses 'magic'.
3173 */
3174 static int
3175pattern_match(char_u *pat, char_u *text, int ic)
3176{
3177 int matches = FALSE;
3178 char_u *save_cpo;
3179 regmatch_T regmatch;
3180
3181 /* avoid 'l' flag in 'cpoptions' */
3182 save_cpo = p_cpo;
3183 p_cpo = (char_u *)"";
3184 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3185 if (regmatch.regprog != NULL)
3186 {
3187 regmatch.rm_ic = ic;
3188 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3189 vim_regfree(regmatch.regprog);
3190 }
3191 p_cpo = save_cpo;
3192 return matches;
3193}
3194
3195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003197 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3199 */
3200
3201/*
3202 * Handle zero level expression.
3203 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003204 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003205 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 * Return OK or FAIL.
3207 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209eval0(
3210 char_u *arg,
3211 typval_T *rettv,
3212 char_u **nextcmd,
3213 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 int ret;
3216 char_u *p;
3217
3218 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003219 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (ret == FAIL || !ends_excmd(*p))
3221 {
3222 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003223 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 /*
3225 * Report the invalid expression unless the expression evaluation has
3226 * been cancelled due to an aborting error, an interrupt, or an
3227 * exception.
3228 */
3229 if (!aborting())
3230 EMSG2(_(e_invexpr2), arg);
3231 ret = FAIL;
3232 }
3233 if (nextcmd != NULL)
3234 *nextcmd = check_nextcmd(p);
3235
3236 return ret;
3237}
3238
3239/*
3240 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003241 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 *
3243 * "arg" must point to the first non-white of the expression.
3244 * "arg" is advanced to the next non-white after the recognized expression.
3245 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003246 * Note: "rettv.v_lock" is not set.
3247 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * Return OK or FAIL.
3249 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003251eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252{
3253 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 /*
3257 * Get the first variable.
3258 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003259 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 return FAIL;
3261
3262 if ((*arg)[0] == '?')
3263 {
3264 result = FALSE;
3265 if (evaluate)
3266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 int error = FALSE;
3268
3269 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003271 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272 if (error)
3273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275
3276 /*
3277 * Get the second variable.
3278 */
3279 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 return FAIL;
3282
3283 /*
3284 * Check for the ":".
3285 */
3286 if ((*arg)[0] != ':')
3287 {
3288 EMSG(_("E109: Missing ':' after '?'"));
3289 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003290 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292 }
3293
3294 /*
3295 * Get the third variable.
3296 */
3297 *arg = skipwhite(*arg + 1);
3298 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3299 {
3300 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 return FAIL;
3303 }
3304 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003305 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307
3308 return OK;
3309}
3310
3311/*
3312 * Handle first level expression:
3313 * expr2 || expr2 || expr2 logical OR
3314 *
3315 * "arg" must point to the first non-white of the expression.
3316 * "arg" is advanced to the next non-white after the recognized expression.
3317 *
3318 * Return OK or FAIL.
3319 */
3320 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 long result;
3325 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328 /*
3329 * Get the first variable.
3330 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 return FAIL;
3333
3334 /*
3335 * Repeat until there is no following "||".
3336 */
3337 first = TRUE;
3338 result = FALSE;
3339 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3340 {
3341 if (evaluate && first)
3342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003343 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003346 if (error)
3347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 first = FALSE;
3349 }
3350
3351 /*
3352 * Get the second variable.
3353 */
3354 *arg = skipwhite(*arg + 2);
3355 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3356 return FAIL;
3357
3358 /*
3359 * Compute the result.
3360 */
3361 if (evaluate && !result)
3362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003363 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003365 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003366 if (error)
3367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 if (evaluate)
3370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003371 rettv->v_type = VAR_NUMBER;
3372 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 }
3375
3376 return OK;
3377}
3378
3379/*
3380 * Handle second level expression:
3381 * expr3 && expr3 && expr3 logical AND
3382 *
3383 * "arg" must point to the first non-white of the expression.
3384 * "arg" is advanced to the next non-white after the recognized expression.
3385 *
3386 * Return OK or FAIL.
3387 */
3388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
Bram Moolenaar33570922005-01-25 22:26:29 +00003391 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 long result;
3393 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /*
3397 * Get the first variable.
3398 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 return FAIL;
3401
3402 /*
3403 * Repeat until there is no following "&&".
3404 */
3405 first = TRUE;
3406 result = TRUE;
3407 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3408 {
3409 if (evaluate && first)
3410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003414 if (error)
3415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 first = FALSE;
3417 }
3418
3419 /*
3420 * Get the second variable.
3421 */
3422 *arg = skipwhite(*arg + 2);
3423 if (eval4(arg, &var2, evaluate && result) == FAIL)
3424 return FAIL;
3425
3426 /*
3427 * Compute the result.
3428 */
3429 if (evaluate && result)
3430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003431 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003434 if (error)
3435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 if (evaluate)
3438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 rettv->v_type = VAR_NUMBER;
3440 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 }
3443
3444 return OK;
3445}
3446
3447/*
3448 * Handle third level expression:
3449 * var1 == var2
3450 * var1 =~ var2
3451 * var1 != var2
3452 * var1 !~ var2
3453 * var1 > var2
3454 * var1 >= var2
3455 * var1 < var2
3456 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003457 * var1 is var2
3458 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 *
3460 * "arg" must point to the first non-white of the expression.
3461 * "arg" is advanced to the next non-white after the recognized expression.
3462 *
3463 * Return OK or FAIL.
3464 */
3465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003466eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
Bram Moolenaar33570922005-01-25 22:26:29 +00003468 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 char_u *p;
3470 int i;
3471 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003472 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 /*
3477 * Get the first variable.
3478 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003479 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 return FAIL;
3481
3482 p = *arg;
3483 switch (p[0])
3484 {
3485 case '=': if (p[1] == '=')
3486 type = TYPE_EQUAL;
3487 else if (p[1] == '~')
3488 type = TYPE_MATCH;
3489 break;
3490 case '!': if (p[1] == '=')
3491 type = TYPE_NEQUAL;
3492 else if (p[1] == '~')
3493 type = TYPE_NOMATCH;
3494 break;
3495 case '>': if (p[1] != '=')
3496 {
3497 type = TYPE_GREATER;
3498 len = 1;
3499 }
3500 else
3501 type = TYPE_GEQUAL;
3502 break;
3503 case '<': if (p[1] != '=')
3504 {
3505 type = TYPE_SMALLER;
3506 len = 1;
3507 }
3508 else
3509 type = TYPE_SEQUAL;
3510 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003511 case 'i': if (p[1] == 's')
3512 {
3513 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3514 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003515 i = p[len];
3516 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003517 {
3518 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3519 type_is = TRUE;
3520 }
3521 }
3522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524
3525 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003526 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 */
3528 if (type != TYPE_UNKNOWN)
3529 {
3530 /* extra question mark appended: ignore case */
3531 if (p[len] == '?')
3532 {
3533 ic = TRUE;
3534 ++len;
3535 }
3536 /* extra '#' appended: match case */
3537 else if (p[len] == '#')
3538 {
3539 ic = FALSE;
3540 ++len;
3541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else
3544 ic = p_ic;
3545
3546 /*
3547 * Get the second variable.
3548 */
3549 *arg = skipwhite(p + len);
3550 if (eval5(arg, &var2, evaluate) == FAIL)
3551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003552 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 return FAIL;
3554 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003555 if (evaluate)
3556 {
3557 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3558
3559 clear_tv(&var2);
3560 return ret;
3561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563
3564 return OK;
3565}
3566
3567/*
3568 * Handle fourth level expression:
3569 * + number addition
3570 * - number subtraction
3571 * . string concatenation
3572 *
3573 * "arg" must point to the first non-white of the expression.
3574 * "arg" is advanced to the next non-white after the recognized expression.
3575 *
3576 * Return OK or FAIL.
3577 */
3578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
Bram Moolenaar33570922005-01-25 22:26:29 +00003581 typval_T var2;
3582 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003584 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003585#ifdef FEAT_FLOAT
3586 float_T f1 = 0, f2 = 0;
3587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *s1, *s2;
3589 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3590 char_u *p;
3591
3592 /*
3593 * Get the first variable.
3594 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003595 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 return FAIL;
3597
3598 /*
3599 * Repeat computing, until no '+', '-' or '.' is following.
3600 */
3601 for (;;)
3602 {
3603 op = **arg;
3604 if (op != '+' && op != '-' && op != '.')
3605 break;
3606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003607 if ((op != '+' || rettv->v_type != VAR_LIST)
3608#ifdef FEAT_FLOAT
3609 && (op == '.' || rettv->v_type != VAR_FLOAT)
3610#endif
3611 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 {
3613 /* For "list + ...", an illegal use of the first operand as
3614 * a number cannot be determined before evaluating the 2nd
3615 * operand: if this is also a list, all is ok.
3616 * For "something . ...", "something - ..." or "non-list + ...",
3617 * we know that the first operand needs to be a string or number
3618 * without evaluating the 2nd operand. So check before to avoid
3619 * side effects after an error. */
3620 if (evaluate && get_tv_string_chk(rettv) == NULL)
3621 {
3622 clear_tv(rettv);
3623 return FAIL;
3624 }
3625 }
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 /*
3628 * Get the second variable.
3629 */
3630 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003631 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 return FAIL;
3635 }
3636
3637 if (evaluate)
3638 {
3639 /*
3640 * Compute the result.
3641 */
3642 if (op == '.')
3643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003644 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3645 s2 = get_tv_string_buf_chk(&var2, buf2);
3646 if (s2 == NULL) /* type error ? */
3647 {
3648 clear_tv(rettv);
3649 clear_tv(&var2);
3650 return FAIL;
3651 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003652 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 clear_tv(rettv);
3654 rettv->v_type = VAR_STRING;
3655 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003657 else if (op == '+' && rettv->v_type == VAR_LIST
3658 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003659 {
3660 /* concatenate Lists */
3661 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3662 &var3) == FAIL)
3663 {
3664 clear_tv(rettv);
3665 clear_tv(&var2);
3666 return FAIL;
3667 }
3668 clear_tv(rettv);
3669 *rettv = var3;
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 else
3672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003673 int error = FALSE;
3674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003675#ifdef FEAT_FLOAT
3676 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003678 f1 = rettv->vval.v_float;
3679 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003681 else
3682#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003683 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003684 n1 = get_tv_number_chk(rettv, &error);
3685 if (error)
3686 {
3687 /* This can only happen for "list + non-list". For
3688 * "non-list + ..." or "something - ...", we returned
3689 * before evaluating the 2nd operand. */
3690 clear_tv(rettv);
3691 return FAIL;
3692 }
3693#ifdef FEAT_FLOAT
3694 if (var2.v_type == VAR_FLOAT)
3695 f1 = n1;
3696#endif
3697 }
3698#ifdef FEAT_FLOAT
3699 if (var2.v_type == VAR_FLOAT)
3700 {
3701 f2 = var2.vval.v_float;
3702 n2 = 0;
3703 }
3704 else
3705#endif
3706 {
3707 n2 = get_tv_number_chk(&var2, &error);
3708 if (error)
3709 {
3710 clear_tv(rettv);
3711 clear_tv(&var2);
3712 return FAIL;
3713 }
3714#ifdef FEAT_FLOAT
3715 if (rettv->v_type == VAR_FLOAT)
3716 f2 = n2;
3717#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003720
3721#ifdef FEAT_FLOAT
3722 /* If there is a float on either side the result is a float. */
3723 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3724 {
3725 if (op == '+')
3726 f1 = f1 + f2;
3727 else
3728 f1 = f1 - f2;
3729 rettv->v_type = VAR_FLOAT;
3730 rettv->vval.v_float = f1;
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733#endif
3734 {
3735 if (op == '+')
3736 n1 = n1 + n2;
3737 else
3738 n1 = n1 - n2;
3739 rettv->v_type = VAR_NUMBER;
3740 rettv->vval.v_number = n1;
3741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746 return OK;
3747}
3748
3749/*
3750 * Handle fifth level expression:
3751 * * number multiplication
3752 * / number division
3753 * % number modulo
3754 *
3755 * "arg" must point to the first non-white of the expression.
3756 * "arg" is advanced to the next non-white after the recognized expression.
3757 *
3758 * Return OK or FAIL.
3759 */
3760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761eval6(
3762 char_u **arg,
3763 typval_T *rettv,
3764 int evaluate,
3765 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003769 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003770#ifdef FEAT_FLOAT
3771 int use_float = FALSE;
3772 float_T f1 = 0, f2;
3773#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 /*
3777 * Get the first variable.
3778 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003779 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781
3782 /*
3783 * Repeat computing, until no '*', '/' or '%' is following.
3784 */
3785 for (;;)
3786 {
3787 op = **arg;
3788 if (op != '*' && op != '/' && op != '%')
3789 break;
3790
3791 if (evaluate)
3792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003793#ifdef FEAT_FLOAT
3794 if (rettv->v_type == VAR_FLOAT)
3795 {
3796 f1 = rettv->vval.v_float;
3797 use_float = TRUE;
3798 n1 = 0;
3799 }
3800 else
3801#endif
3802 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003804 if (error)
3805 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807 else
3808 n1 = 0;
3809
3810 /*
3811 * Get the second variable.
3812 */
3813 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003814 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return FAIL;
3816
3817 if (evaluate)
3818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003819#ifdef FEAT_FLOAT
3820 if (var2.v_type == VAR_FLOAT)
3821 {
3822 if (!use_float)
3823 {
3824 f1 = n1;
3825 use_float = TRUE;
3826 }
3827 f2 = var2.vval.v_float;
3828 n2 = 0;
3829 }
3830 else
3831#endif
3832 {
3833 n2 = get_tv_number_chk(&var2, &error);
3834 clear_tv(&var2);
3835 if (error)
3836 return FAIL;
3837#ifdef FEAT_FLOAT
3838 if (use_float)
3839 f2 = n2;
3840#endif
3841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 /*
3844 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003845 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003847#ifdef FEAT_FLOAT
3848 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003850 if (op == '*')
3851 f1 = f1 * f2;
3852 else if (op == '/')
3853 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003854# ifdef VMS
3855 /* VMS crashes on divide by zero, work around it */
3856 if (f2 == 0.0)
3857 {
3858 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003859 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003860 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003861 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003862 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003863 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003864 }
3865 else
3866 f1 = f1 / f2;
3867# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 /* We rely on the floating point library to handle divide
3869 * by zero to result in "inf" and not a crash. */
3870 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003871# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003874 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00003875 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003876 return FAIL;
3877 }
3878 rettv->v_type = VAR_FLOAT;
3879 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003884 if (op == '*')
3885 n1 = n1 * n2;
3886 else if (op == '/')
3887 {
3888 if (n2 == 0) /* give an error message? */
3889 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003890 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003891 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003892 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003893 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003894 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003895 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003896 }
3897 else
3898 n1 = n1 / n2;
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003901 {
3902 if (n2 == 0) /* give an error message? */
3903 n1 = 0;
3904 else
3905 n1 = n1 % n2;
3906 }
3907 rettv->v_type = VAR_NUMBER;
3908 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 }
3912
3913 return OK;
3914}
3915
3916/*
3917 * Handle sixth level expression:
3918 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003919 * "string" string constant
3920 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * &option-name option value
3922 * @r register contents
3923 * identifier variable value
3924 * function() function call
3925 * $VAR environment variable
3926 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003927 * [expr, expr] List
3928 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 *
3930 * Also handle:
3931 * ! in front logical NOT
3932 * - in front unary minus
3933 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003934 * trailing [] subscript in String or List
3935 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 *
3937 * "arg" must point to the first non-white of the expression.
3938 * "arg" is advanced to the next non-white after the recognized expression.
3939 *
3940 * Return OK or FAIL.
3941 */
3942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003943eval7(
3944 char_u **arg,
3945 typval_T *rettv,
3946 int evaluate,
3947 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003949 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int len;
3951 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 char_u *start_leader, *end_leader;
3953 int ret = OK;
3954 char_u *alias;
3955
3956 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003963 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 */
3965 start_leader = *arg;
3966 while (**arg == '!' || **arg == '-' || **arg == '+')
3967 *arg = skipwhite(*arg + 1);
3968 end_leader = *arg;
3969
3970 switch (**arg)
3971 {
3972 /*
3973 * Number constant.
3974 */
3975 case '0':
3976 case '1':
3977 case '2':
3978 case '3':
3979 case '4':
3980 case '5':
3981 case '6':
3982 case '7':
3983 case '8':
3984 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003985 {
3986#ifdef FEAT_FLOAT
3987 char_u *p = skipdigits(*arg + 1);
3988 int get_float = FALSE;
3989
3990 /* We accept a float when the format matches
3991 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003992 * strict to avoid backwards compatibility problems.
3993 * Don't look for a float after the "." operator, so that
3994 * ":let vers = 1.2.3" doesn't fail. */
3995 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003997 get_float = TRUE;
3998 p = skipdigits(p + 2);
3999 if (*p == 'e' || *p == 'E')
4000 {
4001 ++p;
4002 if (*p == '-' || *p == '+')
4003 ++p;
4004 if (!vim_isdigit(*p))
4005 get_float = FALSE;
4006 else
4007 p = skipdigits(p + 1);
4008 }
4009 if (ASCII_ISALPHA(*p) || *p == '.')
4010 get_float = FALSE;
4011 }
4012 if (get_float)
4013 {
4014 float_T f;
4015
4016 *arg += string2float(*arg, &f);
4017 if (evaluate)
4018 {
4019 rettv->v_type = VAR_FLOAT;
4020 rettv->vval.v_float = f;
4021 }
4022 }
4023 else
4024#endif
4025 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004026 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004027 *arg += len;
4028 if (evaluate)
4029 {
4030 rettv->v_type = VAR_NUMBER;
4031 rettv->vval.v_number = n;
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037 /*
4038 * String constant: "string".
4039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 break;
4042
4043 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004047 break;
4048
4049 /*
4050 * List: [expr, expr]
4051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 break;
4054
4055 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004056 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 * Dictionary: {key: val, key: val}
4058 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004059 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4060 if (ret == NOTDONE)
4061 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004062 break;
4063
4064 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004065 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004067 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 break;
4069
4070 /*
4071 * Environment variable: $VAR.
4072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 break;
4075
4076 /*
4077 * Register contents: @r.
4078 */
4079 case '@': ++*arg;
4080 if (evaluate)
4081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004083 rettv->vval.v_string = get_reg_contents(**arg,
4084 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 if (**arg != NUL)
4087 ++*arg;
4088 break;
4089
4090 /*
4091 * nested expression: (expression).
4092 */
4093 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (**arg == ')')
4096 ++*arg;
4097 else if (ret == OK)
4098 {
4099 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 ret = FAIL;
4102 }
4103 break;
4104
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 break;
4107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004108
4109 if (ret == NOTDONE)
4110 {
4111 /*
4112 * Must be a variable or function name.
4113 * Can also be a curly-braces kind of name: {expr}.
4114 */
4115 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004116 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 if (alias != NULL)
4118 s = alias;
4119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004120 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 ret = FAIL;
4122 else
4123 {
4124 if (**arg == '(') /* recursive! */
4125 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004126 partial_T *partial;
4127
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004128 if (!evaluate)
4129 check_vars(s, len);
4130
Bram Moolenaar8c711452005-01-14 21:53:12 +00004131 /* If "s" is the name of a variable of type VAR_FUNC
4132 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004133 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004134
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004135 /* Need to make a copy, in case evaluating the arguments makes
4136 * the name invalid. */
4137 s = vim_strsave(s);
4138 if (s == NULL)
4139 ret = FAIL;
4140 else
4141 /* Invoke the function. */
4142 ret = get_func_tv(s, len, rettv, arg,
4143 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4144 &len, evaluate, partial, NULL);
4145 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004146
4147 /* If evaluate is FALSE rettv->v_type was not set in
4148 * get_func_tv, but it's needed in handle_subscript() to parse
4149 * what follows. So set it here. */
4150 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4151 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004152 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004153 rettv->v_type = VAR_FUNC;
4154 }
4155
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 /* Stop the expression evaluation when immediately
4157 * aborting on error, or when an interrupt occurred or
4158 * an exception was thrown but not caught. */
4159 if (aborting())
4160 {
4161 if (ret == OK)
4162 clear_tv(rettv);
4163 ret = FAIL;
4164 }
4165 }
4166 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004167 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004168 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004169 {
4170 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004171 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004173 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004174 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004175 }
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 *arg = skipwhite(*arg);
4178
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004179 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4180 * expr(expr). */
4181 if (ret == OK)
4182 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 /*
4185 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4186 */
4187 if (ret == OK && evaluate && end_leader > start_leader)
4188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004190 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004191#ifdef FEAT_FLOAT
4192 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004194 if (rettv->v_type == VAR_FLOAT)
4195 f = rettv->vval.v_float;
4196 else
4197#endif
4198 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 clear_tv(rettv);
4202 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 else
4205 {
4206 while (end_leader > start_leader)
4207 {
4208 --end_leader;
4209 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004210 {
4211#ifdef FEAT_FLOAT
4212 if (rettv->v_type == VAR_FLOAT)
4213 f = !f;
4214 else
4215#endif
4216 val = !val;
4217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004219 {
4220#ifdef FEAT_FLOAT
4221 if (rettv->v_type == VAR_FLOAT)
4222 f = -f;
4223 else
4224#endif
4225 val = -val;
4226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004228#ifdef FEAT_FLOAT
4229 if (rettv->v_type == VAR_FLOAT)
4230 {
4231 clear_tv(rettv);
4232 rettv->vval.v_float = f;
4233 }
4234 else
4235#endif
4236 {
4237 clear_tv(rettv);
4238 rettv->v_type = VAR_NUMBER;
4239 rettv->vval.v_number = val;
4240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243
4244 return ret;
4245}
4246
4247/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004248 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4249 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004250 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4251 */
4252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004253eval_index(
4254 char_u **arg,
4255 typval_T *rettv,
4256 int evaluate,
4257 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258{
4259 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004260 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 long len = -1;
4263 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004265 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266
Bram Moolenaara03f2332016-02-06 18:09:59 +01004267 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004268 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004269 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004270 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004271 if (verbose)
4272 EMSG(_("E695: Cannot index a Funcref"));
4273 return FAIL;
4274 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004275#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004276 if (verbose)
4277 EMSG(_(e_float_as_string));
4278 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004279#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004280 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004281 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004282 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004283 if (verbose)
4284 EMSG(_("E909: Cannot index a special variable"));
4285 return FAIL;
4286 case VAR_UNKNOWN:
4287 if (evaluate)
4288 return FAIL;
4289 /* FALLTHROUGH */
4290
4291 case VAR_STRING:
4292 case VAR_NUMBER:
4293 case VAR_LIST:
4294 case VAR_DICT:
4295 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004297
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004298 init_tv(&var1);
4299 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004301 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302 /*
4303 * dict.name
4304 */
4305 key = *arg + 1;
4306 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4307 ;
4308 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004309 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004311 }
4312 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004313 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 /*
4315 * something[idx]
4316 *
4317 * Get the (first) variable from inside the [].
4318 */
4319 *arg = skipwhite(*arg + 1);
4320 if (**arg == ':')
4321 empty1 = TRUE;
4322 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4323 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4325 {
4326 /* not a number or string */
4327 clear_tv(&var1);
4328 return FAIL;
4329 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004330
4331 /*
4332 * Get the second variable from inside the [:].
4333 */
4334 if (**arg == ':')
4335 {
4336 range = TRUE;
4337 *arg = skipwhite(*arg + 1);
4338 if (**arg == ']')
4339 empty2 = TRUE;
4340 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (!empty1)
4343 clear_tv(&var1);
4344 return FAIL;
4345 }
4346 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4347 {
4348 /* not a number or string */
4349 if (!empty1)
4350 clear_tv(&var1);
4351 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 return FAIL;
4353 }
4354 }
4355
4356 /* Check for the ']'. */
4357 if (**arg != ']')
4358 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004359 if (verbose)
4360 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004361 clear_tv(&var1);
4362 if (range)
4363 clear_tv(&var2);
4364 return FAIL;
4365 }
4366 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004367 }
4368
4369 if (evaluate)
4370 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004371 n1 = 0;
4372 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 n1 = get_tv_number(&var1);
4375 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004376 }
4377 if (range)
4378 {
4379 if (empty2)
4380 n2 = -1;
4381 else
4382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 n2 = get_tv_number(&var2);
4384 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004385 }
4386 }
4387
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004390 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004391 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004392 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004393 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004394 case VAR_SPECIAL:
4395 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004396 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004397 break; /* not evaluating, skipping over subscript */
4398
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004399 case VAR_NUMBER:
4400 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004402 len = (long)STRLEN(s);
4403 if (range)
4404 {
4405 /* The resulting variable is a substring. If the indexes
4406 * are out of range the result is empty. */
4407 if (n1 < 0)
4408 {
4409 n1 = len + n1;
4410 if (n1 < 0)
4411 n1 = 0;
4412 }
4413 if (n2 < 0)
4414 n2 = len + n2;
4415 else if (n2 >= len)
4416 n2 = len;
4417 if (n1 >= len || n2 < 0 || n1 > n2)
4418 s = NULL;
4419 else
4420 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4421 }
4422 else
4423 {
4424 /* The resulting variable is a string of a single
4425 * character. If the index is too big or negative the
4426 * result is empty. */
4427 if (n1 >= len || n1 < 0)
4428 s = NULL;
4429 else
4430 s = vim_strnsave(s + n1, 1);
4431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 clear_tv(rettv);
4433 rettv->v_type = VAR_STRING;
4434 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004435 break;
4436
4437 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439 if (n1 < 0)
4440 n1 = len + n1;
4441 if (!empty1 && (n1 < 0 || n1 >= len))
4442 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004443 /* For a range we allow invalid values and return an empty
4444 * list. A list index out of range is an error. */
4445 if (!range)
4446 {
4447 if (verbose)
4448 EMSGN(_(e_listidx), n1);
4449 return FAIL;
4450 }
4451 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 }
4453 if (range)
4454 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004455 list_T *l;
4456 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457
4458 if (n2 < 0)
4459 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004460 else if (n2 >= len)
4461 n2 = len - 1;
4462 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004463 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 l = list_alloc();
4465 if (l == NULL)
4466 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 n1 <= n2; ++n1)
4469 {
4470 if (list_append_tv(l, &item->li_tv) == FAIL)
4471 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004472 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 return FAIL;
4474 }
4475 item = item->li_next;
4476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004478 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004479 }
4480 else
4481 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004482 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
4484 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 }
4486 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004487
4488 case VAR_DICT:
4489 if (range)
4490 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004491 if (verbose)
4492 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 if (len == -1)
4494 clear_tv(&var1);
4495 return FAIL;
4496 }
4497 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004498 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499
4500 if (len == -1)
4501 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004502 key = get_tv_string_chk(&var1);
4503 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 clear_tv(&var1);
4506 return FAIL;
4507 }
4508 }
4509
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004510 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004511
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004512 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004513 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 if (len == -1)
4515 clear_tv(&var1);
4516 if (item == NULL)
4517 return FAIL;
4518
4519 copy_tv(&item->di_tv, &var1);
4520 clear_tv(rettv);
4521 *rettv = var1;
4522 }
4523 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 }
4525 }
4526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004527 return OK;
4528}
4529
4530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 * Get an option value.
4532 * "arg" points to the '&' or '+' before the option name.
4533 * "arg" is advanced to character after the option name.
4534 * Return OK or FAIL.
4535 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004537get_option_tv(
4538 char_u **arg,
4539 typval_T *rettv, /* when NULL, only check if option exists */
4540 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
4542 char_u *option_end;
4543 long numval;
4544 char_u *stringval;
4545 int opt_type;
4546 int c;
4547 int working = (**arg == '+'); /* has("+option") */
4548 int ret = OK;
4549 int opt_flags;
4550
4551 /*
4552 * Isolate the option name and find its value.
4553 */
4554 option_end = find_option_end(arg, &opt_flags);
4555 if (option_end == NULL)
4556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004557 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 EMSG2(_("E112: Option name missing: %s"), *arg);
4559 return FAIL;
4560 }
4561
4562 if (!evaluate)
4563 {
4564 *arg = option_end;
4565 return OK;
4566 }
4567
4568 c = *option_end;
4569 *option_end = NUL;
4570 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 if (opt_type == -3) /* invalid name */
4574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 EMSG2(_("E113: Unknown option: %s"), *arg);
4577 ret = FAIL;
4578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 if (opt_type == -2) /* hidden string option */
4582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 rettv->v_type = VAR_STRING;
4584 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 else if (opt_type == -1) /* hidden number option */
4587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 rettv->v_type = VAR_NUMBER;
4589 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 else if (opt_type == 1) /* number option */
4592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 else /* string option */
4597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 rettv->v_type = VAR_STRING;
4599 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 }
4602 else if (working && (opt_type == -2 || opt_type == -1))
4603 ret = FAIL;
4604
4605 *option_end = c; /* put back for error messages */
4606 *arg = option_end;
4607
4608 return ret;
4609}
4610
4611/*
4612 * Allocate a variable for a string constant.
4613 * Return OK or FAIL.
4614 */
4615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004616get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 char_u *p;
4619 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int extra = 0;
4621
4622 /*
4623 * Find the end of the string, skipping backslashed characters.
4624 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004625 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 if (*p == '\\' && p[1] != NUL)
4628 {
4629 ++p;
4630 /* A "\<x>" form occupies at least 4 characters, and produces up
4631 * to 6 characters: reserve space for 2 extra */
4632 if (*p == '<')
4633 extra += 2;
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636
4637 if (*p != '"')
4638 {
4639 EMSG2(_("E114: Missing quote: %s"), *arg);
4640 return FAIL;
4641 }
4642
4643 /* If only parsing, set *arg and return here */
4644 if (!evaluate)
4645 {
4646 *arg = p + 1;
4647 return OK;
4648 }
4649
4650 /*
4651 * Copy the string into allocated memory, handling backslashed
4652 * characters.
4653 */
4654 name = alloc((unsigned)(p - *arg + extra));
4655 if (name == NULL)
4656 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 rettv->v_type = VAR_STRING;
4658 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
4662 if (*p == '\\')
4663 {
4664 switch (*++p)
4665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 case 'b': *name++ = BS; ++p; break;
4667 case 'e': *name++ = ESC; ++p; break;
4668 case 'f': *name++ = FF; ++p; break;
4669 case 'n': *name++ = NL; ++p; break;
4670 case 'r': *name++ = CAR; ++p; break;
4671 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 case 'X': /* hex: "\x1", "\x12" */
4674 case 'x':
4675 case 'u': /* Unicode: "\u0023" */
4676 case 'U':
4677 if (vim_isxdigit(p[1]))
4678 {
4679 int n, nr;
4680 int c = toupper(*p);
4681
4682 if (c == 'X')
4683 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004684 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004686 else
4687 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 nr = 0;
4689 while (--n >= 0 && vim_isxdigit(p[1]))
4690 {
4691 ++p;
4692 nr = (nr << 4) + hex2nr(*p);
4693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#ifdef FEAT_MBYTE
4696 /* For "\u" store the number according to
4697 * 'encoding'. */
4698 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 else
4701#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 break;
4705
4706 /* octal: "\1", "\12", "\123" */
4707 case '0':
4708 case '1':
4709 case '2':
4710 case '3':
4711 case '4':
4712 case '5':
4713 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 case '7': *name = *p++ - '0';
4715 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 *name = (*name << 3) + *p++ - '0';
4718 if (*p >= '0' && *p <= '7')
4719 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004721 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 break;
4723
4724 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004725 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (extra != 0)
4727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004728 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 break;
4730 }
4731 /* FALLTHROUGH */
4732
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 break;
4735 }
4736 }
4737 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004742 if (*p != NUL) /* just in case */
4743 ++p;
4744 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 return OK;
4747}
4748
4749/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 * Return OK or FAIL.
4752 */
4753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004754get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004757 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004758 int reduce = 0;
4759
4760 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004761 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004762 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004763 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004765 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004768 break;
4769 ++reduce;
4770 ++p;
4771 }
4772 }
4773
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004777 return FAIL;
4778 }
4779
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004781 if (!evaluate)
4782 {
4783 *arg = p + 1;
4784 return OK;
4785 }
4786
4787 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004789 */
4790 str = alloc((unsigned)((p - *arg) - reduce));
4791 if (str == NULL)
4792 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 rettv->v_type = VAR_STRING;
4794 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004795
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004801 break;
4802 ++p;
4803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004805 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004807 *arg = p + 1;
4808
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004809 return OK;
4810}
4811
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004812/*
4813 * Return the function name of the partial.
4814 */
4815 char_u *
4816partial_name(partial_T *pt)
4817{
4818 if (pt->pt_name != NULL)
4819 return pt->pt_name;
4820 return pt->pt_func->uf_name;
4821}
4822
Bram Moolenaarddecc252016-04-06 22:59:37 +02004823 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004824partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004825{
4826 int i;
4827
4828 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004829 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004830 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004832 if (pt->pt_name != NULL)
4833 {
4834 func_unref(pt->pt_name);
4835 vim_free(pt->pt_name);
4836 }
4837 else
4838 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004839 vim_free(pt);
4840}
4841
4842/*
4843 * Unreference a closure: decrement the reference count and free it when it
4844 * becomes zero.
4845 */
4846 void
4847partial_unref(partial_T *pt)
4848{
4849 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004850 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004851}
4852
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004853static int tv_equal_recurse_limit;
4854
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004855 static int
4856func_equal(
4857 typval_T *tv1,
4858 typval_T *tv2,
4859 int ic) /* ignore case */
4860{
4861 char_u *s1, *s2;
4862 dict_T *d1, *d2;
4863 int a1, a2;
4864 int i;
4865
4866 /* empty and NULL function name considered the same */
4867 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004868 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004869 if (s1 != NULL && *s1 == NUL)
4870 s1 = NULL;
4871 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004872 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004873 if (s2 != NULL && *s2 == NUL)
4874 s2 = NULL;
4875 if (s1 == NULL || s2 == NULL)
4876 {
4877 if (s1 != s2)
4878 return FALSE;
4879 }
4880 else if (STRCMP(s1, s2) != 0)
4881 return FALSE;
4882
4883 /* empty dict and NULL dict is different */
4884 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4885 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4886 if (d1 == NULL || d2 == NULL)
4887 {
4888 if (d1 != d2)
4889 return FALSE;
4890 }
4891 else if (!dict_equal(d1, d2, ic, TRUE))
4892 return FALSE;
4893
4894 /* empty list and no list considered the same */
4895 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4896 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4897 if (a1 != a2)
4898 return FALSE;
4899 for (i = 0; i < a1; ++i)
4900 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4901 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4902 return FALSE;
4903
4904 return TRUE;
4905}
4906
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004907/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004908 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004909 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004911 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004912 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004913tv_equal(
4914 typval_T *tv1,
4915 typval_T *tv2,
4916 int ic, /* ignore case */
4917 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004918{
4919 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004921 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004922 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004923
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004924 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004925 * recursiveness to a limit. We guess they are equal then.
4926 * A fixed limit has the problem of still taking an awful long time.
4927 * Reduce the limit every time running into it. That should work fine for
4928 * deeply linked structures that are not recursively linked and catch
4929 * recursiveness quickly. */
4930 if (!recursive)
4931 tv_equal_recurse_limit = 1000;
4932 if (recursive_cnt >= tv_equal_recurse_limit)
4933 {
4934 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004935 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004936 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004937
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004938 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4939 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004940 if ((tv1->v_type == VAR_FUNC
4941 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4942 && (tv2->v_type == VAR_FUNC
4943 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4944 {
4945 ++recursive_cnt;
4946 r = func_equal(tv1, tv2, ic);
4947 --recursive_cnt;
4948 return r;
4949 }
4950
4951 if (tv1->v_type != tv2->v_type)
4952 return FALSE;
4953
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004954 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004955 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004956 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004957 ++recursive_cnt;
4958 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4959 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004960 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004961
4962 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004963 ++recursive_cnt;
4964 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
4965 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004966 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004967
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004968 case VAR_NUMBER:
4969 return tv1->vval.v_number == tv2->vval.v_number;
4970
4971 case VAR_STRING:
4972 s1 = get_tv_string_buf(tv1, buf1);
4973 s2 = get_tv_string_buf(tv2, buf2);
4974 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004975
4976 case VAR_SPECIAL:
4977 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004978
4979 case VAR_FLOAT:
4980#ifdef FEAT_FLOAT
4981 return tv1->vval.v_float == tv2->vval.v_float;
4982#endif
4983 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01004985 return tv1->vval.v_job == tv2->vval.v_job;
4986#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01004987 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004988#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01004989 return tv1->vval.v_channel == tv2->vval.v_channel;
4990#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004991 case VAR_FUNC:
4992 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004993 case VAR_UNKNOWN:
4994 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004995 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004996
Bram Moolenaara03f2332016-02-06 18:09:59 +01004997 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
4998 * does not equal anything, not even itself. */
4999 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005000}
5001
5002/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005003 * Return the next (unique) copy ID.
5004 * Used for serializing nested structures.
5005 */
5006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005007get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005008{
5009 current_copyID += COPYID_INC;
5010 return current_copyID;
5011}
5012
5013/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005014 * Garbage collection for lists and dictionaries.
5015 *
5016 * We use reference counts to be able to free most items right away when they
5017 * are no longer used. But for composite items it's possible that it becomes
5018 * unused while the reference count is > 0: When there is a recursive
5019 * reference. Example:
5020 * :let l = [1, 2, 3]
5021 * :let d = {9: l}
5022 * :let l[1] = d
5023 *
5024 * Since this is quite unusual we handle this with garbage collection: every
5025 * once in a while find out which lists and dicts are not referenced from any
5026 * variable.
5027 *
5028 * Here is a good reference text about garbage collection (refers to Python
5029 * but it applies to all reference-counting mechanisms):
5030 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005031 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005032
5033/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005034 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005035 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005036 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005037 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005038 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005039garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005040{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005041 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005042 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005043 buf_T *buf;
5044 win_T *wp;
5045 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005046 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005047 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005048
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005049 if (!testing)
5050 {
5051 /* Only do this once. */
5052 want_garbage_collect = FALSE;
5053 may_garbage_collect = FALSE;
5054 garbage_collect_at_exit = FALSE;
5055 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005056
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005057 /* We advance by two because we add one for items referenced through
5058 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005059 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005060
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005061 /*
5062 * 1. Go through all accessible variables and mark all lists and dicts
5063 * with copyID.
5064 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005065
5066 /* Don't free variables in the previous_funccal list unless they are only
5067 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005068 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005069 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005070
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005071 /* script-local variables */
5072 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005073 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005074
5075 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005076 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005077 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5078 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005079
5080 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005081 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005082 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5083 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005084 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005085 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5086 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005087
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005088 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005089 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5091 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005092 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005093 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005094
5095 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005096 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005097
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005098 /* named functions (matters for closures) */
5099 abort = abort || set_ref_in_functions(copyID);
5100
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005101 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005102 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005103
Bram Moolenaard812df62008-11-09 12:46:09 +00005104 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005106
Bram Moolenaar1dced572012-04-05 16:54:08 +02005107#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005108 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005109#endif
5110
Bram Moolenaardb913952012-06-29 12:54:53 +02005111#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005112 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005113#endif
5114
5115#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005116 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005117#endif
5118
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005119#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005120 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005121 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005122#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005123#ifdef FEAT_NETBEANS_INTG
5124 abort = abort || set_ref_in_nb_channel(copyID);
5125#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005126
Bram Moolenaare3188e22016-05-31 21:13:04 +02005127#ifdef FEAT_TIMERS
5128 abort = abort || set_ref_in_timer(copyID);
5129#endif
5130
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005131#ifdef FEAT_QUICKFIX
5132 abort = abort || set_ref_in_quickfix(copyID);
5133#endif
5134
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005135#ifdef FEAT_TERMINAL
5136 abort = abort || set_ref_in_term(copyID);
5137#endif
5138
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005139 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005140 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005141 /*
5142 * 2. Free lists and dictionaries that are not referenced.
5143 */
5144 did_free = free_unref_items(copyID);
5145
5146 /*
5147 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005148 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005149 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005150 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005151 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 else if (p_verbose > 0)
5153 {
5154 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5155 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005156
5157 return did_free;
5158}
5159
5160/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005161 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005162 */
5163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005164free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005165{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005166 int did_free = FALSE;
5167
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005168 /* Let all "free" functions know that we are here. This means no
5169 * dictionaries, lists, channels or jobs are to be freed, because we will
5170 * do that here. */
5171 in_free_unref_items = TRUE;
5172
5173 /*
5174 * PASS 1: free the contents of the items. We don't free the items
5175 * themselves yet, so that it is possible to decrement refcount counters
5176 */
5177
Bram Moolenaarcd524592016-07-17 14:57:05 +02005178 /* Go through the list of dicts and free items without the copyID. */
5179 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005180
Bram Moolenaarda861d62016-07-17 15:46:27 +02005181 /* Go through the list of lists and free items without the copyID. */
5182 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005183
5184#ifdef FEAT_JOB_CHANNEL
5185 /* Go through the list of jobs and free items without the copyID. This
5186 * must happen before doing channels, because jobs refer to channels, but
5187 * the reference from the channel to the job isn't tracked. */
5188 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5189
5190 /* Go through the list of channels and free items without the copyID. */
5191 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5192#endif
5193
5194 /*
5195 * PASS 2: free the items themselves.
5196 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005197 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005198 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005199
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005200#ifdef FEAT_JOB_CHANNEL
5201 /* Go through the list of jobs and free items without the copyID. This
5202 * must happen before doing channels, because jobs refer to channels, but
5203 * the reference from the channel to the job isn't tracked. */
5204 free_unused_jobs(copyID, COPYID_MASK);
5205
5206 /* Go through the list of channels and free items without the copyID. */
5207 free_unused_channels(copyID, COPYID_MASK);
5208#endif
5209
5210 in_free_unref_items = FALSE;
5211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212 return did_free;
5213}
5214
5215/*
5216 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005217 * "list_stack" is used to add lists to be marked. Can be NULL.
5218 *
5219 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005222set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223{
5224 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005225 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005227 hashtab_T *cur_ht;
5228 ht_stack_T *ht_stack = NULL;
5229 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005231 cur_ht = ht;
5232 for (;;)
5233 {
5234 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005235 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005236 /* Mark each item in the hashtab. If the item contains a hashtab
5237 * it is added to ht_stack, if it contains a list it is added to
5238 * list_stack. */
5239 todo = (int)cur_ht->ht_used;
5240 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5241 if (!HASHITEM_EMPTY(hi))
5242 {
5243 --todo;
5244 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5245 &ht_stack, list_stack);
5246 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248
5249 if (ht_stack == NULL)
5250 break;
5251
5252 /* take an item from the stack */
5253 cur_ht = ht_stack->ht;
5254 tempitem = ht_stack;
5255 ht_stack = ht_stack->prev;
5256 free(tempitem);
5257 }
5258
5259 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260}
5261
5262/*
5263 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005264 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5265 *
5266 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005267 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005269set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005271 listitem_T *li;
5272 int abort = FALSE;
5273 list_T *cur_l;
5274 list_stack_T *list_stack = NULL;
5275 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 cur_l = l;
5278 for (;;)
5279 {
5280 if (!abort)
5281 /* Mark each item in the list. If the item contains a hashtab
5282 * it is added to ht_stack, if it contains a list it is added to
5283 * list_stack. */
5284 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5285 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5286 ht_stack, &list_stack);
5287 if (list_stack == NULL)
5288 break;
5289
5290 /* take an item from the stack */
5291 cur_l = list_stack->list;
5292 tempitem = list_stack;
5293 list_stack = list_stack->prev;
5294 free(tempitem);
5295 }
5296
5297 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298}
5299
5300/*
5301 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 * "list_stack" is used to add lists to be marked. Can be NULL.
5303 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5304 *
5305 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005306 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005308set_ref_in_item(
5309 typval_T *tv,
5310 int copyID,
5311 ht_stack_T **ht_stack,
5312 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005313{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005314 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005316 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005317 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005318 dict_T *dd = tv->vval.v_dict;
5319
Bram Moolenaara03f2332016-02-06 18:09:59 +01005320 if (dd != NULL && dd->dv_copyID != copyID)
5321 {
5322 /* Didn't see this dict yet. */
5323 dd->dv_copyID = copyID;
5324 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005325 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005326 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5327 }
5328 else
5329 {
5330 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5331 if (newitem == NULL)
5332 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005333 else
5334 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005335 newitem->ht = &dd->dv_hashtab;
5336 newitem->prev = *ht_stack;
5337 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005338 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005339 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005340 }
5341 }
5342 else if (tv->v_type == VAR_LIST)
5343 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005344 list_T *ll = tv->vval.v_list;
5345
Bram Moolenaara03f2332016-02-06 18:09:59 +01005346 if (ll != NULL && ll->lv_copyID != copyID)
5347 {
5348 /* Didn't see this list yet. */
5349 ll->lv_copyID = copyID;
5350 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005351 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 abort = set_ref_in_list(ll, copyID, ht_stack);
5353 }
5354 else
5355 {
5356 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005358 if (newitem == NULL)
5359 abort = TRUE;
5360 else
5361 {
5362 newitem->list = ll;
5363 newitem->prev = *list_stack;
5364 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005365 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005366 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005367 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005368 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005369 else if (tv->v_type == VAR_FUNC)
5370 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005371 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005372 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005373 else if (tv->v_type == VAR_PARTIAL)
5374 {
5375 partial_T *pt = tv->vval.v_partial;
5376 int i;
5377
5378 /* A partial does not have a copyID, because it cannot contain itself.
5379 */
5380 if (pt != NULL)
5381 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005382 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005383
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 if (pt->pt_dict != NULL)
5385 {
5386 typval_T dtv;
5387
5388 dtv.v_type = VAR_DICT;
5389 dtv.vval.v_dict = pt->pt_dict;
5390 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5391 }
5392
5393 for (i = 0; i < pt->pt_argc; ++i)
5394 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5395 ht_stack, list_stack);
5396 }
5397 }
5398#ifdef FEAT_JOB_CHANNEL
5399 else if (tv->v_type == VAR_JOB)
5400 {
5401 job_T *job = tv->vval.v_job;
5402 typval_T dtv;
5403
5404 if (job != NULL && job->jv_copyID != copyID)
5405 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005406 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005407 if (job->jv_channel != NULL)
5408 {
5409 dtv.v_type = VAR_CHANNEL;
5410 dtv.vval.v_channel = job->jv_channel;
5411 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5412 }
5413 if (job->jv_exit_partial != NULL)
5414 {
5415 dtv.v_type = VAR_PARTIAL;
5416 dtv.vval.v_partial = job->jv_exit_partial;
5417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5418 }
5419 }
5420 }
5421 else if (tv->v_type == VAR_CHANNEL)
5422 {
5423 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005424 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005425 typval_T dtv;
5426 jsonq_T *jq;
5427 cbq_T *cq;
5428
5429 if (ch != NULL && ch->ch_copyID != copyID)
5430 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005431 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005432 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005433 {
5434 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5435 jq = jq->jq_next)
5436 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5437 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5438 cq = cq->cq_next)
5439 if (cq->cq_partial != NULL)
5440 {
5441 dtv.v_type = VAR_PARTIAL;
5442 dtv.vval.v_partial = cq->cq_partial;
5443 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5444 }
5445 if (ch->ch_part[part].ch_partial != NULL)
5446 {
5447 dtv.v_type = VAR_PARTIAL;
5448 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5449 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5450 }
5451 }
5452 if (ch->ch_partial != NULL)
5453 {
5454 dtv.v_type = VAR_PARTIAL;
5455 dtv.vval.v_partial = ch->ch_partial;
5456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5457 }
5458 if (ch->ch_close_partial != NULL)
5459 {
5460 dtv.v_type = VAR_PARTIAL;
5461 dtv.vval.v_partial = ch->ch_close_partial;
5462 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5463 }
5464 }
5465 }
5466#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005467 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005468}
5469
Bram Moolenaar17a13432016-01-24 14:22:10 +01005470 static char *
5471get_var_special_name(int nr)
5472{
5473 switch (nr)
5474 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005475 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005476 case VVAL_TRUE: return "v:true";
5477 case VVAL_NONE: return "v:none";
5478 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005479 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005480 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005481 return "42";
5482}
5483
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 * Return a string with the string representation of a variable.
5486 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005487 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005488 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005489 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5490 * stings as "string()", otherwise does not put quotes around strings, as
5491 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005492 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5493 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005494 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005496 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005497echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498 typval_T *tv,
5499 char_u **tofree,
5500 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005501 int copyID,
5502 int echo_style,
5503 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005504 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005506 static int recurse = 0;
5507 char_u *r = NULL;
5508
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005511 if (!did_echo_string_emsg)
5512 {
5513 /* Only give this message once for a recursive call to avoid
5514 * flooding the user with errors. And stop iterating over lists
5515 * and dicts. */
5516 did_echo_string_emsg = TRUE;
5517 EMSG(_("E724: variable nested too deep for displaying"));
5518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005519 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005520 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005521 }
5522 ++recurse;
5523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 switch (tv->v_type)
5525 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005526 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005527 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005528 {
5529 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005530 r = tv->vval.v_string;
5531 if (r == NULL)
5532 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005533 }
5534 else
5535 {
5536 *tofree = string_quote(tv->vval.v_string, FALSE);
5537 r = *tofree;
5538 }
5539 break;
5540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005542 if (echo_style)
5543 {
5544 *tofree = NULL;
5545 r = tv->vval.v_string;
5546 }
5547 else
5548 {
5549 *tofree = string_quote(tv->vval.v_string, TRUE);
5550 r = *tofree;
5551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005552 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005553
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005554 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005555 {
5556 partial_T *pt = tv->vval.v_partial;
5557 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005558 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005559 garray_T ga;
5560 int i;
5561 char_u *tf;
5562
5563 ga_init2(&ga, 1, 100);
5564 ga_concat(&ga, (char_u *)"function(");
5565 if (fname != NULL)
5566 {
5567 ga_concat(&ga, fname);
5568 vim_free(fname);
5569 }
5570 if (pt != NULL && pt->pt_argc > 0)
5571 {
5572 ga_concat(&ga, (char_u *)", [");
5573 for (i = 0; i < pt->pt_argc; ++i)
5574 {
5575 if (i > 0)
5576 ga_concat(&ga, (char_u *)", ");
5577 ga_concat(&ga,
5578 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5579 vim_free(tf);
5580 }
5581 ga_concat(&ga, (char_u *)"]");
5582 }
5583 if (pt != NULL && pt->pt_dict != NULL)
5584 {
5585 typval_T dtv;
5586
5587 ga_concat(&ga, (char_u *)", ");
5588 dtv.v_type = VAR_DICT;
5589 dtv.vval.v_dict = pt->pt_dict;
5590 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5591 vim_free(tf);
5592 }
5593 ga_concat(&ga, (char_u *)")");
5594
5595 *tofree = ga.ga_data;
5596 r = *tofree;
5597 break;
5598 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005601 if (tv->vval.v_list == NULL)
5602 {
5603 *tofree = NULL;
5604 r = NULL;
5605 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005606 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5607 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005608 {
5609 *tofree = NULL;
5610 r = (char_u *)"[...]";
5611 }
5612 else
5613 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005614 int old_copyID = tv->vval.v_list->lv_copyID;
5615
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005616 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005617 *tofree = list2string(tv, copyID, restore_copyID);
5618 if (restore_copyID)
5619 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620 r = *tofree;
5621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005622 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005623
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 if (tv->vval.v_dict == NULL)
5626 {
5627 *tofree = NULL;
5628 r = NULL;
5629 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005630 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5631 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005632 {
5633 *tofree = NULL;
5634 r = (char_u *)"{...}";
5635 }
5636 else
5637 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005638 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005639 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005640 *tofree = dict2string(tv, copyID, restore_copyID);
5641 if (restore_copyID)
5642 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643 r = *tofree;
5644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005648 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005649 *tofree = NULL;
5650 r = get_tv_string_buf(tv, numbuf);
5651 break;
5652
Bram Moolenaar835dc632016-02-07 14:27:38 +01005653 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005654 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005655 *tofree = NULL;
5656 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005657 if (composite_val)
5658 {
5659 *tofree = string_quote(r, FALSE);
5660 r = *tofree;
5661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005663
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005664 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005665#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005666 *tofree = NULL;
5667 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5668 r = numbuf;
5669 break;
5670#endif
5671
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005672 case VAR_SPECIAL:
5673 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005674 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005675 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005677
Bram Moolenaar8502c702014-06-17 12:51:16 +02005678 if (--recurse == 0)
5679 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005680 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681}
5682
5683/*
5684 * Return a string with the string representation of a variable.
5685 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5686 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005687 * Does not put quotes around strings, as ":echo" displays values.
5688 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5689 * May return NULL.
5690 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005691 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005692echo_string(
5693 typval_T *tv,
5694 char_u **tofree,
5695 char_u *numbuf,
5696 int copyID)
5697{
5698 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5699}
5700
5701/*
5702 * Return a string with the string representation of a variable.
5703 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5704 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005706 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005709tv2string(
5710 typval_T *tv,
5711 char_u **tofree,
5712 char_u *numbuf,
5713 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005714{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005715 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716}
5717
5718/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005719 * Return string "str" in ' quotes, doubling ' characters.
5720 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005723 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005724string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005725{
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 char_u *p, *r, *s;
5728
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 len = (function ? 13 : 3);
5730 if (str != NULL)
5731 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005732 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005733 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 if (*p == '\'')
5735 ++len;
5736 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 s = r = alloc(len);
5738 if (r != NULL)
5739 {
5740 if (function)
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 r += 10;
5744 }
5745 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 if (str != NULL)
5748 for (p = str; *p != NUL; )
5749 {
5750 if (*p == '\'')
5751 *r++ = '\'';
5752 MB_COPY_CHAR(p, r);
5753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 if (function)
5756 *r++ = ')';
5757 *r++ = NUL;
5758 }
5759 return s;
5760}
5761
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005762#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005763/*
5764 * Convert the string "text" to a floating point number.
5765 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5766 * this always uses a decimal point.
5767 * Returns the length of the text that was consumed.
5768 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770string2float(
5771 char_u *text,
5772 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005773{
5774 char *s = (char *)text;
5775 float_T f;
5776
Bram Moolenaar62473612017-01-08 19:25:40 +01005777 /* MS-Windows does not deal with "inf" and "nan" properly. */
5778 if (STRNICMP(text, "inf", 3) == 0)
5779 {
5780 *value = INFINITY;
5781 return 3;
5782 }
5783 if (STRNICMP(text, "-inf", 3) == 0)
5784 {
5785 *value = -INFINITY;
5786 return 4;
5787 }
5788 if (STRNICMP(text, "nan", 3) == 0)
5789 {
5790 *value = NAN;
5791 return 3;
5792 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005793 f = strtod(s, &s);
5794 *value = f;
5795 return (int)((char_u *)s - text);
5796}
5797#endif
5798
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 * Get the value of an environment variable.
5801 * "arg" is pointing to the '$'. It is advanced to after the name.
5802 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005803 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 */
5805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005806get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 char_u *string = NULL;
5809 int len;
5810 int cc;
5811 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005812 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
5814 ++*arg;
5815 name = *arg;
5816 len = get_env_len(arg);
5817 if (evaluate)
5818 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005819 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005820 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005821
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005822 cc = name[len];
5823 name[len] = NUL;
5824 /* first try vim_getenv(), fast for normal environment vars */
5825 string = vim_getenv(name, &mustfree);
5826 if (string != NULL && *string != NUL)
5827 {
5828 if (!mustfree)
5829 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005831 else
5832 {
5833 if (mustfree)
5834 vim_free(string);
5835
5836 /* next try expanding things like $VIM and ${HOME} */
5837 string = expand_env_save(name - 1);
5838 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005839 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005840 }
5841 name[len] = cc;
5842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 rettv->v_type = VAR_STRING;
5844 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
5846
5847 return OK;
5848}
5849
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005850
5851
5852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005854 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857var2fpos(
5858 typval_T *varp,
5859 int dollar_lnum, /* TRUE when $ is last line */
5860 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005862 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005864 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
Bram Moolenaara5525202006-03-02 22:52:09 +00005866 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005867 if (varp->v_type == VAR_LIST)
5868 {
5869 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005870 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005871 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005872 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005873
5874 l = varp->vval.v_list;
5875 if (l == NULL)
5876 return NULL;
5877
5878 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005879 pos.lnum = list_find_nr(l, 0L, &error);
5880 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005881 return NULL; /* invalid line number */
5882
5883 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005884 pos.col = list_find_nr(l, 1L, &error);
5885 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005886 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005887 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005888
5889 /* We accept "$" for the column number: last column. */
5890 li = list_find(l, 1L);
5891 if (li != NULL && li->li_tv.v_type == VAR_STRING
5892 && li->li_tv.vval.v_string != NULL
5893 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5894 pos.col = len + 1;
5895
Bram Moolenaara5525202006-03-02 22:52:09 +00005896 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005897 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005898 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005899 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005900
Bram Moolenaara5525202006-03-02 22:52:09 +00005901#ifdef FEAT_VIRTUALEDIT
5902 /* Get the virtual offset. Defaults to zero. */
5903 pos.coladd = list_find_nr(l, 2L, &error);
5904 if (error)
5905 pos.coladd = 0;
5906#endif
5907
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005908 return &pos;
5909 }
5910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005911 name = get_tv_string_chk(varp);
5912 if (name == NULL)
5913 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005914 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005916 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5917 {
5918 if (VIsual_active)
5919 return &VIsual;
5920 return &curwin->w_cursor;
5921 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005922 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005924 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5926 return NULL;
5927 return pp;
5928 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005929
5930#ifdef FEAT_VIRTUALEDIT
5931 pos.coladd = 0;
5932#endif
5933
Bram Moolenaar477933c2007-07-17 14:32:23 +00005934 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005935 {
5936 pos.col = 0;
5937 if (name[1] == '0') /* "w0": first visible line */
5938 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005939 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005940 /* In silent Ex mode topline is zero, but that's not a valid line
5941 * number; use one instead. */
5942 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005943 return &pos;
5944 }
5945 else if (name[1] == '$') /* "w$": last visible line */
5946 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005947 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005948 /* In silent Ex mode botline is zero, return zero then. */
5949 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005950 return &pos;
5951 }
5952 }
5953 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005955 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
5957 pos.lnum = curbuf->b_ml.ml_line_count;
5958 pos.col = 0;
5959 }
5960 else
5961 {
5962 pos.lnum = curwin->w_cursor.lnum;
5963 pos.col = (colnr_T)STRLEN(ml_get_curline());
5964 }
5965 return &pos;
5966 }
5967 return NULL;
5968}
5969
5970/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005971 * Convert list in "arg" into a position and optional file number.
5972 * When "fnump" is NULL there is no file number, only 3 items.
5973 * Note that the column is passed on as-is, the caller may want to decrement
5974 * it to use 1 for the first column.
5975 * Return FAIL when conversion is not possible, doesn't check the position for
5976 * validity.
5977 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979list2fpos(
5980 typval_T *arg,
5981 pos_T *posp,
5982 int *fnump,
5983 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005984{
5985 list_T *l = arg->vval.v_list;
5986 long i = 0;
5987 long n;
5988
Bram Moolenaar493c1782014-05-28 14:34:46 +02005989 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5990 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00005991 if (arg->v_type != VAR_LIST
5992 || l == NULL
5993 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005994 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005995 return FAIL;
5996
5997 if (fnump != NULL)
5998 {
5999 n = list_find_nr(l, i++, NULL); /* fnum */
6000 if (n < 0)
6001 return FAIL;
6002 if (n == 0)
6003 n = curbuf->b_fnum; /* current buffer */
6004 *fnump = n;
6005 }
6006
6007 n = list_find_nr(l, i++, NULL); /* lnum */
6008 if (n < 0)
6009 return FAIL;
6010 posp->lnum = n;
6011
6012 n = list_find_nr(l, i++, NULL); /* col */
6013 if (n < 0)
6014 return FAIL;
6015 posp->col = n;
6016
6017#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006018 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006019 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006020 posp->coladd = 0;
6021 else
6022 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006023#endif
6024
Bram Moolenaar493c1782014-05-28 14:34:46 +02006025 if (curswantp != NULL)
6026 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6027
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006028 return OK;
6029}
6030
6031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 * Get the length of an environment variable name.
6033 * Advance "arg" to the first character after the name.
6034 * Return 0 for error.
6035 */
6036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006037get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 char_u *p;
6040 int len;
6041
6042 for (p = *arg; vim_isIDc(*p); ++p)
6043 ;
6044 if (p == *arg) /* no name found */
6045 return 0;
6046
6047 len = (int)(p - *arg);
6048 *arg = p;
6049 return len;
6050}
6051
6052/*
6053 * Get the length of the name of a function or internal variable.
6054 * "arg" is advanced to the first non-white character after the name.
6055 * Return 0 if something is wrong.
6056 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 char_u *p;
6061 int len;
6062
6063 /* Find the end of the name. */
6064 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006065 {
6066 if (*p == ':')
6067 {
6068 /* "s:" is start of "s:var", but "n:" is not and can be used in
6069 * slice "[n:]". Also "xx:" is not a namespace. */
6070 len = (int)(p - *arg);
6071 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6072 || len > 1)
6073 break;
6074 }
6075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 if (p == *arg) /* no name found */
6077 return 0;
6078
6079 len = (int)(p - *arg);
6080 *arg = skipwhite(p);
6081
6082 return len;
6083}
6084
6085/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006086 * Get the length of the name of a variable or function.
6087 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006089 * Return -1 if curly braces expansion failed.
6090 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 * If the name contains 'magic' {}'s, expand them and return the
6092 * expanded name in an allocated string via 'alias' - caller must free.
6093 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095get_name_len(
6096 char_u **arg,
6097 char_u **alias,
6098 int evaluate,
6099 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 char_u *p;
6103 char_u *expr_start;
6104 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 *alias = NULL; /* default to no alias */
6107
6108 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6109 && (*arg)[2] == (int)KE_SNR)
6110 {
6111 /* hard coded <SNR>, already translated */
6112 *arg += 3;
6113 return get_id_len(arg) + 3;
6114 }
6115 len = eval_fname_script(*arg);
6116 if (len > 0)
6117 {
6118 /* literal "<SID>", "s:" or "<SNR>" */
6119 *arg += len;
6120 }
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006123 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006125 p = find_name_end(*arg, &expr_start, &expr_end,
6126 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 if (expr_start != NULL)
6128 {
6129 char_u *temp_string;
6130
6131 if (!evaluate)
6132 {
6133 len += (int)(p - *arg);
6134 *arg = skipwhite(p);
6135 return len;
6136 }
6137
6138 /*
6139 * Include any <SID> etc in the expanded string:
6140 * Thus the -len here.
6141 */
6142 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6143 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006144 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 *alias = temp_string;
6146 *arg = skipwhite(p);
6147 return (int)STRLEN(temp_string);
6148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006151 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 EMSG2(_(e_invexpr2), *arg);
6153
6154 return len;
6155}
6156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157/*
6158 * Find the end of a variable or function name, taking care of magic braces.
6159 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6160 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006161 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006162 * Return a pointer to just after the name. Equal to "arg" if there is no
6163 * valid name.
6164 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006165 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166find_name_end(
6167 char_u *arg,
6168 char_u **expr_start,
6169 char_u **expr_end,
6170 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006172 int mb_nest = 0;
6173 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006175 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 *expr_start = NULL;
6180 *expr_end = NULL;
6181 }
6182
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006183 /* Quick check for valid starting character. */
6184 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6185 return arg;
6186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006187 for (p = arg; *p != NUL
6188 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006190 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006191 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006192 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006194 if (*p == '\'')
6195 {
6196 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006197 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006198 ;
6199 if (*p == NUL)
6200 break;
6201 }
6202 else if (*p == '"')
6203 {
6204 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006205 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006206 if (*p == '\\' && p[1] != NUL)
6207 ++p;
6208 if (*p == NUL)
6209 break;
6210 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006211 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6212 {
6213 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006214 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006215 len = (int)(p - arg);
6216 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006217 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006218 break;
6219 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006221 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006223 if (*p == '[')
6224 ++br_nest;
6225 else if (*p == ']')
6226 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006228
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006229 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006231 if (*p == '{')
6232 {
6233 mb_nest++;
6234 if (expr_start != NULL && *expr_start == NULL)
6235 *expr_start = p;
6236 }
6237 else if (*p == '}')
6238 {
6239 mb_nest--;
6240 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6241 *expr_end = p;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
6245
6246 return p;
6247}
6248
6249/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006250 * Expands out the 'magic' {}'s in a variable/function name.
6251 * Note that this can call itself recursively, to deal with
6252 * constructs like foo{bar}{baz}{bam}
6253 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6254 * "in_start" ^
6255 * "expr_start" ^
6256 * "expr_end" ^
6257 * "in_end" ^
6258 *
6259 * Returns a new allocated string, which the caller must free.
6260 * Returns NULL for failure.
6261 */
6262 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006263make_expanded_name(
6264 char_u *in_start,
6265 char_u *expr_start,
6266 char_u *expr_end,
6267 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006268{
6269 char_u c1;
6270 char_u *retval = NULL;
6271 char_u *temp_result;
6272 char_u *nextcmd = NULL;
6273
6274 if (expr_end == NULL || in_end == NULL)
6275 return NULL;
6276 *expr_start = NUL;
6277 *expr_end = NUL;
6278 c1 = *in_end;
6279 *in_end = NUL;
6280
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006281 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006282 if (temp_result != NULL && nextcmd == NULL)
6283 {
6284 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6285 + (in_end - expr_end) + 1));
6286 if (retval != NULL)
6287 {
6288 STRCPY(retval, in_start);
6289 STRCAT(retval, temp_result);
6290 STRCAT(retval, expr_end + 1);
6291 }
6292 }
6293 vim_free(temp_result);
6294
6295 *in_end = c1; /* put char back for error messages */
6296 *expr_start = '{';
6297 *expr_end = '}';
6298
6299 if (retval != NULL)
6300 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006301 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006302 if (expr_start != NULL)
6303 {
6304 /* Further expansion! */
6305 temp_result = make_expanded_name(retval, expr_start,
6306 expr_end, temp_result);
6307 vim_free(retval);
6308 retval = temp_result;
6309 }
6310 }
6311
6312 return retval;
6313}
6314
6315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006317 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006320eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006322 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6323}
6324
6325/*
6326 * Return TRUE if character "c" can be used as the first character in a
6327 * variable or function name (excluding '{' and '}').
6328 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006331{
6332 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333}
6334
6335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 * Set number v: variable to "val".
6337 */
6338 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006339set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
6343
6344/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006345 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006347 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351}
6352
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006353/*
6354 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006355 * If the String variable has never been set, return an empty string.
6356 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006357 */
6358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006360{
6361 return get_tv_string(&vimvars[idx].vv_tv);
6362}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006365 * Get List v: variable value. Caller must take care of reference count when
6366 * needed.
6367 */
6368 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006369get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006370{
6371 return vimvars[idx].vv_list;
6372}
6373
6374/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006375 * Get Dict v: variable value. Caller must take care of reference count when
6376 * needed.
6377 */
6378 dict_T *
6379get_vim_var_dict(int idx)
6380{
6381 return vimvars[idx].vv_dict;
6382}
6383
6384/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006385 * Set v:char to character "c".
6386 */
6387 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006388set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006389{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006390 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006391
6392#ifdef FEAT_MBYTE
6393 if (has_mbyte)
6394 buf[(*mb_char2bytes)(c, buf)] = NUL;
6395 else
6396#endif
6397 {
6398 buf[0] = c;
6399 buf[1] = NUL;
6400 }
6401 set_vim_var_string(VV_CHAR, buf, -1);
6402}
6403
6404/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006405 * Set v:count to "count" and v:count1 to "count1".
6406 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 */
6408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409set_vcount(
6410 long count,
6411 long count1,
6412 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006414 if (set_prevcount)
6415 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006416 vimvars[VV_COUNT].vv_nr = count;
6417 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418}
6419
6420/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006421 * Save variables that might be changed as a side effect. Used when executing
6422 * a timer callback.
6423 */
6424 void
6425save_vimvars(vimvars_save_T *vvsave)
6426{
6427 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6428 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6429 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6430}
6431
6432/*
6433 * Restore variables saved by save_vimvars().
6434 */
6435 void
6436restore_vimvars(vimvars_save_T *vvsave)
6437{
6438 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6439 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6440 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6441}
6442
6443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 * Set string v: variable to a copy of "val".
6445 */
6446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006447set_vim_var_string(
6448 int idx,
6449 char_u *val,
6450 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
Bram Moolenaara542c682016-01-31 16:28:04 +01006452 clear_tv(&vimvars[idx].vv_di.di_tv);
6453 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006455 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460}
6461
6462/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006463 * Set List v: variable to "val".
6464 */
6465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006466set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006467{
Bram Moolenaara542c682016-01-31 16:28:04 +01006468 clear_tv(&vimvars[idx].vv_di.di_tv);
6469 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006470 vimvars[idx].vv_list = val;
6471 if (val != NULL)
6472 ++val->lv_refcount;
6473}
6474
6475/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006476 * Set Dictionary v: variable to "val".
6477 */
6478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006480{
Bram Moolenaara542c682016-01-31 16:28:04 +01006481 clear_tv(&vimvars[idx].vv_di.di_tv);
6482 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006483 vimvars[idx].vv_dict = val;
6484 if (val != NULL)
6485 {
6486 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006487 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006488 }
6489}
6490
6491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 * Set v:register if needed.
6493 */
6494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 char_u regname;
6498
6499 if (c == 0 || c == ' ')
6500 regname = '"';
6501 else
6502 regname = c;
6503 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 set_vim_var_string(VV_REG, &regname, 1);
6506}
6507
6508/*
6509 * Get or set v:exception. If "oldval" == NULL, return the current value.
6510 * Otherwise, restore the value to "oldval" and return NULL.
6511 * Must always be called in pairs to save and restore v:exception! Does not
6512 * take care of memory allocations.
6513 */
6514 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006515v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 return NULL;
6522}
6523
6524/*
6525 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6526 * Otherwise, restore the value to "oldval" and return NULL.
6527 * Must always be called in pairs to save and restore v:throwpoint! Does not
6528 * take care of memory allocations.
6529 */
6530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006531v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 return NULL;
6538}
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540/*
6541 * Set v:cmdarg.
6542 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6543 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6544 * Must always be called in pairs!
6545 */
6546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 char_u *oldval;
6550 char_u *newval;
6551 unsigned len;
6552
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006554 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006556 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006558 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
6560
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006561 if (eap->force_bin == FORCE_BIN)
6562 len = 6;
6563 else if (eap->force_bin == FORCE_NOBIN)
6564 len = 8;
6565 else
6566 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006567
6568 if (eap->read_edit)
6569 len += 7;
6570
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006571 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006572 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006573# ifdef FEAT_MBYTE
6574 if (eap->force_enc != 0)
6575 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006576 if (eap->bad_char != 0)
6577 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006578# endif
6579
6580 newval = alloc(len + 1);
6581 if (newval == NULL)
6582 return NULL;
6583
6584 if (eap->force_bin == FORCE_BIN)
6585 sprintf((char *)newval, " ++bin");
6586 else if (eap->force_bin == FORCE_NOBIN)
6587 sprintf((char *)newval, " ++nobin");
6588 else
6589 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006590
6591 if (eap->read_edit)
6592 STRCAT(newval, " ++edit");
6593
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006594 if (eap->force_ff != 0)
6595 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006596 eap->force_ff == 'u' ? "unix"
6597 : eap->force_ff == 'd' ? "dos"
6598 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006599#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006600 if (eap->force_enc != 0)
6601 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6602 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006603 if (eap->bad_char == BAD_KEEP)
6604 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6605 else if (eap->bad_char == BAD_DROP)
6606 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6607 else if (eap->bad_char != 0)
6608 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006609#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006610 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006611 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614/*
6615 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006616 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006619get_var_tv(
6620 char_u *name,
6621 int len, /* length of "name" */
6622 typval_T *rettv, /* NULL when only checking existence */
6623 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6624 int verbose, /* may give error message */
6625 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632 /* truncate the name, so that we can use strcmp() */
6633 cc = name[len];
6634 name[len] = NUL;
6635
6636 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 * Check for user-defined variables.
6638 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006639 v = find_var(name, NULL, no_autoload);
6640 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006642 tv = &v->di_tv;
6643 if (dip != NULL)
6644 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006649 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006650 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 ret = FAIL;
6652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006653 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006654 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656 name[len] = cc;
6657
6658 return ret;
6659}
6660
6661/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006662 * Check if variable "name[len]" is a local variable or an argument.
6663 * If so, "*eval_lavars_used" is set to TRUE.
6664 */
6665 static void
6666check_vars(char_u *name, int len)
6667{
6668 int cc;
6669 char_u *varname;
6670 hashtab_T *ht;
6671
6672 if (eval_lavars_used == NULL)
6673 return;
6674
6675 /* truncate the name, so that we can use strcmp() */
6676 cc = name[len];
6677 name[len] = NUL;
6678
6679 ht = find_var_ht(name, &varname);
6680 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6681 {
6682 if (find_var(name, NULL, TRUE) != NULL)
6683 *eval_lavars_used = TRUE;
6684 }
6685
6686 name[len] = cc;
6687}
6688
6689/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006690 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6691 * Also handle function call with Funcref variable: func(expr)
6692 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6693 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006695handle_subscript(
6696 char_u **arg,
6697 typval_T *rettv,
6698 int evaluate, /* do more than finding the end */
6699 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006700{
6701 int ret = OK;
6702 dict_T *selfdict = NULL;
6703 char_u *s;
6704 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006705 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006706
6707 while (ret == OK
6708 && (**arg == '['
6709 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006710 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6711 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006712 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006713 {
6714 if (**arg == '(')
6715 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006716 partial_T *pt = NULL;
6717
Bram Moolenaard9fba312005-06-26 22:34:35 +00006718 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006719 if (evaluate)
6720 {
6721 functv = *rettv;
6722 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006723
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006724 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006725 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006726 {
6727 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006728 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006729 }
6730 else
6731 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006732 }
6733 else
6734 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006735 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006736 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006737 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738
6739 /* Clear the funcref afterwards, so that deleting it while
6740 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006741 if (evaluate)
6742 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006743
6744 /* Stop the expression evaluation when immediately aborting on
6745 * error, or when an interrupt occurred or an exception was thrown
6746 * but not caught. */
6747 if (aborting())
6748 {
6749 if (ret == OK)
6750 clear_tv(rettv);
6751 ret = FAIL;
6752 }
6753 dict_unref(selfdict);
6754 selfdict = NULL;
6755 }
6756 else /* **arg == '[' || **arg == '.' */
6757 {
6758 dict_unref(selfdict);
6759 if (rettv->v_type == VAR_DICT)
6760 {
6761 selfdict = rettv->vval.v_dict;
6762 if (selfdict != NULL)
6763 ++selfdict->dv_refcount;
6764 }
6765 else
6766 selfdict = NULL;
6767 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6768 {
6769 clear_tv(rettv);
6770 ret = FAIL;
6771 }
6772 }
6773 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006774
Bram Moolenaar1d429612016-05-24 15:44:17 +02006775 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6776 * Don't do this when "Func" is already a partial that was bound
6777 * explicitly (pt_auto is FALSE). */
6778 if (selfdict != NULL
6779 && (rettv->v_type == VAR_FUNC
6780 || (rettv->v_type == VAR_PARTIAL
6781 && (rettv->vval.v_partial->pt_auto
6782 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006783 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006784
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006785 dict_unref(selfdict);
6786 return ret;
6787}
6788
6789/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006790 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 * value).
6792 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006793 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006794alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795{
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006797}
6798
6799/*
6800 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * The string "s" must have been allocated, it is consumed.
6802 * Return NULL for out of memory, the variable otherwise.
6803 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006804 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006805alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006809 rettv = alloc_tv();
6810 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 rettv->v_type = VAR_STRING;
6813 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815 else
6816 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818}
6819
6820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006821 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 if (varp != NULL)
6827 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006828 switch (varp->v_type)
6829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006831 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006832 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006833 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834 vim_free(varp->vval.v_string);
6835 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006836 case VAR_PARTIAL:
6837 partial_unref(varp->vval.v_partial);
6838 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006839 case VAR_LIST:
6840 list_unref(varp->vval.v_list);
6841 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006842 case VAR_DICT:
6843 dict_unref(varp->vval.v_dict);
6844 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006845 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006846#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006847 job_unref(varp->vval.v_job);
6848 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006849#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006850 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006852 channel_unref(varp->vval.v_channel);
6853 break;
6854#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006855 case VAR_NUMBER:
6856 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006857 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006858 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006859 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 vim_free(varp);
6862 }
6863}
6864
6865/*
6866 * Free the memory for a variable value and set the value to NULL or 0.
6867 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 if (varp != NULL)
6872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006873 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006875 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006876 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006877 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006878 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01006879 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006880 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006881 case VAR_PARTIAL:
6882 partial_unref(varp->vval.v_partial);
6883 varp->vval.v_partial = NULL;
6884 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006885 case VAR_LIST:
6886 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006887 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006888 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889 case VAR_DICT:
6890 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006891 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006894 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006895 varp->vval.v_number = 0;
6896 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006897 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006898#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006899 varp->vval.v_float = 0.0;
6900 break;
6901#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006902 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006904 job_unref(varp->vval.v_job);
6905 varp->vval.v_job = NULL;
6906#endif
6907 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006908 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006910 channel_unref(varp->vval.v_channel);
6911 varp->vval.v_channel = NULL;
6912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006913 case VAR_UNKNOWN:
6914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006916 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 }
6918}
6919
6920/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006921 * Set the value of a variable to NULL without freeing items.
6922 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006924init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925{
6926 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00006927 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006928}
6929
6930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 * Get the number value of a variable.
6932 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006933 * For incompatible types, return 0.
6934 * get_tv_number_chk() is similar to get_tv_number(), but informs the
6935 * caller of incompatible types: it sets *denote to TRUE if "denote"
6936 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006938 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006939get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006941 int error = FALSE;
6942
6943 return get_tv_number_chk(varp, &error); /* return 0L on error */
6944}
6945
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006946 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006947get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006948{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006949 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006951 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006953 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006954 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006955 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006956#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00006957 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006958 break;
6959#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006961 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006962 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006963 break;
6964 case VAR_STRING:
6965 if (varp->vval.v_string != NULL)
6966 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006967 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006968 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006969 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006970 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006971 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006972 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006973 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006974 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006975 case VAR_SPECIAL:
6976 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
6977 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006978 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006979#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006980 EMSG(_("E910: Using a Job as a Number"));
6981 break;
6982#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006983 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006985 EMSG(_("E913: Using a Channel as a Number"));
6986 break;
6987#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01006988 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01006989 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006992 if (denote == NULL) /* useful for values that must be unsigned */
6993 n = -1;
6994 else
6995 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006996 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997}
6998
Bram Moolenaarf7edf402016-01-19 23:36:15 +01006999#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007001get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007002{
7003 switch (varp->v_type)
7004 {
7005 case VAR_NUMBER:
7006 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007007 case VAR_FLOAT:
7008 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007009 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007010 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007011 EMSG(_("E891: Using a Funcref as a Float"));
7012 break;
7013 case VAR_STRING:
7014 EMSG(_("E892: Using a String as a Float"));
7015 break;
7016 case VAR_LIST:
7017 EMSG(_("E893: Using a List as a Float"));
7018 break;
7019 case VAR_DICT:
7020 EMSG(_("E894: Using a Dictionary as a Float"));
7021 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007022 case VAR_SPECIAL:
7023 EMSG(_("E907: Using a special value as a Float"));
7024 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007025 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007026# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007027 EMSG(_("E911: Using a Job as a Float"));
7028 break;
7029# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007030 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007031# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007032 EMSG(_("E914: Using a Channel as a Float"));
7033 break;
7034# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007035 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007036 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007037 break;
7038 }
7039 return 0;
7040}
7041#endif
7042
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 * Get the string value of a variable.
7045 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007046 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7047 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 * If the String variable has never been set, return an empty string.
7049 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007050 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7051 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007054get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 static char_u mybuf[NUMBUFLEN];
7057
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007058 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007061 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007062get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007064 char_u *res = get_tv_string_buf_chk(varp, buf);
7065
7066 return res != NULL ? res : (char_u *)"";
7067}
7068
Bram Moolenaar7d647822014-04-05 21:28:56 +02007069/*
7070 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7071 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007073get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007074{
7075 static char_u mybuf[NUMBUFLEN];
7076
7077 return get_tv_string_buf_chk(varp, mybuf);
7078}
7079
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007080 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007081get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007082{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007086 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007087 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007088 return buf;
7089 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007090 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007091 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007092 break;
7093 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007094 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095 break;
7096 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007097 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007098 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007099 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007100#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007101 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007102 break;
7103#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007104 case VAR_STRING:
7105 if (varp->vval.v_string != NULL)
7106 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007107 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007108 case VAR_SPECIAL:
7109 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7110 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007111 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007112#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007113 {
7114 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007115 char *status;
7116
7117 if (job == NULL)
7118 return (char_u *)"no process";
7119 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007120 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007121 : "run";
7122# ifdef UNIX
7123 vim_snprintf((char *)buf, NUMBUFLEN,
7124 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007125# elif defined(WIN32)
7126 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007127 "process %ld %s",
7128 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007129 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007130# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007131 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007132 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7133# endif
7134 return buf;
7135 }
7136#endif
7137 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007138 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007140 {
7141 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007142 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007143
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007144 if (channel == NULL)
7145 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7146 else
7147 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007148 "channel %d %s", channel->ch_id, status);
7149 return buf;
7150 }
7151#endif
7152 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007153 case VAR_UNKNOWN:
7154 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
7160/*
7161 * Find variable "name" in the list of variables.
7162 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007164 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007165 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007167 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007168find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007172 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaara7043832005-01-21 11:56:39 +00007174 ht = find_var_ht(name, &varname);
7175 if (htp != NULL)
7176 *htp = ht;
7177 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007179 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7180 if (ret != NULL)
7181 return ret;
7182
7183 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007184 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185}
7186
7187/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007188 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007189 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007191 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007192find_var_in_ht(
7193 hashtab_T *ht,
7194 int htname,
7195 char_u *varname,
7196 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007197{
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 hashitem_T *hi;
7199
7200 if (*varname == NUL)
7201 {
7202 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007203 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007205 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 case 'g': return &globvars_var;
7207 case 'v': return &vimvars_var;
7208 case 'b': return &curbuf->b_bufvar;
7209 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007210 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007211 case 'l': return get_funccal_local_var();
7212 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 }
7214 return NULL;
7215 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007216
7217 hi = hash_find(ht, varname);
7218 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007219 {
7220 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007221 * worked find the variable again. Don't auto-load a script if it was
7222 * loaded already, otherwise it would be loaded every time when
7223 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007224 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007225 {
7226 /* Note: script_autoload() may make "hi" invalid. It must either
7227 * be obtained again or not used. */
7228 if (!script_autoload(varname, FALSE) || aborting())
7229 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007230 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007231 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007232 if (HASHITEM_EMPTY(hi))
7233 return NULL;
7234 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007236}
7237
7238/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007240 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007241 * Set "varname" to the start of name without ':'.
7242 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007243 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007246 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007247 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007248
Bram Moolenaar73627d02015-08-11 15:46:09 +02007249 if (name[0] == NUL)
7250 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 if (name[1] != ':')
7252 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007253 /* The name must not start with a colon or #. */
7254 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 return NULL;
7256 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007257
7258 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007259 hi = hash_find(&compat_hashtab, name);
7260 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007261 return &compat_hashtab;
7262
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007263 ht = get_funccal_local_ht();
7264 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007266 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007269 if (*name == 'g') /* global variable */
7270 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007271 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7272 */
7273 if (vim_strchr(name + 2, ':') != NULL
7274 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007275 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007277 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007279 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007280 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007281 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 if (*name == 'v') /* v: variable */
7283 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007284 if (*name == 'a') /* a: function argument */
7285 return get_funccal_args_ht();
7286 if (*name == 'l') /* l: local function variable */
7287 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (*name == 's' /* script variable */
7289 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7290 return &SCRIPT_VARS(current_SID);
7291 return NULL;
7292}
7293
7294/*
7295 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007296 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 * Returns NULL when it doesn't exist.
7298 */
7299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007304 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (v == NULL)
7306 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
7309
7310/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 * sourcing this script and when executing functions defined in the script.
7313 */
7314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
Bram Moolenaara7043832005-01-21 11:56:39 +00007317 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashtab_T *ht;
7319 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007320
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7322 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007323 /* Re-allocating ga_data means that an ht_array pointing to
7324 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007326 for (i = 1; i <= ga_scripts.ga_len; ++i)
7327 {
7328 ht = &SCRIPT_VARS(i);
7329 if (ht->ht_mask == HT_INIT_SIZE - 1)
7330 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007331 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007333 }
7334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 while (ga_scripts.ga_len < id)
7336 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007337 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007338 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007339 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
7342 }
7343}
7344
7345/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7347 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 */
7349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007350init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007353 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007354 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007355 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007356 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 dict_var->di_tv.vval.v_dict = dict;
7358 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007359 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7361 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
7363
7364/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007365 * Unreference a dictionary initialized by init_var_dict().
7366 */
7367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007368unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007369{
7370 /* Now the dict needs to be freed if no one else is using it, go back to
7371 * normal reference counting. */
7372 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7373 dict_unref(dict);
7374}
7375
7376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 * Frees all allocated variables and the value they contain.
7379 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 */
7381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007382vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007383{
7384 vars_clear_ext(ht, TRUE);
7385}
7386
7387/*
7388 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7389 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007391vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
Bram Moolenaara7043832005-01-21 11:56:39 +00007393 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 hashitem_T *hi;
7395 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007398 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007399 for (hi = ht->ht_array; todo > 0; ++hi)
7400 {
7401 if (!HASHITEM_EMPTY(hi))
7402 {
7403 --todo;
7404
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007406 * ht_array might change then. hash_clear() takes care of it
7407 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 v = HI2DI(hi);
7409 if (free_val)
7410 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007411 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007413 }
7414 }
7415 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007416 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
Bram Moolenaara7043832005-01-21 11:56:39 +00007419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 * Delete a variable from hashtab "ht" at item "hi".
7421 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007427
7428 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 clear_tv(&di->di_tv);
7430 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
7433/*
7434 * List the value of one internal variable.
7435 */
7436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007437list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 char_u *tofree;
7440 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007441 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007442
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007443 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007445 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007446 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447}
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450list_one_var_a(
7451 char_u *prefix,
7452 char_u *name,
7453 int type,
7454 char_u *string,
7455 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
Bram Moolenaar31859182007-08-14 20:41:13 +00007457 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7458 msg_start();
7459 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (name != NULL) /* "a:" vars don't have a name stored */
7461 msg_puts(name);
7462 msg_putchar(' ');
7463 msg_advance(22);
7464 if (type == VAR_NUMBER)
7465 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007466 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007467 msg_putchar('*');
7468 else if (type == VAR_LIST)
7469 {
7470 msg_putchar('[');
7471 if (*string == '[')
7472 ++string;
7473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 else if (type == VAR_DICT)
7475 {
7476 msg_putchar('{');
7477 if (*string == '{')
7478 ++string;
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 else
7481 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007485 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007486 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007487 if (*first)
7488 {
7489 msg_clr_eos();
7490 *first = FALSE;
7491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492}
7493
7494/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007495 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 * If the variable already exists, the value is updated.
7497 * Otherwise the variable is created.
7498 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007500set_var(
7501 char_u *name,
7502 typval_T *tv,
7503 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007509 ht = find_var_ht(name, &varname);
7510 if (ht == NULL || *varname == NUL)
7511 {
7512 EMSG2(_(e_illvar), name);
7513 return;
7514 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007515 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007516
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007517 /* Search in parent scope which is possible to reference from lambda */
7518 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007519 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007520
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007521 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7522 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007523 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007528 if (var_check_ro(v->di_flags, name, FALSE)
7529 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007531
7532 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007533 * Handle setting internal v: variables separately where needed to
7534 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007535 */
7536 if (ht == &vimvarht)
7537 {
7538 if (v->di_tv.v_type == VAR_STRING)
7539 {
7540 vim_free(v->di_tv.vval.v_string);
7541 if (copy || tv->v_type != VAR_STRING)
7542 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7543 else
7544 {
7545 /* Take over the string to avoid an extra alloc/free. */
7546 v->di_tv.vval.v_string = tv->vval.v_string;
7547 tv->vval.v_string = NULL;
7548 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007549 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007551 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007552 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007554 if (STRCMP(varname, "searchforward") == 0)
7555 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007556#ifdef FEAT_SEARCH_EXTRA
7557 else if (STRCMP(varname, "hlsearch") == 0)
7558 {
7559 no_hlsearch = !v->di_tv.vval.v_number;
7560 redraw_all_later(SOME_VALID);
7561 }
7562#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007563 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007564 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007565 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007566 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 }
7568
7569 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
7571 else /* add a new variable */
7572 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007573 /* Can't add "v:" variable. */
7574 if (ht == &vimvarht)
7575 {
7576 EMSG2(_(e_illvar), name);
7577 return;
7578 }
7579
Bram Moolenaar92124a32005-06-17 22:03:40 +00007580 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007581 if (!valid_varname(varname))
7582 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007583
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007584 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7585 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007586 if (v == NULL)
7587 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007591 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 return;
7593 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007594 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007596
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007597 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007599 else
7600 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007602 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605}
7606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007608 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 * Also give an error message.
7610 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007612var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007613{
7614 if (flags & DI_FLAGS_RO)
7615 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007616 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 return TRUE;
7618 }
7619 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7620 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007621 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 return TRUE;
7623 }
7624 return FALSE;
7625}
7626
7627/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007628 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7629 * Also give an error message.
7630 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007633{
7634 if (flags & DI_FLAGS_FIX)
7635 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007636 EMSG2(_("E795: Cannot delete variable %s"),
7637 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007638 return TRUE;
7639 }
7640 return FALSE;
7641}
7642
7643/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007644 * Check if a funcref is assigned to a valid variable name.
7645 * Return TRUE and give an error if not.
7646 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007648var_check_func_name(
7649 char_u *name, /* points to start of variable name */
7650 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007651{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007652 /* Allow for w: b: s: and t:. */
7653 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007654 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7655 ? name[2] : name[0]))
7656 {
7657 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7658 name);
7659 return TRUE;
7660 }
7661 /* Don't allow hiding a function. When "v" is not NULL we might be
7662 * assigning another function to the same var, the type is checked
7663 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007664 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007665 {
7666 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7667 name);
7668 return TRUE;
7669 }
7670 return FALSE;
7671}
7672
7673/*
7674 * Check if a variable name is valid.
7675 * Return FALSE and give an error if not.
7676 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007679{
7680 char_u *p;
7681
7682 for (p = varname; *p != NUL; ++p)
7683 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7684 && *p != AUTOLOAD_CHAR)
7685 {
7686 EMSG2(_(e_illvar), varname);
7687 return FALSE;
7688 }
7689 return TRUE;
7690}
7691
7692/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007693 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007694 * Also give an error message, using "name" or _("name") when use_gettext is
7695 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007696 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007698tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699{
7700 if (lock & VAR_LOCKED)
7701 {
7702 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007703 name == NULL ? (char_u *)_("Unknown")
7704 : use_gettext ? (char_u *)_(name)
7705 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007706 return TRUE;
7707 }
7708 if (lock & VAR_FIXED)
7709 {
7710 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007711 name == NULL ? (char_u *)_("Unknown")
7712 : use_gettext ? (char_u *)_(name)
7713 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007714 return TRUE;
7715 }
7716 return FALSE;
7717}
7718
7719/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007720 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007721 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007723 * It is OK for "from" and "to" to point to the same item. This is used to
7724 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007730 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007731 switch (from->v_type)
7732 {
7733 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007734 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007735 to->vval.v_number = from->vval.v_number;
7736 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007737 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007738#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007739 to->vval.v_float = from->vval.v_float;
7740 break;
7741#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007742 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007743#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007744 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007745 if (to->vval.v_job != NULL)
7746 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007747 break;
7748#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007749 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007750#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007751 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007752 if (to->vval.v_channel != NULL)
7753 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007754 break;
7755#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 case VAR_STRING:
7757 case VAR_FUNC:
7758 if (from->vval.v_string == NULL)
7759 to->vval.v_string = NULL;
7760 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007761 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007763 if (from->v_type == VAR_FUNC)
7764 func_ref(to->vval.v_string);
7765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007767 case VAR_PARTIAL:
7768 if (from->vval.v_partial == NULL)
7769 to->vval.v_partial = NULL;
7770 else
7771 {
7772 to->vval.v_partial = from->vval.v_partial;
7773 ++to->vval.v_partial->pt_refcount;
7774 }
7775 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 case VAR_LIST:
7777 if (from->vval.v_list == NULL)
7778 to->vval.v_list = NULL;
7779 else
7780 {
7781 to->vval.v_list = from->vval.v_list;
7782 ++to->vval.v_list->lv_refcount;
7783 }
7784 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 case VAR_DICT:
7786 if (from->vval.v_dict == NULL)
7787 to->vval.v_dict = NULL;
7788 else
7789 {
7790 to->vval.v_dict = from->vval.v_dict;
7791 ++to->vval.v_dict->dv_refcount;
7792 }
7793 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007794 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007795 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 break;
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
7800/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007801 * Make a copy of an item.
7802 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007803 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7804 * reference to an already copied list/dict can be used.
7805 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007808item_copy(
7809 typval_T *from,
7810 typval_T *to,
7811 int deep,
7812 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813{
7814 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007815 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {
7819 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007820 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 }
7822 ++recurse;
7823
7824 switch (from->v_type)
7825 {
7826 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 case VAR_STRING:
7829 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007830 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007831 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007832 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007833 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007834 copy_tv(from, to);
7835 break;
7836 case VAR_LIST:
7837 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007838 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007839 if (from->vval.v_list == NULL)
7840 to->vval.v_list = NULL;
7841 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7842 {
7843 /* use the copy made earlier */
7844 to->vval.v_list = from->vval.v_list->lv_copylist;
7845 ++to->vval.v_list->lv_refcount;
7846 }
7847 else
7848 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7849 if (to->vval.v_list == NULL)
7850 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 break;
7852 case VAR_DICT:
7853 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007854 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007855 if (from->vval.v_dict == NULL)
7856 to->vval.v_dict = NULL;
7857 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7858 {
7859 /* use the copy made earlier */
7860 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7861 ++to->vval.v_dict->dv_refcount;
7862 }
7863 else
7864 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7865 if (to->vval.v_dict == NULL)
7866 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007867 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007868 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007869 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007870 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871 }
7872 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007873 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874}
7875
7876/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 * This function is used by f_input() and f_inputdialog() functions. The third
7878 * argument to f_input() specifies the type of completion to use at the
7879 * prompt. The third argument to f_inputdialog() specifies the value to return
7880 * when the user cancels the prompt.
7881 */
7882 void
7883get_user_input(
7884 typval_T *argvars,
7885 typval_T *rettv,
7886 int inputdialog,
7887 int secret)
7888{
7889 char_u *prompt = get_tv_string_chk(&argvars[0]);
7890 char_u *p = NULL;
7891 int c;
7892 char_u buf[NUMBUFLEN];
7893 int cmd_silent_save = cmd_silent;
7894 char_u *defstr = (char_u *)"";
7895 int xp_type = EXPAND_NOTHING;
7896 char_u *xp_arg = NULL;
7897
7898 rettv->v_type = VAR_STRING;
7899 rettv->vval.v_string = NULL;
7900
7901#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007902 /* While starting up, there is no place to enter text. When running tests
7903 * with --not-a-term we assume feedkeys() will be used. */
7904 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 return;
7906#endif
7907
7908 cmd_silent = FALSE; /* Want to see the prompt. */
7909 if (prompt != NULL)
7910 {
7911 /* Only the part of the message after the last NL is considered as
7912 * prompt for the command line */
7913 p = vim_strrchr(prompt, '\n');
7914 if (p == NULL)
7915 p = prompt;
7916 else
7917 {
7918 ++p;
7919 c = *p;
7920 *p = NUL;
7921 msg_start();
7922 msg_clr_eos();
7923 msg_puts_attr(prompt, echo_attr);
7924 msg_didout = FALSE;
7925 msg_starthere();
7926 *p = c;
7927 }
7928 cmdline_row = msg_row;
7929
7930 if (argvars[1].v_type != VAR_UNKNOWN)
7931 {
7932 defstr = get_tv_string_buf_chk(&argvars[1], buf);
7933 if (defstr != NULL)
7934 stuffReadbuffSpec(defstr);
7935
7936 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
7937 {
7938 char_u *xp_name;
7939 int xp_namelen;
7940 long argt;
7941
7942 /* input() with a third argument: completion */
7943 rettv->vval.v_string = NULL;
7944
7945 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
7946 if (xp_name == NULL)
7947 return;
7948
7949 xp_namelen = (int)STRLEN(xp_name);
7950
7951 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
7952 &xp_arg) == FAIL)
7953 return;
7954 }
7955 }
7956
7957 if (defstr != NULL)
7958 {
7959 int save_ex_normal_busy = ex_normal_busy;
7960 ex_normal_busy = 0;
7961 rettv->vval.v_string =
7962 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
7963 xp_type, xp_arg);
7964 ex_normal_busy = save_ex_normal_busy;
7965 }
7966 if (inputdialog && rettv->vval.v_string == NULL
7967 && argvars[1].v_type != VAR_UNKNOWN
7968 && argvars[2].v_type != VAR_UNKNOWN)
7969 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
7970 &argvars[2], buf));
7971
7972 vim_free(xp_arg);
7973
7974 /* since the user typed this, no need to wait for return */
7975 need_wait_return = FALSE;
7976 msg_didout = FALSE;
7977 }
7978 cmd_silent = cmd_silent_save;
7979}
7980
7981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 * ":echo expr1 ..." print each argument separated with a space, add a
7983 * newline at the end.
7984 * ":echon expr1 ..." print each argument plain.
7985 */
7986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007990 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007991 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 char_u *p;
7993 int needclr = TRUE;
7994 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007995 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997 if (eap->skip)
7998 ++emsg_skip;
7999 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008001 /* If eval1() causes an error message the text from the command may
8002 * still need to be cleared. E.g., "echo 22,44". */
8003 need_clr_eos = needclr;
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {
8008 /*
8009 * Report the invalid expression unless the expression evaluation
8010 * has been cancelled due to an aborting error, an interrupt, or an
8011 * exception.
8012 */
8013 if (!aborting())
8014 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 break;
8017 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018 need_clr_eos = FALSE;
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 if (!eap->skip)
8021 {
8022 if (atstart)
8023 {
8024 atstart = FALSE;
8025 /* Call msg_start() after eval1(), evaluating the expression
8026 * may cause a message to appear. */
8027 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008028 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008029 /* Mark the saved text as finishing the line, so that what
8030 * follows is displayed on a new line when scrolling back
8031 * at the more prompt. */
8032 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 else if (eap->cmdidx == CMD_echo)
8037 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008038 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008039 if (p != NULL)
8040 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008042 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008044 if (*p != TAB && needclr)
8045 {
8046 /* remove any text still there from the command */
8047 msg_clr_eos();
8048 needclr = FALSE;
8049 }
8050 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008053 {
8054#ifdef FEAT_MBYTE
8055 if (has_mbyte)
8056 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008057 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008058
8059 (void)msg_outtrans_len_attr(p, i, echo_attr);
8060 p += i - 1;
8061 }
8062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008064 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008067 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008069 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 arg = skipwhite(arg);
8071 }
8072 eap->nextcmd = check_nextcmd(arg);
8073
8074 if (eap->skip)
8075 --emsg_skip;
8076 else
8077 {
8078 /* remove text that may still be there from the command */
8079 if (needclr)
8080 msg_clr_eos();
8081 if (eap->cmdidx == CMD_echo)
8082 msg_end();
8083 }
8084}
8085
8086/*
8087 * ":echohl {name}".
8088 */
8089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008090ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008092 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
8095/*
8096 * ":execute expr1 ..." execute the result of an expression.
8097 * ":echomsg expr1 ..." Print a message
8098 * ":echoerr expr1 ..." Print an error
8099 * Each gets spaces around each argument and a newline at the end for
8100 * echo commands
8101 */
8102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008103ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008106 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 int ret = OK;
8108 char_u *p;
8109 garray_T ga;
8110 int len;
8111 int save_did_emsg;
8112
8113 ga_init2(&ga, 1, 80);
8114
8115 if (eap->skip)
8116 ++emsg_skip;
8117 while (*arg != NUL && *arg != '|' && *arg != '\n')
8118 {
8119 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {
8122 /*
8123 * Report the invalid expression unless the expression evaluation
8124 * has been cancelled due to an aborting error, an interrupt, or an
8125 * exception.
8126 */
8127 if (!aborting())
8128 EMSG2(_(e_invexpr2), p);
8129 ret = FAIL;
8130 break;
8131 }
8132
8133 if (!eap->skip)
8134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 len = (int)STRLEN(p);
8137 if (ga_grow(&ga, len + 2) == FAIL)
8138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 ret = FAIL;
8141 break;
8142 }
8143 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 ga.ga_len += len;
8147 }
8148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 arg = skipwhite(arg);
8151 }
8152
8153 if (ret != FAIL && ga.ga_data != NULL)
8154 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008155 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8156 {
8157 /* Mark the already saved text as finishing the line, so that what
8158 * follows is displayed on a new line when scrolling back at the
8159 * more prompt. */
8160 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008161 }
8162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008166 out_flush();
8167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 else if (eap->cmdidx == CMD_echoerr)
8169 {
8170 /* We don't want to abort following commands, restore did_emsg. */
8171 save_did_emsg = did_emsg;
8172 EMSG((char_u *)ga.ga_data);
8173 if (!force_abort)
8174 did_emsg = save_did_emsg;
8175 }
8176 else if (eap->cmdidx == CMD_execute)
8177 do_cmdline((char_u *)ga.ga_data,
8178 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8179 }
8180
8181 ga_clear(&ga);
8182
8183 if (eap->skip)
8184 --emsg_skip;
8185
8186 eap->nextcmd = check_nextcmd(arg);
8187}
8188
8189/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 * Find window specified by "vp" in tabpage "tp".
8191 */
8192 win_T *
8193find_win_by_nr(
8194 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008195 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008196{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 int nr;
8199
8200 nr = (int)get_tv_number_chk(vp, NULL);
8201
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 if (nr < 0)
8203 return NULL;
8204 if (nr == 0)
8205 return curwin;
8206
Bram Moolenaar29323592016-07-24 22:04:11 +02008207 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8208 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (nr >= LOWEST_WIN_ID)
8210 {
8211 if (wp->w_id == nr)
8212 return wp;
8213 }
8214 else if (--nr <= 0)
8215 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008216 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 if (nr >= LOWEST_WIN_ID)
8218 return NULL;
8219 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220}
8221
8222/*
8223 * Find window specified by "wvp" in tabpage "tvp".
8224 */
8225 win_T *
8226find_tabwin(
8227 typval_T *wvp, /* VAR_UNKNOWN for current window */
8228 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8229{
8230 win_T *wp = NULL;
8231 tabpage_T *tp = NULL;
8232 long n;
8233
8234 if (wvp->v_type != VAR_UNKNOWN)
8235 {
8236 if (tvp->v_type != VAR_UNKNOWN)
8237 {
8238 n = (long)get_tv_number(tvp);
8239 if (n >= 0)
8240 tp = find_tabpage(n);
8241 }
8242 else
8243 tp = curtab;
8244
8245 if (tp != NULL)
8246 wp = find_win_by_nr(wvp, tp);
8247 }
8248 else
8249 wp = curwin;
8250
8251 return wp;
8252}
8253
8254/*
8255 * getwinvar() and gettabwinvar()
8256 */
8257 void
8258getwinvar(
8259 typval_T *argvars,
8260 typval_T *rettv,
8261 int off) /* 1 for gettabwinvar() */
8262{
8263 win_T *win;
8264 char_u *varname;
8265 dictitem_T *v;
8266 tabpage_T *tp = NULL;
8267 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 win_T *oldcurwin;
8269 tabpage_T *oldtabpage;
8270 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 if (off == 1)
8273 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8274 else
8275 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 win = find_win_by_nr(&argvars[off], tp);
8277 varname = get_tv_string_chk(&argvars[off + 1]);
8278 ++emsg_off;
8279
8280 rettv->v_type = VAR_STRING;
8281 rettv->vval.v_string = NULL;
8282
8283 if (win != NULL && varname != NULL)
8284 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 /* Set curwin to be our win, temporarily. Also set the tabpage,
8286 * otherwise the window is not valid. Only do this when needed,
8287 * autocommands get blocked. */
8288 need_switch_win = !(tp == curtab && win == curwin);
8289 if (!need_switch_win
8290 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008292 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008294 if (varname[1] == NUL)
8295 {
8296 /* get all window-local options in a dict */
8297 dict_T *opts = get_winbuf_options(FALSE);
8298
8299 if (opts != NULL)
8300 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008301 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008302 done = TRUE;
8303 }
8304 }
8305 else if (get_option_tv(&varname, rettv, 1) == OK)
8306 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 done = TRUE;
8308 }
8309 else
8310 {
8311 /* Look up the variable. */
8312 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8313 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8314 varname, FALSE);
8315 if (v != NULL)
8316 {
8317 copy_tv(&v->di_tv, rettv);
8318 done = TRUE;
8319 }
8320 }
8321 }
8322
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 if (need_switch_win)
8324 /* restore previous notion of curwin */
8325 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 }
8327
8328 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8329 /* use the default return value */
8330 copy_tv(&argvars[off + 2], rettv);
8331
8332 --emsg_off;
8333}
8334
8335/*
8336 * "setwinvar()" and "settabwinvar()" functions
8337 */
8338 void
8339setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8340{
8341 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 win_T *save_curwin;
8343 tabpage_T *save_curtab;
8344 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345 char_u *varname, *winvarname;
8346 typval_T *varp;
8347 char_u nbuf[NUMBUFLEN];
8348 tabpage_T *tp = NULL;
8349
8350 if (check_restricted() || check_secure())
8351 return;
8352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 if (off == 1)
8354 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8355 else
8356 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 win = find_win_by_nr(&argvars[off], tp);
8358 varname = get_tv_string_chk(&argvars[off + 1]);
8359 varp = &argvars[off + 2];
8360
8361 if (win != NULL && varname != NULL && varp != NULL)
8362 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 need_switch_win = !(tp == curtab && win == curwin);
8364 if (!need_switch_win
8365 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 {
8367 if (*varname == '&')
8368 {
8369 long numval;
8370 char_u *strval;
8371 int error = FALSE;
8372
8373 ++varname;
8374 numval = (long)get_tv_number_chk(varp, &error);
8375 strval = get_tv_string_buf_chk(varp, nbuf);
8376 if (!error && strval != NULL)
8377 set_option_value(varname, numval, strval, OPT_LOCAL);
8378 }
8379 else
8380 {
8381 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8382 if (winvarname != NULL)
8383 {
8384 STRCPY(winvarname, "w:");
8385 STRCPY(winvarname + 2, varname);
8386 set_var(winvarname, varp, TRUE);
8387 vim_free(winvarname);
8388 }
8389 }
8390 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008391 if (need_switch_win)
8392 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393 }
8394}
8395
8396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8398 * "arg" points to the "&" or '+' when called, to "option" when returning.
8399 * Returns NULL when no option name found. Otherwise pointer to the char
8400 * after the option name.
8401 */
8402 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008403find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 char_u *p = *arg;
8406
8407 ++p;
8408 if (*p == 'g' && p[1] == ':')
8409 {
8410 *opt_flags = OPT_GLOBAL;
8411 p += 2;
8412 }
8413 else if (*p == 'l' && p[1] == ':')
8414 {
8415 *opt_flags = OPT_LOCAL;
8416 p += 2;
8417 }
8418 else
8419 *opt_flags = 0;
8420
8421 if (!ASCII_ISALPHA(*p))
8422 return NULL;
8423 *arg = p;
8424
8425 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8426 p += 4; /* termcap option */
8427 else
8428 while (ASCII_ISALPHA(*p))
8429 ++p;
8430 return p;
8431}
8432
8433/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008434 * Return the autoload script name for a function or variable name.
8435 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008437 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008438autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008439{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008440 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008441 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008442
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008443 /* Get the script file name: replace '#' with '/', append ".vim". */
8444 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8445 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008446 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008447 STRCPY(scriptname, "autoload/");
8448 STRCAT(scriptname, name);
8449 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8450 STRCAT(scriptname, ".vim");
8451 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8452 *p = '/';
8453 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008454}
8455
8456/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008457 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008458 * Return TRUE if a package was loaded.
8459 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008461script_autoload(
8462 char_u *name,
8463 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464{
8465 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008466 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008467 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008468 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008470 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008471 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008472 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008473 return FALSE;
8474
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008475 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008477 /* Find the name in the list of previously loaded package names. Skip
8478 * "autoload/", it's always the same. */
8479 for (i = 0; i < ga_loaded.ga_len; ++i)
8480 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8481 break;
8482 if (!reload && i < ga_loaded.ga_len)
8483 ret = FALSE; /* was loaded already */
8484 else
8485 {
8486 /* Remember the name if it wasn't loaded already. */
8487 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8488 {
8489 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8490 tofree = NULL;
8491 }
8492
8493 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008494 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008495 ret = TRUE;
8496 }
8497
8498 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008499 return ret;
8500}
8501
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8503typedef enum
8504{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008505 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8506 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8507 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508} var_flavour_T;
8509
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008510static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
8512 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008513var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 char_u *p = varname;
8516
8517 if (ASCII_ISUPPER(*p))
8518 {
8519 while (*(++p))
8520 if (ASCII_ISLOWER(*p))
8521 return VAR_FLAVOUR_SESSION;
8522 return VAR_FLAVOUR_VIMINFO;
8523 }
8524 else
8525 return VAR_FLAVOUR_DEFAULT;
8526}
8527#endif
8528
8529#if defined(FEAT_VIMINFO) || defined(PROTO)
8530/*
8531 * Restore global vars that start with a capital from the viminfo file
8532 */
8533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008534read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008537 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008539 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
8541 if (!writing && (find_viminfo_parameter('!') != NULL))
8542 {
8543 tab = vim_strchr(virp->vir_line + 1, '\t');
8544 if (tab != NULL)
8545 {
8546 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008547 switch (*tab)
8548 {
8549 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008550#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008551 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008553 case 'D': type = VAR_DICT; break;
8554 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008555 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 tab = vim_strchr(tab, '\t');
8559 if (tab != NULL)
8560 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008561 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008562 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008563 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008565#ifdef FEAT_FLOAT
8566 else if (type == VAR_FLOAT)
8567 (void)string2float(tab + 1, &tv.vval.v_float);
8568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008570 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008571 if (type == VAR_DICT || type == VAR_LIST)
8572 {
8573 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8574
8575 if (etv == NULL)
8576 /* Failed to parse back the dict or list, use it as a
8577 * string. */
8578 tv.v_type = VAR_STRING;
8579 else
8580 {
8581 vim_free(tv.vval.v_string);
8582 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008583 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008584 }
8585 }
8586
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008587 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008588 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008589 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008590 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008591
8592 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008593 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008594 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8595 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597 }
8598 }
8599
8600 return viminfo_readline(virp);
8601}
8602
8603/*
8604 * Write global vars that start with a capital to the viminfo file
8605 */
8606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008607write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
Bram Moolenaar33570922005-01-25 22:26:29 +00008609 hashitem_T *hi;
8610 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008611 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008612 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008613 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008615 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 if (find_viminfo_parameter('!') == NULL)
8618 return;
8619
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008620 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008621
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008622 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008627 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008628 this_var = HI2DI(hi);
8629 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008632 {
8633 case VAR_STRING: s = "STR"; break;
8634 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008635 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008636 case VAR_DICT: s = "DIC"; break;
8637 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008638 case VAR_SPECIAL: s = "XPL"; break;
8639
8640 case VAR_UNKNOWN:
8641 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008642 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008643 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008644 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008645 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008646 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008648 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008649 if (p != NULL)
8650 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008651 vim_free(tofree);
8652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
8654 }
8655}
8656#endif
8657
8658#if defined(FEAT_SESSION) || defined(PROTO)
8659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008660store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
Bram Moolenaar33570922005-01-25 22:26:29 +00008662 hashitem_T *hi;
8663 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008664 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 char_u *p, *t;
8666
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008667 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008668 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008670 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008672 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008673 this_var = HI2DI(hi);
8674 if ((this_var->di_tv.v_type == VAR_NUMBER
8675 || this_var->di_tv.v_type == VAR_STRING)
8676 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008677 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008678 /* Escape special characters with a backslash. Turn a LF and
8679 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008680 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008681 (char_u *)"\\\"\n\r");
8682 if (p == NULL) /* out of memory */
8683 break;
8684 for (t = p; *t != NUL; ++t)
8685 if (*t == '\n')
8686 *t = 'n';
8687 else if (*t == '\r')
8688 *t = 'r';
8689 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 this_var->di_key,
8691 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8692 : ' ',
8693 p,
8694 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8695 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008696 || put_eol(fd) == FAIL)
8697 {
8698 vim_free(p);
8699 return FAIL;
8700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 vim_free(p);
8702 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008703#ifdef FEAT_FLOAT
8704 else if (this_var->di_tv.v_type == VAR_FLOAT
8705 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8706 {
8707 float_T f = this_var->di_tv.vval.v_float;
8708 int sign = ' ';
8709
8710 if (f < 0)
8711 {
8712 f = -f;
8713 sign = '-';
8714 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008715 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008716 this_var->di_key, sign, f) < 0)
8717 || put_eol(fd) == FAIL)
8718 return FAIL;
8719 }
8720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 }
8722 }
8723 return OK;
8724}
8725#endif
8726
Bram Moolenaar661b1822005-07-28 22:36:45 +00008727/*
8728 * Display script name where an item was last set.
8729 * Should only be invoked when 'verbose' is non-zero.
8730 */
8731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008732last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008733{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008734 char_u *p;
8735
Bram Moolenaar661b1822005-07-28 22:36:45 +00008736 if (scriptID != 0)
8737 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008738 p = home_replace_save(NULL, get_scriptname(scriptID));
8739 if (p != NULL)
8740 {
8741 verbose_enter();
8742 MSG_PUTS(_("\n\tLast set from "));
8743 MSG_PUTS(p);
8744 vim_free(p);
8745 verbose_leave();
8746 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008747 }
8748}
8749
Bram Moolenaar53744302015-07-17 17:38:22 +02008750/* reset v:option_new, v:option_old and v:option_type */
8751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008752reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008753{
8754 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8755 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8756 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8757}
8758
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759/*
8760 * Prepare "gap" for an assert error and add the sourcing position.
8761 */
8762 void
8763prepare_assert_error(garray_T *gap)
8764{
8765 char buf[NUMBUFLEN];
8766
8767 ga_init2(gap, 1, 100);
8768 if (sourcing_name != NULL)
8769 {
8770 ga_concat(gap, sourcing_name);
8771 if (sourcing_lnum > 0)
8772 ga_concat(gap, (char_u *)" ");
8773 }
8774 if (sourcing_lnum > 0)
8775 {
8776 sprintf(buf, "line %ld", (long)sourcing_lnum);
8777 ga_concat(gap, (char_u *)buf);
8778 }
8779 if (sourcing_name != NULL || sourcing_lnum > 0)
8780 ga_concat(gap, (char_u *)": ");
8781}
8782
8783/*
8784 * Add an assert error to v:errors.
8785 */
8786 void
8787assert_error(garray_T *gap)
8788{
8789 struct vimvar *vp = &vimvars[VV_ERRORS];
8790
8791 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8792 /* Make sure v:errors is a list. */
8793 set_vim_var_list(VV_ERRORS, list_alloc());
8794 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8795}
8796
Bram Moolenaar65a54642018-04-28 16:56:53 +02008797 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798assert_equal_common(typval_T *argvars, assert_type_T atype)
8799{
8800 garray_T ga;
8801
8802 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8803 != (atype == ASSERT_EQUAL))
8804 {
8805 prepare_assert_error(&ga);
8806 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8807 atype);
8808 assert_error(&ga);
8809 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008810 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008812 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813}
8814
Bram Moolenaar65a54642018-04-28 16:56:53 +02008815 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01008816assert_equalfile(typval_T *argvars)
8817{
8818 char_u buf1[NUMBUFLEN];
8819 char_u buf2[NUMBUFLEN];
8820 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1);
8821 char_u *fname2 = get_tv_string_buf_chk(&argvars[1], buf2);
8822 garray_T ga;
8823 FILE *fd1;
8824 FILE *fd2;
8825
8826 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008827 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008828
8829 IObuff[0] = NUL;
8830 fd1 = mch_fopen((char *)fname1, READBIN);
8831 if (fd1 == NULL)
8832 {
8833 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
8834 }
8835 else
8836 {
8837 fd2 = mch_fopen((char *)fname2, READBIN);
8838 if (fd2 == NULL)
8839 {
8840 fclose(fd1);
8841 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
8842 }
8843 else
8844 {
8845 int c1, c2;
8846 long count = 0;
8847
8848 for (;;)
8849 {
8850 c1 = fgetc(fd1);
8851 c2 = fgetc(fd2);
8852 if (c1 == EOF)
8853 {
8854 if (c2 != EOF)
8855 STRCPY(IObuff, "first file is shorter");
8856 break;
8857 }
8858 else if (c2 == EOF)
8859 {
8860 STRCPY(IObuff, "second file is shorter");
8861 break;
8862 }
8863 else if (c1 != c2)
8864 {
8865 vim_snprintf((char *)IObuff, IOSIZE,
8866 "difference at byte %ld", count);
8867 break;
8868 }
8869 ++count;
8870 }
Bram Moolenaar30494182018-02-20 21:46:05 +01008871 fclose(fd1);
8872 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01008873 }
8874 }
8875 if (IObuff[0] != NUL)
8876 {
8877 prepare_assert_error(&ga);
8878 ga_concat(&ga, IObuff);
8879 assert_error(&ga);
8880 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008881 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008882 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008883 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008884}
8885
Bram Moolenaar65a54642018-04-28 16:56:53 +02008886 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887assert_match_common(typval_T *argvars, assert_type_T atype)
8888{
8889 garray_T ga;
8890 char_u buf1[NUMBUFLEN];
8891 char_u buf2[NUMBUFLEN];
8892 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8893 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8894
8895 if (pat == NULL || text == NULL)
8896 EMSG(_(e_invarg));
8897 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8898 {
8899 prepare_assert_error(&ga);
8900 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8901 atype);
8902 assert_error(&ga);
8903 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008904 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008906 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907}
8908
Bram Moolenaar65a54642018-04-28 16:56:53 +02008909 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02008910assert_inrange(typval_T *argvars)
8911{
8912 garray_T ga;
8913 int error = FALSE;
8914 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
8915 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
8916 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
8917 char_u *tofree;
8918 char msg[200];
8919 char_u numbuf[NUMBUFLEN];
8920
8921 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008922 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008923 if (actual < lower || actual > upper)
8924 {
8925 prepare_assert_error(&ga);
8926 if (argvars[3].v_type != VAR_UNKNOWN)
8927 {
8928 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
8929 vim_free(tofree);
8930 }
8931 else
8932 {
8933 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
8934 (long)lower, (long)upper, (long)actual);
8935 ga_concat(&ga, (char_u *)msg);
8936 }
8937 assert_error(&ga);
8938 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008939 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008940 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008941 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008942}
8943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944/*
8945 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02008946 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02008948 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949assert_bool(typval_T *argvars, int isTrue)
8950{
8951 int error = FALSE;
8952 garray_T ga;
8953
8954 if (argvars[0].v_type == VAR_SPECIAL
8955 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02008956 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008957 if (argvars[0].v_type != VAR_NUMBER
8958 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8959 || error)
8960 {
8961 prepare_assert_error(&ga);
8962 fill_assert_error(&ga, &argvars[1],
8963 (char_u *)(isTrue ? "True" : "False"),
8964 NULL, &argvars[0], ASSERT_OTHER);
8965 assert_error(&ga);
8966 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008967 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008969 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970}
8971
Bram Moolenaar65a54642018-04-28 16:56:53 +02008972 int
Bram Moolenaar42205552017-03-18 19:42:22 +01008973assert_report(typval_T *argvars)
8974{
8975 garray_T ga;
8976
8977 prepare_assert_error(&ga);
8978 ga_concat(&ga, get_tv_string(&argvars[0]));
8979 assert_error(&ga);
8980 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008981 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01008982}
8983
Bram Moolenaar65a54642018-04-28 16:56:53 +02008984 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985assert_exception(typval_T *argvars)
8986{
8987 garray_T ga;
8988 char_u *error = get_tv_string_chk(&argvars[0]);
8989
8990 if (vimvars[VV_EXCEPTION].vv_str == NULL)
8991 {
8992 prepare_assert_error(&ga);
8993 ga_concat(&ga, (char_u *)"v:exception is not set");
8994 assert_error(&ga);
8995 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008996 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 }
8998 else if (error != NULL
8999 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9000 {
9001 prepare_assert_error(&ga);
9002 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9003 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9004 assert_error(&ga);
9005 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009006 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009008 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009}
9010
Bram Moolenaar65a54642018-04-28 16:56:53 +02009011 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009012assert_beeps(typval_T *argvars)
9013{
9014 char_u *cmd = get_tv_string_chk(&argvars[0]);
9015 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009016 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009017
9018 called_vim_beep = FALSE;
9019 suppress_errthrow = TRUE;
9020 emsg_silent = FALSE;
9021 do_cmdline_cmd(cmd);
9022 if (!called_vim_beep)
9023 {
9024 prepare_assert_error(&ga);
9025 ga_concat(&ga, (char_u *)"command did not beep: ");
9026 ga_concat(&ga, cmd);
9027 assert_error(&ga);
9028 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009029 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009030 }
9031
9032 suppress_errthrow = FALSE;
9033 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009034 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009035}
9036
Bram Moolenaar65a54642018-04-28 16:56:53 +02009037 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038assert_fails(typval_T *argvars)
9039{
9040 char_u *cmd = get_tv_string_chk(&argvars[0]);
9041 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009042 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043
9044 called_emsg = FALSE;
9045 suppress_errthrow = TRUE;
9046 emsg_silent = TRUE;
9047 do_cmdline_cmd(cmd);
9048 if (!called_emsg)
9049 {
9050 prepare_assert_error(&ga);
9051 ga_concat(&ga, (char_u *)"command did not fail: ");
9052 ga_concat(&ga, cmd);
9053 assert_error(&ga);
9054 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009055 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 }
9057 else if (argvars[1].v_type != VAR_UNKNOWN)
9058 {
9059 char_u buf[NUMBUFLEN];
9060 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9061
9062 if (error == NULL
9063 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9064 {
9065 prepare_assert_error(&ga);
9066 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9067 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9068 assert_error(&ga);
9069 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009070 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 }
9072 }
9073
9074 called_emsg = FALSE;
9075 suppress_errthrow = FALSE;
9076 emsg_silent = FALSE;
9077 emsg_on_display = FALSE;
9078 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009079 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080}
9081
9082/*
9083 * Append "str" to "gap", escaping unprintable characters.
9084 * Changes NL to \n, CR to \r, etc.
9085 */
9086 static void
9087ga_concat_esc(garray_T *gap, char_u *str)
9088{
9089 char_u *p;
9090 char_u buf[NUMBUFLEN];
9091
9092 if (str == NULL)
9093 {
9094 ga_concat(gap, (char_u *)"NULL");
9095 return;
9096 }
9097
9098 for (p = str; *p != NUL; ++p)
9099 switch (*p)
9100 {
9101 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9102 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9103 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9104 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9105 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9106 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9107 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9108 default:
9109 if (*p < ' ')
9110 {
9111 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9112 ga_concat(gap, buf);
9113 }
9114 else
9115 ga_append(gap, *p);
9116 break;
9117 }
9118}
9119
9120/*
9121 * Fill "gap" with information about an assert error.
9122 */
9123 void
9124fill_assert_error(
9125 garray_T *gap,
9126 typval_T *opt_msg_tv,
9127 char_u *exp_str,
9128 typval_T *exp_tv,
9129 typval_T *got_tv,
9130 assert_type_T atype)
9131{
9132 char_u numbuf[NUMBUFLEN];
9133 char_u *tofree;
9134
9135 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9136 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009137 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9138 vim_free(tofree);
9139 ga_concat(gap, (char_u *)": ");
9140 }
9141
9142 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9143 ga_concat(gap, (char_u *)"Pattern ");
9144 else if (atype == ASSERT_NOTEQUAL)
9145 ga_concat(gap, (char_u *)"Expected not equal to ");
9146 else
9147 ga_concat(gap, (char_u *)"Expected ");
9148 if (exp_str == NULL)
9149 {
9150 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 vim_free(tofree);
9152 }
9153 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009154 ga_concat_esc(gap, exp_str);
9155 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009157 if (atype == ASSERT_MATCH)
9158 ga_concat(gap, (char_u *)" does not match ");
9159 else if (atype == ASSERT_NOTMATCH)
9160 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009162 ga_concat(gap, (char_u *)" but got ");
9163 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9164 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 }
9166}
9167
Bram Moolenaar31988702018-02-13 12:57:42 +01009168/*
9169 * Compare "typ1" and "typ2". Put the result in "typ1".
9170 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009171 int
9172typval_compare(
9173 typval_T *typ1, /* first operand */
9174 typval_T *typ2, /* second operand */
9175 exptype_T type, /* operator */
9176 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009177 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009178{
9179 int i;
9180 varnumber_T n1, n2;
9181 char_u *s1, *s2;
9182 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9183
Bram Moolenaar31988702018-02-13 12:57:42 +01009184 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009185 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009186 /* For "is" a different type always means FALSE, for "notis"
9187 * it means TRUE. */
9188 n1 = (type == TYPE_NEQUAL);
9189 }
9190 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9191 {
9192 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009193 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009194 n1 = (typ1->v_type == typ2->v_type
9195 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009196 if (type == TYPE_NEQUAL)
9197 n1 = !n1;
9198 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009199 else if (typ1->v_type != typ2->v_type
9200 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009201 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009202 if (typ1->v_type != typ2->v_type)
9203 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009204 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009205 EMSG(_("E692: Invalid operation for List"));
9206 clear_tv(typ1);
9207 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009208 }
9209 else
9210 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009211 /* Compare two Lists for being equal or unequal. */
9212 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9213 ic, FALSE);
9214 if (type == TYPE_NEQUAL)
9215 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009216 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009217 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009218
9219 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9220 {
9221 if (type_is)
9222 {
9223 n1 = (typ1->v_type == typ2->v_type
9224 && typ1->vval.v_dict == typ2->vval.v_dict);
9225 if (type == TYPE_NEQUAL)
9226 n1 = !n1;
9227 }
9228 else if (typ1->v_type != typ2->v_type
9229 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9230 {
9231 if (typ1->v_type != typ2->v_type)
9232 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9233 else
9234 EMSG(_("E736: Invalid operation for Dictionary"));
9235 clear_tv(typ1);
9236 return FAIL;
9237 }
9238 else
9239 {
9240 /* Compare two Dictionaries for being equal or unequal. */
9241 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9242 ic, FALSE);
9243 if (type == TYPE_NEQUAL)
9244 n1 = !n1;
9245 }
9246 }
9247
9248 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9249 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9250 {
9251 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9252 {
9253 EMSG(_("E694: Invalid operation for Funcrefs"));
9254 clear_tv(typ1);
9255 return FAIL;
9256 }
9257 if ((typ1->v_type == VAR_PARTIAL
9258 && typ1->vval.v_partial == NULL)
9259 || (typ2->v_type == VAR_PARTIAL
9260 && typ2->vval.v_partial == NULL))
9261 /* when a partial is NULL assume not equal */
9262 n1 = FALSE;
9263 else if (type_is)
9264 {
9265 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9266 /* strings are considered the same if their value is
9267 * the same */
9268 n1 = tv_equal(typ1, typ2, ic, FALSE);
9269 else if (typ1->v_type == VAR_PARTIAL
9270 && typ2->v_type == VAR_PARTIAL)
9271 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9272 else
9273 n1 = FALSE;
9274 }
9275 else
9276 n1 = tv_equal(typ1, typ2, ic, FALSE);
9277 if (type == TYPE_NEQUAL)
9278 n1 = !n1;
9279 }
9280
9281#ifdef FEAT_FLOAT
9282 /*
9283 * If one of the two variables is a float, compare as a float.
9284 * When using "=~" or "!~", always compare as string.
9285 */
9286 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9287 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9288 {
9289 float_T f1, f2;
9290
9291 if (typ1->v_type == VAR_FLOAT)
9292 f1 = typ1->vval.v_float;
9293 else
9294 f1 = get_tv_number(typ1);
9295 if (typ2->v_type == VAR_FLOAT)
9296 f2 = typ2->vval.v_float;
9297 else
9298 f2 = get_tv_number(typ2);
9299 n1 = FALSE;
9300 switch (type)
9301 {
9302 case TYPE_EQUAL: n1 = (f1 == f2); break;
9303 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9304 case TYPE_GREATER: n1 = (f1 > f2); break;
9305 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9306 case TYPE_SMALLER: n1 = (f1 < f2); break;
9307 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9308 case TYPE_UNKNOWN:
9309 case TYPE_MATCH:
9310 case TYPE_NOMATCH: break; /* avoid gcc warning */
9311 }
9312 }
9313#endif
9314
9315 /*
9316 * If one of the two variables is a number, compare as a number.
9317 * When using "=~" or "!~", always compare as string.
9318 */
9319 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9320 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9321 {
9322 n1 = get_tv_number(typ1);
9323 n2 = get_tv_number(typ2);
9324 switch (type)
9325 {
9326 case TYPE_EQUAL: n1 = (n1 == n2); break;
9327 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9328 case TYPE_GREATER: n1 = (n1 > n2); break;
9329 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9330 case TYPE_SMALLER: n1 = (n1 < n2); break;
9331 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9332 case TYPE_UNKNOWN:
9333 case TYPE_MATCH:
9334 case TYPE_NOMATCH: break; /* avoid gcc warning */
9335 }
9336 }
9337 else
9338 {
9339 s1 = get_tv_string_buf(typ1, buf1);
9340 s2 = get_tv_string_buf(typ2, buf2);
9341 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9342 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9343 else
9344 i = 0;
9345 n1 = FALSE;
9346 switch (type)
9347 {
9348 case TYPE_EQUAL: n1 = (i == 0); break;
9349 case TYPE_NEQUAL: n1 = (i != 0); break;
9350 case TYPE_GREATER: n1 = (i > 0); break;
9351 case TYPE_GEQUAL: n1 = (i >= 0); break;
9352 case TYPE_SMALLER: n1 = (i < 0); break;
9353 case TYPE_SEQUAL: n1 = (i <= 0); break;
9354
9355 case TYPE_MATCH:
9356 case TYPE_NOMATCH:
9357 n1 = pattern_match(s2, s1, ic);
9358 if (type == TYPE_NOMATCH)
9359 n1 = !n1;
9360 break;
9361
9362 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9363 }
9364 }
9365 clear_tv(typ1);
9366 typ1->v_type = VAR_NUMBER;
9367 typ1->vval.v_number = n1;
9368
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009369 return OK;
9370}
9371
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009372 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009373typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009374{
9375 char_u *tofree;
9376 char_u numbuf[NUMBUFLEN];
9377 char_u *ret = NULL;
9378
9379 if (arg == NULL)
9380 return vim_strsave((char_u *)"(does not exist)");
9381 ret = tv2string(arg, &tofree, numbuf, 0);
9382 /* Make a copy if we have a value but it's not in allocated memory. */
9383 if (ret != NULL && tofree == NULL)
9384 ret = vim_strsave(ret);
9385 return ret;
9386}
9387
9388 int
9389var_exists(char_u *var)
9390{
9391 char_u *name;
9392 char_u *tofree;
9393 typval_T tv;
9394 int len = 0;
9395 int n = FALSE;
9396
9397 /* get_name_len() takes care of expanding curly braces */
9398 name = var;
9399 len = get_name_len(&var, &tofree, TRUE, FALSE);
9400 if (len > 0)
9401 {
9402 if (tofree != NULL)
9403 name = tofree;
9404 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9405 if (n)
9406 {
9407 /* handle d.key, l[idx], f(expr) */
9408 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9409 if (n)
9410 clear_tv(&tv);
9411 }
9412 }
9413 if (*var != NUL)
9414 n = FALSE;
9415
9416 vim_free(tofree);
9417 return n;
9418}
9419
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420#endif /* FEAT_EVAL */
9421
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009423#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
9425#ifdef WIN3264
9426/*
9427 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9428 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009429static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9430static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9431static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432
9433/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009434 * Get the short path (8.3) for the filename in "fnamep".
9435 * Only works for a valid file name.
9436 * When the path gets longer "fnamep" is changed and the allocated buffer
9437 * is put in "bufp".
9438 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9439 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 */
9441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009444 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 char_u *newbuf;
9446
9447 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009448 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (l > len - 1)
9450 {
9451 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009452 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 newbuf = vim_strnsave(*fnamep, l);
9454 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009455 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
9457 vim_free(*bufp);
9458 *fnamep = *bufp = newbuf;
9459
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009460 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009461 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 }
9463
9464 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009465 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
9468/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009469 * Get the short path (8.3) for the filename in "fname". The converted
9470 * path is returned in "bufp".
9471 *
9472 * Some of the directories specified in "fname" may not exist. This function
9473 * will shorten the existing directories at the beginning of the path and then
9474 * append the remaining non-existing path.
9475 *
9476 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009477 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009478 * bufp - Pointer to an allocated buffer for the filename.
9479 * fnamelen - Length of the filename pointed to by fname
9480 *
9481 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 */
9483 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009484shortpath_for_invalid_fname(
9485 char_u **fname,
9486 char_u **bufp,
9487 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009489 char_u *short_fname, *save_fname, *pbuf_unused;
9490 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009492 int old_len, len;
9493 int new_len, sfx_len;
9494 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
9496 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009497 old_len = *fnamelen;
9498 save_fname = vim_strnsave(*fname, old_len);
9499 pbuf_unused = NULL;
9500 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009502 endp = save_fname + old_len - 1; /* Find the end of the copy */
9503 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009505 /*
9506 * Try shortening the supplied path till it succeeds by removing one
9507 * directory at a time from the tail of the path.
9508 */
9509 len = 0;
9510 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009512 /* go back one path-separator */
9513 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9514 --endp;
9515 if (endp <= save_fname)
9516 break; /* processed the complete path */
9517
9518 /*
9519 * Replace the path separator with a NUL and try to shorten the
9520 * resulting path.
9521 */
9522 ch = *endp;
9523 *endp = 0;
9524 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009525 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009526 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9527 {
9528 retval = FAIL;
9529 goto theend;
9530 }
9531 *endp = ch; /* preserve the string */
9532
9533 if (len > 0)
9534 break; /* successfully shortened the path */
9535
9536 /* failed to shorten the path. Skip the path separator */
9537 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009540 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009542 /*
9543 * Succeeded in shortening the path. Now concatenate the shortened
9544 * path with the remaining path at the tail.
9545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009547 /* Compute the length of the new path. */
9548 sfx_len = (int)(save_endp - endp) + 1;
9549 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009551 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009553 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009555 /* There is not enough space in the currently allocated string,
9556 * copy it to a buffer big enough. */
9557 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009559 {
9560 retval = FAIL;
9561 goto theend;
9562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 }
9564 else
9565 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009566 /* Transfer short_fname to the main buffer (it's big enough),
9567 * unless get_short_pathname() did its work in-place. */
9568 *fname = *bufp = save_fname;
9569 if (short_fname != save_fname)
9570 vim_strncpy(save_fname, short_fname, len);
9571 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009573
9574 /* concat the not-shortened part of the path */
9575 vim_strncpy(*fname + len, endp, sfx_len);
9576 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009578
9579theend:
9580 vim_free(pbuf_unused);
9581 vim_free(save_fname);
9582
9583 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
9585
9586/*
9587 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009588 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 */
9590 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591shortpath_for_partial(
9592 char_u **fnamep,
9593 char_u **bufp,
9594 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 int sepcount, len, tflen;
9597 char_u *p;
9598 char_u *pbuf, *tfname;
9599 int hasTilde;
9600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009601 /* Count up the path separators from the RHS.. so we know which part
9602 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009604 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 if (vim_ispathsep(*p))
9606 ++sepcount;
9607
9608 /* Need full path first (use expand_env() to remove a "~/") */
9609 hasTilde = (**fnamep == '~');
9610 if (hasTilde)
9611 pbuf = tfname = expand_env_save(*fnamep);
9612 else
9613 pbuf = tfname = FullName_save(*fnamep, FALSE);
9614
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009615 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009617 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9618 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619
9620 if (len == 0)
9621 {
9622 /* Don't have a valid filename, so shorten the rest of the
9623 * path if we can. This CAN give us invalid 8.3 filenames, but
9624 * there's not a lot of point in guessing what it might be.
9625 */
9626 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009627 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9628 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 }
9630
9631 /* Count the paths backward to find the beginning of the desired string. */
9632 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009633 {
9634#ifdef FEAT_MBYTE
9635 if (has_mbyte)
9636 p -= mb_head_off(tfname, p);
9637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 if (vim_ispathsep(*p))
9639 {
9640 if (sepcount == 0 || (hasTilde && sepcount == 1))
9641 break;
9642 else
9643 sepcount --;
9644 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (hasTilde)
9647 {
9648 --p;
9649 if (p >= tfname)
9650 *p = '~';
9651 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009652 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 }
9654 else
9655 ++p;
9656
9657 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9658 vim_free(*bufp);
9659 *fnamelen = (int)STRLEN(p);
9660 *bufp = pbuf;
9661 *fnamep = p;
9662
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009663 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664}
9665#endif /* WIN3264 */
9666
9667/*
9668 * Adjust a filename, according to a string of modifiers.
9669 * *fnamep must be NUL terminated when called. When returning, the length is
9670 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009671 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 * When there is an error, *fnamep is set to NULL.
9673 */
9674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009675modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009676 char_u *src, // string with modifiers
9677 int tilde_file, // "~" is a file name, not $HOME
9678 int *usedlen, // characters after src that are used
9679 char_u **fnamep, // file name so far
9680 char_u **bufp, // buffer for allocated file name or NULL
9681 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 int valid = 0;
9684 char_u *tail;
9685 char_u *s, *p, *pbuf;
9686 char_u dirname[MAXPATHL];
9687 int c;
9688 int has_fullname = 0;
9689#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009690 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 int has_shortname = 0;
9692#endif
9693
9694repeat:
9695 /* ":p" - full path/file_name */
9696 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9697 {
9698 has_fullname = 1;
9699
9700 valid |= VALID_PATH;
9701 *usedlen += 2;
9702
9703 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9704 if ((*fnamep)[0] == '~'
9705#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9706 && ((*fnamep)[1] == '/'
9707# ifdef BACKSLASH_IN_FILENAME
9708 || (*fnamep)[1] == '\\'
9709# endif
9710 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009712 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 )
9714 {
9715 *fnamep = expand_env_save(*fnamep);
9716 vim_free(*bufp); /* free any allocated file name */
9717 *bufp = *fnamep;
9718 if (*fnamep == NULL)
9719 return -1;
9720 }
9721
9722 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009723 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 {
9725 if (vim_ispathsep(*p)
9726 && p[1] == '.'
9727 && (p[2] == NUL
9728 || vim_ispathsep(p[2])
9729 || (p[2] == '.'
9730 && (p[3] == NUL || vim_ispathsep(p[3])))))
9731 break;
9732 }
9733
9734 /* FullName_save() is slow, don't use it when not needed. */
9735 if (*p != NUL || !vim_isAbsName(*fnamep))
9736 {
9737 *fnamep = FullName_save(*fnamep, *p != NUL);
9738 vim_free(*bufp); /* free any allocated file name */
9739 *bufp = *fnamep;
9740 if (*fnamep == NULL)
9741 return -1;
9742 }
9743
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009744#ifdef WIN3264
9745# if _WIN32_WINNT >= 0x0500
9746 if (vim_strchr(*fnamep, '~') != NULL)
9747 {
9748 /* Expand 8.3 filename to full path. Needed to make sure the same
9749 * file does not have two different names.
9750 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9751 p = alloc(_MAX_PATH + 1);
9752 if (p != NULL)
9753 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009754 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009755 {
9756 vim_free(*bufp);
9757 *bufp = *fnamep = p;
9758 }
9759 else
9760 vim_free(p);
9761 }
9762 }
9763# endif
9764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 /* Append a path separator to a directory. */
9766 if (mch_isdir(*fnamep))
9767 {
9768 /* Make room for one or two extra characters. */
9769 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9770 vim_free(*bufp); /* free any allocated file name */
9771 *bufp = *fnamep;
9772 if (*fnamep == NULL)
9773 return -1;
9774 add_pathsep(*fnamep);
9775 }
9776 }
9777
9778 /* ":." - path relative to the current directory */
9779 /* ":~" - path relative to the home directory */
9780 /* ":8" - shortname path - postponed till after */
9781 while (src[*usedlen] == ':'
9782 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9783 {
9784 *usedlen += 2;
9785 if (c == '8')
9786 {
9787#ifdef WIN3264
9788 has_shortname = 1; /* Postpone this. */
9789#endif
9790 continue;
9791 }
9792 pbuf = NULL;
9793 /* Need full path first (use expand_env() to remove a "~/") */
9794 if (!has_fullname)
9795 {
9796 if (c == '.' && **fnamep == '~')
9797 p = pbuf = expand_env_save(*fnamep);
9798 else
9799 p = pbuf = FullName_save(*fnamep, FALSE);
9800 }
9801 else
9802 p = *fnamep;
9803
9804 has_fullname = 0;
9805
9806 if (p != NULL)
9807 {
9808 if (c == '.')
9809 {
9810 mch_dirname(dirname, MAXPATHL);
9811 s = shorten_fname(p, dirname);
9812 if (s != NULL)
9813 {
9814 *fnamep = s;
9815 if (pbuf != NULL)
9816 {
9817 vim_free(*bufp); /* free any allocated file name */
9818 *bufp = pbuf;
9819 pbuf = NULL;
9820 }
9821 }
9822 }
9823 else
9824 {
9825 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9826 /* Only replace it when it starts with '~' */
9827 if (*dirname == '~')
9828 {
9829 s = vim_strsave(dirname);
9830 if (s != NULL)
9831 {
9832 *fnamep = s;
9833 vim_free(*bufp);
9834 *bufp = s;
9835 }
9836 }
9837 }
9838 vim_free(pbuf);
9839 }
9840 }
9841
9842 tail = gettail(*fnamep);
9843 *fnamelen = (int)STRLEN(*fnamep);
9844
9845 /* ":h" - head, remove "/file_name", can be repeated */
9846 /* Don't remove the first "/" or "c:\" */
9847 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9848 {
9849 valid |= VALID_HEAD;
9850 *usedlen += 2;
9851 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009852 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009853 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 *fnamelen = (int)(tail - *fnamep);
9855#ifdef VMS
9856 if (*fnamelen > 0)
9857 *fnamelen += 1; /* the path separator is part of the path */
9858#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009859 if (*fnamelen == 0)
9860 {
9861 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9862 p = vim_strsave((char_u *)".");
9863 if (p == NULL)
9864 return -1;
9865 vim_free(*bufp);
9866 *bufp = *fnamep = tail = p;
9867 *fnamelen = 1;
9868 }
9869 else
9870 {
9871 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009872 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 }
9875
9876 /* ":8" - shortname */
9877 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9878 {
9879 *usedlen += 2;
9880#ifdef WIN3264
9881 has_shortname = 1;
9882#endif
9883 }
9884
9885#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009886 /*
9887 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 */
9889 if (has_shortname)
9890 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009891 /* Copy the string if it is shortened by :h and when it wasn't copied
9892 * yet, because we are going to change it in place. Avoids changing
9893 * the buffer name for "%:8". */
9894 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 {
9896 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009897 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 return -1;
9899 vim_free(*bufp);
9900 *bufp = *fnamep = p;
9901 }
9902
9903 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009904 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (!has_fullname && !vim_isAbsName(*fnamep))
9906 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009907 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 return -1;
9909 }
9910 else
9911 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009912 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
Bram Moolenaardc935552011-08-17 15:23:23 +02009914 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009916 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 return -1;
9918
9919 if (l == 0)
9920 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009921 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009923 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return -1;
9925 }
9926 *fnamelen = l;
9927 }
9928 }
9929#endif /* WIN3264 */
9930
9931 /* ":t" - tail, just the basename */
9932 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9933 {
9934 *usedlen += 2;
9935 *fnamelen -= (int)(tail - *fnamep);
9936 *fnamep = tail;
9937 }
9938
9939 /* ":e" - extension, can be repeated */
9940 /* ":r" - root, without extension, can be repeated */
9941 while (src[*usedlen] == ':'
9942 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9943 {
9944 /* find a '.' in the tail:
9945 * - for second :e: before the current fname
9946 * - otherwise: The last '.'
9947 */
9948 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9949 s = *fnamep - 2;
9950 else
9951 s = *fnamep + *fnamelen - 1;
9952 for ( ; s > tail; --s)
9953 if (s[0] == '.')
9954 break;
9955 if (src[*usedlen + 1] == 'e') /* :e */
9956 {
9957 if (s > tail)
9958 {
9959 *fnamelen += (int)(*fnamep - (s + 1));
9960 *fnamep = s + 1;
9961#ifdef VMS
9962 /* cut version from the extension */
9963 s = *fnamep + *fnamelen - 1;
9964 for ( ; s > *fnamep; --s)
9965 if (s[0] == ';')
9966 break;
9967 if (s > *fnamep)
9968 *fnamelen = s - *fnamep;
9969#endif
9970 }
9971 else if (*fnamep <= tail)
9972 *fnamelen = 0;
9973 }
9974 else /* :r */
9975 {
9976 if (s > tail) /* remove one extension */
9977 *fnamelen = (int)(s - *fnamep);
9978 }
9979 *usedlen += 2;
9980 }
9981
9982 /* ":s?pat?foo?" - substitute */
9983 /* ":gs?pat?foo?" - global substitute */
9984 if (src[*usedlen] == ':'
9985 && (src[*usedlen + 1] == 's'
9986 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9987 {
9988 char_u *str;
9989 char_u *pat;
9990 char_u *sub;
9991 int sep;
9992 char_u *flags;
9993 int didit = FALSE;
9994
9995 flags = (char_u *)"";
9996 s = src + *usedlen + 2;
9997 if (src[*usedlen + 1] == 'g')
9998 {
9999 flags = (char_u *)"g";
10000 ++s;
10001 }
10002
10003 sep = *s++;
10004 if (sep)
10005 {
10006 /* find end of pattern */
10007 p = vim_strchr(s, sep);
10008 if (p != NULL)
10009 {
10010 pat = vim_strnsave(s, (int)(p - s));
10011 if (pat != NULL)
10012 {
10013 s = p + 1;
10014 /* find end of substitution */
10015 p = vim_strchr(s, sep);
10016 if (p != NULL)
10017 {
10018 sub = vim_strnsave(s, (int)(p - s));
10019 str = vim_strnsave(*fnamep, *fnamelen);
10020 if (sub != NULL && str != NULL)
10021 {
10022 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010023 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (s != NULL)
10025 {
10026 *fnamep = s;
10027 *fnamelen = (int)STRLEN(s);
10028 vim_free(*bufp);
10029 *bufp = s;
10030 didit = TRUE;
10031 }
10032 }
10033 vim_free(sub);
10034 vim_free(str);
10035 }
10036 vim_free(pat);
10037 }
10038 }
10039 /* after using ":s", repeat all the modifiers */
10040 if (didit)
10041 goto repeat;
10042 }
10043 }
10044
Bram Moolenaar26df0922014-02-23 23:39:13 +010010045 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10046 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010047 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010048 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010049 if (c != NUL)
10050 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010051 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010052 if (c != NUL)
10053 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010054 if (p == NULL)
10055 return -1;
10056 vim_free(*bufp);
10057 *bufp = *fnamep = p;
10058 *fnamelen = (int)STRLEN(p);
10059 *usedlen += 2;
10060 }
10061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 return valid;
10063}
10064
10065/*
10066 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010067 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 * "flags" can be "g" to do a global substitute.
10069 * Returns an allocated string, NULL for error.
10070 */
10071 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010072do_string_sub(
10073 char_u *str,
10074 char_u *pat,
10075 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010076 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010077 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
10079 int sublen;
10080 regmatch_T regmatch;
10081 int i;
10082 int do_all;
10083 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010084 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 garray_T ga;
10086 char_u *ret;
10087 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010088 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
10090 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10091 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010092 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
10094 ga_init2(&ga, 1, 200);
10095
10096 do_all = (flags[0] == 'g');
10097
10098 regmatch.rm_ic = p_ic;
10099 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10100 if (regmatch.regprog != NULL)
10101 {
10102 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010103 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10105 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010106 /* Skip empty match except for first match. */
10107 if (regmatch.startp[0] == regmatch.endp[0])
10108 {
10109 if (zero_width == regmatch.startp[0])
10110 {
10111 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010112 i = MB_PTR2LEN(tail);
10113 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10114 (size_t)i);
10115 ga.ga_len += i;
10116 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010117 continue;
10118 }
10119 zero_width = regmatch.startp[0];
10120 }
10121
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 /*
10123 * Get some space for a temporary buffer to do the substitution
10124 * into. It will contain:
10125 * - The text up to where the match is.
10126 * - The substituted text.
10127 * - The text after the match.
10128 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010129 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010130 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10132 {
10133 ga_clear(&ga);
10134 break;
10135 }
10136
10137 /* copy the text up to where the match is */
10138 i = (int)(regmatch.startp[0] - tail);
10139 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10140 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010141 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 + ga.ga_len + i, TRUE, TRUE, FALSE);
10143 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010144 tail = regmatch.endp[0];
10145 if (*tail == NUL)
10146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 if (!do_all)
10148 break;
10149 }
10150
10151 if (ga.ga_data != NULL)
10152 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10153
Bram Moolenaar473de612013-06-08 18:19:48 +020010154 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 }
10156
10157 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10158 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010159 if (p_cpo == empty_option)
10160 p_cpo = save_cpo;
10161 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010162 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010163 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164
10165 return ret;
10166}
10167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 static int
10169filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10170{
10171 typval_T rettv;
10172 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010174
10175 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10176 argv[0] = vimvars[VV_KEY].vv_tv;
10177 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010178 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10179 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180 if (map)
10181 {
10182 /* map(): replace the list item value */
10183 clear_tv(tv);
10184 rettv.v_lock = 0;
10185 *tv = rettv;
10186 }
10187 else
10188 {
10189 int error = FALSE;
10190
10191 /* filter(): when expr is zero remove the item */
10192 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10193 clear_tv(&rettv);
10194 /* On type error, nothing has been removed; return FAIL to stop the
10195 * loop. The error message was given by get_tv_number_chk(). */
10196 if (error)
10197 goto theend;
10198 }
10199 retval = OK;
10200theend:
10201 clear_tv(&vimvars[VV_VAL].vv_tv);
10202 return retval;
10203}
10204
10205
10206/*
10207 * Implementation of map() and filter().
10208 */
10209 void
10210filter_map(typval_T *argvars, typval_T *rettv, int map)
10211{
10212 typval_T *expr;
10213 listitem_T *li, *nli;
10214 list_T *l = NULL;
10215 dictitem_T *di;
10216 hashtab_T *ht;
10217 hashitem_T *hi;
10218 dict_T *d = NULL;
10219 typval_T save_val;
10220 typval_T save_key;
10221 int rem;
10222 int todo;
10223 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10224 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10225 : N_("filter() argument"));
10226 int save_did_emsg;
10227 int idx = 0;
10228
10229 if (argvars[0].v_type == VAR_LIST)
10230 {
10231 if ((l = argvars[0].vval.v_list) == NULL
10232 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10233 return;
10234 }
10235 else if (argvars[0].v_type == VAR_DICT)
10236 {
10237 if ((d = argvars[0].vval.v_dict) == NULL
10238 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10239 return;
10240 }
10241 else
10242 {
10243 EMSG2(_(e_listdictarg), ermsg);
10244 return;
10245 }
10246
10247 expr = &argvars[1];
10248 /* On type errors, the preceding call has already displayed an error
10249 * message. Avoid a misleading error message for an empty string that
10250 * was not passed as argument. */
10251 if (expr->v_type != VAR_UNKNOWN)
10252 {
10253 prepare_vimvar(VV_VAL, &save_val);
10254
10255 /* We reset "did_emsg" to be able to detect whether an error
10256 * occurred during evaluation of the expression. */
10257 save_did_emsg = did_emsg;
10258 did_emsg = FALSE;
10259
10260 prepare_vimvar(VV_KEY, &save_key);
10261 if (argvars[0].v_type == VAR_DICT)
10262 {
10263 vimvars[VV_KEY].vv_type = VAR_STRING;
10264
10265 ht = &d->dv_hashtab;
10266 hash_lock(ht);
10267 todo = (int)ht->ht_used;
10268 for (hi = ht->ht_array; todo > 0; ++hi)
10269 {
10270 if (!HASHITEM_EMPTY(hi))
10271 {
10272 int r;
10273
10274 --todo;
10275 di = HI2DI(hi);
10276 if (map &&
10277 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10278 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10279 break;
10280 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10281 r = filter_map_one(&di->di_tv, expr, map, &rem);
10282 clear_tv(&vimvars[VV_KEY].vv_tv);
10283 if (r == FAIL || did_emsg)
10284 break;
10285 if (!map && rem)
10286 {
10287 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10288 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10289 break;
10290 dictitem_remove(d, di);
10291 }
10292 }
10293 }
10294 hash_unlock(ht);
10295 }
10296 else
10297 {
10298 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10299
10300 for (li = l->lv_first; li != NULL; li = nli)
10301 {
10302 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10303 break;
10304 nli = li->li_next;
10305 vimvars[VV_KEY].vv_nr = idx;
10306 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10307 || did_emsg)
10308 break;
10309 if (!map && rem)
10310 listitem_remove(l, li);
10311 ++idx;
10312 }
10313 }
10314
10315 restore_vimvar(VV_KEY, &save_key);
10316 restore_vimvar(VV_VAL, &save_val);
10317
10318 did_emsg |= save_did_emsg;
10319 }
10320
10321 copy_tv(&argvars[0], rettv);
10322}
10323
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */