blob: 26aa0e34e71d778620cc69bf9f845c8eed7fe250 [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 {
732 EMSG2(_(e_invexpr2), s);
733 return FAIL;
734 }
735 }
736 return OK;
737}
738
739/*
740 * Like eval_to_bool() but using a typval_T instead of a string.
741 * Works for string, funcref and partial.
742 */
743 int
744eval_expr_to_bool(typval_T *expr, int *error)
745{
746 typval_T rettv;
747 int res;
748
749 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
750 {
751 *error = TRUE;
752 return FALSE;
753 }
754 res = (get_tv_number_chk(&rettv, error) != 0);
755 clear_tv(&rettv);
756 return res;
757}
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759/*
760 * Top level evaluation function, returning a string. If "skip" is TRUE,
761 * only parsing to "nextcmd" is done, without reporting errors. Return
762 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
763 */
764 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100765eval_to_string_skip(
766 char_u *arg,
767 char_u **nextcmd,
768 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
Bram Moolenaar33570922005-01-25 22:26:29 +0000770 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 char_u *retval;
772
773 if (skip)
774 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000775 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 retval = NULL;
777 else
778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 retval = vim_strsave(get_tv_string(&tv));
780 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 if (skip)
783 --emsg_skip;
784
785 return retval;
786}
787
788/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000789 * Skip over an expression at "*pp".
790 * Return FAIL for an error, OK otherwise.
791 */
792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000796
797 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000798 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799}
800
801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000803 * When "convert" is TRUE convert a List into a sequence of lines and convert
804 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * Return pointer to allocated memory, or NULL for failure.
806 */
807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100808eval_to_string(
809 char_u *arg,
810 char_u **nextcmd,
811 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
Bram Moolenaar33570922005-01-25 22:26:29 +0000813 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000815 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000816#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000817 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000820 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 retval = NULL;
822 else
823 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000824 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000825 {
826 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000827 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200828 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200829 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200830 if (tv.vval.v_list->lv_len > 0)
831 ga_append(&ga, NL);
832 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833 ga_append(&ga, NUL);
834 retval = (char_u *)ga.ga_data;
835 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000836#ifdef FEAT_FLOAT
837 else if (convert && tv.v_type == VAR_FLOAT)
838 {
839 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
840 retval = vim_strsave(numbuf);
841 }
842#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000843 else
844 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847
848 return retval;
849}
850
851/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000852 * Call eval_to_string() without using current local variables and using
853 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
855 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100856eval_to_string_safe(
857 char_u *arg,
858 char_u **nextcmd,
859 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 char_u *retval;
862 void *save_funccalp;
863
864 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
867 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000868 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000869 if (use_sandbox)
870 --sandbox;
871 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 restore_funccal(save_funccalp);
873 return retval;
874}
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876/*
877 * Top level evaluation function, returning a number.
878 * Evaluates "expr" silently.
879 * Returns -1 for an error.
880 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200881 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100882eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200885 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000886 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 ++emsg_off;
889
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 retval = -1;
892 else
893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000894 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 --emsg_off;
898
899 return retval;
900}
901
Bram Moolenaara40058a2005-07-11 22:42:07 +0000902/*
903 * Prepare v: variable "idx" to be used.
904 * Save the current typeval in "save_tv".
905 * When not used yet add the variable to the v: hashtable.
906 */
907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000909{
910 *save_tv = vimvars[idx].vv_tv;
911 if (vimvars[idx].vv_type == VAR_UNKNOWN)
912 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
913}
914
915/*
916 * Restore v: variable "idx" to typeval "save_tv".
917 * When no longer defined, remove the variable from the v: hashtable.
918 */
919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000921{
922 hashitem_T *hi;
923
Bram Moolenaara40058a2005-07-11 22:42:07 +0000924 vimvars[idx].vv_tv = *save_tv;
925 if (vimvars[idx].vv_type == VAR_UNKNOWN)
926 {
927 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
928 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100929 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000930 else
931 hash_remove(&vimvarht, hi);
932 }
933}
934
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000935#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000936/*
937 * Evaluate an expression to a list with suggestions.
938 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000939 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000940 */
941 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100942eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000943{
944 typval_T save_val;
945 typval_T rettv;
946 list_T *list = NULL;
947 char_u *p = skipwhite(expr);
948
949 /* Set "v:val" to the bad word. */
950 prepare_vimvar(VV_VAL, &save_val);
951 vimvars[VV_VAL].vv_type = VAR_STRING;
952 vimvars[VV_VAL].vv_str = badword;
953 if (p_verbose == 0)
954 ++emsg_off;
955
956 if (eval1(&p, &rettv, TRUE) == OK)
957 {
958 if (rettv.v_type != VAR_LIST)
959 clear_tv(&rettv);
960 else
961 list = rettv.vval.v_list;
962 }
963
964 if (p_verbose == 0)
965 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000966 restore_vimvar(VV_VAL, &save_val);
967
968 return list;
969}
970
971/*
972 * "list" is supposed to contain two items: a word and a number. Return the
973 * word in "pp" and the number as the return value.
974 * Return -1 if anything isn't right.
975 * Used to get the good word and score from the eval_spell_expr() result.
976 */
977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000979{
980 listitem_T *li;
981
982 li = list->lv_first;
983 if (li == NULL)
984 return -1;
985 *pp = get_tv_string(&li->li_tv);
986
987 li = li->li_next;
988 if (li == NULL)
989 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200990 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000991}
992#endif
993
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000994/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000995 * Top level evaluation function.
996 * Returns an allocated typval_T with the result.
997 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998 */
999 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001000eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001001{
1002 typval_T *tv;
1003
1004 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001005 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001006 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001007
1008 return tv;
1009}
1010
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001013 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001014 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1015 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001016 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001019call_vim_function(
1020 char_u *func,
1021 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001022 typval_T *argv,
1023 typval_T *rettv,
1024 int safe) /* use the sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 int doesrange;
1027 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001028 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (safe)
1031 {
1032 save_funccalp = save_funccal();
1033 ++sandbox;
1034 }
1035
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001036 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001037 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001039 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (safe)
1041 {
1042 --sandbox;
1043 restore_funccal(save_funccalp);
1044 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001045
1046 if (ret == FAIL)
1047 clear_tv(rettv);
1048
1049 return ret;
1050}
1051
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001052/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001053 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001054 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001055 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1056 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001057 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001058 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001059call_func_retnr(
1060 char_u *func,
1061 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001062 typval_T *argv,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001064{
1065 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001066 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001067
Bram Moolenaarffa96842018-06-12 22:05:14 +02001068 if (call_vim_function(func, argc, argv, &rettv, safe) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001069 return -1;
1070
1071 retval = get_tv_number_chk(&rettv, NULL);
1072 clear_tv(&rettv);
1073 return retval;
1074}
1075
1076#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1077 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1078
Bram Moolenaar4f688582007-07-24 12:34:30 +00001079# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001080/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001081 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001082 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001083 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1084 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001085 */
1086 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001087call_func_retstr(
1088 char_u *func,
1089 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001090 typval_T *argv,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001091 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001092{
1093 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001094 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095
Bram Moolenaarffa96842018-06-12 22:05:14 +02001096 if (call_vim_function(func, argc, argv, &rettv, safe) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001097 return NULL;
1098
1099 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 return retval;
1102}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001103# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001104
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001105/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001106 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001107 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1108 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001109 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001110 */
1111 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112call_func_retlist(
1113 char_u *func,
1114 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001115 typval_T *argv,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001116 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001117{
1118 typval_T rettv;
1119
Bram Moolenaarffa96842018-06-12 22:05:14 +02001120 if (call_vim_function(func, argc, argv, &rettv, safe) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001121 return NULL;
1122
1123 if (rettv.v_type != VAR_LIST)
1124 {
1125 clear_tv(&rettv);
1126 return NULL;
1127 }
1128
1129 return rettv.vval.v_list;
1130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#endif
1132
Bram Moolenaar05159a02005-02-26 23:04:13 +00001133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_FOLDING
1135/*
1136 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1137 * it in "*cp". Doesn't give error messages.
1138 */
1139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001140eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
Bram Moolenaar33570922005-01-25 22:26:29 +00001142 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001143 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001145 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1146 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001149 if (use_sandbox)
1150 ++sandbox;
1151 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 retval = 0;
1155 else
1156 {
1157 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 if (tv.v_type == VAR_NUMBER)
1159 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001160 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 retval = 0;
1162 else
1163 {
1164 /* If the result is a string, check if there is a non-digit before
1165 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (!VIM_ISDIGIT(*s) && *s != '-')
1168 *cp = *s++;
1169 retval = atol((char *)s);
1170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001174 if (use_sandbox)
1175 --sandbox;
1176 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001178 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180#endif
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 * ":let" list all variable values
1184 * ":let var1 var2" list variable values
1185 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001186 * ":let var += expr" assignment command.
1187 * ":let var -= expr" assignment command.
1188 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001189 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 */
1191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001192ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193{
1194 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001196 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 int var_count = 0;
1199 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001200 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001201 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001202 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaardb552d602006-03-23 22:59:57 +00001204 argend = skip_var_list(arg, &var_count, &semicolon);
1205 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001206 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001207 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1208 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001209 expr = skipwhite(argend);
1210 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1211 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001213 /*
1214 * ":let" without "=": list variables
1215 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001216 if (*arg == '[')
1217 EMSG(_(e_invarg));
1218 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001220 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001223 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001224 list_glob_vars(&first);
1225 list_buf_vars(&first);
1226 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001227 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001228 list_script_vars(&first);
1229 list_func_vars(&first);
1230 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 eap->nextcmd = check_nextcmd(arg);
1233 }
1234 else
1235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001236 op[0] = '=';
1237 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001238 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001239 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001240 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1241 op[0] = *expr; /* +=, -= or .= */
1242 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001243 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001244 else
1245 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (eap->skip)
1248 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001249 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (eap->skip)
1251 {
1252 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001253 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 --emsg_skip;
1255 }
1256 else if (i != FAIL)
1257 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001258 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001259 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 }
1263}
1264
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001265/*
1266 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1267 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 * When "nextchars" is not NULL it points to a string with characters that
1269 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1270 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271 * Returns OK or FAIL;
1272 */
1273 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274ex_let_vars(
1275 char_u *arg_start,
1276 typval_T *tv,
1277 int copy, /* copy values from "tv", don't move */
1278 int semicolon, /* from skip_var_list() */
1279 int var_count, /* from skip_var_list() */
1280 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281{
1282 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001283 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001284 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001285 listitem_T *item;
1286 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001287
1288 if (*arg != '[')
1289 {
1290 /*
1291 * ":let var = expr" or ":for var in list"
1292 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001293 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001294 return FAIL;
1295 return OK;
1296 }
1297
1298 /*
1299 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1300 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001301 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001302 {
1303 EMSG(_(e_listreq));
1304 return FAIL;
1305 }
1306
1307 i = list_len(l);
1308 if (semicolon == 0 && var_count < i)
1309 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001310 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001311 return FAIL;
1312 }
1313 if (var_count - semicolon > i)
1314 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001315 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001316 return FAIL;
1317 }
1318
1319 item = l->lv_first;
1320 while (*arg != ']')
1321 {
1322 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001323 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 item = item->li_next;
1325 if (arg == NULL)
1326 return FAIL;
1327
1328 arg = skipwhite(arg);
1329 if (*arg == ';')
1330 {
1331 /* Put the rest of the list (may be empty) in the var after ';'.
1332 * Create a new list for this. */
1333 l = list_alloc();
1334 if (l == NULL)
1335 return FAIL;
1336 while (item != NULL)
1337 {
1338 list_append_tv(l, &item->li_tv);
1339 item = item->li_next;
1340 }
1341
1342 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001343 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001344 ltv.vval.v_list = l;
1345 l->lv_refcount = 1;
1346
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001347 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1348 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001349 clear_tv(&ltv);
1350 if (arg == NULL)
1351 return FAIL;
1352 break;
1353 }
1354 else if (*arg != ',' && *arg != ']')
1355 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001356 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001357 return FAIL;
1358 }
1359 }
1360
1361 return OK;
1362}
1363
1364/*
1365 * Skip over assignable variable "var" or list of variables "[var, var]".
1366 * Used for ":let varvar = expr" and ":for varvar in expr".
1367 * For "[var, var]" increment "*var_count" for each variable.
1368 * for "[var, var; var]" set "semicolon".
1369 * Return NULL for an error.
1370 */
1371 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372skip_var_list(
1373 char_u *arg,
1374 int *var_count,
1375 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001376{
1377 char_u *p, *s;
1378
1379 if (*arg == '[')
1380 {
1381 /* "[var, var]": find the matching ']'. */
1382 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001383 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001384 {
1385 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1386 s = skip_var_one(p);
1387 if (s == p)
1388 {
1389 EMSG2(_(e_invarg2), p);
1390 return NULL;
1391 }
1392 ++*var_count;
1393
1394 p = skipwhite(s);
1395 if (*p == ']')
1396 break;
1397 else if (*p == ';')
1398 {
1399 if (*semicolon == 1)
1400 {
1401 EMSG(_("Double ; in list of variables"));
1402 return NULL;
1403 }
1404 *semicolon = 1;
1405 }
1406 else if (*p != ',')
1407 {
1408 EMSG2(_(e_invarg2), p);
1409 return NULL;
1410 }
1411 }
1412 return p + 1;
1413 }
1414 else
1415 return skip_var_one(arg);
1416}
1417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001418/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001419 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001420 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001421 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001423skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001425 if (*arg == '@' && arg[1] != NUL)
1426 return arg + 2;
1427 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1428 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429}
1430
Bram Moolenaara7043832005-01-21 11:56:39 +00001431/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 * List variables for hashtab "ht" with prefix "prefix".
1433 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001434 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001436list_hashtable_vars(
1437 hashtab_T *ht,
1438 char_u *prefix,
1439 int empty,
1440 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001441{
Bram Moolenaar33570922005-01-25 22:26:29 +00001442 hashitem_T *hi;
1443 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001444 int todo;
1445
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001446 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001447 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1448 {
1449 if (!HASHITEM_EMPTY(hi))
1450 {
1451 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001452 di = HI2DI(hi);
1453 if (empty || di->di_tv.v_type != VAR_STRING
1454 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001455 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001456 }
1457 }
1458}
1459
1460/*
1461 * List global variables.
1462 */
1463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001464list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001465{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001466 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001467}
1468
1469/*
1470 * List buffer variables.
1471 */
1472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001474{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001475 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001477}
1478
1479/*
1480 * List window variables.
1481 */
1482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001484{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001485 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001486 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001487}
1488
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001489/*
1490 * List tab page variables.
1491 */
1492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001494{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001495 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001496 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001497}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001498
Bram Moolenaara7043832005-01-21 11:56:39 +00001499/*
1500 * List Vim variables.
1501 */
1502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001505 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506}
1507
1508/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001509 * List script-local variables, if there is a script.
1510 */
1511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001513{
1514 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001515 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1516 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001517}
1518
1519/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 * List variables in "arg".
1521 */
1522 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524{
1525 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001528 char_u *name_start;
1529 char_u *arg_subsc;
1530 char_u *tofree;
1531 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532
1533 while (!ends_excmd(*arg) && !got_int)
1534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001537 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001538 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001539 {
1540 emsg_severe = TRUE;
1541 EMSG(_(e_trailing));
1542 break;
1543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001547 /* get_name_len() takes care of expanding curly braces */
1548 name_start = name = arg;
1549 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1550 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 /* This is mainly to keep test 49 working: when expanding
1553 * curly braces fails overrule the exception error message. */
1554 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001556 emsg_severe = TRUE;
1557 EMSG2(_(e_invarg2), arg);
1558 break;
1559 }
1560 error = TRUE;
1561 }
1562 else
1563 {
1564 if (tofree != NULL)
1565 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001566 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001567 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001568 else
1569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001570 /* handle d.key, l[idx], f(expr) */
1571 arg_subsc = arg;
1572 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001574 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001576 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001579 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001580 case 'g': list_glob_vars(first); break;
1581 case 'b': list_buf_vars(first); break;
1582 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001583 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001584 case 'v': list_vim_vars(first); break;
1585 case 's': list_script_vars(first); break;
1586 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001587 default:
1588 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001589 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001590 }
1591 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592 {
1593 char_u numbuf[NUMBUFLEN];
1594 char_u *tf;
1595 int c;
1596 char_u *s;
1597
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001598 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599 c = *arg;
1600 *arg = NUL;
1601 list_one_var_a((char_u *)"",
1602 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001603 tv.v_type,
1604 s == NULL ? (char_u *)"" : s,
1605 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001606 *arg = c;
1607 vim_free(tf);
1608 }
1609 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001610 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 }
1612 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001613
1614 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001616
1617 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 }
1619
1620 return arg;
1621}
1622
1623/*
1624 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1625 * Returns a pointer to the char just after the var name.
1626 * Returns NULL if there is an error.
1627 */
1628 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001629ex_let_one(
1630 char_u *arg, /* points to variable name */
1631 typval_T *tv, /* value to assign to variable */
1632 int copy, /* copy value from "tv" */
1633 char_u *endchars, /* valid chars after variable name or NULL */
1634 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635{
1636 int c1;
1637 char_u *name;
1638 char_u *p;
1639 char_u *arg_end = NULL;
1640 int len;
1641 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643
1644 /*
1645 * ":let $VAR = expr": Set environment variable.
1646 */
1647 if (*arg == '$')
1648 {
1649 /* Find the end of the name. */
1650 ++arg;
1651 name = arg;
1652 len = get_env_len(&arg);
1653 if (len == 0)
1654 EMSG2(_(e_invarg2), name - 1);
1655 else
1656 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 if (op != NULL && (*op == '+' || *op == '-'))
1658 EMSG2(_(e_letwrong), op);
1659 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001662 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 {
1664 c1 = name[len];
1665 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001666 p = get_tv_string_chk(tv);
1667 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 {
1669 int mustfree = FALSE;
1670 char_u *s = vim_getenv(name, &mustfree);
1671
1672 if (s != NULL)
1673 {
1674 p = tofree = concat_str(s, p);
1675 if (mustfree)
1676 vim_free(s);
1677 }
1678 }
1679 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001682 if (STRICMP(name, "HOME") == 0)
1683 init_homedir();
1684 else if (didset_vim && STRICMP(name, "VIM") == 0)
1685 didset_vim = FALSE;
1686 else if (didset_vimruntime
1687 && STRICMP(name, "VIMRUNTIME") == 0)
1688 didset_vimruntime = FALSE;
1689 arg_end = arg;
1690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 }
1694 }
1695 }
1696
1697 /*
1698 * ":let &option = expr": Set option value.
1699 * ":let &l:option = expr": Set local option value.
1700 * ":let &g:option = expr": Set global option value.
1701 */
1702 else if (*arg == '&')
1703 {
1704 /* Find the end of the name. */
1705 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 if (p == NULL || (endchars != NULL
1707 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708 EMSG(_(e_letunexp));
1709 else
1710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001711 long n;
1712 int opt_type;
1713 long numval;
1714 char_u *stringval = NULL;
1715 char_u *s;
1716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 c1 = *p;
1718 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001720 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001721 s = get_tv_string_chk(tv); /* != NULL if number or string */
1722 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 {
1724 opt_type = get_option_value(arg, &numval,
1725 &stringval, opt_flags);
1726 if ((opt_type == 1 && *op == '.')
1727 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001728 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001729 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001730 s = NULL; /* don't set the value */
1731 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 else
1733 {
1734 if (opt_type == 1) /* number */
1735 {
1736 if (*op == '+')
1737 n = numval + n;
1738 else
1739 n = numval - n;
1740 }
1741 else if (opt_type == 0 && stringval != NULL) /* string */
1742 {
1743 s = concat_str(stringval, s);
1744 vim_free(stringval);
1745 stringval = s;
1746 }
1747 }
1748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001749 if (s != NULL)
1750 {
1751 set_option_value(arg, n, s, opt_flags);
1752 arg_end = p;
1753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001754 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001756 }
1757 }
1758
1759 /*
1760 * ":let @r = expr": Set register contents.
1761 */
1762 else if (*arg == '@')
1763 {
1764 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 if (op != NULL && (*op == '+' || *op == '-'))
1766 EMSG2(_(e_letwrong), op);
1767 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001769 EMSG(_(e_letunexp));
1770 else
1771 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001772 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 char_u *s;
1774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001775 p = get_tv_string_chk(tv);
1776 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001778 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 if (s != NULL)
1780 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001781 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 vim_free(s);
1783 }
1784 }
1785 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001787 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001788 arg_end = arg + 1;
1789 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001790 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 }
1792 }
1793
1794 /*
1795 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001798 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001800 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001802 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001805 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1806 EMSG(_(e_letunexp));
1807 else
1808 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001809 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001810 arg_end = p;
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001813 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 }
1815
1816 else
1817 EMSG2(_(e_invarg2), arg);
1818
1819 return arg_end;
1820}
1821
1822/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001823 * Get an lval: variable, Dict item or List item that can be assigned a value
1824 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1825 * "name.key", "name.key[expr]" etc.
1826 * Indexing only works if "name" is an existing List or Dictionary.
1827 * "name" points to the start of the name.
1828 * If "rettv" is not NULL it points to the value to be assigned.
1829 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1830 * wrong; must end in space or cmd separator.
1831 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001832 * flags:
1833 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001834 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001835 * GLV_NO_AUTOLOAD: do not use script autoloading
1836 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001838 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001839 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842get_lval(
1843 char_u *name,
1844 typval_T *rettv,
1845 lval_T *lp,
1846 int unlet,
1847 int skip,
1848 int flags, /* GLV_ values */
1849 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 char_u *expr_start, *expr_end;
1853 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 dictitem_T *v;
1855 typval_T var1;
1856 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001857 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001859 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001860 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001862 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001865 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001866
1867 if (skip)
1868 {
1869 /* When skipping just find the end of the name. */
1870 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001871 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 }
1873
1874 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001875 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001876 if (expr_start != NULL)
1877 {
1878 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001879 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001880 && *p != '[' && *p != '.')
1881 {
1882 EMSG(_(e_trailing));
1883 return NULL;
1884 }
1885
1886 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1887 if (lp->ll_exp_name == NULL)
1888 {
1889 /* Report an invalid expression in braces, unless the
1890 * expression evaluation has been cancelled due to an
1891 * aborting error, an interrupt, or an exception. */
1892 if (!aborting() && !quiet)
1893 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001894 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 EMSG2(_(e_invarg2), name);
1896 return NULL;
1897 }
1898 }
1899 lp->ll_name = lp->ll_exp_name;
1900 }
1901 else
1902 lp->ll_name = name;
1903
1904 /* Without [idx] or .key we are done. */
1905 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1906 return p;
1907
1908 cc = *p;
1909 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001910 /* Only pass &ht when we would write to the variable, it prevents autoload
1911 * as well. */
1912 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1913 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 if (v == NULL && !quiet)
1915 EMSG2(_(e_undefvar), lp->ll_name);
1916 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 if (v == NULL)
1918 return NULL;
1919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 /*
1921 * Loop until no more [idx] or .key is following.
1922 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001924 var1.v_type = VAR_UNKNOWN;
1925 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001926 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1929 && !(lp->ll_tv->v_type == VAR_DICT
1930 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 if (!quiet)
1933 EMSG(_("E689: Can only index a List or Dictionary"));
1934 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 if (!quiet)
1939 EMSG(_("E708: [:] must come last"));
1940 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001942
Bram Moolenaar8c711452005-01-14 21:53:12 +00001943 len = -1;
1944 if (*p == '.')
1945 {
1946 key = p + 1;
1947 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1948 ;
1949 if (len == 0)
1950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (!quiet)
1952 EMSG(_(e_emptykey));
1953 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001954 }
1955 p = key + len;
1956 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001957 else
1958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001959 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001960 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 if (*p == ':')
1962 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001963 else
1964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 empty1 = FALSE;
1966 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 if (get_tv_string_chk(&var1) == NULL)
1969 {
1970 /* not a number or string */
1971 clear_tv(&var1);
1972 return NULL;
1973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 }
1975
1976 /* Optionally get the second index [ :expr]. */
1977 if (*p == ':')
1978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001981 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001983 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001985 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 if (rettv != NULL && (rettv->v_type != VAR_LIST
1987 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 if (!quiet)
1990 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001991 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 }
1994 p = skipwhite(p + 1);
1995 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001997 else
1998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002000 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2001 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002002 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002004 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002005 if (get_tv_string_chk(&var2) == NULL)
2006 {
2007 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002008 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002009 clear_tv(&var2);
2010 return NULL;
2011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002014 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002017
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 if (!quiet)
2021 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002022 clear_tv(&var1);
2023 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 }
2026
2027 /* Skip to past ']'. */
2028 ++p;
2029 }
2030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002032 {
2033 if (len == -1)
2034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002036 key = get_tv_string_chk(&var1); /* is number or string */
2037 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002038 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 }
2042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 lp->ll_list = NULL;
2044 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002045 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002046
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002047 /* When assigning to a scope dictionary check that a function and
2048 * variable name is valid (only variable name unless it is l: or
2049 * g: dictionary). Disallow overwriting a builtin function. */
2050 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002051 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002052 int prevval;
2053 int wrong;
2054
2055 if (len != -1)
2056 {
2057 prevval = key[len];
2058 key[len] = NUL;
2059 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002060 else
2061 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002062 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2063 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002064 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002065 || !valid_varname(key);
2066 if (len != -1)
2067 key[len] = prevval;
2068 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002069 return NULL;
2070 }
2071
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002074 /* Can't add "v:" variable. */
2075 if (lp->ll_dict == &vimvardict)
2076 {
2077 EMSG2(_(e_illvar), name);
2078 return NULL;
2079 }
2080
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002081 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002086 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 }
2089 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002093 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 p = NULL;
2096 break;
2097 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002098 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002099 else if ((flags & GLV_READ_ONLY) == 0
2100 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002101 {
2102 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002103 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002104 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002105
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002106 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 }
2109 else
2110 {
2111 /*
2112 * Get the number and item for the only or first index of the List.
2113 */
2114 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002117 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002118 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002119 clear_tv(&var1);
2120
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 lp->ll_list = lp->ll_tv->vval.v_list;
2123 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2124 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002126 if (lp->ll_n1 < 0)
2127 {
2128 lp->ll_n1 = 0;
2129 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2130 }
2131 }
2132 if (lp->ll_li == NULL)
2133 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002134 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002135 if (!quiet)
2136 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
2139
2140 /*
2141 * May need to find the item or absolute index for the second
2142 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 * When no index given: "lp->ll_empty2" is TRUE.
2144 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002145 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002148 lp->ll_n2 = (long)get_tv_number(&var2);
2149 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002150 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002154 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002155 {
2156 if (!quiet)
2157 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002159 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002161 }
2162
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2164 if (lp->ll_n1 < 0)
2165 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2166 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002167 {
2168 if (!quiet)
2169 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002171 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002172 }
2173
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 }
2177
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002178 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 return p;
2180}
2181
2182/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187{
2188 vim_free(lp->ll_exp_name);
2189 vim_free(lp->ll_newkey);
2190}
2191
2192/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002193 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002195 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 */
2197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198set_var_lval(
2199 lval_T *lp,
2200 char_u *endp,
2201 typval_T *rettv,
2202 int copy,
2203 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204{
2205 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 listitem_T *ri;
2207 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208
2209 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002211 cc = *endp;
2212 *endp = NUL;
2213 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002215 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002216
Bram Moolenaar79518e22017-02-17 16:31:35 +01002217 /* handle +=, -= and .= */
2218 di = NULL;
2219 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2220 &tv, &di, TRUE, FALSE) == OK)
2221 {
2222 if ((di == NULL
2223 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2224 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2225 FALSE)))
2226 && tv_op(&tv, rettv, op) == OK)
2227 set_var(lp->ll_name, &tv, FALSE);
2228 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002231 else
2232 set_var(lp->ll_name, rettv, copy);
2233 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002235 else if (tv_check_lock(lp->ll_newkey == NULL
2236 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002237 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002238 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 else if (lp->ll_range)
2240 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002241 listitem_T *ll_li = lp->ll_li;
2242 int ll_n1 = lp->ll_n1;
2243
2244 /*
2245 * Check whether any of the list items is locked
2246 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002247 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002248 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002249 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002250 return;
2251 ri = ri->li_next;
2252 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2253 break;
2254 ll_li = ll_li->li_next;
2255 ++ll_n1;
2256 }
2257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 /*
2259 * Assign the List values to the list items.
2260 */
2261 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002262 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002263 if (op != NULL && *op != '=')
2264 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2265 else
2266 {
2267 clear_tv(&lp->ll_li->li_tv);
2268 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2269 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 ri = ri->li_next;
2271 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2272 break;
2273 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002274 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002276 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002277 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 ri = NULL;
2279 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002280 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002281 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 lp->ll_li = lp->ll_li->li_next;
2283 ++lp->ll_n1;
2284 }
2285 if (ri != NULL)
2286 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002287 else if (lp->ll_empty2
2288 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 : lp->ll_n1 != lp->ll_n2)
2290 EMSG(_("E711: List value has not enough items"));
2291 }
2292 else
2293 {
2294 /*
2295 * Assign to a List or Dictionary item.
2296 */
2297 if (lp->ll_newkey != NULL)
2298 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 if (op != NULL && *op != '=')
2300 {
2301 EMSG2(_(e_letwrong), op);
2302 return;
2303 }
2304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002306 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (di == NULL)
2308 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002309 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2310 {
2311 vim_free(di);
2312 return;
2313 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002315 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 else if (op != NULL && *op != '=')
2317 {
2318 tv_op(lp->ll_tv, rettv, op);
2319 return;
2320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002323
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 /*
2325 * Assign the value to the variable or list item.
2326 */
2327 if (copy)
2328 copy_tv(rettv, lp->ll_tv);
2329 else
2330 {
2331 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002332 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336}
2337
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002338/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2340 * Returns OK or FAIL.
2341 */
2342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002343tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002345 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u numbuf[NUMBUFLEN];
2347 char_u *s;
2348
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002349 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2350 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2351 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 {
2353 switch (tv1->v_type)
2354 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002355 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 case VAR_DICT:
2357 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002358 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002359 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002360 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002361 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 break;
2363
2364 case VAR_LIST:
2365 if (*op != '+' || tv2->v_type != VAR_LIST)
2366 break;
2367 /* List += List */
2368 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2369 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2370 return OK;
2371
2372 case VAR_NUMBER:
2373 case VAR_STRING:
2374 if (tv2->v_type == VAR_LIST)
2375 break;
2376 if (*op == '+' || *op == '-')
2377 {
2378 /* nr += nr or nr -= nr*/
2379 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002380#ifdef FEAT_FLOAT
2381 if (tv2->v_type == VAR_FLOAT)
2382 {
2383 float_T f = n;
2384
2385 if (*op == '+')
2386 f += tv2->vval.v_float;
2387 else
2388 f -= tv2->vval.v_float;
2389 clear_tv(tv1);
2390 tv1->v_type = VAR_FLOAT;
2391 tv1->vval.v_float = f;
2392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002394#endif
2395 {
2396 if (*op == '+')
2397 n += get_tv_number(tv2);
2398 else
2399 n -= get_tv_number(tv2);
2400 clear_tv(tv1);
2401 tv1->v_type = VAR_NUMBER;
2402 tv1->vval.v_number = n;
2403 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 }
2405 else
2406 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002407 if (tv2->v_type == VAR_FLOAT)
2408 break;
2409
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 /* str .= str */
2411 s = get_tv_string(tv1);
2412 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2413 clear_tv(tv1);
2414 tv1->v_type = VAR_STRING;
2415 tv1->vval.v_string = s;
2416 }
2417 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002418
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002419 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002420#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002421 {
2422 float_T f;
2423
2424 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2425 && tv2->v_type != VAR_NUMBER
2426 && tv2->v_type != VAR_STRING))
2427 break;
2428 if (tv2->v_type == VAR_FLOAT)
2429 f = tv2->vval.v_float;
2430 else
2431 f = get_tv_number(tv2);
2432 if (*op == '+')
2433 tv1->vval.v_float += f;
2434 else
2435 tv1->vval.v_float -= f;
2436 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002437#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002438 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 }
2440 }
2441
2442 EMSG2(_(e_letwrong), op);
2443 return FAIL;
2444}
2445
2446/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 * Evaluate the expression used in a ":for var in expr" command.
2448 * "arg" points to "var".
2449 * Set "*errp" to TRUE for an error, FALSE otherwise;
2450 * Return a pointer that holds the info. Null when there is an error.
2451 */
2452 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453eval_for_line(
2454 char_u *arg,
2455 int *errp,
2456 char_u **nextcmdp,
2457 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002458{
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002460 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002461 typval_T tv;
2462 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002463
2464 *errp = TRUE; /* default: there is an error */
2465
Bram Moolenaar33570922005-01-25 22:26:29 +00002466 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 if (fi == NULL)
2468 return NULL;
2469
2470 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2471 if (expr == NULL)
2472 return fi;
2473
2474 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002475 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002476 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002477 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002478 return fi;
2479 }
2480
2481 if (skip)
2482 ++emsg_skip;
2483 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2484 {
2485 *errp = FALSE;
2486 if (!skip)
2487 {
2488 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002489 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002490 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002491 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002492 clear_tv(&tv);
2493 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002494 else if (l == NULL)
2495 {
2496 /* a null list is like an empty list: do nothing */
2497 clear_tv(&tv);
2498 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002499 else
2500 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002501 /* No need to increment the refcount, it's already set for the
2502 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503 fi->fi_list = l;
2504 list_add_watch(l, &fi->fi_lw);
2505 fi->fi_lw.lw_item = l->lv_first;
2506 }
2507 }
2508 }
2509 if (skip)
2510 --emsg_skip;
2511
2512 return fi;
2513}
2514
2515/*
2516 * Use the first item in a ":for" list. Advance to the next.
2517 * Assign the values to the variable (list). "arg" points to the first one.
2518 * Return TRUE when a valid item was found, FALSE when at end of list or
2519 * something wrong.
2520 */
2521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002522next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002524 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527
2528 item = fi->fi_lw.lw_item;
2529 if (item == NULL)
2530 result = FALSE;
2531 else
2532 {
2533 fi->fi_lw.lw_item = item->li_next;
2534 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2535 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2536 }
2537 return result;
2538}
2539
2540/*
2541 * Free the structure used to store info used by ":for".
2542 */
2543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002544free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545{
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002548 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002549 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002550 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002551 list_unref(fi->fi_list);
2552 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002553 vim_free(fi);
2554}
2555
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2557
2558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002559set_context_for_expression(
2560 expand_T *xp,
2561 char_u *arg,
2562 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int got_eq = FALSE;
2565 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002566 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002568 if (cmdidx == CMD_let)
2569 {
2570 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002571 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002572 {
2573 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002574 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002575 {
2576 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002577 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002578 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002579 break;
2580 }
2581 return;
2582 }
2583 }
2584 else
2585 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2586 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 while ((xp->xp_pattern = vim_strpbrk(arg,
2588 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2589 {
2590 c = *xp->xp_pattern;
2591 if (c == '&')
2592 {
2593 c = xp->xp_pattern[1];
2594 if (c == '&')
2595 {
2596 ++xp->xp_pattern;
2597 xp->xp_context = cmdidx != CMD_let || got_eq
2598 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2599 }
2600 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002603 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2604 xp->xp_pattern += 2;
2605
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608 else if (c == '$')
2609 {
2610 /* environment variable */
2611 xp->xp_context = EXPAND_ENV_VARS;
2612 }
2613 else if (c == '=')
2614 {
2615 got_eq = TRUE;
2616 xp->xp_context = EXPAND_EXPRESSION;
2617 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002618 else if (c == '#'
2619 && xp->xp_context == EXPAND_EXPRESSION)
2620 {
2621 /* Autoload function/variable contains '#'. */
2622 break;
2623 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002624 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 && xp->xp_context == EXPAND_FUNCTIONS
2626 && vim_strchr(xp->xp_pattern, '(') == NULL)
2627 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002628 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 break;
2630 }
2631 else if (cmdidx != CMD_let || got_eq)
2632 {
2633 if (c == '"') /* string */
2634 {
2635 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2636 if (c == '\\' && xp->xp_pattern[1] != NUL)
2637 ++xp->xp_pattern;
2638 xp->xp_context = EXPAND_NOTHING;
2639 }
2640 else if (c == '\'') /* literal string */
2641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2644 /* skip */ ;
2645 xp->xp_context = EXPAND_NOTHING;
2646 }
2647 else if (c == '|')
2648 {
2649 if (xp->xp_pattern[1] == '|')
2650 {
2651 ++xp->xp_pattern;
2652 xp->xp_context = EXPAND_EXPRESSION;
2653 }
2654 else
2655 xp->xp_context = EXPAND_COMMANDS;
2656 }
2657 else
2658 xp->xp_context = EXPAND_EXPRESSION;
2659 }
2660 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002661 /* Doesn't look like something valid, expand as an expression
2662 * anyway. */
2663 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 arg = xp->xp_pattern;
2665 if (*arg != NUL)
2666 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2667 /* skip */ ;
2668 }
2669 xp->xp_pattern = arg;
2670}
2671
2672#endif /* FEAT_CMDL_COMPL */
2673
2674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * ":unlet[!] var1 ... " command.
2676 */
2677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002680 ex_unletlock(eap, eap->arg, 0);
2681}
2682
2683/*
2684 * ":lockvar" and ":unlockvar" commands
2685 */
2686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002690 int deep = 2;
2691
2692 if (eap->forceit)
2693 deep = -1;
2694 else if (vim_isdigit(*arg))
2695 {
2696 deep = getdigits(&arg);
2697 arg = skipwhite(arg);
2698 }
2699
2700 ex_unletlock(eap, arg, deep);
2701}
2702
2703/*
2704 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2705 */
2706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002707ex_unletlock(
2708 exarg_T *eap,
2709 char_u *argstart,
2710 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002711{
2712 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002715 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 do
2718 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002719 if (*arg == '$')
2720 {
2721 char_u *name = ++arg;
2722
2723 if (get_env_len(&arg) == 0)
2724 {
2725 EMSG2(_(e_invarg2), name - 1);
2726 return;
2727 }
2728 vim_unsetenv(name);
2729 arg = skipwhite(arg);
2730 continue;
2731 }
2732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002734 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002735 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lv.ll_name == NULL)
2737 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002738 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (name_end != NULL)
2742 {
2743 emsg_severe = TRUE;
2744 EMSG(_(e_trailing));
2745 }
2746 if (!(eap->skip || error))
2747 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 break;
2749 }
2750
2751 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002752 {
2753 if (eap->cmdidx == CMD_unlet)
2754 {
2755 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2756 error = TRUE;
2757 }
2758 else
2759 {
2760 if (do_lock_var(&lv, name_end, deep,
2761 eap->cmdidx == CMD_lockvar) == FAIL)
2762 error = TRUE;
2763 }
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!eap->skip)
2767 clear_lval(&lv);
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 arg = skipwhite(name_end);
2770 } while (!ends_excmd(*arg));
2771
2772 eap->nextcmd = check_nextcmd(arg);
2773}
2774
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002776do_unlet_var(
2777 lval_T *lp,
2778 char_u *name_end,
2779 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 int ret = OK;
2782 int cc;
2783
2784 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 cc = *name_end;
2787 *name_end = NUL;
2788
2789 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002790 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002794 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002795 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002796 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002797 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002798 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 else if (lp->ll_range)
2800 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002801 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002802 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002803 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002804
2805 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2806 {
2807 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002808 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002809 return FAIL;
2810 ll_li = li;
2811 ++ll_n1;
2812 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813
2814 /* Delete a range of List items. */
2815 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2816 {
2817 li = lp->ll_li->li_next;
2818 listitem_remove(lp->ll_list, lp->ll_li);
2819 lp->ll_li = li;
2820 ++lp->ll_n1;
2821 }
2822 }
2823 else
2824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 /* unlet a List item. */
2827 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002830 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 }
2832
2833 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834}
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836/*
2837 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002838 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
2840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 hashtab_T *ht;
2844 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002845 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002846 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002847 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 ht = find_var_ht(name, &varname);
2850 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002853 if (d == NULL)
2854 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 if (ht == &globvarht)
2856 d = &globvardict;
2857 else if (ht == &compat_hashtab)
2858 d = &vimvardict;
2859 else
2860 {
2861 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2862 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2863 }
2864 if (d == NULL)
2865 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002866 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 return FAIL;
2868 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002871 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002872 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002873 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002874 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002875 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002876 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002877 || var_check_ro(di->di_flags, name, FALSE)
2878 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002879 return FAIL;
2880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 delete_var(ht, hi);
2882 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 if (forceit)
2886 return OK;
2887 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 return FAIL;
2889}
2890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002891/*
2892 * Lock or unlock variable indicated by "lp".
2893 * "deep" is the levels to go (-1 for unlimited);
2894 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2895 */
2896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002897do_lock_var(
2898 lval_T *lp,
2899 char_u *name_end,
2900 int deep,
2901 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902{
2903 int ret = OK;
2904 int cc;
2905 dictitem_T *di;
2906
2907 if (deep == 0) /* nothing to do */
2908 return OK;
2909
2910 if (lp->ll_tv == NULL)
2911 {
2912 cc = *name_end;
2913 *name_end = NUL;
2914
2915 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002916 di = find_var(lp->ll_name, NULL, TRUE);
2917 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002919 else if ((di->di_flags & DI_FLAGS_FIX)
2920 && di->di_tv.v_type != VAR_DICT
2921 && di->di_tv.v_type != VAR_LIST)
2922 /* For historic reasons this error is not given for a list or dict.
2923 * E.g., the b: dict could be locked/unlocked. */
2924 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 else
2926 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002927 if (lock)
2928 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002929 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002930 di->di_flags &= ~DI_FLAGS_LOCK;
2931 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 }
2933 *name_end = cc;
2934 }
2935 else if (lp->ll_range)
2936 {
2937 listitem_T *li = lp->ll_li;
2938
2939 /* (un)lock a range of List items. */
2940 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2941 {
2942 item_lock(&li->li_tv, deep, lock);
2943 li = li->li_next;
2944 ++lp->ll_n1;
2945 }
2946 }
2947 else if (lp->ll_list != NULL)
2948 /* (un)lock a List item. */
2949 item_lock(&lp->ll_li->li_tv, deep, lock);
2950 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002951 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 item_lock(&lp->ll_di->di_tv, deep, lock);
2953
2954 return ret;
2955}
2956
2957/*
2958 * Lock or unlock an item. "deep" is nr of levels to go.
2959 */
2960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002961item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962{
2963 static int recurse = 0;
2964 list_T *l;
2965 listitem_T *li;
2966 dict_T *d;
2967 hashitem_T *hi;
2968 int todo;
2969
2970 if (recurse >= DICT_MAXNEST)
2971 {
2972 EMSG(_("E743: variable nested too deep for (un)lock"));
2973 return;
2974 }
2975 if (deep == 0)
2976 return;
2977 ++recurse;
2978
2979 /* lock/unlock the item itself */
2980 if (lock)
2981 tv->v_lock |= VAR_LOCKED;
2982 else
2983 tv->v_lock &= ~VAR_LOCKED;
2984
2985 switch (tv->v_type)
2986 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002987 case VAR_UNKNOWN:
2988 case VAR_NUMBER:
2989 case VAR_STRING:
2990 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002991 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002992 case VAR_FLOAT:
2993 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002994 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002995 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002996 break;
2997
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002998 case VAR_LIST:
2999 if ((l = tv->vval.v_list) != NULL)
3000 {
3001 if (lock)
3002 l->lv_lock |= VAR_LOCKED;
3003 else
3004 l->lv_lock &= ~VAR_LOCKED;
3005 if (deep < 0 || deep > 1)
3006 /* recursive: lock/unlock the items the List contains */
3007 for (li = l->lv_first; li != NULL; li = li->li_next)
3008 item_lock(&li->li_tv, deep - 1, lock);
3009 }
3010 break;
3011 case VAR_DICT:
3012 if ((d = tv->vval.v_dict) != NULL)
3013 {
3014 if (lock)
3015 d->dv_lock |= VAR_LOCKED;
3016 else
3017 d->dv_lock &= ~VAR_LOCKED;
3018 if (deep < 0 || deep > 1)
3019 {
3020 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003021 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003022 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3023 {
3024 if (!HASHITEM_EMPTY(hi))
3025 {
3026 --todo;
3027 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3028 }
3029 }
3030 }
3031 }
3032 }
3033 --recurse;
3034}
3035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3037/*
3038 * Delete all "menutrans_" variables.
3039 */
3040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003041del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
Bram Moolenaar33570922005-01-25 22:26:29 +00003043 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003044 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
Bram Moolenaar33570922005-01-25 22:26:29 +00003046 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003047 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003048 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003049 {
3050 if (!HASHITEM_EMPTY(hi))
3051 {
3052 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003053 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3054 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003055 }
3056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058}
3059#endif
3060
3061#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3062
3063/*
3064 * Local string buffer for the next two functions to store a variable name
3065 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3066 * get_user_var_name().
3067 */
3068
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003069static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071static char_u *varnamebuf = NULL;
3072static int varnamebuflen = 0;
3073
3074/*
3075 * Function to concatenate a prefix and a variable name.
3076 */
3077 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 int len;
3081
3082 len = (int)STRLEN(name) + 3;
3083 if (len > varnamebuflen)
3084 {
3085 vim_free(varnamebuf);
3086 len += 10; /* some additional space */
3087 varnamebuf = alloc(len);
3088 if (varnamebuf == NULL)
3089 {
3090 varnamebuflen = 0;
3091 return NULL;
3092 }
3093 varnamebuflen = len;
3094 }
3095 *varnamebuf = prefix;
3096 varnamebuf[1] = ':';
3097 STRCPY(varnamebuf + 2, name);
3098 return varnamebuf;
3099}
3100
3101/*
3102 * Function given to ExpandGeneric() to obtain the list of user defined
3103 * (global/buffer/window/built-in) variable names.
3104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003106get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003108 static long_u gdone;
3109 static long_u bdone;
3110 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003111 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003112 static int vidx;
3113 static hashitem_T *hi;
3114 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003117 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003118 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003119 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003120 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003121
3122 /* Global variables */
3123 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003125 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003127 else
3128 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003129 while (HASHITEM_EMPTY(hi))
3130 ++hi;
3131 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3132 return cat_prefix_varname('g', hi->hi_key);
3133 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003135
3136 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003137 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003140 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003142 else
3143 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003144 while (HASHITEM_EMPTY(hi))
3145 ++hi;
3146 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003148
3149 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003150 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003153 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003155 else
3156 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003157 while (HASHITEM_EMPTY(hi))
3158 ++hi;
3159 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003161
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003162 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003163 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003164 if (tdone < ht->ht_used)
3165 {
3166 if (tdone++ == 0)
3167 hi = ht->ht_array;
3168 else
3169 ++hi;
3170 while (HASHITEM_EMPTY(hi))
3171 ++hi;
3172 return cat_prefix_varname('t', hi->hi_key);
3173 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003174
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 /* v: variables */
3176 if (vidx < VV_LEN)
3177 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
Bram Moolenaard23a8232018-02-10 18:45:26 +01003179 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 varnamebuflen = 0;
3181 return NULL;
3182}
3183
3184#endif /* FEAT_CMDL_COMPL */
3185
3186/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003187 * Return TRUE if "pat" matches "text".
3188 * Does not use 'cpo' and always uses 'magic'.
3189 */
3190 static int
3191pattern_match(char_u *pat, char_u *text, int ic)
3192{
3193 int matches = FALSE;
3194 char_u *save_cpo;
3195 regmatch_T regmatch;
3196
3197 /* avoid 'l' flag in 'cpoptions' */
3198 save_cpo = p_cpo;
3199 p_cpo = (char_u *)"";
3200 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3201 if (regmatch.regprog != NULL)
3202 {
3203 regmatch.rm_ic = ic;
3204 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3205 vim_regfree(regmatch.regprog);
3206 }
3207 p_cpo = save_cpo;
3208 return matches;
3209}
3210
3211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003213 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3215 */
3216
3217/*
3218 * Handle zero level expression.
3219 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003220 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003221 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 * Return OK or FAIL.
3223 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225eval0(
3226 char_u *arg,
3227 typval_T *rettv,
3228 char_u **nextcmd,
3229 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 int ret;
3232 char_u *p;
3233
3234 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (ret == FAIL || !ends_excmd(*p))
3237 {
3238 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 /*
3241 * Report the invalid expression unless the expression evaluation has
3242 * been cancelled due to an aborting error, an interrupt, or an
3243 * exception.
3244 */
3245 if (!aborting())
3246 EMSG2(_(e_invexpr2), arg);
3247 ret = FAIL;
3248 }
3249 if (nextcmd != NULL)
3250 *nextcmd = check_nextcmd(p);
3251
3252 return ret;
3253}
3254
3255/*
3256 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003257 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 *
3259 * "arg" must point to the first non-white of the expression.
3260 * "arg" is advanced to the next non-white after the recognized expression.
3261 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003262 * Note: "rettv.v_lock" is not set.
3263 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 * Return OK or FAIL.
3265 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
3272 /*
3273 * Get the first variable.
3274 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003275 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return FAIL;
3277
3278 if ((*arg)[0] == '?')
3279 {
3280 result = FALSE;
3281 if (evaluate)
3282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003283 int error = FALSE;
3284
3285 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288 if (error)
3289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291
3292 /*
3293 * Get the second variable.
3294 */
3295 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003296 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return FAIL;
3298
3299 /*
3300 * Check for the ":".
3301 */
3302 if ((*arg)[0] != ':')
3303 {
3304 EMSG(_("E109: Missing ':' after '?'"));
3305 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003306 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return FAIL;
3308 }
3309
3310 /*
3311 * Get the third variable.
3312 */
3313 *arg = skipwhite(*arg + 1);
3314 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3315 {
3316 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003317 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 return FAIL;
3319 }
3320 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003321 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323
3324 return OK;
3325}
3326
3327/*
3328 * Handle first level expression:
3329 * expr2 || expr2 || expr2 logical OR
3330 *
3331 * "arg" must point to the first non-white of the expression.
3332 * "arg" is advanced to the next non-white after the recognized expression.
3333 *
3334 * Return OK or FAIL.
3335 */
3336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 long result;
3341 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
3344 /*
3345 * Get the first variable.
3346 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 return FAIL;
3349
3350 /*
3351 * Repeat until there is no following "||".
3352 */
3353 first = TRUE;
3354 result = FALSE;
3355 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3356 {
3357 if (evaluate && first)
3358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003361 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 if (error)
3363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 first = FALSE;
3365 }
3366
3367 /*
3368 * Get the second variable.
3369 */
3370 *arg = skipwhite(*arg + 2);
3371 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3372 return FAIL;
3373
3374 /*
3375 * Compute the result.
3376 */
3377 if (evaluate && !result)
3378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003379 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003382 if (error)
3383 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 if (evaluate)
3386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 rettv->v_type = VAR_NUMBER;
3388 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 }
3391
3392 return OK;
3393}
3394
3395/*
3396 * Handle second level expression:
3397 * expr3 && expr3 && expr3 logical AND
3398 *
3399 * "arg" must point to the first non-white of the expression.
3400 * "arg" is advanced to the next non-white after the recognized expression.
3401 *
3402 * Return OK or FAIL.
3403 */
3404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003405eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 long result;
3409 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 /*
3413 * Get the first variable.
3414 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 return FAIL;
3417
3418 /*
3419 * Repeat until there is no following "&&".
3420 */
3421 first = TRUE;
3422 result = TRUE;
3423 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3424 {
3425 if (evaluate && first)
3426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003427 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003430 if (error)
3431 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 first = FALSE;
3433 }
3434
3435 /*
3436 * Get the second variable.
3437 */
3438 *arg = skipwhite(*arg + 2);
3439 if (eval4(arg, &var2, evaluate && result) == FAIL)
3440 return FAIL;
3441
3442 /*
3443 * Compute the result.
3444 */
3445 if (evaluate && result)
3446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003450 if (error)
3451 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 if (evaluate)
3454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 rettv->v_type = VAR_NUMBER;
3456 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458 }
3459
3460 return OK;
3461}
3462
3463/*
3464 * Handle third level expression:
3465 * var1 == var2
3466 * var1 =~ var2
3467 * var1 != var2
3468 * var1 !~ var2
3469 * var1 > var2
3470 * var1 >= var2
3471 * var1 < var2
3472 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003473 * var1 is var2
3474 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 *
3476 * "arg" must point to the first non-white of the expression.
3477 * "arg" is advanced to the next non-white after the recognized expression.
3478 *
3479 * Return OK or FAIL.
3480 */
3481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003482eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *p;
3486 int i;
3487 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003488 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 /*
3493 * Get the first variable.
3494 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return FAIL;
3497
3498 p = *arg;
3499 switch (p[0])
3500 {
3501 case '=': if (p[1] == '=')
3502 type = TYPE_EQUAL;
3503 else if (p[1] == '~')
3504 type = TYPE_MATCH;
3505 break;
3506 case '!': if (p[1] == '=')
3507 type = TYPE_NEQUAL;
3508 else if (p[1] == '~')
3509 type = TYPE_NOMATCH;
3510 break;
3511 case '>': if (p[1] != '=')
3512 {
3513 type = TYPE_GREATER;
3514 len = 1;
3515 }
3516 else
3517 type = TYPE_GEQUAL;
3518 break;
3519 case '<': if (p[1] != '=')
3520 {
3521 type = TYPE_SMALLER;
3522 len = 1;
3523 }
3524 else
3525 type = TYPE_SEQUAL;
3526 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003527 case 'i': if (p[1] == 's')
3528 {
3529 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3530 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003531 i = p[len];
3532 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003533 {
3534 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3535 type_is = TRUE;
3536 }
3537 }
3538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
3541 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
3544 if (type != TYPE_UNKNOWN)
3545 {
3546 /* extra question mark appended: ignore case */
3547 if (p[len] == '?')
3548 {
3549 ic = TRUE;
3550 ++len;
3551 }
3552 /* extra '#' appended: match case */
3553 else if (p[len] == '#')
3554 {
3555 ic = FALSE;
3556 ++len;
3557 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003558 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 else
3560 ic = p_ic;
3561
3562 /*
3563 * Get the second variable.
3564 */
3565 *arg = skipwhite(p + len);
3566 if (eval5(arg, &var2, evaluate) == FAIL)
3567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003568 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return FAIL;
3570 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003571 if (evaluate)
3572 {
3573 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3574
3575 clear_tv(&var2);
3576 return ret;
3577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579
3580 return OK;
3581}
3582
3583/*
3584 * Handle fourth level expression:
3585 * + number addition
3586 * - number subtraction
3587 * . string concatenation
3588 *
3589 * "arg" must point to the first non-white of the expression.
3590 * "arg" is advanced to the next non-white after the recognized expression.
3591 *
3592 * Return OK or FAIL.
3593 */
3594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 typval_T var2;
3598 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003600 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003601#ifdef FEAT_FLOAT
3602 float_T f1 = 0, f2 = 0;
3603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 char_u *s1, *s2;
3605 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3606 char_u *p;
3607
3608 /*
3609 * Get the first variable.
3610 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003611 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 return FAIL;
3613
3614 /*
3615 * Repeat computing, until no '+', '-' or '.' is following.
3616 */
3617 for (;;)
3618 {
3619 op = **arg;
3620 if (op != '+' && op != '-' && op != '.')
3621 break;
3622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003623 if ((op != '+' || rettv->v_type != VAR_LIST)
3624#ifdef FEAT_FLOAT
3625 && (op == '.' || rettv->v_type != VAR_FLOAT)
3626#endif
3627 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003628 {
3629 /* For "list + ...", an illegal use of the first operand as
3630 * a number cannot be determined before evaluating the 2nd
3631 * operand: if this is also a list, all is ok.
3632 * For "something . ...", "something - ..." or "non-list + ...",
3633 * we know that the first operand needs to be a string or number
3634 * without evaluating the 2nd operand. So check before to avoid
3635 * side effects after an error. */
3636 if (evaluate && get_tv_string_chk(rettv) == NULL)
3637 {
3638 clear_tv(rettv);
3639 return FAIL;
3640 }
3641 }
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 /*
3644 * Get the second variable.
3645 */
3646 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003647 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return FAIL;
3651 }
3652
3653 if (evaluate)
3654 {
3655 /*
3656 * Compute the result.
3657 */
3658 if (op == '.')
3659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3661 s2 = get_tv_string_buf_chk(&var2, buf2);
3662 if (s2 == NULL) /* type error ? */
3663 {
3664 clear_tv(rettv);
3665 clear_tv(&var2);
3666 return FAIL;
3667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003668 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 clear_tv(rettv);
3670 rettv->v_type = VAR_STRING;
3671 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003673 else if (op == '+' && rettv->v_type == VAR_LIST
3674 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003675 {
3676 /* concatenate Lists */
3677 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3678 &var3) == FAIL)
3679 {
3680 clear_tv(rettv);
3681 clear_tv(&var2);
3682 return FAIL;
3683 }
3684 clear_tv(rettv);
3685 *rettv = var3;
3686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 else
3688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 int error = FALSE;
3690
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003691#ifdef FEAT_FLOAT
3692 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003693 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003694 f1 = rettv->vval.v_float;
3695 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003696 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003697 else
3698#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003699 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003700 n1 = get_tv_number_chk(rettv, &error);
3701 if (error)
3702 {
3703 /* This can only happen for "list + non-list". For
3704 * "non-list + ..." or "something - ...", we returned
3705 * before evaluating the 2nd operand. */
3706 clear_tv(rettv);
3707 return FAIL;
3708 }
3709#ifdef FEAT_FLOAT
3710 if (var2.v_type == VAR_FLOAT)
3711 f1 = n1;
3712#endif
3713 }
3714#ifdef FEAT_FLOAT
3715 if (var2.v_type == VAR_FLOAT)
3716 {
3717 f2 = var2.vval.v_float;
3718 n2 = 0;
3719 }
3720 else
3721#endif
3722 {
3723 n2 = get_tv_number_chk(&var2, &error);
3724 if (error)
3725 {
3726 clear_tv(rettv);
3727 clear_tv(&var2);
3728 return FAIL;
3729 }
3730#ifdef FEAT_FLOAT
3731 if (rettv->v_type == VAR_FLOAT)
3732 f2 = n2;
3733#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003735 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003736
3737#ifdef FEAT_FLOAT
3738 /* If there is a float on either side the result is a float. */
3739 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3740 {
3741 if (op == '+')
3742 f1 = f1 + f2;
3743 else
3744 f1 = f1 - f2;
3745 rettv->v_type = VAR_FLOAT;
3746 rettv->vval.v_float = f1;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003749#endif
3750 {
3751 if (op == '+')
3752 n1 = n1 + n2;
3753 else
3754 n1 = n1 - n2;
3755 rettv->v_type = VAR_NUMBER;
3756 rettv->vval.v_number = n1;
3757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003759 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 }
3762 return OK;
3763}
3764
3765/*
3766 * Handle fifth level expression:
3767 * * number multiplication
3768 * / number division
3769 * % number modulo
3770 *
3771 * "arg" must point to the first non-white of the expression.
3772 * "arg" is advanced to the next non-white after the recognized expression.
3773 *
3774 * Return OK or FAIL.
3775 */
3776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777eval6(
3778 char_u **arg,
3779 typval_T *rettv,
3780 int evaluate,
3781 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003785 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003786#ifdef FEAT_FLOAT
3787 int use_float = FALSE;
3788 float_T f1 = 0, f2;
3789#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003790 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /*
3793 * Get the first variable.
3794 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003795 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797
3798 /*
3799 * Repeat computing, until no '*', '/' or '%' is following.
3800 */
3801 for (;;)
3802 {
3803 op = **arg;
3804 if (op != '*' && op != '/' && op != '%')
3805 break;
3806
3807 if (evaluate)
3808 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003809#ifdef FEAT_FLOAT
3810 if (rettv->v_type == VAR_FLOAT)
3811 {
3812 f1 = rettv->vval.v_float;
3813 use_float = TRUE;
3814 n1 = 0;
3815 }
3816 else
3817#endif
3818 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003819 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003820 if (error)
3821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 else
3824 n1 = 0;
3825
3826 /*
3827 * Get the second variable.
3828 */
3829 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003830 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return FAIL;
3832
3833 if (evaluate)
3834 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003835#ifdef FEAT_FLOAT
3836 if (var2.v_type == VAR_FLOAT)
3837 {
3838 if (!use_float)
3839 {
3840 f1 = n1;
3841 use_float = TRUE;
3842 }
3843 f2 = var2.vval.v_float;
3844 n2 = 0;
3845 }
3846 else
3847#endif
3848 {
3849 n2 = get_tv_number_chk(&var2, &error);
3850 clear_tv(&var2);
3851 if (error)
3852 return FAIL;
3853#ifdef FEAT_FLOAT
3854 if (use_float)
3855 f2 = n2;
3856#endif
3857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 /*
3860 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003863#ifdef FEAT_FLOAT
3864 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003866 if (op == '*')
3867 f1 = f1 * f2;
3868 else if (op == '/')
3869 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003870# ifdef VMS
3871 /* VMS crashes on divide by zero, work around it */
3872 if (f2 == 0.0)
3873 {
3874 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003875 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003876 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003877 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003878 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003879 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003880 }
3881 else
3882 f1 = f1 / f2;
3883# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003884 /* We rely on the floating point library to handle divide
3885 * by zero to result in "inf" and not a crash. */
3886 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003887# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003890 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00003891 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892 return FAIL;
3893 }
3894 rettv->v_type = VAR_FLOAT;
3895 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003900 if (op == '*')
3901 n1 = n1 * n2;
3902 else if (op == '/')
3903 {
3904 if (n2 == 0) /* give an error message? */
3905 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003906 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003907 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003908 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003909 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003910 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003911 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003912 }
3913 else
3914 n1 = n1 / n2;
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003917 {
3918 if (n2 == 0) /* give an error message? */
3919 n1 = 0;
3920 else
3921 n1 = n1 % n2;
3922 }
3923 rettv->v_type = VAR_NUMBER;
3924 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 }
3928
3929 return OK;
3930}
3931
3932/*
3933 * Handle sixth level expression:
3934 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003935 * "string" string constant
3936 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 * &option-name option value
3938 * @r register contents
3939 * identifier variable value
3940 * function() function call
3941 * $VAR environment variable
3942 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003943 * [expr, expr] List
3944 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 *
3946 * Also handle:
3947 * ! in front logical NOT
3948 * - in front unary minus
3949 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003950 * trailing [] subscript in String or List
3951 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 *
3953 * "arg" must point to the first non-white of the expression.
3954 * "arg" is advanced to the next non-white after the recognized expression.
3955 *
3956 * Return OK or FAIL.
3957 */
3958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959eval7(
3960 char_u **arg,
3961 typval_T *rettv,
3962 int evaluate,
3963 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003965 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int len;
3967 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u *start_leader, *end_leader;
3969 int ret = OK;
3970 char_u *alias;
3971
3972 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003974 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003976 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003979 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 */
3981 start_leader = *arg;
3982 while (**arg == '!' || **arg == '-' || **arg == '+')
3983 *arg = skipwhite(*arg + 1);
3984 end_leader = *arg;
3985
3986 switch (**arg)
3987 {
3988 /*
3989 * Number constant.
3990 */
3991 case '0':
3992 case '1':
3993 case '2':
3994 case '3':
3995 case '4':
3996 case '5':
3997 case '6':
3998 case '7':
3999 case '8':
4000 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004001 {
4002#ifdef FEAT_FLOAT
4003 char_u *p = skipdigits(*arg + 1);
4004 int get_float = FALSE;
4005
4006 /* We accept a float when the format matches
4007 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004008 * strict to avoid backwards compatibility problems.
4009 * Don't look for a float after the "." operator, so that
4010 * ":let vers = 1.2.3" doesn't fail. */
4011 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004013 get_float = TRUE;
4014 p = skipdigits(p + 2);
4015 if (*p == 'e' || *p == 'E')
4016 {
4017 ++p;
4018 if (*p == '-' || *p == '+')
4019 ++p;
4020 if (!vim_isdigit(*p))
4021 get_float = FALSE;
4022 else
4023 p = skipdigits(p + 1);
4024 }
4025 if (ASCII_ISALPHA(*p) || *p == '.')
4026 get_float = FALSE;
4027 }
4028 if (get_float)
4029 {
4030 float_T f;
4031
4032 *arg += string2float(*arg, &f);
4033 if (evaluate)
4034 {
4035 rettv->v_type = VAR_FLOAT;
4036 rettv->vval.v_float = f;
4037 }
4038 }
4039 else
4040#endif
4041 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004042 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004043 *arg += len;
4044 if (evaluate)
4045 {
4046 rettv->v_type = VAR_NUMBER;
4047 rettv->vval.v_number = n;
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053 /*
4054 * String constant: "string".
4055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 break;
4058
4059 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004063 break;
4064
4065 /*
4066 * List: [expr, expr]
4067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
4070
4071 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004072 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 * Dictionary: {key: val, key: val}
4074 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004075 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4076 if (ret == NOTDONE)
4077 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004078 break;
4079
4080 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004081 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004083 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 break;
4085
4086 /*
4087 * Environment variable: $VAR.
4088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 break;
4091
4092 /*
4093 * Register contents: @r.
4094 */
4095 case '@': ++*arg;
4096 if (evaluate)
4097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004099 rettv->vval.v_string = get_reg_contents(**arg,
4100 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 if (**arg != NUL)
4103 ++*arg;
4104 break;
4105
4106 /*
4107 * nested expression: (expression).
4108 */
4109 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004110 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (**arg == ')')
4112 ++*arg;
4113 else if (ret == OK)
4114 {
4115 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 ret = FAIL;
4118 }
4119 break;
4120
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 break;
4123 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004124
4125 if (ret == NOTDONE)
4126 {
4127 /*
4128 * Must be a variable or function name.
4129 * Can also be a curly-braces kind of name: {expr}.
4130 */
4131 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004132 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004133 if (alias != NULL)
4134 s = alias;
4135
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004136 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004137 ret = FAIL;
4138 else
4139 {
4140 if (**arg == '(') /* recursive! */
4141 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004142 partial_T *partial;
4143
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004144 if (!evaluate)
4145 check_vars(s, len);
4146
Bram Moolenaar8c711452005-01-14 21:53:12 +00004147 /* If "s" is the name of a variable of type VAR_FUNC
4148 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004149 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004150
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004151 /* Need to make a copy, in case evaluating the arguments makes
4152 * the name invalid. */
4153 s = vim_strsave(s);
4154 if (s == NULL)
4155 ret = FAIL;
4156 else
4157 /* Invoke the function. */
4158 ret = get_func_tv(s, len, rettv, arg,
4159 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4160 &len, evaluate, partial, NULL);
4161 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004162
4163 /* If evaluate is FALSE rettv->v_type was not set in
4164 * get_func_tv, but it's needed in handle_subscript() to parse
4165 * what follows. So set it here. */
4166 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4167 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004168 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004169 rettv->v_type = VAR_FUNC;
4170 }
4171
Bram Moolenaar8c711452005-01-14 21:53:12 +00004172 /* Stop the expression evaluation when immediately
4173 * aborting on error, or when an interrupt occurred or
4174 * an exception was thrown but not caught. */
4175 if (aborting())
4176 {
4177 if (ret == OK)
4178 clear_tv(rettv);
4179 ret = FAIL;
4180 }
4181 }
4182 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004183 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004184 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004185 {
4186 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004187 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004188 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004189 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004190 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004191 }
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 *arg = skipwhite(*arg);
4194
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004195 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4196 * expr(expr). */
4197 if (ret == OK)
4198 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
4200 /*
4201 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4202 */
4203 if (ret == OK && evaluate && end_leader > start_leader)
4204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004206 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004207#ifdef FEAT_FLOAT
4208 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004209
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004210 if (rettv->v_type == VAR_FLOAT)
4211 f = rettv->vval.v_float;
4212 else
4213#endif
4214 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004217 clear_tv(rettv);
4218 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 else
4221 {
4222 while (end_leader > start_leader)
4223 {
4224 --end_leader;
4225 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004226 {
4227#ifdef FEAT_FLOAT
4228 if (rettv->v_type == VAR_FLOAT)
4229 f = !f;
4230 else
4231#endif
4232 val = !val;
4233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004235 {
4236#ifdef FEAT_FLOAT
4237 if (rettv->v_type == VAR_FLOAT)
4238 f = -f;
4239 else
4240#endif
4241 val = -val;
4242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004244#ifdef FEAT_FLOAT
4245 if (rettv->v_type == VAR_FLOAT)
4246 {
4247 clear_tv(rettv);
4248 rettv->vval.v_float = f;
4249 }
4250 else
4251#endif
4252 {
4253 clear_tv(rettv);
4254 rettv->v_type = VAR_NUMBER;
4255 rettv->vval.v_number = val;
4256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259
4260 return ret;
4261}
4262
4263/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004264 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4265 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4267 */
4268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004269eval_index(
4270 char_u **arg,
4271 typval_T *rettv,
4272 int evaluate,
4273 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274{
4275 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 long len = -1;
4279 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004281 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004282
Bram Moolenaara03f2332016-02-06 18:09:59 +01004283 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004285 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004286 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004287 if (verbose)
4288 EMSG(_("E695: Cannot index a Funcref"));
4289 return FAIL;
4290 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004291#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004292 if (verbose)
4293 EMSG(_(e_float_as_string));
4294 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004295#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004296 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004297 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004298 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004299 if (verbose)
4300 EMSG(_("E909: Cannot index a special variable"));
4301 return FAIL;
4302 case VAR_UNKNOWN:
4303 if (evaluate)
4304 return FAIL;
4305 /* FALLTHROUGH */
4306
4307 case VAR_STRING:
4308 case VAR_NUMBER:
4309 case VAR_LIST:
4310 case VAR_DICT:
4311 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004313
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004314 init_tv(&var1);
4315 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004316 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004317 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318 /*
4319 * dict.name
4320 */
4321 key = *arg + 1;
4322 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4323 ;
4324 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004325 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004327 }
4328 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004329 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004330 /*
4331 * something[idx]
4332 *
4333 * Get the (first) variable from inside the [].
4334 */
4335 *arg = skipwhite(*arg + 1);
4336 if (**arg == ':')
4337 empty1 = TRUE;
4338 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4339 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4341 {
4342 /* not a number or string */
4343 clear_tv(&var1);
4344 return FAIL;
4345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004346
4347 /*
4348 * Get the second variable from inside the [:].
4349 */
4350 if (**arg == ':')
4351 {
4352 range = TRUE;
4353 *arg = skipwhite(*arg + 1);
4354 if (**arg == ']')
4355 empty2 = TRUE;
4356 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (!empty1)
4359 clear_tv(&var1);
4360 return FAIL;
4361 }
4362 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4363 {
4364 /* not a number or string */
4365 if (!empty1)
4366 clear_tv(&var1);
4367 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 return FAIL;
4369 }
4370 }
4371
4372 /* Check for the ']'. */
4373 if (**arg != ']')
4374 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004375 if (verbose)
4376 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 clear_tv(&var1);
4378 if (range)
4379 clear_tv(&var2);
4380 return FAIL;
4381 }
4382 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004383 }
4384
4385 if (evaluate)
4386 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387 n1 = 0;
4388 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 n1 = get_tv_number(&var1);
4391 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004392 }
4393 if (range)
4394 {
4395 if (empty2)
4396 n2 = -1;
4397 else
4398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399 n2 = get_tv_number(&var2);
4400 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 }
4402 }
4403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004405 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004406 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004407 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004408 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004409 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004410 case VAR_SPECIAL:
4411 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004412 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004413 break; /* not evaluating, skipping over subscript */
4414
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004415 case VAR_NUMBER:
4416 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004418 len = (long)STRLEN(s);
4419 if (range)
4420 {
4421 /* The resulting variable is a substring. If the indexes
4422 * are out of range the result is empty. */
4423 if (n1 < 0)
4424 {
4425 n1 = len + n1;
4426 if (n1 < 0)
4427 n1 = 0;
4428 }
4429 if (n2 < 0)
4430 n2 = len + n2;
4431 else if (n2 >= len)
4432 n2 = len;
4433 if (n1 >= len || n2 < 0 || n1 > n2)
4434 s = NULL;
4435 else
4436 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4437 }
4438 else
4439 {
4440 /* The resulting variable is a string of a single
4441 * character. If the index is too big or negative the
4442 * result is empty. */
4443 if (n1 >= len || n1 < 0)
4444 s = NULL;
4445 else
4446 s = vim_strnsave(s + n1, 1);
4447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004448 clear_tv(rettv);
4449 rettv->v_type = VAR_STRING;
4450 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 break;
4452
4453 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 if (n1 < 0)
4456 n1 = len + n1;
4457 if (!empty1 && (n1 < 0 || n1 >= len))
4458 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004459 /* For a range we allow invalid values and return an empty
4460 * list. A list index out of range is an error. */
4461 if (!range)
4462 {
4463 if (verbose)
4464 EMSGN(_(e_listidx), n1);
4465 return FAIL;
4466 }
4467 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 }
4469 if (range)
4470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004471 list_T *l;
4472 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473
4474 if (n2 < 0)
4475 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004476 else if (n2 >= len)
4477 n2 = len - 1;
4478 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004479 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480 l = list_alloc();
4481 if (l == NULL)
4482 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 n1 <= n2; ++n1)
4485 {
4486 if (list_append_tv(l, &item->li_tv) == FAIL)
4487 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004488 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004489 return FAIL;
4490 }
4491 item = item->li_next;
4492 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004494 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 }
4496 else
4497 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004498 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 clear_tv(rettv);
4500 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 }
4502 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503
4504 case VAR_DICT:
4505 if (range)
4506 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004507 if (verbose)
4508 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 if (len == -1)
4510 clear_tv(&var1);
4511 return FAIL;
4512 }
4513 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004514 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515
4516 if (len == -1)
4517 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004518 key = get_tv_string_chk(&var1);
4519 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 clear_tv(&var1);
4522 return FAIL;
4523 }
4524 }
4525
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004526 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004527
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004528 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 if (len == -1)
4531 clear_tv(&var1);
4532 if (item == NULL)
4533 return FAIL;
4534
4535 copy_tv(&item->di_tv, &var1);
4536 clear_tv(rettv);
4537 *rettv = var1;
4538 }
4539 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540 }
4541 }
4542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 return OK;
4544}
4545
4546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 * Get an option value.
4548 * "arg" points to the '&' or '+' before the option name.
4549 * "arg" is advanced to character after the option name.
4550 * Return OK or FAIL.
4551 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004553get_option_tv(
4554 char_u **arg,
4555 typval_T *rettv, /* when NULL, only check if option exists */
4556 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 char_u *option_end;
4559 long numval;
4560 char_u *stringval;
4561 int opt_type;
4562 int c;
4563 int working = (**arg == '+'); /* has("+option") */
4564 int ret = OK;
4565 int opt_flags;
4566
4567 /*
4568 * Isolate the option name and find its value.
4569 */
4570 option_end = find_option_end(arg, &opt_flags);
4571 if (option_end == NULL)
4572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 EMSG2(_("E112: Option name missing: %s"), *arg);
4575 return FAIL;
4576 }
4577
4578 if (!evaluate)
4579 {
4580 *arg = option_end;
4581 return OK;
4582 }
4583
4584 c = *option_end;
4585 *option_end = NUL;
4586 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 if (opt_type == -3) /* invalid name */
4590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 EMSG2(_("E113: Unknown option: %s"), *arg);
4593 ret = FAIL;
4594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
4597 if (opt_type == -2) /* hidden string option */
4598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 rettv->v_type = VAR_STRING;
4600 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602 else if (opt_type == -1) /* hidden number option */
4603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 rettv->v_type = VAR_NUMBER;
4605 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607 else if (opt_type == 1) /* number option */
4608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 rettv->v_type = VAR_NUMBER;
4610 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612 else /* string option */
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 rettv->v_type = VAR_STRING;
4615 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617 }
4618 else if (working && (opt_type == -2 || opt_type == -1))
4619 ret = FAIL;
4620
4621 *option_end = c; /* put back for error messages */
4622 *arg = option_end;
4623
4624 return ret;
4625}
4626
4627/*
4628 * Allocate a variable for a string constant.
4629 * Return OK or FAIL.
4630 */
4631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004632get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 char_u *p;
4635 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 int extra = 0;
4637
4638 /*
4639 * Find the end of the string, skipping backslashed characters.
4640 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004641 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 if (*p == '\\' && p[1] != NUL)
4644 {
4645 ++p;
4646 /* A "\<x>" form occupies at least 4 characters, and produces up
4647 * to 6 characters: reserve space for 2 extra */
4648 if (*p == '<')
4649 extra += 2;
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652
4653 if (*p != '"')
4654 {
4655 EMSG2(_("E114: Missing quote: %s"), *arg);
4656 return FAIL;
4657 }
4658
4659 /* If only parsing, set *arg and return here */
4660 if (!evaluate)
4661 {
4662 *arg = p + 1;
4663 return OK;
4664 }
4665
4666 /*
4667 * Copy the string into allocated memory, handling backslashed
4668 * characters.
4669 */
4670 name = alloc((unsigned)(p - *arg + extra));
4671 if (name == NULL)
4672 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 rettv->v_type = VAR_STRING;
4674 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
4678 if (*p == '\\')
4679 {
4680 switch (*++p)
4681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 case 'b': *name++ = BS; ++p; break;
4683 case 'e': *name++ = ESC; ++p; break;
4684 case 'f': *name++ = FF; ++p; break;
4685 case 'n': *name++ = NL; ++p; break;
4686 case 'r': *name++ = CAR; ++p; break;
4687 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
4689 case 'X': /* hex: "\x1", "\x12" */
4690 case 'x':
4691 case 'u': /* Unicode: "\u0023" */
4692 case 'U':
4693 if (vim_isxdigit(p[1]))
4694 {
4695 int n, nr;
4696 int c = toupper(*p);
4697
4698 if (c == 'X')
4699 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004700 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004702 else
4703 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 nr = 0;
4705 while (--n >= 0 && vim_isxdigit(p[1]))
4706 {
4707 ++p;
4708 nr = (nr << 4) + hex2nr(*p);
4709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004710 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef FEAT_MBYTE
4712 /* For "\u" store the number according to
4713 * 'encoding'. */
4714 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004715 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 else
4717#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 break;
4721
4722 /* octal: "\1", "\12", "\123" */
4723 case '0':
4724 case '1':
4725 case '2':
4726 case '3':
4727 case '4':
4728 case '5':
4729 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 case '7': *name = *p++ - '0';
4731 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 *name = (*name << 3) + *p++ - '0';
4734 if (*p >= '0' && *p <= '7')
4735 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 break;
4739
4740 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004741 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (extra != 0)
4743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 break;
4746 }
4747 /* FALLTHROUGH */
4748
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 break;
4751 }
4752 }
4753 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004757 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004758 if (*p != NUL) /* just in case */
4759 ++p;
4760 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 return OK;
4763}
4764
4765/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 * Return OK or FAIL.
4768 */
4769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004770get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004773 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004774 int reduce = 0;
4775
4776 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004778 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004779 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004783 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004784 break;
4785 ++reduce;
4786 ++p;
4787 }
4788 }
4789
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004793 return FAIL;
4794 }
4795
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004797 if (!evaluate)
4798 {
4799 *arg = p + 1;
4800 return OK;
4801 }
4802
4803 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004805 */
4806 str = alloc((unsigned)((p - *arg) - reduce));
4807 if (str == NULL)
4808 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 rettv->v_type = VAR_STRING;
4810 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004811
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004817 break;
4818 ++p;
4819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004823 *arg = p + 1;
4824
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004825 return OK;
4826}
4827
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004828/*
4829 * Return the function name of the partial.
4830 */
4831 char_u *
4832partial_name(partial_T *pt)
4833{
4834 if (pt->pt_name != NULL)
4835 return pt->pt_name;
4836 return pt->pt_func->uf_name;
4837}
4838
Bram Moolenaarddecc252016-04-06 22:59:37 +02004839 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004840partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004841{
4842 int i;
4843
4844 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004845 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004846 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004847 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004848 if (pt->pt_name != NULL)
4849 {
4850 func_unref(pt->pt_name);
4851 vim_free(pt->pt_name);
4852 }
4853 else
4854 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004855 vim_free(pt);
4856}
4857
4858/*
4859 * Unreference a closure: decrement the reference count and free it when it
4860 * becomes zero.
4861 */
4862 void
4863partial_unref(partial_T *pt)
4864{
4865 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004866 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004867}
4868
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004869static int tv_equal_recurse_limit;
4870
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004871 static int
4872func_equal(
4873 typval_T *tv1,
4874 typval_T *tv2,
4875 int ic) /* ignore case */
4876{
4877 char_u *s1, *s2;
4878 dict_T *d1, *d2;
4879 int a1, a2;
4880 int i;
4881
4882 /* empty and NULL function name considered the same */
4883 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004884 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004885 if (s1 != NULL && *s1 == NUL)
4886 s1 = NULL;
4887 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004888 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004889 if (s2 != NULL && *s2 == NUL)
4890 s2 = NULL;
4891 if (s1 == NULL || s2 == NULL)
4892 {
4893 if (s1 != s2)
4894 return FALSE;
4895 }
4896 else if (STRCMP(s1, s2) != 0)
4897 return FALSE;
4898
4899 /* empty dict and NULL dict is different */
4900 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4901 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4902 if (d1 == NULL || d2 == NULL)
4903 {
4904 if (d1 != d2)
4905 return FALSE;
4906 }
4907 else if (!dict_equal(d1, d2, ic, TRUE))
4908 return FALSE;
4909
4910 /* empty list and no list considered the same */
4911 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4912 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4913 if (a1 != a2)
4914 return FALSE;
4915 for (i = 0; i < a1; ++i)
4916 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4917 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4918 return FALSE;
4919
4920 return TRUE;
4921}
4922
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004923/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004924 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004925 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004927 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004929tv_equal(
4930 typval_T *tv1,
4931 typval_T *tv2,
4932 int ic, /* ignore case */
4933 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004934{
4935 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004936 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004937 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004938 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004939
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004940 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004941 * recursiveness to a limit. We guess they are equal then.
4942 * A fixed limit has the problem of still taking an awful long time.
4943 * Reduce the limit every time running into it. That should work fine for
4944 * deeply linked structures that are not recursively linked and catch
4945 * recursiveness quickly. */
4946 if (!recursive)
4947 tv_equal_recurse_limit = 1000;
4948 if (recursive_cnt >= tv_equal_recurse_limit)
4949 {
4950 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004951 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004952 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004953
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004954 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4955 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004956 if ((tv1->v_type == VAR_FUNC
4957 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4958 && (tv2->v_type == VAR_FUNC
4959 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4960 {
4961 ++recursive_cnt;
4962 r = func_equal(tv1, tv2, ic);
4963 --recursive_cnt;
4964 return r;
4965 }
4966
4967 if (tv1->v_type != tv2->v_type)
4968 return FALSE;
4969
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004970 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004971 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004972 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004973 ++recursive_cnt;
4974 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4975 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004976 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004977
4978 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004979 ++recursive_cnt;
4980 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
4981 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004982 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004983
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004984 case VAR_NUMBER:
4985 return tv1->vval.v_number == tv2->vval.v_number;
4986
4987 case VAR_STRING:
4988 s1 = get_tv_string_buf(tv1, buf1);
4989 s2 = get_tv_string_buf(tv2, buf2);
4990 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004991
4992 case VAR_SPECIAL:
4993 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004994
4995 case VAR_FLOAT:
4996#ifdef FEAT_FLOAT
4997 return tv1->vval.v_float == tv2->vval.v_float;
4998#endif
4999 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005000#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005001 return tv1->vval.v_job == tv2->vval.v_job;
5002#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005003 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005004#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005005 return tv1->vval.v_channel == tv2->vval.v_channel;
5006#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005007 case VAR_FUNC:
5008 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005009 case VAR_UNKNOWN:
5010 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005011 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005012
Bram Moolenaara03f2332016-02-06 18:09:59 +01005013 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5014 * does not equal anything, not even itself. */
5015 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005016}
5017
5018/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005019 * Return the next (unique) copy ID.
5020 * Used for serializing nested structures.
5021 */
5022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005024{
5025 current_copyID += COPYID_INC;
5026 return current_copyID;
5027}
5028
5029/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005030 * Garbage collection for lists and dictionaries.
5031 *
5032 * We use reference counts to be able to free most items right away when they
5033 * are no longer used. But for composite items it's possible that it becomes
5034 * unused while the reference count is > 0: When there is a recursive
5035 * reference. Example:
5036 * :let l = [1, 2, 3]
5037 * :let d = {9: l}
5038 * :let l[1] = d
5039 *
5040 * Since this is quite unusual we handle this with garbage collection: every
5041 * once in a while find out which lists and dicts are not referenced from any
5042 * variable.
5043 *
5044 * Here is a good reference text about garbage collection (refers to Python
5045 * but it applies to all reference-counting mechanisms):
5046 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005047 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005048
5049/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005050 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005051 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005052 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005053 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005054 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005055garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005056{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005057 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005058 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005059 buf_T *buf;
5060 win_T *wp;
5061 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005062 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005063 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005064
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005065 if (!testing)
5066 {
5067 /* Only do this once. */
5068 want_garbage_collect = FALSE;
5069 may_garbage_collect = FALSE;
5070 garbage_collect_at_exit = FALSE;
5071 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005072
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005073 /* We advance by two because we add one for items referenced through
5074 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005075 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005076
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005077 /*
5078 * 1. Go through all accessible variables and mark all lists and dicts
5079 * with copyID.
5080 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005081
5082 /* Don't free variables in the previous_funccal list unless they are only
5083 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005084 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005085 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005086
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005087 /* script-local variables */
5088 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005089 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005090
5091 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005092 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005093 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5094 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005095
5096 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005097 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005098 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5099 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005100 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005101 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5102 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005103
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005104 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005105 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005106 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5107 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005108 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005109 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005110
5111 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005113
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005114 /* named functions (matters for closures) */
5115 abort = abort || set_ref_in_functions(copyID);
5116
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005117 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005119
Bram Moolenaard812df62008-11-09 12:46:09 +00005120 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005121 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005122
Bram Moolenaar1dced572012-04-05 16:54:08 +02005123#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005124 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005125#endif
5126
Bram Moolenaardb913952012-06-29 12:54:53 +02005127#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005128 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005129#endif
5130
5131#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005132 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005133#endif
5134
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005136 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005137 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005138#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005139#ifdef FEAT_NETBEANS_INTG
5140 abort = abort || set_ref_in_nb_channel(copyID);
5141#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005142
Bram Moolenaare3188e22016-05-31 21:13:04 +02005143#ifdef FEAT_TIMERS
5144 abort = abort || set_ref_in_timer(copyID);
5145#endif
5146
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005147#ifdef FEAT_QUICKFIX
5148 abort = abort || set_ref_in_quickfix(copyID);
5149#endif
5150
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005151#ifdef FEAT_TERMINAL
5152 abort = abort || set_ref_in_term(copyID);
5153#endif
5154
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005155 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005156 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005157 /*
5158 * 2. Free lists and dictionaries that are not referenced.
5159 */
5160 did_free = free_unref_items(copyID);
5161
5162 /*
5163 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005165 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005166 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005167 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005168 else if (p_verbose > 0)
5169 {
5170 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5171 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005172
5173 return did_free;
5174}
5175
5176/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005177 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005178 */
5179 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005180free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005181{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005182 int did_free = FALSE;
5183
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005184 /* Let all "free" functions know that we are here. This means no
5185 * dictionaries, lists, channels or jobs are to be freed, because we will
5186 * do that here. */
5187 in_free_unref_items = TRUE;
5188
5189 /*
5190 * PASS 1: free the contents of the items. We don't free the items
5191 * themselves yet, so that it is possible to decrement refcount counters
5192 */
5193
Bram Moolenaarcd524592016-07-17 14:57:05 +02005194 /* Go through the list of dicts and free items without the copyID. */
5195 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005196
Bram Moolenaarda861d62016-07-17 15:46:27 +02005197 /* Go through the list of lists and free items without the copyID. */
5198 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005199
5200#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 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5205
5206 /* Go through the list of channels and free items without the copyID. */
5207 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5208#endif
5209
5210 /*
5211 * PASS 2: free the items themselves.
5212 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005213 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005214 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005215
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005216#ifdef FEAT_JOB_CHANNEL
5217 /* Go through the list of jobs and free items without the copyID. This
5218 * must happen before doing channels, because jobs refer to channels, but
5219 * the reference from the channel to the job isn't tracked. */
5220 free_unused_jobs(copyID, COPYID_MASK);
5221
5222 /* Go through the list of channels and free items without the copyID. */
5223 free_unused_channels(copyID, COPYID_MASK);
5224#endif
5225
5226 in_free_unref_items = FALSE;
5227
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228 return did_free;
5229}
5230
5231/*
5232 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005233 * "list_stack" is used to add lists to be marked. Can be NULL.
5234 *
5235 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005236 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005238set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005239{
5240 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005241 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005242 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005243 hashtab_T *cur_ht;
5244 ht_stack_T *ht_stack = NULL;
5245 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005246
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005247 cur_ht = ht;
5248 for (;;)
5249 {
5250 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 /* Mark each item in the hashtab. If the item contains a hashtab
5253 * it is added to ht_stack, if it contains a list it is added to
5254 * list_stack. */
5255 todo = (int)cur_ht->ht_used;
5256 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5257 if (!HASHITEM_EMPTY(hi))
5258 {
5259 --todo;
5260 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5261 &ht_stack, list_stack);
5262 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005263 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005264
5265 if (ht_stack == NULL)
5266 break;
5267
5268 /* take an item from the stack */
5269 cur_ht = ht_stack->ht;
5270 tempitem = ht_stack;
5271 ht_stack = ht_stack->prev;
5272 free(tempitem);
5273 }
5274
5275 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276}
5277
5278/*
5279 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005280 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5281 *
5282 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005283 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005285set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005286{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005287 listitem_T *li;
5288 int abort = FALSE;
5289 list_T *cur_l;
5290 list_stack_T *list_stack = NULL;
5291 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005292
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 cur_l = l;
5294 for (;;)
5295 {
5296 if (!abort)
5297 /* Mark each item in the list. If the item contains a hashtab
5298 * it is added to ht_stack, if it contains a list it is added to
5299 * list_stack. */
5300 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5301 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5302 ht_stack, &list_stack);
5303 if (list_stack == NULL)
5304 break;
5305
5306 /* take an item from the stack */
5307 cur_l = list_stack->list;
5308 tempitem = list_stack;
5309 list_stack = list_stack->prev;
5310 free(tempitem);
5311 }
5312
5313 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314}
5315
5316/*
5317 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005318 * "list_stack" is used to add lists to be marked. Can be NULL.
5319 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5320 *
5321 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005324set_ref_in_item(
5325 typval_T *tv,
5326 int copyID,
5327 ht_stack_T **ht_stack,
5328 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005329{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005330 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005332 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005333 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005334 dict_T *dd = tv->vval.v_dict;
5335
Bram Moolenaara03f2332016-02-06 18:09:59 +01005336 if (dd != NULL && dd->dv_copyID != copyID)
5337 {
5338 /* Didn't see this dict yet. */
5339 dd->dv_copyID = copyID;
5340 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005341 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005342 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5343 }
5344 else
5345 {
5346 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5347 if (newitem == NULL)
5348 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005349 else
5350 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005351 newitem->ht = &dd->dv_hashtab;
5352 newitem->prev = *ht_stack;
5353 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005354 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005355 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 }
5357 }
5358 else if (tv->v_type == VAR_LIST)
5359 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005360 list_T *ll = tv->vval.v_list;
5361
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 if (ll != NULL && ll->lv_copyID != copyID)
5363 {
5364 /* Didn't see this list yet. */
5365 ll->lv_copyID = copyID;
5366 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005367 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 abort = set_ref_in_list(ll, copyID, ht_stack);
5369 }
5370 else
5371 {
5372 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005373 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 if (newitem == NULL)
5375 abort = TRUE;
5376 else
5377 {
5378 newitem->list = ll;
5379 newitem->prev = *list_stack;
5380 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005381 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005382 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005383 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005384 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005385 else if (tv->v_type == VAR_FUNC)
5386 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005387 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005388 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005389 else if (tv->v_type == VAR_PARTIAL)
5390 {
5391 partial_T *pt = tv->vval.v_partial;
5392 int i;
5393
5394 /* A partial does not have a copyID, because it cannot contain itself.
5395 */
5396 if (pt != NULL)
5397 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005398 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005399
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005400 if (pt->pt_dict != NULL)
5401 {
5402 typval_T dtv;
5403
5404 dtv.v_type = VAR_DICT;
5405 dtv.vval.v_dict = pt->pt_dict;
5406 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5407 }
5408
5409 for (i = 0; i < pt->pt_argc; ++i)
5410 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5411 ht_stack, list_stack);
5412 }
5413 }
5414#ifdef FEAT_JOB_CHANNEL
5415 else if (tv->v_type == VAR_JOB)
5416 {
5417 job_T *job = tv->vval.v_job;
5418 typval_T dtv;
5419
5420 if (job != NULL && job->jv_copyID != copyID)
5421 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005422 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 if (job->jv_channel != NULL)
5424 {
5425 dtv.v_type = VAR_CHANNEL;
5426 dtv.vval.v_channel = job->jv_channel;
5427 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5428 }
5429 if (job->jv_exit_partial != NULL)
5430 {
5431 dtv.v_type = VAR_PARTIAL;
5432 dtv.vval.v_partial = job->jv_exit_partial;
5433 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5434 }
5435 }
5436 }
5437 else if (tv->v_type == VAR_CHANNEL)
5438 {
5439 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005440 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441 typval_T dtv;
5442 jsonq_T *jq;
5443 cbq_T *cq;
5444
5445 if (ch != NULL && ch->ch_copyID != copyID)
5446 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005447 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005448 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005449 {
5450 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5451 jq = jq->jq_next)
5452 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5453 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5454 cq = cq->cq_next)
5455 if (cq->cq_partial != NULL)
5456 {
5457 dtv.v_type = VAR_PARTIAL;
5458 dtv.vval.v_partial = cq->cq_partial;
5459 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5460 }
5461 if (ch->ch_part[part].ch_partial != NULL)
5462 {
5463 dtv.v_type = VAR_PARTIAL;
5464 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5465 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5466 }
5467 }
5468 if (ch->ch_partial != NULL)
5469 {
5470 dtv.v_type = VAR_PARTIAL;
5471 dtv.vval.v_partial = ch->ch_partial;
5472 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5473 }
5474 if (ch->ch_close_partial != NULL)
5475 {
5476 dtv.v_type = VAR_PARTIAL;
5477 dtv.vval.v_partial = ch->ch_close_partial;
5478 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5479 }
5480 }
5481 }
5482#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005483 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005484}
5485
Bram Moolenaar17a13432016-01-24 14:22:10 +01005486 static char *
5487get_var_special_name(int nr)
5488{
5489 switch (nr)
5490 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005491 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005492 case VVAL_TRUE: return "v:true";
5493 case VVAL_NONE: return "v:none";
5494 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005495 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005496 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005497 return "42";
5498}
5499
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 * Return a string with the string representation of a variable.
5502 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005504 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005505 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5506 * stings as "string()", otherwise does not put quotes around strings, as
5507 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005508 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5509 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005510 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005512 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005513echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005514 typval_T *tv,
5515 char_u **tofree,
5516 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005517 int copyID,
5518 int echo_style,
5519 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005520 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005522 static int recurse = 0;
5523 char_u *r = NULL;
5524
Bram Moolenaar33570922005-01-25 22:26:29 +00005525 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005526 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005527 if (!did_echo_string_emsg)
5528 {
5529 /* Only give this message once for a recursive call to avoid
5530 * flooding the user with errors. And stop iterating over lists
5531 * and dicts. */
5532 did_echo_string_emsg = TRUE;
5533 EMSG(_("E724: variable nested too deep for displaying"));
5534 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005535 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005536 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005537 }
5538 ++recurse;
5539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 switch (tv->v_type)
5541 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005542 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005543 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005544 {
5545 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005546 r = tv->vval.v_string;
5547 if (r == NULL)
5548 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005549 }
5550 else
5551 {
5552 *tofree = string_quote(tv->vval.v_string, FALSE);
5553 r = *tofree;
5554 }
5555 break;
5556
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005558 if (echo_style)
5559 {
5560 *tofree = NULL;
5561 r = tv->vval.v_string;
5562 }
5563 else
5564 {
5565 *tofree = string_quote(tv->vval.v_string, TRUE);
5566 r = *tofree;
5567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005568 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005569
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005570 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005571 {
5572 partial_T *pt = tv->vval.v_partial;
5573 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005574 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005575 garray_T ga;
5576 int i;
5577 char_u *tf;
5578
5579 ga_init2(&ga, 1, 100);
5580 ga_concat(&ga, (char_u *)"function(");
5581 if (fname != NULL)
5582 {
5583 ga_concat(&ga, fname);
5584 vim_free(fname);
5585 }
5586 if (pt != NULL && pt->pt_argc > 0)
5587 {
5588 ga_concat(&ga, (char_u *)", [");
5589 for (i = 0; i < pt->pt_argc; ++i)
5590 {
5591 if (i > 0)
5592 ga_concat(&ga, (char_u *)", ");
5593 ga_concat(&ga,
5594 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5595 vim_free(tf);
5596 }
5597 ga_concat(&ga, (char_u *)"]");
5598 }
5599 if (pt != NULL && pt->pt_dict != NULL)
5600 {
5601 typval_T dtv;
5602
5603 ga_concat(&ga, (char_u *)", ");
5604 dtv.v_type = VAR_DICT;
5605 dtv.vval.v_dict = pt->pt_dict;
5606 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5607 vim_free(tf);
5608 }
5609 ga_concat(&ga, (char_u *)")");
5610
5611 *tofree = ga.ga_data;
5612 r = *tofree;
5613 break;
5614 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005617 if (tv->vval.v_list == NULL)
5618 {
5619 *tofree = NULL;
5620 r = NULL;
5621 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005622 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5623 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005624 {
5625 *tofree = NULL;
5626 r = (char_u *)"[...]";
5627 }
5628 else
5629 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005630 int old_copyID = tv->vval.v_list->lv_copyID;
5631
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005632 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005633 *tofree = list2string(tv, copyID, restore_copyID);
5634 if (restore_copyID)
5635 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005636 r = *tofree;
5637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005641 if (tv->vval.v_dict == NULL)
5642 {
5643 *tofree = NULL;
5644 r = NULL;
5645 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005646 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5647 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005648 {
5649 *tofree = NULL;
5650 r = (char_u *)"{...}";
5651 }
5652 else
5653 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005654 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005655 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005656 *tofree = dict2string(tv, copyID, restore_copyID);
5657 if (restore_copyID)
5658 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005659 r = *tofree;
5660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005661 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005664 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005665 *tofree = NULL;
5666 r = get_tv_string_buf(tv, numbuf);
5667 break;
5668
Bram Moolenaar835dc632016-02-07 14:27:38 +01005669 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005670 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005671 *tofree = NULL;
5672 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005673 if (composite_val)
5674 {
5675 *tofree = string_quote(r, FALSE);
5676 r = *tofree;
5677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005679
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005680 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005681#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005682 *tofree = NULL;
5683 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5684 r = numbuf;
5685 break;
5686#endif
5687
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005688 case VAR_SPECIAL:
5689 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005690 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005691 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005693
Bram Moolenaar8502c702014-06-17 12:51:16 +02005694 if (--recurse == 0)
5695 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005696 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697}
5698
5699/*
5700 * Return a string with the string representation of a variable.
5701 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5702 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005703 * Does not put quotes around strings, as ":echo" displays values.
5704 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5705 * May return NULL.
5706 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005708echo_string(
5709 typval_T *tv,
5710 char_u **tofree,
5711 char_u *numbuf,
5712 int copyID)
5713{
5714 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5715}
5716
5717/*
5718 * Return a string with the string representation of a variable.
5719 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5720 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005722 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005724 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005725tv2string(
5726 typval_T *tv,
5727 char_u **tofree,
5728 char_u *numbuf,
5729 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005731 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732}
5733
5734/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005735 * Return string "str" in ' quotes, doubling ' characters.
5736 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005739 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741{
Bram Moolenaar33570922005-01-25 22:26:29 +00005742 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 char_u *p, *r, *s;
5744
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 len = (function ? 13 : 3);
5746 if (str != NULL)
5747 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005748 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005749 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005750 if (*p == '\'')
5751 ++len;
5752 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 s = r = alloc(len);
5754 if (r != NULL)
5755 {
5756 if (function)
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 r += 10;
5760 }
5761 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005763 if (str != NULL)
5764 for (p = str; *p != NUL; )
5765 {
5766 if (*p == '\'')
5767 *r++ = '\'';
5768 MB_COPY_CHAR(p, r);
5769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 if (function)
5772 *r++ = ')';
5773 *r++ = NUL;
5774 }
5775 return s;
5776}
5777
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005778#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005779/*
5780 * Convert the string "text" to a floating point number.
5781 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5782 * this always uses a decimal point.
5783 * Returns the length of the text that was consumed.
5784 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005785 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005786string2float(
5787 char_u *text,
5788 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005789{
5790 char *s = (char *)text;
5791 float_T f;
5792
Bram Moolenaar62473612017-01-08 19:25:40 +01005793 /* MS-Windows does not deal with "inf" and "nan" properly. */
5794 if (STRNICMP(text, "inf", 3) == 0)
5795 {
5796 *value = INFINITY;
5797 return 3;
5798 }
5799 if (STRNICMP(text, "-inf", 3) == 0)
5800 {
5801 *value = -INFINITY;
5802 return 4;
5803 }
5804 if (STRNICMP(text, "nan", 3) == 0)
5805 {
5806 *value = NAN;
5807 return 3;
5808 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005809 f = strtod(s, &s);
5810 *value = f;
5811 return (int)((char_u *)s - text);
5812}
5813#endif
5814
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 * Get the value of an environment variable.
5817 * "arg" is pointing to the '$'. It is advanced to after the name.
5818 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005819 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 */
5821 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005822get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 char_u *string = NULL;
5825 int len;
5826 int cc;
5827 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005828 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
5830 ++*arg;
5831 name = *arg;
5832 len = get_env_len(arg);
5833 if (evaluate)
5834 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005835 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005836 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005837
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005838 cc = name[len];
5839 name[len] = NUL;
5840 /* first try vim_getenv(), fast for normal environment vars */
5841 string = vim_getenv(name, &mustfree);
5842 if (string != NULL && *string != NUL)
5843 {
5844 if (!mustfree)
5845 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005847 else
5848 {
5849 if (mustfree)
5850 vim_free(string);
5851
5852 /* next try expanding things like $VIM and ${HOME} */
5853 string = expand_env_save(name - 1);
5854 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005855 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005856 }
5857 name[len] = cc;
5858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 rettv->v_type = VAR_STRING;
5860 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862
5863 return OK;
5864}
5865
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005866
5867
5868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005870 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005872 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005873var2fpos(
5874 typval_T *varp,
5875 int dollar_lnum, /* TRUE when $ is last line */
5876 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005878 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005880 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaara5525202006-03-02 22:52:09 +00005882 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005883 if (varp->v_type == VAR_LIST)
5884 {
5885 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005886 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005887 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005888 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005889
5890 l = varp->vval.v_list;
5891 if (l == NULL)
5892 return NULL;
5893
5894 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005895 pos.lnum = list_find_nr(l, 0L, &error);
5896 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005897 return NULL; /* invalid line number */
5898
5899 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005900 pos.col = list_find_nr(l, 1L, &error);
5901 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005902 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005903 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005904
5905 /* We accept "$" for the column number: last column. */
5906 li = list_find(l, 1L);
5907 if (li != NULL && li->li_tv.v_type == VAR_STRING
5908 && li->li_tv.vval.v_string != NULL
5909 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5910 pos.col = len + 1;
5911
Bram Moolenaara5525202006-03-02 22:52:09 +00005912 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005913 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005914 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005915 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005916
Bram Moolenaara5525202006-03-02 22:52:09 +00005917#ifdef FEAT_VIRTUALEDIT
5918 /* Get the virtual offset. Defaults to zero. */
5919 pos.coladd = list_find_nr(l, 2L, &error);
5920 if (error)
5921 pos.coladd = 0;
5922#endif
5923
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005924 return &pos;
5925 }
5926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005927 name = get_tv_string_chk(varp);
5928 if (name == NULL)
5929 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005930 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005932 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5933 {
5934 if (VIsual_active)
5935 return &VIsual;
5936 return &curwin->w_cursor;
5937 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005938 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005940 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5942 return NULL;
5943 return pp;
5944 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005945
5946#ifdef FEAT_VIRTUALEDIT
5947 pos.coladd = 0;
5948#endif
5949
Bram Moolenaar477933c2007-07-17 14:32:23 +00005950 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005951 {
5952 pos.col = 0;
5953 if (name[1] == '0') /* "w0": first visible line */
5954 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005955 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005956 /* In silent Ex mode topline is zero, but that's not a valid line
5957 * number; use one instead. */
5958 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005959 return &pos;
5960 }
5961 else if (name[1] == '$') /* "w$": last visible line */
5962 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005963 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005964 /* In silent Ex mode botline is zero, return zero then. */
5965 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005966 return &pos;
5967 }
5968 }
5969 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005971 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 pos.lnum = curbuf->b_ml.ml_line_count;
5974 pos.col = 0;
5975 }
5976 else
5977 {
5978 pos.lnum = curwin->w_cursor.lnum;
5979 pos.col = (colnr_T)STRLEN(ml_get_curline());
5980 }
5981 return &pos;
5982 }
5983 return NULL;
5984}
5985
5986/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005987 * Convert list in "arg" into a position and optional file number.
5988 * When "fnump" is NULL there is no file number, only 3 items.
5989 * Note that the column is passed on as-is, the caller may want to decrement
5990 * it to use 1 for the first column.
5991 * Return FAIL when conversion is not possible, doesn't check the position for
5992 * validity.
5993 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995list2fpos(
5996 typval_T *arg,
5997 pos_T *posp,
5998 int *fnump,
5999 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006000{
6001 list_T *l = arg->vval.v_list;
6002 long i = 0;
6003 long n;
6004
Bram Moolenaar493c1782014-05-28 14:34:46 +02006005 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6006 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006007 if (arg->v_type != VAR_LIST
6008 || l == NULL
6009 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006010 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006011 return FAIL;
6012
6013 if (fnump != NULL)
6014 {
6015 n = list_find_nr(l, i++, NULL); /* fnum */
6016 if (n < 0)
6017 return FAIL;
6018 if (n == 0)
6019 n = curbuf->b_fnum; /* current buffer */
6020 *fnump = n;
6021 }
6022
6023 n = list_find_nr(l, i++, NULL); /* lnum */
6024 if (n < 0)
6025 return FAIL;
6026 posp->lnum = n;
6027
6028 n = list_find_nr(l, i++, NULL); /* col */
6029 if (n < 0)
6030 return FAIL;
6031 posp->col = n;
6032
6033#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006034 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006035 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006036 posp->coladd = 0;
6037 else
6038 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006039#endif
6040
Bram Moolenaar493c1782014-05-28 14:34:46 +02006041 if (curswantp != NULL)
6042 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6043
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006044 return OK;
6045}
6046
6047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 * Get the length of an environment variable name.
6049 * Advance "arg" to the first character after the name.
6050 * Return 0 for error.
6051 */
6052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 char_u *p;
6056 int len;
6057
6058 for (p = *arg; vim_isIDc(*p); ++p)
6059 ;
6060 if (p == *arg) /* no name found */
6061 return 0;
6062
6063 len = (int)(p - *arg);
6064 *arg = p;
6065 return len;
6066}
6067
6068/*
6069 * Get the length of the name of a function or internal variable.
6070 * "arg" is advanced to the first non-white character after the name.
6071 * Return 0 if something is wrong.
6072 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 char_u *p;
6077 int len;
6078
6079 /* Find the end of the name. */
6080 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006081 {
6082 if (*p == ':')
6083 {
6084 /* "s:" is start of "s:var", but "n:" is not and can be used in
6085 * slice "[n:]". Also "xx:" is not a namespace. */
6086 len = (int)(p - *arg);
6087 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6088 || len > 1)
6089 break;
6090 }
6091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (p == *arg) /* no name found */
6093 return 0;
6094
6095 len = (int)(p - *arg);
6096 *arg = skipwhite(p);
6097
6098 return len;
6099}
6100
6101/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006102 * Get the length of the name of a variable or function.
6103 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006105 * Return -1 if curly braces expansion failed.
6106 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 * If the name contains 'magic' {}'s, expand them and return the
6108 * expanded name in an allocated string via 'alias' - caller must free.
6109 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111get_name_len(
6112 char_u **arg,
6113 char_u **alias,
6114 int evaluate,
6115 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 char_u *p;
6119 char_u *expr_start;
6120 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 *alias = NULL; /* default to no alias */
6123
6124 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6125 && (*arg)[2] == (int)KE_SNR)
6126 {
6127 /* hard coded <SNR>, already translated */
6128 *arg += 3;
6129 return get_id_len(arg) + 3;
6130 }
6131 len = eval_fname_script(*arg);
6132 if (len > 0)
6133 {
6134 /* literal "<SID>", "s:" or "<SNR>" */
6135 *arg += len;
6136 }
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006139 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006141 p = find_name_end(*arg, &expr_start, &expr_end,
6142 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (expr_start != NULL)
6144 {
6145 char_u *temp_string;
6146
6147 if (!evaluate)
6148 {
6149 len += (int)(p - *arg);
6150 *arg = skipwhite(p);
6151 return len;
6152 }
6153
6154 /*
6155 * Include any <SID> etc in the expanded string:
6156 * Thus the -len here.
6157 */
6158 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6159 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006160 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 *alias = temp_string;
6162 *arg = skipwhite(p);
6163 return (int)STRLEN(temp_string);
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006167 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 EMSG2(_(e_invexpr2), *arg);
6169
6170 return len;
6171}
6172
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006173/*
6174 * Find the end of a variable or function name, taking care of magic braces.
6175 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6176 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006177 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006178 * Return a pointer to just after the name. Equal to "arg" if there is no
6179 * valid name.
6180 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006181 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006182find_name_end(
6183 char_u *arg,
6184 char_u **expr_start,
6185 char_u **expr_end,
6186 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006188 int mb_nest = 0;
6189 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006191 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006195 *expr_start = NULL;
6196 *expr_end = NULL;
6197 }
6198
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006199 /* Quick check for valid starting character. */
6200 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6201 return arg;
6202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006203 for (p = arg; *p != NUL
6204 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006206 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006207 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006208 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006210 if (*p == '\'')
6211 {
6212 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006213 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006214 ;
6215 if (*p == NUL)
6216 break;
6217 }
6218 else if (*p == '"')
6219 {
6220 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006221 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006222 if (*p == '\\' && p[1] != NUL)
6223 ++p;
6224 if (*p == NUL)
6225 break;
6226 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006227 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6228 {
6229 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006230 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006231 len = (int)(p - arg);
6232 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006233 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006234 break;
6235 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006237 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006239 if (*p == '[')
6240 ++br_nest;
6241 else if (*p == ']')
6242 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006244
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006245 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006247 if (*p == '{')
6248 {
6249 mb_nest++;
6250 if (expr_start != NULL && *expr_start == NULL)
6251 *expr_start = p;
6252 }
6253 else if (*p == '}')
6254 {
6255 mb_nest--;
6256 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6257 *expr_end = p;
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
6261
6262 return p;
6263}
6264
6265/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006266 * Expands out the 'magic' {}'s in a variable/function name.
6267 * Note that this can call itself recursively, to deal with
6268 * constructs like foo{bar}{baz}{bam}
6269 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6270 * "in_start" ^
6271 * "expr_start" ^
6272 * "expr_end" ^
6273 * "in_end" ^
6274 *
6275 * Returns a new allocated string, which the caller must free.
6276 * Returns NULL for failure.
6277 */
6278 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006279make_expanded_name(
6280 char_u *in_start,
6281 char_u *expr_start,
6282 char_u *expr_end,
6283 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006284{
6285 char_u c1;
6286 char_u *retval = NULL;
6287 char_u *temp_result;
6288 char_u *nextcmd = NULL;
6289
6290 if (expr_end == NULL || in_end == NULL)
6291 return NULL;
6292 *expr_start = NUL;
6293 *expr_end = NUL;
6294 c1 = *in_end;
6295 *in_end = NUL;
6296
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006297 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006298 if (temp_result != NULL && nextcmd == NULL)
6299 {
6300 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6301 + (in_end - expr_end) + 1));
6302 if (retval != NULL)
6303 {
6304 STRCPY(retval, in_start);
6305 STRCAT(retval, temp_result);
6306 STRCAT(retval, expr_end + 1);
6307 }
6308 }
6309 vim_free(temp_result);
6310
6311 *in_end = c1; /* put char back for error messages */
6312 *expr_start = '{';
6313 *expr_end = '}';
6314
6315 if (retval != NULL)
6316 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006317 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006318 if (expr_start != NULL)
6319 {
6320 /* Further expansion! */
6321 temp_result = make_expanded_name(retval, expr_start,
6322 expr_end, temp_result);
6323 vim_free(retval);
6324 retval = temp_result;
6325 }
6326 }
6327
6328 return retval;
6329}
6330
6331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006336eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006338 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6339}
6340
6341/*
6342 * Return TRUE if character "c" can be used as the first character in a
6343 * variable or function name (excluding '{' and '}').
6344 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006347{
6348 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349}
6350
6351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 * Set number v: variable to "val".
6353 */
6354 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006355set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358}
6359
6360/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006361 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006363 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006364get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367}
6368
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006369/*
6370 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006371 * If the String variable has never been set, return an empty string.
6372 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006373 */
6374 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006376{
6377 return get_tv_string(&vimvars[idx].vv_tv);
6378}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006379
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006381 * Get List v: variable value. Caller must take care of reference count when
6382 * needed.
6383 */
6384 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006385get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006386{
6387 return vimvars[idx].vv_list;
6388}
6389
6390/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006391 * Get Dict v: variable value. Caller must take care of reference count when
6392 * needed.
6393 */
6394 dict_T *
6395get_vim_var_dict(int idx)
6396{
6397 return vimvars[idx].vv_dict;
6398}
6399
6400/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006401 * Set v:char to character "c".
6402 */
6403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006404set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006405{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006406 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006407
6408#ifdef FEAT_MBYTE
6409 if (has_mbyte)
6410 buf[(*mb_char2bytes)(c, buf)] = NUL;
6411 else
6412#endif
6413 {
6414 buf[0] = c;
6415 buf[1] = NUL;
6416 }
6417 set_vim_var_string(VV_CHAR, buf, -1);
6418}
6419
6420/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006421 * Set v:count to "count" and v:count1 to "count1".
6422 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 */
6424 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006425set_vcount(
6426 long count,
6427 long count1,
6428 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006430 if (set_prevcount)
6431 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 vimvars[VV_COUNT].vv_nr = count;
6433 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434}
6435
6436/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006437 * Save variables that might be changed as a side effect. Used when executing
6438 * a timer callback.
6439 */
6440 void
6441save_vimvars(vimvars_save_T *vvsave)
6442{
6443 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6444 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6445 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6446}
6447
6448/*
6449 * Restore variables saved by save_vimvars().
6450 */
6451 void
6452restore_vimvars(vimvars_save_T *vvsave)
6453{
6454 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6455 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6456 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6457}
6458
6459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * Set string v: variable to a copy of "val".
6461 */
6462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463set_vim_var_string(
6464 int idx,
6465 char_u *val,
6466 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
Bram Moolenaara542c682016-01-31 16:28:04 +01006468 clear_tv(&vimvars[idx].vv_di.di_tv);
6469 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006471 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006475 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
6477
6478/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006479 * Set List v: variable to "val".
6480 */
6481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006482set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006483{
Bram Moolenaara542c682016-01-31 16:28:04 +01006484 clear_tv(&vimvars[idx].vv_di.di_tv);
6485 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006486 vimvars[idx].vv_list = val;
6487 if (val != NULL)
6488 ++val->lv_refcount;
6489}
6490
6491/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006492 * Set Dictionary v: variable to "val".
6493 */
6494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006496{
Bram Moolenaara542c682016-01-31 16:28:04 +01006497 clear_tv(&vimvars[idx].vv_di.di_tv);
6498 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006499 vimvars[idx].vv_dict = val;
6500 if (val != NULL)
6501 {
6502 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006503 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006504 }
6505}
6506
6507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 * Set v:register if needed.
6509 */
6510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 char_u regname;
6514
6515 if (c == 0 || c == ' ')
6516 regname = '"';
6517 else
6518 regname = c;
6519 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 set_vim_var_string(VV_REG, &regname, 1);
6522}
6523
6524/*
6525 * Get or set v:exception. 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:exception! Does not
6528 * take care of memory allocations.
6529 */
6530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006531v_exception(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_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 return NULL;
6538}
6539
6540/*
6541 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6542 * Otherwise, restore the value to "oldval" and return NULL.
6543 * Must always be called in pairs to save and restore v:throwpoint! Does not
6544 * take care of memory allocations.
6545 */
6546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 return NULL;
6554}
6555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556/*
6557 * Set v:cmdarg.
6558 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6559 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6560 * Must always be called in pairs!
6561 */
6562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006563set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 char_u *oldval;
6566 char_u *newval;
6567 unsigned len;
6568
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006570 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006572 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006574 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006577 if (eap->force_bin == FORCE_BIN)
6578 len = 6;
6579 else if (eap->force_bin == FORCE_NOBIN)
6580 len = 8;
6581 else
6582 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006583
6584 if (eap->read_edit)
6585 len += 7;
6586
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006587 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006588 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006589# ifdef FEAT_MBYTE
6590 if (eap->force_enc != 0)
6591 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006592 if (eap->bad_char != 0)
6593 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006594# endif
6595
6596 newval = alloc(len + 1);
6597 if (newval == NULL)
6598 return NULL;
6599
6600 if (eap->force_bin == FORCE_BIN)
6601 sprintf((char *)newval, " ++bin");
6602 else if (eap->force_bin == FORCE_NOBIN)
6603 sprintf((char *)newval, " ++nobin");
6604 else
6605 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006606
6607 if (eap->read_edit)
6608 STRCAT(newval, " ++edit");
6609
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006610 if (eap->force_ff != 0)
6611 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006612 eap->force_ff == 'u' ? "unix"
6613 : eap->force_ff == 'd' ? "dos"
6614 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006615#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006616 if (eap->force_enc != 0)
6617 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6618 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006619 if (eap->bad_char == BAD_KEEP)
6620 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6621 else if (eap->bad_char == BAD_DROP)
6622 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6623 else if (eap->bad_char != 0)
6624 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006625#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006626 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006627 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630/*
6631 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006632 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635get_var_tv(
6636 char_u *name,
6637 int len, /* length of "name" */
6638 typval_T *rettv, /* NULL when only checking existence */
6639 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6640 int verbose, /* may give error message */
6641 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648 /* truncate the name, so that we can use strcmp() */
6649 cc = name[len];
6650 name[len] = NUL;
6651
6652 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 * Check for user-defined variables.
6654 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006655 v = find_var(name, NULL, no_autoload);
6656 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006658 tv = &v->di_tv;
6659 if (dip != NULL)
6660 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006665 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006666 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 ret = FAIL;
6668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006669 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006670 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 name[len] = cc;
6673
6674 return ret;
6675}
6676
6677/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006678 * Check if variable "name[len]" is a local variable or an argument.
6679 * If so, "*eval_lavars_used" is set to TRUE.
6680 */
6681 static void
6682check_vars(char_u *name, int len)
6683{
6684 int cc;
6685 char_u *varname;
6686 hashtab_T *ht;
6687
6688 if (eval_lavars_used == NULL)
6689 return;
6690
6691 /* truncate the name, so that we can use strcmp() */
6692 cc = name[len];
6693 name[len] = NUL;
6694
6695 ht = find_var_ht(name, &varname);
6696 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6697 {
6698 if (find_var(name, NULL, TRUE) != NULL)
6699 *eval_lavars_used = TRUE;
6700 }
6701
6702 name[len] = cc;
6703}
6704
6705/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006706 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6707 * Also handle function call with Funcref variable: func(expr)
6708 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6709 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006711handle_subscript(
6712 char_u **arg,
6713 typval_T *rettv,
6714 int evaluate, /* do more than finding the end */
6715 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006716{
6717 int ret = OK;
6718 dict_T *selfdict = NULL;
6719 char_u *s;
6720 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006721 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006722
6723 while (ret == OK
6724 && (**arg == '['
6725 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006726 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6727 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006728 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006729 {
6730 if (**arg == '(')
6731 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006732 partial_T *pt = NULL;
6733
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006735 if (evaluate)
6736 {
6737 functv = *rettv;
6738 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006739
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006740 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006741 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006742 {
6743 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006744 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006745 }
6746 else
6747 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006748 }
6749 else
6750 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006751 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006752 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006753 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754
6755 /* Clear the funcref afterwards, so that deleting it while
6756 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006757 if (evaluate)
6758 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006759
6760 /* Stop the expression evaluation when immediately aborting on
6761 * error, or when an interrupt occurred or an exception was thrown
6762 * but not caught. */
6763 if (aborting())
6764 {
6765 if (ret == OK)
6766 clear_tv(rettv);
6767 ret = FAIL;
6768 }
6769 dict_unref(selfdict);
6770 selfdict = NULL;
6771 }
6772 else /* **arg == '[' || **arg == '.' */
6773 {
6774 dict_unref(selfdict);
6775 if (rettv->v_type == VAR_DICT)
6776 {
6777 selfdict = rettv->vval.v_dict;
6778 if (selfdict != NULL)
6779 ++selfdict->dv_refcount;
6780 }
6781 else
6782 selfdict = NULL;
6783 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6784 {
6785 clear_tv(rettv);
6786 ret = FAIL;
6787 }
6788 }
6789 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006790
Bram Moolenaar1d429612016-05-24 15:44:17 +02006791 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6792 * Don't do this when "Func" is already a partial that was bound
6793 * explicitly (pt_auto is FALSE). */
6794 if (selfdict != NULL
6795 && (rettv->v_type == VAR_FUNC
6796 || (rettv->v_type == VAR_PARTIAL
6797 && (rettv->vval.v_partial->pt_auto
6798 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006799 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006800
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006801 dict_unref(selfdict);
6802 return ret;
6803}
6804
6805/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006806 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006807 * value).
6808 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006809 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006810alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006811{
Bram Moolenaar33570922005-01-25 22:26:29 +00006812 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813}
6814
6815/*
6816 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 * The string "s" must have been allocated, it is consumed.
6818 * Return NULL for out of memory, the variable otherwise.
6819 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006820 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
Bram Moolenaar33570922005-01-25 22:26:29 +00006823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006825 rettv = alloc_tv();
6826 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006828 rettv->v_type = VAR_STRING;
6829 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831 else
6832 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834}
6835
6836/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 if (varp != NULL)
6843 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844 switch (varp->v_type)
6845 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006847 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006848 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006849 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 vim_free(varp->vval.v_string);
6851 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006852 case VAR_PARTIAL:
6853 partial_unref(varp->vval.v_partial);
6854 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855 case VAR_LIST:
6856 list_unref(varp->vval.v_list);
6857 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006858 case VAR_DICT:
6859 dict_unref(varp->vval.v_dict);
6860 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006861 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006862#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006863 job_unref(varp->vval.v_job);
6864 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006865#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006866 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006867#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006868 channel_unref(varp->vval.v_channel);
6869 break;
6870#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006871 case VAR_NUMBER:
6872 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006873 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006874 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006875 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 vim_free(varp);
6878 }
6879}
6880
6881/*
6882 * Free the memory for a variable value and set the value to NULL or 0.
6883 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006885clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 if (varp != NULL)
6888 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006889 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006891 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006892 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006893 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006894 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01006895 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006897 case VAR_PARTIAL:
6898 partial_unref(varp->vval.v_partial);
6899 varp->vval.v_partial = NULL;
6900 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006901 case VAR_LIST:
6902 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006903 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006904 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006905 case VAR_DICT:
6906 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006907 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006910 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006911 varp->vval.v_number = 0;
6912 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006913 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006914#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006915 varp->vval.v_float = 0.0;
6916 break;
6917#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006918 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006919#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006920 job_unref(varp->vval.v_job);
6921 varp->vval.v_job = NULL;
6922#endif
6923 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006924 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006925#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006926 channel_unref(varp->vval.v_channel);
6927 varp->vval.v_channel = NULL;
6928#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006929 case VAR_UNKNOWN:
6930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006932 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
6934}
6935
6936/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006937 * Set the value of a variable to NULL without freeing items.
6938 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006940init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941{
6942 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006944}
6945
6946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 * Get the number value of a variable.
6948 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006949 * For incompatible types, return 0.
6950 * get_tv_number_chk() is similar to get_tv_number(), but informs the
6951 * caller of incompatible types: it sets *denote to TRUE if "denote"
6952 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006954 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006957 int error = FALSE;
6958
6959 return get_tv_number_chk(varp, &error); /* return 0L on error */
6960}
6961
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006962 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006964{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006965 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006967 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006969 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006970 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006971 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006972#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00006973 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006974 break;
6975#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006977 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006978 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006979 break;
6980 case VAR_STRING:
6981 if (varp->vval.v_string != NULL)
6982 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006983 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006984 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006985 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006986 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006987 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006988 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006989 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006990 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006991 case VAR_SPECIAL:
6992 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
6993 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006994 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006995#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006996 EMSG(_("E910: Using a Job as a Number"));
6997 break;
6998#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006999 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007000#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007001 EMSG(_("E913: Using a Channel as a Number"));
7002 break;
7003#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007004 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007005 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007006 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007008 if (denote == NULL) /* useful for values that must be unsigned */
7009 n = -1;
7010 else
7011 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007012 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013}
7014
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007015#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007018{
7019 switch (varp->v_type)
7020 {
7021 case VAR_NUMBER:
7022 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007023 case VAR_FLOAT:
7024 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007025 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007026 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007027 EMSG(_("E891: Using a Funcref as a Float"));
7028 break;
7029 case VAR_STRING:
7030 EMSG(_("E892: Using a String as a Float"));
7031 break;
7032 case VAR_LIST:
7033 EMSG(_("E893: Using a List as a Float"));
7034 break;
7035 case VAR_DICT:
7036 EMSG(_("E894: Using a Dictionary as a Float"));
7037 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007038 case VAR_SPECIAL:
7039 EMSG(_("E907: Using a special value as a Float"));
7040 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007041 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007042# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007043 EMSG(_("E911: Using a Job as a Float"));
7044 break;
7045# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007046 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007047# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007048 EMSG(_("E914: Using a Channel as a Float"));
7049 break;
7050# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007051 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007052 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007053 break;
7054 }
7055 return 0;
7056}
7057#endif
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 * Get the string value of a variable.
7061 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007062 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7063 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 * If the String variable has never been set, return an empty string.
7065 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007066 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7067 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007069 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 static char_u mybuf[NUMBUFLEN];
7073
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007074 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075}
7076
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007077 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007080 char_u *res = get_tv_string_buf_chk(varp, buf);
7081
7082 return res != NULL ? res : (char_u *)"";
7083}
7084
Bram Moolenaar7d647822014-04-05 21:28:56 +02007085/*
7086 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7087 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007088 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007090{
7091 static char_u mybuf[NUMBUFLEN];
7092
7093 return get_tv_string_buf_chk(varp, mybuf);
7094}
7095
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007096 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007097get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007098{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007099 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007101 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007102 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007103 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007104 return buf;
7105 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007106 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007107 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 break;
7109 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007110 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111 break;
7112 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007113 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007115 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007116#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007117 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007118 break;
7119#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007120 case VAR_STRING:
7121 if (varp->vval.v_string != NULL)
7122 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007124 case VAR_SPECIAL:
7125 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7126 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007127 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007128#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007129 {
7130 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007131 char *status;
7132
7133 if (job == NULL)
7134 return (char_u *)"no process";
7135 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007136 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007137 : "run";
7138# ifdef UNIX
7139 vim_snprintf((char *)buf, NUMBUFLEN,
7140 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007141# elif defined(WIN32)
7142 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007143 "process %ld %s",
7144 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007145 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007146# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007147 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007148 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7149# endif
7150 return buf;
7151 }
7152#endif
7153 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007154 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007155#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007156 {
7157 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007158 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007159
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007160 if (channel == NULL)
7161 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7162 else
7163 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007164 "channel %d %s", channel->ch_id, status);
7165 return buf;
7166 }
7167#endif
7168 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 case VAR_UNKNOWN:
7170 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007173 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174}
7175
7176/*
7177 * Find variable "name" in the list of variables.
7178 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007179 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007180 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007183 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007184find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007188 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
Bram Moolenaara7043832005-01-21 11:56:39 +00007190 ht = find_var_ht(name, &varname);
7191 if (htp != NULL)
7192 *htp = ht;
7193 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007195 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7196 if (ret != NULL)
7197 return ret;
7198
7199 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007200 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201}
7202
7203/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007204 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007205 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208find_var_in_ht(
7209 hashtab_T *ht,
7210 int htname,
7211 char_u *varname,
7212 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 hashitem_T *hi;
7215
7216 if (*varname == NUL)
7217 {
7218 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007219 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007220 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007221 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 case 'g': return &globvars_var;
7223 case 'v': return &vimvars_var;
7224 case 'b': return &curbuf->b_bufvar;
7225 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007226 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007227 case 'l': return get_funccal_local_var();
7228 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 }
7230 return NULL;
7231 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007232
7233 hi = hash_find(ht, varname);
7234 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007235 {
7236 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007237 * worked find the variable again. Don't auto-load a script if it was
7238 * loaded already, otherwise it would be loaded every time when
7239 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007240 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007241 {
7242 /* Note: script_autoload() may make "hi" invalid. It must either
7243 * be obtained again or not used. */
7244 if (!script_autoload(varname, FALSE) || aborting())
7245 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007246 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007247 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007248 if (HASHITEM_EMPTY(hi))
7249 return NULL;
7250 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007252}
7253
7254/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007256 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007257 * Set "varname" to the start of name without ':'.
7258 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007259 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007260find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007262 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007263 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007264
Bram Moolenaar73627d02015-08-11 15:46:09 +02007265 if (name[0] == NUL)
7266 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 if (name[1] != ':')
7268 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007269 /* The name must not start with a colon or #. */
7270 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 return NULL;
7272 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007273
7274 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007275 hi = hash_find(&compat_hashtab, name);
7276 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007277 return &compat_hashtab;
7278
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007279 ht = get_funccal_local_ht();
7280 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007282 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007285 if (*name == 'g') /* global variable */
7286 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007287 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7288 */
7289 if (vim_strchr(name + 2, ':') != NULL
7290 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007291 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007293 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007295 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007296 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007297 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 if (*name == 'v') /* v: variable */
7299 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007300 if (*name == 'a') /* a: function argument */
7301 return get_funccal_args_ht();
7302 if (*name == 'l') /* l: local function variable */
7303 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 if (*name == 's' /* script variable */
7305 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7306 return &SCRIPT_VARS(current_SID);
7307 return NULL;
7308}
7309
7310/*
7311 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007312 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 * Returns NULL when it doesn't exist.
7314 */
7315 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007316get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007320 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (v == NULL)
7322 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324}
7325
7326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 * sourcing this script and when executing functions defined in the script.
7329 */
7330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
Bram Moolenaara7043832005-01-21 11:56:39 +00007333 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 hashtab_T *ht;
7335 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7338 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007339 /* Re-allocating ga_data means that an ht_array pointing to
7340 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007342 for (i = 1; i <= ga_scripts.ga_len; ++i)
7343 {
7344 ht = &SCRIPT_VARS(i);
7345 if (ht->ht_mask == HT_INIT_SIZE - 1)
7346 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007347 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007349 }
7350
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 while (ga_scripts.ga_len < id)
7352 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007353 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007354 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007355 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358 }
7359}
7360
7361/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7363 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 */
7365 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007366init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007369 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007370 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007371 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007372 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 dict_var->di_tv.vval.v_dict = dict;
7374 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007375 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7377 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378}
7379
7380/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007381 * Unreference a dictionary initialized by init_var_dict().
7382 */
7383 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007384unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007385{
7386 /* Now the dict needs to be freed if no one else is using it, go back to
7387 * normal reference counting. */
7388 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7389 dict_unref(dict);
7390}
7391
7392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 * Frees all allocated variables and the value they contain.
7395 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 */
7397 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007398vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007399{
7400 vars_clear_ext(ht, TRUE);
7401}
7402
7403/*
7404 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7405 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007407vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaara7043832005-01-21 11:56:39 +00007409 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 hashitem_T *hi;
7411 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007414 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007415 for (hi = ht->ht_array; todo > 0; ++hi)
7416 {
7417 if (!HASHITEM_EMPTY(hi))
7418 {
7419 --todo;
7420
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007422 * ht_array might change then. hash_clear() takes care of it
7423 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 v = HI2DI(hi);
7425 if (free_val)
7426 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007427 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007429 }
7430 }
7431 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007432 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433}
7434
Bram Moolenaara7043832005-01-21 11:56:39 +00007435/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 * Delete a variable from hashtab "ht" at item "hi".
7437 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007440delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007443
7444 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 clear_tv(&di->di_tv);
7446 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447}
7448
7449/*
7450 * List the value of one internal variable.
7451 */
7452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007453list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007455 char_u *tofree;
7456 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007457 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007458
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007459 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007461 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007462 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007466list_one_var_a(
7467 char_u *prefix,
7468 char_u *name,
7469 int type,
7470 char_u *string,
7471 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
Bram Moolenaar31859182007-08-14 20:41:13 +00007473 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7474 msg_start();
7475 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if (name != NULL) /* "a:" vars don't have a name stored */
7477 msg_puts(name);
7478 msg_putchar(' ');
7479 msg_advance(22);
7480 if (type == VAR_NUMBER)
7481 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007482 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483 msg_putchar('*');
7484 else if (type == VAR_LIST)
7485 {
7486 msg_putchar('[');
7487 if (*string == '[')
7488 ++string;
7489 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 else if (type == VAR_DICT)
7491 {
7492 msg_putchar('{');
7493 if (*string == '{')
7494 ++string;
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 else
7497 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007498
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007500
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007501 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007502 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007503 if (*first)
7504 {
7505 msg_clr_eos();
7506 *first = FALSE;
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508}
7509
7510/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 * If the variable already exists, the value is updated.
7513 * Otherwise the variable is created.
7514 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007516set_var(
7517 char_u *name,
7518 typval_T *tv,
7519 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007525 ht = find_var_ht(name, &varname);
7526 if (ht == NULL || *varname == NUL)
7527 {
7528 EMSG2(_(e_illvar), name);
7529 return;
7530 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007531 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007532
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007533 /* Search in parent scope which is possible to reference from lambda */
7534 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007535 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007536
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007537 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7538 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007539 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007544 if (var_check_ro(v->di_flags, name, FALSE)
7545 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007547
7548 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007549 * Handle setting internal v: variables separately where needed to
7550 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 */
7552 if (ht == &vimvarht)
7553 {
7554 if (v->di_tv.v_type == VAR_STRING)
7555 {
7556 vim_free(v->di_tv.vval.v_string);
7557 if (copy || tv->v_type != VAR_STRING)
7558 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7559 else
7560 {
7561 /* Take over the string to avoid an extra alloc/free. */
7562 v->di_tv.vval.v_string = tv->vval.v_string;
7563 tv->vval.v_string = NULL;
7564 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007565 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007567 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007568 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570 if (STRCMP(varname, "searchforward") == 0)
7571 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007572#ifdef FEAT_SEARCH_EXTRA
7573 else if (STRCMP(varname, "hlsearch") == 0)
7574 {
7575 no_hlsearch = !v->di_tv.vval.v_number;
7576 redraw_all_later(SOME_VALID);
7577 }
7578#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007579 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007580 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007581 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007582 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 }
7584
7585 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587 else /* add a new variable */
7588 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007589 /* Can't add "v:" variable. */
7590 if (ht == &vimvarht)
7591 {
7592 EMSG2(_(e_illvar), name);
7593 return;
7594 }
7595
Bram Moolenaar92124a32005-06-17 22:03:40 +00007596 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007597 if (!valid_varname(varname))
7598 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007599
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007600 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7601 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007602 if (v == NULL)
7603 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007607 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 return;
7609 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007610 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007612
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007613 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007615 else
7616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007618 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621}
7622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007624 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 * Also give an error message.
7626 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007628var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007629{
7630 if (flags & DI_FLAGS_RO)
7631 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007632 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 return TRUE;
7634 }
7635 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7636 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007637 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 return TRUE;
7639 }
7640 return FALSE;
7641}
7642
7643/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007644 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7645 * Also give an error message.
7646 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007648var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007649{
7650 if (flags & DI_FLAGS_FIX)
7651 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007652 EMSG2(_("E795: Cannot delete variable %s"),
7653 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007654 return TRUE;
7655 }
7656 return FALSE;
7657}
7658
7659/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007660 * Check if a funcref is assigned to a valid variable name.
7661 * Return TRUE and give an error if not.
7662 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007664var_check_func_name(
7665 char_u *name, /* points to start of variable name */
7666 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007667{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007668 /* Allow for w: b: s: and t:. */
7669 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007670 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7671 ? name[2] : name[0]))
7672 {
7673 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7674 name);
7675 return TRUE;
7676 }
7677 /* Don't allow hiding a function. When "v" is not NULL we might be
7678 * assigning another function to the same var, the type is checked
7679 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007680 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007681 {
7682 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7683 name);
7684 return TRUE;
7685 }
7686 return FALSE;
7687}
7688
7689/*
7690 * Check if a variable name is valid.
7691 * Return FALSE and give an error if not.
7692 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007694valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007695{
7696 char_u *p;
7697
7698 for (p = varname; *p != NUL; ++p)
7699 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7700 && *p != AUTOLOAD_CHAR)
7701 {
7702 EMSG2(_(e_illvar), varname);
7703 return FALSE;
7704 }
7705 return TRUE;
7706}
7707
7708/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007709 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007710 * Also give an error message, using "name" or _("name") when use_gettext is
7711 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007712 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007715{
7716 if (lock & VAR_LOCKED)
7717 {
7718 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007719 name == NULL ? (char_u *)_("Unknown")
7720 : use_gettext ? (char_u *)_(name)
7721 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007722 return TRUE;
7723 }
7724 if (lock & VAR_FIXED)
7725 {
7726 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007727 name == NULL ? (char_u *)_("Unknown")
7728 : use_gettext ? (char_u *)_(name)
7729 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007730 return TRUE;
7731 }
7732 return FALSE;
7733}
7734
7735/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007737 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007739 * It is OK for "from" and "to" to point to the same item. This is used to
7740 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007741 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007743copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007746 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 switch (from->v_type)
7748 {
7749 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007750 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007751 to->vval.v_number = from->vval.v_number;
7752 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007753 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007754#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007755 to->vval.v_float = from->vval.v_float;
7756 break;
7757#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007760 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007761 if (to->vval.v_job != NULL)
7762 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007763 break;
7764#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007765 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007766#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007767 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007768 if (to->vval.v_channel != NULL)
7769 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007770 break;
7771#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772 case VAR_STRING:
7773 case VAR_FUNC:
7774 if (from->vval.v_string == NULL)
7775 to->vval.v_string = NULL;
7776 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007777 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007778 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007779 if (from->v_type == VAR_FUNC)
7780 func_ref(to->vval.v_string);
7781 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007783 case VAR_PARTIAL:
7784 if (from->vval.v_partial == NULL)
7785 to->vval.v_partial = NULL;
7786 else
7787 {
7788 to->vval.v_partial = from->vval.v_partial;
7789 ++to->vval.v_partial->pt_refcount;
7790 }
7791 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792 case VAR_LIST:
7793 if (from->vval.v_list == NULL)
7794 to->vval.v_list = NULL;
7795 else
7796 {
7797 to->vval.v_list = from->vval.v_list;
7798 ++to->vval.v_list->lv_refcount;
7799 }
7800 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007801 case VAR_DICT:
7802 if (from->vval.v_dict == NULL)
7803 to->vval.v_dict = NULL;
7804 else
7805 {
7806 to->vval.v_dict = from->vval.v_dict;
7807 ++to->vval.v_dict->dv_refcount;
7808 }
7809 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007810 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007811 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 break;
7813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815
7816/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 * Make a copy of an item.
7818 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007819 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7820 * reference to an already copied list/dict can be used.
7821 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824item_copy(
7825 typval_T *from,
7826 typval_T *to,
7827 int deep,
7828 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829{
7830 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007831 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007834 {
7835 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007836 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 }
7838 ++recurse;
7839
7840 switch (from->v_type)
7841 {
7842 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007843 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 case VAR_STRING:
7845 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007846 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007847 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007848 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007849 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 copy_tv(from, to);
7851 break;
7852 case VAR_LIST:
7853 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007854 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007855 if (from->vval.v_list == NULL)
7856 to->vval.v_list = NULL;
7857 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7858 {
7859 /* use the copy made earlier */
7860 to->vval.v_list = from->vval.v_list->lv_copylist;
7861 ++to->vval.v_list->lv_refcount;
7862 }
7863 else
7864 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7865 if (to->vval.v_list == NULL)
7866 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007867 break;
7868 case VAR_DICT:
7869 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007870 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007871 if (from->vval.v_dict == NULL)
7872 to->vval.v_dict = NULL;
7873 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7874 {
7875 /* use the copy made earlier */
7876 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7877 ++to->vval.v_dict->dv_refcount;
7878 }
7879 else
7880 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7881 if (to->vval.v_dict == NULL)
7882 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007884 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007885 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007886 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887 }
7888 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007889 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890}
7891
7892/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 * This function is used by f_input() and f_inputdialog() functions. The third
7894 * argument to f_input() specifies the type of completion to use at the
7895 * prompt. The third argument to f_inputdialog() specifies the value to return
7896 * when the user cancels the prompt.
7897 */
7898 void
7899get_user_input(
7900 typval_T *argvars,
7901 typval_T *rettv,
7902 int inputdialog,
7903 int secret)
7904{
7905 char_u *prompt = get_tv_string_chk(&argvars[0]);
7906 char_u *p = NULL;
7907 int c;
7908 char_u buf[NUMBUFLEN];
7909 int cmd_silent_save = cmd_silent;
7910 char_u *defstr = (char_u *)"";
7911 int xp_type = EXPAND_NOTHING;
7912 char_u *xp_arg = NULL;
7913
7914 rettv->v_type = VAR_STRING;
7915 rettv->vval.v_string = NULL;
7916
7917#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007918 /* While starting up, there is no place to enter text. When running tests
7919 * with --not-a-term we assume feedkeys() will be used. */
7920 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 return;
7922#endif
7923
7924 cmd_silent = FALSE; /* Want to see the prompt. */
7925 if (prompt != NULL)
7926 {
7927 /* Only the part of the message after the last NL is considered as
7928 * prompt for the command line */
7929 p = vim_strrchr(prompt, '\n');
7930 if (p == NULL)
7931 p = prompt;
7932 else
7933 {
7934 ++p;
7935 c = *p;
7936 *p = NUL;
7937 msg_start();
7938 msg_clr_eos();
7939 msg_puts_attr(prompt, echo_attr);
7940 msg_didout = FALSE;
7941 msg_starthere();
7942 *p = c;
7943 }
7944 cmdline_row = msg_row;
7945
7946 if (argvars[1].v_type != VAR_UNKNOWN)
7947 {
7948 defstr = get_tv_string_buf_chk(&argvars[1], buf);
7949 if (defstr != NULL)
7950 stuffReadbuffSpec(defstr);
7951
7952 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
7953 {
7954 char_u *xp_name;
7955 int xp_namelen;
7956 long argt;
7957
7958 /* input() with a third argument: completion */
7959 rettv->vval.v_string = NULL;
7960
7961 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
7962 if (xp_name == NULL)
7963 return;
7964
7965 xp_namelen = (int)STRLEN(xp_name);
7966
7967 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
7968 &xp_arg) == FAIL)
7969 return;
7970 }
7971 }
7972
7973 if (defstr != NULL)
7974 {
7975 int save_ex_normal_busy = ex_normal_busy;
7976 ex_normal_busy = 0;
7977 rettv->vval.v_string =
7978 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
7979 xp_type, xp_arg);
7980 ex_normal_busy = save_ex_normal_busy;
7981 }
7982 if (inputdialog && rettv->vval.v_string == NULL
7983 && argvars[1].v_type != VAR_UNKNOWN
7984 && argvars[2].v_type != VAR_UNKNOWN)
7985 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
7986 &argvars[2], buf));
7987
7988 vim_free(xp_arg);
7989
7990 /* since the user typed this, no need to wait for return */
7991 need_wait_return = FALSE;
7992 msg_didout = FALSE;
7993 }
7994 cmd_silent = cmd_silent_save;
7995}
7996
7997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 * ":echo expr1 ..." print each argument separated with a space, add a
7999 * newline at the end.
8000 * ":echon expr1 ..." print each argument plain.
8001 */
8002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 char_u *p;
8009 int needclr = TRUE;
8010 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008011 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
8013 if (eap->skip)
8014 ++emsg_skip;
8015 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008017 /* If eval1() causes an error message the text from the command may
8018 * still need to be cleared. E.g., "echo 22,44". */
8019 need_clr_eos = needclr;
8020
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
8024 /*
8025 * Report the invalid expression unless the expression evaluation
8026 * has been cancelled due to an aborting error, an interrupt, or an
8027 * exception.
8028 */
8029 if (!aborting())
8030 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008031 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 break;
8033 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034 need_clr_eos = FALSE;
8035
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (!eap->skip)
8037 {
8038 if (atstart)
8039 {
8040 atstart = FALSE;
8041 /* Call msg_start() after eval1(), evaluating the expression
8042 * may cause a message to appear. */
8043 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008044 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008045 /* Mark the saved text as finishing the line, so that what
8046 * follows is displayed on a new line when scrolling back
8047 * at the more prompt. */
8048 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052 else if (eap->cmdidx == CMD_echo)
8053 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008054 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008055 if (p != NULL)
8056 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008058 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008060 if (*p != TAB && needclr)
8061 {
8062 /* remove any text still there from the command */
8063 msg_clr_eos();
8064 needclr = FALSE;
8065 }
8066 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 }
8068 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008069 {
8070#ifdef FEAT_MBYTE
8071 if (has_mbyte)
8072 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008073 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008074
8075 (void)msg_outtrans_len_attr(p, i, echo_attr);
8076 p += i - 1;
8077 }
8078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008080 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008083 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008085 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 arg = skipwhite(arg);
8087 }
8088 eap->nextcmd = check_nextcmd(arg);
8089
8090 if (eap->skip)
8091 --emsg_skip;
8092 else
8093 {
8094 /* remove text that may still be there from the command */
8095 if (needclr)
8096 msg_clr_eos();
8097 if (eap->cmdidx == CMD_echo)
8098 msg_end();
8099 }
8100}
8101
8102/*
8103 * ":echohl {name}".
8104 */
8105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008106ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008108 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109}
8110
8111/*
8112 * ":execute expr1 ..." execute the result of an expression.
8113 * ":echomsg expr1 ..." Print a message
8114 * ":echoerr expr1 ..." Print an error
8115 * Each gets spaces around each argument and a newline at the end for
8116 * echo commands
8117 */
8118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008119ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 int ret = OK;
8124 char_u *p;
8125 garray_T ga;
8126 int len;
8127 int save_did_emsg;
8128
8129 ga_init2(&ga, 1, 80);
8130
8131 if (eap->skip)
8132 ++emsg_skip;
8133 while (*arg != NUL && *arg != '|' && *arg != '\n')
8134 {
8135 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
8138 /*
8139 * Report the invalid expression unless the expression evaluation
8140 * has been cancelled due to an aborting error, an interrupt, or an
8141 * exception.
8142 */
8143 if (!aborting())
8144 EMSG2(_(e_invexpr2), p);
8145 ret = FAIL;
8146 break;
8147 }
8148
8149 if (!eap->skip)
8150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008151 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 len = (int)STRLEN(p);
8153 if (ga_grow(&ga, len + 2) == FAIL)
8154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 ret = FAIL;
8157 break;
8158 }
8159 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 ga.ga_len += len;
8163 }
8164
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008165 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 arg = skipwhite(arg);
8167 }
8168
8169 if (ret != FAIL && ga.ga_data != NULL)
8170 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008171 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8172 {
8173 /* Mark the already saved text as finishing the line, so that what
8174 * follows is displayed on a new line when scrolling back at the
8175 * more prompt. */
8176 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008177 }
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008182 out_flush();
8183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 else if (eap->cmdidx == CMD_echoerr)
8185 {
8186 /* We don't want to abort following commands, restore did_emsg. */
8187 save_did_emsg = did_emsg;
8188 EMSG((char_u *)ga.ga_data);
8189 if (!force_abort)
8190 did_emsg = save_did_emsg;
8191 }
8192 else if (eap->cmdidx == CMD_execute)
8193 do_cmdline((char_u *)ga.ga_data,
8194 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8195 }
8196
8197 ga_clear(&ga);
8198
8199 if (eap->skip)
8200 --emsg_skip;
8201
8202 eap->nextcmd = check_nextcmd(arg);
8203}
8204
8205/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 * Find window specified by "vp" in tabpage "tp".
8207 */
8208 win_T *
8209find_win_by_nr(
8210 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008211 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 int nr;
8215
8216 nr = (int)get_tv_number_chk(vp, NULL);
8217
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 if (nr < 0)
8219 return NULL;
8220 if (nr == 0)
8221 return curwin;
8222
Bram Moolenaar29323592016-07-24 22:04:11 +02008223 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8224 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225 if (nr >= LOWEST_WIN_ID)
8226 {
8227 if (wp->w_id == nr)
8228 return wp;
8229 }
8230 else if (--nr <= 0)
8231 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008232 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 if (nr >= LOWEST_WIN_ID)
8234 return NULL;
8235 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236}
8237
8238/*
8239 * Find window specified by "wvp" in tabpage "tvp".
8240 */
8241 win_T *
8242find_tabwin(
8243 typval_T *wvp, /* VAR_UNKNOWN for current window */
8244 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8245{
8246 win_T *wp = NULL;
8247 tabpage_T *tp = NULL;
8248 long n;
8249
8250 if (wvp->v_type != VAR_UNKNOWN)
8251 {
8252 if (tvp->v_type != VAR_UNKNOWN)
8253 {
8254 n = (long)get_tv_number(tvp);
8255 if (n >= 0)
8256 tp = find_tabpage(n);
8257 }
8258 else
8259 tp = curtab;
8260
8261 if (tp != NULL)
8262 wp = find_win_by_nr(wvp, tp);
8263 }
8264 else
8265 wp = curwin;
8266
8267 return wp;
8268}
8269
8270/*
8271 * getwinvar() and gettabwinvar()
8272 */
8273 void
8274getwinvar(
8275 typval_T *argvars,
8276 typval_T *rettv,
8277 int off) /* 1 for gettabwinvar() */
8278{
8279 win_T *win;
8280 char_u *varname;
8281 dictitem_T *v;
8282 tabpage_T *tp = NULL;
8283 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 win_T *oldcurwin;
8285 tabpage_T *oldtabpage;
8286 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 if (off == 1)
8289 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8290 else
8291 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 win = find_win_by_nr(&argvars[off], tp);
8293 varname = get_tv_string_chk(&argvars[off + 1]);
8294 ++emsg_off;
8295
8296 rettv->v_type = VAR_STRING;
8297 rettv->vval.v_string = NULL;
8298
8299 if (win != NULL && varname != NULL)
8300 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 /* Set curwin to be our win, temporarily. Also set the tabpage,
8302 * otherwise the window is not valid. Only do this when needed,
8303 * autocommands get blocked. */
8304 need_switch_win = !(tp == curtab && win == curwin);
8305 if (!need_switch_win
8306 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008308 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008310 if (varname[1] == NUL)
8311 {
8312 /* get all window-local options in a dict */
8313 dict_T *opts = get_winbuf_options(FALSE);
8314
8315 if (opts != NULL)
8316 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008317 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008318 done = TRUE;
8319 }
8320 }
8321 else if (get_option_tv(&varname, rettv, 1) == OK)
8322 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 done = TRUE;
8324 }
8325 else
8326 {
8327 /* Look up the variable. */
8328 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8329 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8330 varname, FALSE);
8331 if (v != NULL)
8332 {
8333 copy_tv(&v->di_tv, rettv);
8334 done = TRUE;
8335 }
8336 }
8337 }
8338
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 if (need_switch_win)
8340 /* restore previous notion of curwin */
8341 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 }
8343
8344 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8345 /* use the default return value */
8346 copy_tv(&argvars[off + 2], rettv);
8347
8348 --emsg_off;
8349}
8350
8351/*
8352 * "setwinvar()" and "settabwinvar()" functions
8353 */
8354 void
8355setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8356{
8357 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 win_T *save_curwin;
8359 tabpage_T *save_curtab;
8360 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 char_u *varname, *winvarname;
8362 typval_T *varp;
8363 char_u nbuf[NUMBUFLEN];
8364 tabpage_T *tp = NULL;
8365
8366 if (check_restricted() || check_secure())
8367 return;
8368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 if (off == 1)
8370 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8371 else
8372 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 win = find_win_by_nr(&argvars[off], tp);
8374 varname = get_tv_string_chk(&argvars[off + 1]);
8375 varp = &argvars[off + 2];
8376
8377 if (win != NULL && varname != NULL && varp != NULL)
8378 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 need_switch_win = !(tp == curtab && win == curwin);
8380 if (!need_switch_win
8381 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 {
8383 if (*varname == '&')
8384 {
8385 long numval;
8386 char_u *strval;
8387 int error = FALSE;
8388
8389 ++varname;
8390 numval = (long)get_tv_number_chk(varp, &error);
8391 strval = get_tv_string_buf_chk(varp, nbuf);
8392 if (!error && strval != NULL)
8393 set_option_value(varname, numval, strval, OPT_LOCAL);
8394 }
8395 else
8396 {
8397 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8398 if (winvarname != NULL)
8399 {
8400 STRCPY(winvarname, "w:");
8401 STRCPY(winvarname + 2, varname);
8402 set_var(winvarname, varp, TRUE);
8403 vim_free(winvarname);
8404 }
8405 }
8406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 if (need_switch_win)
8408 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 }
8410}
8411
8412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8414 * "arg" points to the "&" or '+' when called, to "option" when returning.
8415 * Returns NULL when no option name found. Otherwise pointer to the char
8416 * after the option name.
8417 */
8418 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008419find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 char_u *p = *arg;
8422
8423 ++p;
8424 if (*p == 'g' && p[1] == ':')
8425 {
8426 *opt_flags = OPT_GLOBAL;
8427 p += 2;
8428 }
8429 else if (*p == 'l' && p[1] == ':')
8430 {
8431 *opt_flags = OPT_LOCAL;
8432 p += 2;
8433 }
8434 else
8435 *opt_flags = 0;
8436
8437 if (!ASCII_ISALPHA(*p))
8438 return NULL;
8439 *arg = p;
8440
8441 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8442 p += 4; /* termcap option */
8443 else
8444 while (ASCII_ISALPHA(*p))
8445 ++p;
8446 return p;
8447}
8448
8449/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008450 * Return the autoload script name for a function or variable name.
8451 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008453 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008454autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008455{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008456 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008457 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008458
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008459 /* Get the script file name: replace '#' with '/', append ".vim". */
8460 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8461 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008462 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008463 STRCPY(scriptname, "autoload/");
8464 STRCAT(scriptname, name);
8465 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8466 STRCAT(scriptname, ".vim");
8467 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8468 *p = '/';
8469 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008470}
8471
8472/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008473 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 * Return TRUE if a package was loaded.
8475 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008477script_autoload(
8478 char_u *name,
8479 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008480{
8481 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008482 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008483 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008484 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008485
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008486 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008487 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008488 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008489 return FALSE;
8490
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008491 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008493 /* Find the name in the list of previously loaded package names. Skip
8494 * "autoload/", it's always the same. */
8495 for (i = 0; i < ga_loaded.ga_len; ++i)
8496 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8497 break;
8498 if (!reload && i < ga_loaded.ga_len)
8499 ret = FALSE; /* was loaded already */
8500 else
8501 {
8502 /* Remember the name if it wasn't loaded already. */
8503 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8504 {
8505 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8506 tofree = NULL;
8507 }
8508
8509 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008510 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008511 ret = TRUE;
8512 }
8513
8514 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008515 return ret;
8516}
8517
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8519typedef enum
8520{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008521 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8522 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8523 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524} var_flavour_T;
8525
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008526static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527
8528 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008529var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 char_u *p = varname;
8532
8533 if (ASCII_ISUPPER(*p))
8534 {
8535 while (*(++p))
8536 if (ASCII_ISLOWER(*p))
8537 return VAR_FLAVOUR_SESSION;
8538 return VAR_FLAVOUR_VIMINFO;
8539 }
8540 else
8541 return VAR_FLAVOUR_DEFAULT;
8542}
8543#endif
8544
8545#if defined(FEAT_VIMINFO) || defined(PROTO)
8546/*
8547 * Restore global vars that start with a capital from the viminfo file
8548 */
8549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008550read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008553 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008554 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008555 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
8557 if (!writing && (find_viminfo_parameter('!') != NULL))
8558 {
8559 tab = vim_strchr(virp->vir_line + 1, '\t');
8560 if (tab != NULL)
8561 {
8562 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008563 switch (*tab)
8564 {
8565 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008566#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008567 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008568#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008569 case 'D': type = VAR_DICT; break;
8570 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008571 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574 tab = vim_strchr(tab, '\t');
8575 if (tab != NULL)
8576 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008577 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008578 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008579 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008581#ifdef FEAT_FLOAT
8582 else if (type == VAR_FLOAT)
8583 (void)string2float(tab + 1, &tv.vval.v_float);
8584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008586 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008587 if (type == VAR_DICT || type == VAR_LIST)
8588 {
8589 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8590
8591 if (etv == NULL)
8592 /* Failed to parse back the dict or list, use it as a
8593 * string. */
8594 tv.v_type = VAR_STRING;
8595 else
8596 {
8597 vim_free(tv.vval.v_string);
8598 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008599 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008600 }
8601 }
8602
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008603 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008604 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008605 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008606 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008607
8608 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008609 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008610 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8611 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613 }
8614 }
8615
8616 return viminfo_readline(virp);
8617}
8618
8619/*
8620 * Write global vars that start with a capital to the viminfo file
8621 */
8622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008623write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 hashitem_T *hi;
8626 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008627 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008628 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008629 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008631 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632
8633 if (find_viminfo_parameter('!') == NULL)
8634 return;
8635
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008636 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008637
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008638 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008641 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008643 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008644 this_var = HI2DI(hi);
8645 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008648 {
8649 case VAR_STRING: s = "STR"; break;
8650 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008651 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008652 case VAR_DICT: s = "DIC"; break;
8653 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008654 case VAR_SPECIAL: s = "XPL"; break;
8655
8656 case VAR_UNKNOWN:
8657 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008658 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008659 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008660 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008661 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008662 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008664 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008665 if (p != NULL)
8666 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008667 vim_free(tofree);
8668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670 }
8671}
8672#endif
8673
8674#if defined(FEAT_SESSION) || defined(PROTO)
8675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008676store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 hashitem_T *hi;
8679 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008680 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 char_u *p, *t;
8682
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008683 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008686 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008688 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 this_var = HI2DI(hi);
8690 if ((this_var->di_tv.v_type == VAR_NUMBER
8691 || this_var->di_tv.v_type == VAR_STRING)
8692 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008693 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008694 /* Escape special characters with a backslash. Turn a LF and
8695 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008697 (char_u *)"\\\"\n\r");
8698 if (p == NULL) /* out of memory */
8699 break;
8700 for (t = p; *t != NUL; ++t)
8701 if (*t == '\n')
8702 *t = 'n';
8703 else if (*t == '\r')
8704 *t = 'r';
8705 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 this_var->di_key,
8707 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8708 : ' ',
8709 p,
8710 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8711 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008712 || put_eol(fd) == FAIL)
8713 {
8714 vim_free(p);
8715 return FAIL;
8716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 vim_free(p);
8718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719#ifdef FEAT_FLOAT
8720 else if (this_var->di_tv.v_type == VAR_FLOAT
8721 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8722 {
8723 float_T f = this_var->di_tv.vval.v_float;
8724 int sign = ' ';
8725
8726 if (f < 0)
8727 {
8728 f = -f;
8729 sign = '-';
8730 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008731 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008732 this_var->di_key, sign, f) < 0)
8733 || put_eol(fd) == FAIL)
8734 return FAIL;
8735 }
8736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 }
8738 }
8739 return OK;
8740}
8741#endif
8742
Bram Moolenaar661b1822005-07-28 22:36:45 +00008743/*
8744 * Display script name where an item was last set.
8745 * Should only be invoked when 'verbose' is non-zero.
8746 */
8747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008749{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008750 char_u *p;
8751
Bram Moolenaar661b1822005-07-28 22:36:45 +00008752 if (scriptID != 0)
8753 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008754 p = home_replace_save(NULL, get_scriptname(scriptID));
8755 if (p != NULL)
8756 {
8757 verbose_enter();
8758 MSG_PUTS(_("\n\tLast set from "));
8759 MSG_PUTS(p);
8760 vim_free(p);
8761 verbose_leave();
8762 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008763 }
8764}
8765
Bram Moolenaar53744302015-07-17 17:38:22 +02008766/* reset v:option_new, v:option_old and v:option_type */
8767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008768reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008769{
8770 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8771 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8772 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8773}
8774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775/*
8776 * Prepare "gap" for an assert error and add the sourcing position.
8777 */
8778 void
8779prepare_assert_error(garray_T *gap)
8780{
8781 char buf[NUMBUFLEN];
8782
8783 ga_init2(gap, 1, 100);
8784 if (sourcing_name != NULL)
8785 {
8786 ga_concat(gap, sourcing_name);
8787 if (sourcing_lnum > 0)
8788 ga_concat(gap, (char_u *)" ");
8789 }
8790 if (sourcing_lnum > 0)
8791 {
8792 sprintf(buf, "line %ld", (long)sourcing_lnum);
8793 ga_concat(gap, (char_u *)buf);
8794 }
8795 if (sourcing_name != NULL || sourcing_lnum > 0)
8796 ga_concat(gap, (char_u *)": ");
8797}
8798
8799/*
8800 * Add an assert error to v:errors.
8801 */
8802 void
8803assert_error(garray_T *gap)
8804{
8805 struct vimvar *vp = &vimvars[VV_ERRORS];
8806
8807 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8808 /* Make sure v:errors is a list. */
8809 set_vim_var_list(VV_ERRORS, list_alloc());
8810 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8811}
8812
Bram Moolenaar65a54642018-04-28 16:56:53 +02008813 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814assert_equal_common(typval_T *argvars, assert_type_T atype)
8815{
8816 garray_T ga;
8817
8818 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8819 != (atype == ASSERT_EQUAL))
8820 {
8821 prepare_assert_error(&ga);
8822 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8823 atype);
8824 assert_error(&ga);
8825 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008826 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008828 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829}
8830
Bram Moolenaar65a54642018-04-28 16:56:53 +02008831 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01008832assert_equalfile(typval_T *argvars)
8833{
8834 char_u buf1[NUMBUFLEN];
8835 char_u buf2[NUMBUFLEN];
8836 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1);
8837 char_u *fname2 = get_tv_string_buf_chk(&argvars[1], buf2);
8838 garray_T ga;
8839 FILE *fd1;
8840 FILE *fd2;
8841
8842 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008843 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008844
8845 IObuff[0] = NUL;
8846 fd1 = mch_fopen((char *)fname1, READBIN);
8847 if (fd1 == NULL)
8848 {
8849 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
8850 }
8851 else
8852 {
8853 fd2 = mch_fopen((char *)fname2, READBIN);
8854 if (fd2 == NULL)
8855 {
8856 fclose(fd1);
8857 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
8858 }
8859 else
8860 {
8861 int c1, c2;
8862 long count = 0;
8863
8864 for (;;)
8865 {
8866 c1 = fgetc(fd1);
8867 c2 = fgetc(fd2);
8868 if (c1 == EOF)
8869 {
8870 if (c2 != EOF)
8871 STRCPY(IObuff, "first file is shorter");
8872 break;
8873 }
8874 else if (c2 == EOF)
8875 {
8876 STRCPY(IObuff, "second file is shorter");
8877 break;
8878 }
8879 else if (c1 != c2)
8880 {
8881 vim_snprintf((char *)IObuff, IOSIZE,
8882 "difference at byte %ld", count);
8883 break;
8884 }
8885 ++count;
8886 }
Bram Moolenaar30494182018-02-20 21:46:05 +01008887 fclose(fd1);
8888 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01008889 }
8890 }
8891 if (IObuff[0] != NUL)
8892 {
8893 prepare_assert_error(&ga);
8894 ga_concat(&ga, IObuff);
8895 assert_error(&ga);
8896 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008897 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008898 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008899 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01008900}
8901
Bram Moolenaar65a54642018-04-28 16:56:53 +02008902 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903assert_match_common(typval_T *argvars, assert_type_T atype)
8904{
8905 garray_T ga;
8906 char_u buf1[NUMBUFLEN];
8907 char_u buf2[NUMBUFLEN];
8908 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8909 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8910
8911 if (pat == NULL || text == NULL)
8912 EMSG(_(e_invarg));
8913 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8914 {
8915 prepare_assert_error(&ga);
8916 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8917 atype);
8918 assert_error(&ga);
8919 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008920 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008922 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923}
8924
Bram Moolenaar65a54642018-04-28 16:56:53 +02008925 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02008926assert_inrange(typval_T *argvars)
8927{
8928 garray_T ga;
8929 int error = FALSE;
8930 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
8931 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
8932 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
8933 char_u *tofree;
8934 char msg[200];
8935 char_u numbuf[NUMBUFLEN];
8936
8937 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02008938 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008939 if (actual < lower || actual > upper)
8940 {
8941 prepare_assert_error(&ga);
8942 if (argvars[3].v_type != VAR_UNKNOWN)
8943 {
8944 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
8945 vim_free(tofree);
8946 }
8947 else
8948 {
8949 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
8950 (long)lower, (long)upper, (long)actual);
8951 ga_concat(&ga, (char_u *)msg);
8952 }
8953 assert_error(&ga);
8954 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008955 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008956 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008957 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02008958}
8959
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960/*
8961 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02008962 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02008964 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965assert_bool(typval_T *argvars, int isTrue)
8966{
8967 int error = FALSE;
8968 garray_T ga;
8969
8970 if (argvars[0].v_type == VAR_SPECIAL
8971 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02008972 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973 if (argvars[0].v_type != VAR_NUMBER
8974 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8975 || error)
8976 {
8977 prepare_assert_error(&ga);
8978 fill_assert_error(&ga, &argvars[1],
8979 (char_u *)(isTrue ? "True" : "False"),
8980 NULL, &argvars[0], ASSERT_OTHER);
8981 assert_error(&ga);
8982 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008983 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02008985 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986}
8987
Bram Moolenaar65a54642018-04-28 16:56:53 +02008988 int
Bram Moolenaar42205552017-03-18 19:42:22 +01008989assert_report(typval_T *argvars)
8990{
8991 garray_T ga;
8992
8993 prepare_assert_error(&ga);
8994 ga_concat(&ga, get_tv_string(&argvars[0]));
8995 assert_error(&ga);
8996 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02008997 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01008998}
8999
Bram Moolenaar65a54642018-04-28 16:56:53 +02009000 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001assert_exception(typval_T *argvars)
9002{
9003 garray_T ga;
9004 char_u *error = get_tv_string_chk(&argvars[0]);
9005
9006 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9007 {
9008 prepare_assert_error(&ga);
9009 ga_concat(&ga, (char_u *)"v:exception is not set");
9010 assert_error(&ga);
9011 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009012 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 }
9014 else if (error != NULL
9015 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9016 {
9017 prepare_assert_error(&ga);
9018 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9019 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9020 assert_error(&ga);
9021 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009022 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009024 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025}
9026
Bram Moolenaar65a54642018-04-28 16:56:53 +02009027 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009028assert_beeps(typval_T *argvars)
9029{
9030 char_u *cmd = get_tv_string_chk(&argvars[0]);
9031 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009032 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009033
9034 called_vim_beep = FALSE;
9035 suppress_errthrow = TRUE;
9036 emsg_silent = FALSE;
9037 do_cmdline_cmd(cmd);
9038 if (!called_vim_beep)
9039 {
9040 prepare_assert_error(&ga);
9041 ga_concat(&ga, (char_u *)"command did not beep: ");
9042 ga_concat(&ga, cmd);
9043 assert_error(&ga);
9044 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009045 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009046 }
9047
9048 suppress_errthrow = FALSE;
9049 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009050 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009051}
9052
Bram Moolenaar65a54642018-04-28 16:56:53 +02009053 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054assert_fails(typval_T *argvars)
9055{
9056 char_u *cmd = get_tv_string_chk(&argvars[0]);
9057 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009058 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059
9060 called_emsg = FALSE;
9061 suppress_errthrow = TRUE;
9062 emsg_silent = TRUE;
9063 do_cmdline_cmd(cmd);
9064 if (!called_emsg)
9065 {
9066 prepare_assert_error(&ga);
9067 ga_concat(&ga, (char_u *)"command did not fail: ");
9068 ga_concat(&ga, cmd);
9069 assert_error(&ga);
9070 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009071 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 }
9073 else if (argvars[1].v_type != VAR_UNKNOWN)
9074 {
9075 char_u buf[NUMBUFLEN];
9076 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9077
9078 if (error == NULL
9079 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9080 {
9081 prepare_assert_error(&ga);
9082 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9083 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9084 assert_error(&ga);
9085 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009086 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 }
9088 }
9089
9090 called_emsg = FALSE;
9091 suppress_errthrow = FALSE;
9092 emsg_silent = FALSE;
9093 emsg_on_display = FALSE;
9094 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009095 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096}
9097
9098/*
9099 * Append "str" to "gap", escaping unprintable characters.
9100 * Changes NL to \n, CR to \r, etc.
9101 */
9102 static void
9103ga_concat_esc(garray_T *gap, char_u *str)
9104{
9105 char_u *p;
9106 char_u buf[NUMBUFLEN];
9107
9108 if (str == NULL)
9109 {
9110 ga_concat(gap, (char_u *)"NULL");
9111 return;
9112 }
9113
9114 for (p = str; *p != NUL; ++p)
9115 switch (*p)
9116 {
9117 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9118 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9119 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9120 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9121 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9122 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9123 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9124 default:
9125 if (*p < ' ')
9126 {
9127 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9128 ga_concat(gap, buf);
9129 }
9130 else
9131 ga_append(gap, *p);
9132 break;
9133 }
9134}
9135
9136/*
9137 * Fill "gap" with information about an assert error.
9138 */
9139 void
9140fill_assert_error(
9141 garray_T *gap,
9142 typval_T *opt_msg_tv,
9143 char_u *exp_str,
9144 typval_T *exp_tv,
9145 typval_T *got_tv,
9146 assert_type_T atype)
9147{
9148 char_u numbuf[NUMBUFLEN];
9149 char_u *tofree;
9150
9151 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9152 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009153 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9154 vim_free(tofree);
9155 ga_concat(gap, (char_u *)": ");
9156 }
9157
9158 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9159 ga_concat(gap, (char_u *)"Pattern ");
9160 else if (atype == ASSERT_NOTEQUAL)
9161 ga_concat(gap, (char_u *)"Expected not equal to ");
9162 else
9163 ga_concat(gap, (char_u *)"Expected ");
9164 if (exp_str == NULL)
9165 {
9166 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167 vim_free(tofree);
9168 }
9169 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009170 ga_concat_esc(gap, exp_str);
9171 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009173 if (atype == ASSERT_MATCH)
9174 ga_concat(gap, (char_u *)" does not match ");
9175 else if (atype == ASSERT_NOTMATCH)
9176 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009178 ga_concat(gap, (char_u *)" but got ");
9179 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9180 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 }
9182}
9183
Bram Moolenaar31988702018-02-13 12:57:42 +01009184/*
9185 * Compare "typ1" and "typ2". Put the result in "typ1".
9186 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009187 int
9188typval_compare(
9189 typval_T *typ1, /* first operand */
9190 typval_T *typ2, /* second operand */
9191 exptype_T type, /* operator */
9192 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009193 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009194{
9195 int i;
9196 varnumber_T n1, n2;
9197 char_u *s1, *s2;
9198 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9199
Bram Moolenaar31988702018-02-13 12:57:42 +01009200 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009201 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009202 /* For "is" a different type always means FALSE, for "notis"
9203 * it means TRUE. */
9204 n1 = (type == TYPE_NEQUAL);
9205 }
9206 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9207 {
9208 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009209 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009210 n1 = (typ1->v_type == typ2->v_type
9211 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009212 if (type == TYPE_NEQUAL)
9213 n1 = !n1;
9214 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009215 else if (typ1->v_type != typ2->v_type
9216 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009217 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009218 if (typ1->v_type != typ2->v_type)
9219 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009220 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009221 EMSG(_("E692: Invalid operation for List"));
9222 clear_tv(typ1);
9223 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009224 }
9225 else
9226 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009227 /* Compare two Lists for being equal or unequal. */
9228 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9229 ic, FALSE);
9230 if (type == TYPE_NEQUAL)
9231 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009232 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009233 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009234
9235 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9236 {
9237 if (type_is)
9238 {
9239 n1 = (typ1->v_type == typ2->v_type
9240 && typ1->vval.v_dict == typ2->vval.v_dict);
9241 if (type == TYPE_NEQUAL)
9242 n1 = !n1;
9243 }
9244 else if (typ1->v_type != typ2->v_type
9245 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9246 {
9247 if (typ1->v_type != typ2->v_type)
9248 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9249 else
9250 EMSG(_("E736: Invalid operation for Dictionary"));
9251 clear_tv(typ1);
9252 return FAIL;
9253 }
9254 else
9255 {
9256 /* Compare two Dictionaries for being equal or unequal. */
9257 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9258 ic, FALSE);
9259 if (type == TYPE_NEQUAL)
9260 n1 = !n1;
9261 }
9262 }
9263
9264 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9265 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9266 {
9267 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9268 {
9269 EMSG(_("E694: Invalid operation for Funcrefs"));
9270 clear_tv(typ1);
9271 return FAIL;
9272 }
9273 if ((typ1->v_type == VAR_PARTIAL
9274 && typ1->vval.v_partial == NULL)
9275 || (typ2->v_type == VAR_PARTIAL
9276 && typ2->vval.v_partial == NULL))
9277 /* when a partial is NULL assume not equal */
9278 n1 = FALSE;
9279 else if (type_is)
9280 {
9281 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9282 /* strings are considered the same if their value is
9283 * the same */
9284 n1 = tv_equal(typ1, typ2, ic, FALSE);
9285 else if (typ1->v_type == VAR_PARTIAL
9286 && typ2->v_type == VAR_PARTIAL)
9287 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9288 else
9289 n1 = FALSE;
9290 }
9291 else
9292 n1 = tv_equal(typ1, typ2, ic, FALSE);
9293 if (type == TYPE_NEQUAL)
9294 n1 = !n1;
9295 }
9296
9297#ifdef FEAT_FLOAT
9298 /*
9299 * If one of the two variables is a float, compare as a float.
9300 * When using "=~" or "!~", always compare as string.
9301 */
9302 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9303 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9304 {
9305 float_T f1, f2;
9306
9307 if (typ1->v_type == VAR_FLOAT)
9308 f1 = typ1->vval.v_float;
9309 else
9310 f1 = get_tv_number(typ1);
9311 if (typ2->v_type == VAR_FLOAT)
9312 f2 = typ2->vval.v_float;
9313 else
9314 f2 = get_tv_number(typ2);
9315 n1 = FALSE;
9316 switch (type)
9317 {
9318 case TYPE_EQUAL: n1 = (f1 == f2); break;
9319 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9320 case TYPE_GREATER: n1 = (f1 > f2); break;
9321 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9322 case TYPE_SMALLER: n1 = (f1 < f2); break;
9323 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9324 case TYPE_UNKNOWN:
9325 case TYPE_MATCH:
9326 case TYPE_NOMATCH: break; /* avoid gcc warning */
9327 }
9328 }
9329#endif
9330
9331 /*
9332 * If one of the two variables is a number, compare as a number.
9333 * When using "=~" or "!~", always compare as string.
9334 */
9335 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9336 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9337 {
9338 n1 = get_tv_number(typ1);
9339 n2 = get_tv_number(typ2);
9340 switch (type)
9341 {
9342 case TYPE_EQUAL: n1 = (n1 == n2); break;
9343 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9344 case TYPE_GREATER: n1 = (n1 > n2); break;
9345 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9346 case TYPE_SMALLER: n1 = (n1 < n2); break;
9347 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9348 case TYPE_UNKNOWN:
9349 case TYPE_MATCH:
9350 case TYPE_NOMATCH: break; /* avoid gcc warning */
9351 }
9352 }
9353 else
9354 {
9355 s1 = get_tv_string_buf(typ1, buf1);
9356 s2 = get_tv_string_buf(typ2, buf2);
9357 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9358 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9359 else
9360 i = 0;
9361 n1 = FALSE;
9362 switch (type)
9363 {
9364 case TYPE_EQUAL: n1 = (i == 0); break;
9365 case TYPE_NEQUAL: n1 = (i != 0); break;
9366 case TYPE_GREATER: n1 = (i > 0); break;
9367 case TYPE_GEQUAL: n1 = (i >= 0); break;
9368 case TYPE_SMALLER: n1 = (i < 0); break;
9369 case TYPE_SEQUAL: n1 = (i <= 0); break;
9370
9371 case TYPE_MATCH:
9372 case TYPE_NOMATCH:
9373 n1 = pattern_match(s2, s1, ic);
9374 if (type == TYPE_NOMATCH)
9375 n1 = !n1;
9376 break;
9377
9378 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9379 }
9380 }
9381 clear_tv(typ1);
9382 typ1->v_type = VAR_NUMBER;
9383 typ1->vval.v_number = n1;
9384
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009385 return OK;
9386}
9387
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009388 char_u *
9389typval_tostring(arg)
9390 typval_T *arg;
9391{
9392 char_u *tofree;
9393 char_u numbuf[NUMBUFLEN];
9394 char_u *ret = NULL;
9395
9396 if (arg == NULL)
9397 return vim_strsave((char_u *)"(does not exist)");
9398 ret = tv2string(arg, &tofree, numbuf, 0);
9399 /* Make a copy if we have a value but it's not in allocated memory. */
9400 if (ret != NULL && tofree == NULL)
9401 ret = vim_strsave(ret);
9402 return ret;
9403}
9404
9405 int
9406var_exists(char_u *var)
9407{
9408 char_u *name;
9409 char_u *tofree;
9410 typval_T tv;
9411 int len = 0;
9412 int n = FALSE;
9413
9414 /* get_name_len() takes care of expanding curly braces */
9415 name = var;
9416 len = get_name_len(&var, &tofree, TRUE, FALSE);
9417 if (len > 0)
9418 {
9419 if (tofree != NULL)
9420 name = tofree;
9421 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9422 if (n)
9423 {
9424 /* handle d.key, l[idx], f(expr) */
9425 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9426 if (n)
9427 clear_tv(&tv);
9428 }
9429 }
9430 if (*var != NUL)
9431 n = FALSE;
9432
9433 vim_free(tofree);
9434 return n;
9435}
9436
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#endif /* FEAT_EVAL */
9438
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009440#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
9442#ifdef WIN3264
9443/*
9444 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9445 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009446static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9447static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9448static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449
9450/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009451 * Get the short path (8.3) for the filename in "fnamep".
9452 * Only works for a valid file name.
9453 * When the path gets longer "fnamep" is changed and the allocated buffer
9454 * is put in "bufp".
9455 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9456 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 */
9458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009459get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009461 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 char_u *newbuf;
9463
9464 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009465 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 if (l > len - 1)
9467 {
9468 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009469 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 newbuf = vim_strnsave(*fnamep, l);
9471 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 vim_free(*bufp);
9475 *fnamep = *bufp = newbuf;
9476
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009477 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009478 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 }
9480
9481 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009482 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
9485/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009486 * Get the short path (8.3) for the filename in "fname". The converted
9487 * path is returned in "bufp".
9488 *
9489 * Some of the directories specified in "fname" may not exist. This function
9490 * will shorten the existing directories at the beginning of the path and then
9491 * append the remaining non-existing path.
9492 *
9493 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009494 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009495 * bufp - Pointer to an allocated buffer for the filename.
9496 * fnamelen - Length of the filename pointed to by fname
9497 *
9498 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 */
9500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501shortpath_for_invalid_fname(
9502 char_u **fname,
9503 char_u **bufp,
9504 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009506 char_u *short_fname, *save_fname, *pbuf_unused;
9507 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009509 int old_len, len;
9510 int new_len, sfx_len;
9511 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
9513 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009514 old_len = *fnamelen;
9515 save_fname = vim_strnsave(*fname, old_len);
9516 pbuf_unused = NULL;
9517 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009519 endp = save_fname + old_len - 1; /* Find the end of the copy */
9520 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009522 /*
9523 * Try shortening the supplied path till it succeeds by removing one
9524 * directory at a time from the tail of the path.
9525 */
9526 len = 0;
9527 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009529 /* go back one path-separator */
9530 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9531 --endp;
9532 if (endp <= save_fname)
9533 break; /* processed the complete path */
9534
9535 /*
9536 * Replace the path separator with a NUL and try to shorten the
9537 * resulting path.
9538 */
9539 ch = *endp;
9540 *endp = 0;
9541 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009542 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009543 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9544 {
9545 retval = FAIL;
9546 goto theend;
9547 }
9548 *endp = ch; /* preserve the string */
9549
9550 if (len > 0)
9551 break; /* successfully shortened the path */
9552
9553 /* failed to shorten the path. Skip the path separator */
9554 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 }
9556
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009557 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009559 /*
9560 * Succeeded in shortening the path. Now concatenate the shortened
9561 * path with the remaining path at the tail.
9562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009564 /* Compute the length of the new path. */
9565 sfx_len = (int)(save_endp - endp) + 1;
9566 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009568 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009570 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009572 /* There is not enough space in the currently allocated string,
9573 * copy it to a buffer big enough. */
9574 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009576 {
9577 retval = FAIL;
9578 goto theend;
9579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 }
9581 else
9582 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009583 /* Transfer short_fname to the main buffer (it's big enough),
9584 * unless get_short_pathname() did its work in-place. */
9585 *fname = *bufp = save_fname;
9586 if (short_fname != save_fname)
9587 vim_strncpy(save_fname, short_fname, len);
9588 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009590
9591 /* concat the not-shortened part of the path */
9592 vim_strncpy(*fname + len, endp, sfx_len);
9593 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009595
9596theend:
9597 vim_free(pbuf_unused);
9598 vim_free(save_fname);
9599
9600 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009605 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 */
9607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608shortpath_for_partial(
9609 char_u **fnamep,
9610 char_u **bufp,
9611 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 int sepcount, len, tflen;
9614 char_u *p;
9615 char_u *pbuf, *tfname;
9616 int hasTilde;
9617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009618 /* Count up the path separators from the RHS.. so we know which part
9619 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009621 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 if (vim_ispathsep(*p))
9623 ++sepcount;
9624
9625 /* Need full path first (use expand_env() to remove a "~/") */
9626 hasTilde = (**fnamep == '~');
9627 if (hasTilde)
9628 pbuf = tfname = expand_env_save(*fnamep);
9629 else
9630 pbuf = tfname = FullName_save(*fnamep, FALSE);
9631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009632 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009634 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636
9637 if (len == 0)
9638 {
9639 /* Don't have a valid filename, so shorten the rest of the
9640 * path if we can. This CAN give us invalid 8.3 filenames, but
9641 * there's not a lot of point in guessing what it might be.
9642 */
9643 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009644 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 }
9647
9648 /* Count the paths backward to find the beginning of the desired string. */
9649 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009650 {
9651#ifdef FEAT_MBYTE
9652 if (has_mbyte)
9653 p -= mb_head_off(tfname, p);
9654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 if (vim_ispathsep(*p))
9656 {
9657 if (sepcount == 0 || (hasTilde && sepcount == 1))
9658 break;
9659 else
9660 sepcount --;
9661 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 if (hasTilde)
9664 {
9665 --p;
9666 if (p >= tfname)
9667 *p = '~';
9668 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 }
9671 else
9672 ++p;
9673
9674 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9675 vim_free(*bufp);
9676 *fnamelen = (int)STRLEN(p);
9677 *bufp = pbuf;
9678 *fnamep = p;
9679
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009680 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681}
9682#endif /* WIN3264 */
9683
9684/*
9685 * Adjust a filename, according to a string of modifiers.
9686 * *fnamep must be NUL terminated when called. When returning, the length is
9687 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009688 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 * When there is an error, *fnamep is set to NULL.
9690 */
9691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009692modify_fname(
9693 char_u *src, /* string with modifiers */
9694 int *usedlen, /* characters after src that are used */
9695 char_u **fnamep, /* file name so far */
9696 char_u **bufp, /* buffer for allocated file name or NULL */
9697 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 int valid = 0;
9700 char_u *tail;
9701 char_u *s, *p, *pbuf;
9702 char_u dirname[MAXPATHL];
9703 int c;
9704 int has_fullname = 0;
9705#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009706 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 int has_shortname = 0;
9708#endif
9709
9710repeat:
9711 /* ":p" - full path/file_name */
9712 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9713 {
9714 has_fullname = 1;
9715
9716 valid |= VALID_PATH;
9717 *usedlen += 2;
9718
9719 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9720 if ((*fnamep)[0] == '~'
9721#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9722 && ((*fnamep)[1] == '/'
9723# ifdef BACKSLASH_IN_FILENAME
9724 || (*fnamep)[1] == '\\'
9725# endif
9726 || (*fnamep)[1] == NUL)
9727
9728#endif
9729 )
9730 {
9731 *fnamep = expand_env_save(*fnamep);
9732 vim_free(*bufp); /* free any allocated file name */
9733 *bufp = *fnamep;
9734 if (*fnamep == NULL)
9735 return -1;
9736 }
9737
9738 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009739 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
9741 if (vim_ispathsep(*p)
9742 && p[1] == '.'
9743 && (p[2] == NUL
9744 || vim_ispathsep(p[2])
9745 || (p[2] == '.'
9746 && (p[3] == NUL || vim_ispathsep(p[3])))))
9747 break;
9748 }
9749
9750 /* FullName_save() is slow, don't use it when not needed. */
9751 if (*p != NUL || !vim_isAbsName(*fnamep))
9752 {
9753 *fnamep = FullName_save(*fnamep, *p != NUL);
9754 vim_free(*bufp); /* free any allocated file name */
9755 *bufp = *fnamep;
9756 if (*fnamep == NULL)
9757 return -1;
9758 }
9759
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009760#ifdef WIN3264
9761# if _WIN32_WINNT >= 0x0500
9762 if (vim_strchr(*fnamep, '~') != NULL)
9763 {
9764 /* Expand 8.3 filename to full path. Needed to make sure the same
9765 * file does not have two different names.
9766 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9767 p = alloc(_MAX_PATH + 1);
9768 if (p != NULL)
9769 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009770 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009771 {
9772 vim_free(*bufp);
9773 *bufp = *fnamep = p;
9774 }
9775 else
9776 vim_free(p);
9777 }
9778 }
9779# endif
9780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 /* Append a path separator to a directory. */
9782 if (mch_isdir(*fnamep))
9783 {
9784 /* Make room for one or two extra characters. */
9785 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9786 vim_free(*bufp); /* free any allocated file name */
9787 *bufp = *fnamep;
9788 if (*fnamep == NULL)
9789 return -1;
9790 add_pathsep(*fnamep);
9791 }
9792 }
9793
9794 /* ":." - path relative to the current directory */
9795 /* ":~" - path relative to the home directory */
9796 /* ":8" - shortname path - postponed till after */
9797 while (src[*usedlen] == ':'
9798 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9799 {
9800 *usedlen += 2;
9801 if (c == '8')
9802 {
9803#ifdef WIN3264
9804 has_shortname = 1; /* Postpone this. */
9805#endif
9806 continue;
9807 }
9808 pbuf = NULL;
9809 /* Need full path first (use expand_env() to remove a "~/") */
9810 if (!has_fullname)
9811 {
9812 if (c == '.' && **fnamep == '~')
9813 p = pbuf = expand_env_save(*fnamep);
9814 else
9815 p = pbuf = FullName_save(*fnamep, FALSE);
9816 }
9817 else
9818 p = *fnamep;
9819
9820 has_fullname = 0;
9821
9822 if (p != NULL)
9823 {
9824 if (c == '.')
9825 {
9826 mch_dirname(dirname, MAXPATHL);
9827 s = shorten_fname(p, dirname);
9828 if (s != NULL)
9829 {
9830 *fnamep = s;
9831 if (pbuf != NULL)
9832 {
9833 vim_free(*bufp); /* free any allocated file name */
9834 *bufp = pbuf;
9835 pbuf = NULL;
9836 }
9837 }
9838 }
9839 else
9840 {
9841 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9842 /* Only replace it when it starts with '~' */
9843 if (*dirname == '~')
9844 {
9845 s = vim_strsave(dirname);
9846 if (s != NULL)
9847 {
9848 *fnamep = s;
9849 vim_free(*bufp);
9850 *bufp = s;
9851 }
9852 }
9853 }
9854 vim_free(pbuf);
9855 }
9856 }
9857
9858 tail = gettail(*fnamep);
9859 *fnamelen = (int)STRLEN(*fnamep);
9860
9861 /* ":h" - head, remove "/file_name", can be repeated */
9862 /* Don't remove the first "/" or "c:\" */
9863 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9864 {
9865 valid |= VALID_HEAD;
9866 *usedlen += 2;
9867 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009868 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009869 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 *fnamelen = (int)(tail - *fnamep);
9871#ifdef VMS
9872 if (*fnamelen > 0)
9873 *fnamelen += 1; /* the path separator is part of the path */
9874#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009875 if (*fnamelen == 0)
9876 {
9877 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9878 p = vim_strsave((char_u *)".");
9879 if (p == NULL)
9880 return -1;
9881 vim_free(*bufp);
9882 *bufp = *fnamep = tail = p;
9883 *fnamelen = 1;
9884 }
9885 else
9886 {
9887 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009888 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 }
9891
9892 /* ":8" - shortname */
9893 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9894 {
9895 *usedlen += 2;
9896#ifdef WIN3264
9897 has_shortname = 1;
9898#endif
9899 }
9900
9901#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009902 /*
9903 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 */
9905 if (has_shortname)
9906 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009907 /* Copy the string if it is shortened by :h and when it wasn't copied
9908 * yet, because we are going to change it in place. Avoids changing
9909 * the buffer name for "%:8". */
9910 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 {
9912 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009913 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 return -1;
9915 vim_free(*bufp);
9916 *bufp = *fnamep = p;
9917 }
9918
9919 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009920 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (!has_fullname && !vim_isAbsName(*fnamep))
9922 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009923 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return -1;
9925 }
9926 else
9927 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009928 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaardc935552011-08-17 15:23:23 +02009930 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009932 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 return -1;
9934
9935 if (l == 0)
9936 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009937 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009939 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 return -1;
9941 }
9942 *fnamelen = l;
9943 }
9944 }
9945#endif /* WIN3264 */
9946
9947 /* ":t" - tail, just the basename */
9948 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9949 {
9950 *usedlen += 2;
9951 *fnamelen -= (int)(tail - *fnamep);
9952 *fnamep = tail;
9953 }
9954
9955 /* ":e" - extension, can be repeated */
9956 /* ":r" - root, without extension, can be repeated */
9957 while (src[*usedlen] == ':'
9958 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9959 {
9960 /* find a '.' in the tail:
9961 * - for second :e: before the current fname
9962 * - otherwise: The last '.'
9963 */
9964 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9965 s = *fnamep - 2;
9966 else
9967 s = *fnamep + *fnamelen - 1;
9968 for ( ; s > tail; --s)
9969 if (s[0] == '.')
9970 break;
9971 if (src[*usedlen + 1] == 'e') /* :e */
9972 {
9973 if (s > tail)
9974 {
9975 *fnamelen += (int)(*fnamep - (s + 1));
9976 *fnamep = s + 1;
9977#ifdef VMS
9978 /* cut version from the extension */
9979 s = *fnamep + *fnamelen - 1;
9980 for ( ; s > *fnamep; --s)
9981 if (s[0] == ';')
9982 break;
9983 if (s > *fnamep)
9984 *fnamelen = s - *fnamep;
9985#endif
9986 }
9987 else if (*fnamep <= tail)
9988 *fnamelen = 0;
9989 }
9990 else /* :r */
9991 {
9992 if (s > tail) /* remove one extension */
9993 *fnamelen = (int)(s - *fnamep);
9994 }
9995 *usedlen += 2;
9996 }
9997
9998 /* ":s?pat?foo?" - substitute */
9999 /* ":gs?pat?foo?" - global substitute */
10000 if (src[*usedlen] == ':'
10001 && (src[*usedlen + 1] == 's'
10002 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10003 {
10004 char_u *str;
10005 char_u *pat;
10006 char_u *sub;
10007 int sep;
10008 char_u *flags;
10009 int didit = FALSE;
10010
10011 flags = (char_u *)"";
10012 s = src + *usedlen + 2;
10013 if (src[*usedlen + 1] == 'g')
10014 {
10015 flags = (char_u *)"g";
10016 ++s;
10017 }
10018
10019 sep = *s++;
10020 if (sep)
10021 {
10022 /* find end of pattern */
10023 p = vim_strchr(s, sep);
10024 if (p != NULL)
10025 {
10026 pat = vim_strnsave(s, (int)(p - s));
10027 if (pat != NULL)
10028 {
10029 s = p + 1;
10030 /* find end of substitution */
10031 p = vim_strchr(s, sep);
10032 if (p != NULL)
10033 {
10034 sub = vim_strnsave(s, (int)(p - s));
10035 str = vim_strnsave(*fnamep, *fnamelen);
10036 if (sub != NULL && str != NULL)
10037 {
10038 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010039 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (s != NULL)
10041 {
10042 *fnamep = s;
10043 *fnamelen = (int)STRLEN(s);
10044 vim_free(*bufp);
10045 *bufp = s;
10046 didit = TRUE;
10047 }
10048 }
10049 vim_free(sub);
10050 vim_free(str);
10051 }
10052 vim_free(pat);
10053 }
10054 }
10055 /* after using ":s", repeat all the modifiers */
10056 if (didit)
10057 goto repeat;
10058 }
10059 }
10060
Bram Moolenaar26df0922014-02-23 23:39:13 +010010061 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10062 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010063 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010064 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010065 if (c != NUL)
10066 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010067 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010068 if (c != NUL)
10069 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010070 if (p == NULL)
10071 return -1;
10072 vim_free(*bufp);
10073 *bufp = *fnamep = p;
10074 *fnamelen = (int)STRLEN(p);
10075 *usedlen += 2;
10076 }
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 return valid;
10079}
10080
10081/*
10082 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010083 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * "flags" can be "g" to do a global substitute.
10085 * Returns an allocated string, NULL for error.
10086 */
10087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010088do_string_sub(
10089 char_u *str,
10090 char_u *pat,
10091 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010092 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010093 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
10095 int sublen;
10096 regmatch_T regmatch;
10097 int i;
10098 int do_all;
10099 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010100 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 garray_T ga;
10102 char_u *ret;
10103 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010104 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
10106 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10107 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010108 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109
10110 ga_init2(&ga, 1, 200);
10111
10112 do_all = (flags[0] == 'g');
10113
10114 regmatch.rm_ic = p_ic;
10115 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10116 if (regmatch.regprog != NULL)
10117 {
10118 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010119 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10121 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010122 /* Skip empty match except for first match. */
10123 if (regmatch.startp[0] == regmatch.endp[0])
10124 {
10125 if (zero_width == regmatch.startp[0])
10126 {
10127 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010128 i = MB_PTR2LEN(tail);
10129 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10130 (size_t)i);
10131 ga.ga_len += i;
10132 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010133 continue;
10134 }
10135 zero_width = regmatch.startp[0];
10136 }
10137
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 /*
10139 * Get some space for a temporary buffer to do the substitution
10140 * into. It will contain:
10141 * - The text up to where the match is.
10142 * - The substituted text.
10143 * - The text after the match.
10144 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010145 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010146 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10148 {
10149 ga_clear(&ga);
10150 break;
10151 }
10152
10153 /* copy the text up to where the match is */
10154 i = (int)(regmatch.startp[0] - tail);
10155 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10156 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010157 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 + ga.ga_len + i, TRUE, TRUE, FALSE);
10159 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010160 tail = regmatch.endp[0];
10161 if (*tail == NUL)
10162 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (!do_all)
10164 break;
10165 }
10166
10167 if (ga.ga_data != NULL)
10168 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10169
Bram Moolenaar473de612013-06-08 18:19:48 +020010170 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172
10173 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10174 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010175 if (p_cpo == empty_option)
10176 p_cpo = save_cpo;
10177 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010178 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010179 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
10181 return ret;
10182}
10183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184 static int
10185filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10186{
10187 typval_T rettv;
10188 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190
10191 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10192 argv[0] = vimvars[VV_KEY].vv_tv;
10193 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010194 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10195 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 if (map)
10197 {
10198 /* map(): replace the list item value */
10199 clear_tv(tv);
10200 rettv.v_lock = 0;
10201 *tv = rettv;
10202 }
10203 else
10204 {
10205 int error = FALSE;
10206
10207 /* filter(): when expr is zero remove the item */
10208 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10209 clear_tv(&rettv);
10210 /* On type error, nothing has been removed; return FAIL to stop the
10211 * loop. The error message was given by get_tv_number_chk(). */
10212 if (error)
10213 goto theend;
10214 }
10215 retval = OK;
10216theend:
10217 clear_tv(&vimvars[VV_VAL].vv_tv);
10218 return retval;
10219}
10220
10221
10222/*
10223 * Implementation of map() and filter().
10224 */
10225 void
10226filter_map(typval_T *argvars, typval_T *rettv, int map)
10227{
10228 typval_T *expr;
10229 listitem_T *li, *nli;
10230 list_T *l = NULL;
10231 dictitem_T *di;
10232 hashtab_T *ht;
10233 hashitem_T *hi;
10234 dict_T *d = NULL;
10235 typval_T save_val;
10236 typval_T save_key;
10237 int rem;
10238 int todo;
10239 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10240 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10241 : N_("filter() argument"));
10242 int save_did_emsg;
10243 int idx = 0;
10244
10245 if (argvars[0].v_type == VAR_LIST)
10246 {
10247 if ((l = argvars[0].vval.v_list) == NULL
10248 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10249 return;
10250 }
10251 else if (argvars[0].v_type == VAR_DICT)
10252 {
10253 if ((d = argvars[0].vval.v_dict) == NULL
10254 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10255 return;
10256 }
10257 else
10258 {
10259 EMSG2(_(e_listdictarg), ermsg);
10260 return;
10261 }
10262
10263 expr = &argvars[1];
10264 /* On type errors, the preceding call has already displayed an error
10265 * message. Avoid a misleading error message for an empty string that
10266 * was not passed as argument. */
10267 if (expr->v_type != VAR_UNKNOWN)
10268 {
10269 prepare_vimvar(VV_VAL, &save_val);
10270
10271 /* We reset "did_emsg" to be able to detect whether an error
10272 * occurred during evaluation of the expression. */
10273 save_did_emsg = did_emsg;
10274 did_emsg = FALSE;
10275
10276 prepare_vimvar(VV_KEY, &save_key);
10277 if (argvars[0].v_type == VAR_DICT)
10278 {
10279 vimvars[VV_KEY].vv_type = VAR_STRING;
10280
10281 ht = &d->dv_hashtab;
10282 hash_lock(ht);
10283 todo = (int)ht->ht_used;
10284 for (hi = ht->ht_array; todo > 0; ++hi)
10285 {
10286 if (!HASHITEM_EMPTY(hi))
10287 {
10288 int r;
10289
10290 --todo;
10291 di = HI2DI(hi);
10292 if (map &&
10293 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10294 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10295 break;
10296 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10297 r = filter_map_one(&di->di_tv, expr, map, &rem);
10298 clear_tv(&vimvars[VV_KEY].vv_tv);
10299 if (r == FAIL || did_emsg)
10300 break;
10301 if (!map && rem)
10302 {
10303 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10304 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10305 break;
10306 dictitem_remove(d, di);
10307 }
10308 }
10309 }
10310 hash_unlock(ht);
10311 }
10312 else
10313 {
10314 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10315
10316 for (li = l->lv_first; li != NULL; li = nli)
10317 {
10318 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10319 break;
10320 nli = li->li_next;
10321 vimvars[VV_KEY].vv_nr = idx;
10322 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10323 || did_emsg)
10324 break;
10325 if (!map && rem)
10326 listitem_remove(l, li);
10327 ++idx;
10328 }
10329 }
10330
10331 restore_vimvar(VV_KEY, &save_key);
10332 restore_vimvar(VV_VAL, &save_val);
10333
10334 did_emsg |= save_did_emsg;
10335 }
10336
10337 copy_tv(&argvars[0], rettv);
10338}
10339
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */