blob: b4250de171f1c51e4c7edaf91b1c9009df14164f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200190 {VV_NAME("termrgbresp", VAR_STRING), VV_RO},
191 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
192 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
193 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000194};
195
196/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000197#define vv_type vv_di.di_tv.v_type
198#define vv_nr vv_di.di_tv.vval.v_number
199#define vv_float vv_di.di_tv.vval.v_float
200#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000201#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200202#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000203#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000204
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200205static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000206#define vimvarht vimvardict.dv_hashtab
207
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100208static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
209static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
210static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_glob_vars(int *first);
212static void list_buf_vars(int *first);
213static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000214#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000216#endif
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 Moolenaar42a45122015-07-10 17:56:23 +0200323 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100324 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100325
326 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
327 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
328 set_vim_var_nr(VV_NONE, VVAL_NONE);
329 set_vim_var_nr(VV_NULL, VVAL_NULL);
330
Bram Moolenaarf562e722016-07-19 17:25:25 +0200331 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
332 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
333 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
334 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
335 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
336 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
337 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
338 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
339 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
340 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
341
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200342 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200343
344#ifdef EBCDIC
345 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100346 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200347 */
348 sortFunctions();
349#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000350}
351
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000352#if defined(EXITFREE) || defined(PROTO)
353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100354eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000355{
356 int i;
357 struct vimvar *p;
358
359 for (i = 0; i < VV_LEN; ++i)
360 {
361 p = &vimvars[i];
362 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000363 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000364 vim_free(p->vv_str);
365 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 }
367 else if (p->vv_di.di_tv.v_type == VAR_LIST)
368 {
369 list_unref(p->vv_list);
370 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000371 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000372 }
373 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000374 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000375 hash_clear(&compat_hashtab);
376
Bram Moolenaard9fba312005-06-26 22:34:35 +0000377 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100378# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200379 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100380# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000381
382 /* global variables */
383 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000384
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000385 /* autoloaded script names */
386 ga_clear_strings(&ga_loaded);
387
Bram Moolenaarcca74132013-09-25 21:00:28 +0200388 /* Script-local variables. First clear all the variables and in a second
389 * loop free the scriptvar_T, because a variable in one script might hold
390 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200393 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200395 ga_clear(&ga_scripts);
396
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000397 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200398 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000399
400 /* functions */
401 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000402}
403#endif
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405
406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 * Set an internal variable to a string value. Creates the variable if it does
408 * not already exist.
409 */
410 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100411set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000413 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000414 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416 val = vim_strsave(value);
417 if (val != NULL)
418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000419 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000420 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000423 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
426}
427
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000428static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200429#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000430static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000431static char_u *redir_endp = NULL;
432static char_u *redir_varname = NULL;
433
434/*
435 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100436 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000437 * Returns OK if successfully completed the setup. FAIL otherwise.
438 */
439 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100440var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000441{
442 int save_emsg;
443 int err;
444 typval_T tv;
445
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000446 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000447 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000448 {
449 EMSG(_(e_invarg));
450 return FAIL;
451 }
452
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000453 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000454 redir_varname = vim_strsave(name);
455 if (redir_varname == NULL)
456 return FAIL;
457
458 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
459 if (redir_lval == NULL)
460 {
461 var_redir_stop();
462 return FAIL;
463 }
464
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000465 /* The output is stored in growarray "redir_ga" until redirection ends. */
466 ga_init2(&redir_ga, (int)sizeof(char), 500);
467
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000468 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100469 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000470 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
472 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200473 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474 if (redir_endp != NULL && *redir_endp != NUL)
475 /* Trailing characters are present after the variable name */
476 EMSG(_(e_trailing));
477 else
478 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000479 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000480 var_redir_stop();
481 return FAIL;
482 }
483
484 /* check if we can write to the variable: set it to or append an empty
485 * string */
486 save_emsg = did_emsg;
487 did_emsg = FALSE;
488 tv.v_type = VAR_STRING;
489 tv.vval.v_string = (char_u *)"";
490 if (append)
491 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
492 else
493 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200494 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000496 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 if (err)
498 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000499 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500 var_redir_stop();
501 return FAIL;
502 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000503
504 return OK;
505}
506
507/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000508 * Append "value[value_len]" to the variable set by var_redir_start().
509 * The actual appending is postponed until redirection ends, because the value
510 * appended may in fact be the string we write to, changing it may cause freed
511 * memory to be used:
512 * :redir => foo
513 * :let foo
514 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 */
516 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100517var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000519 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520
521 if (redir_lval == NULL)
522 return;
523
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000524 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000526 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000530 {
531 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000532 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000533 }
534 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536}
537
538/*
539 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000540 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 */
542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100543var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000545 typval_T tv;
546
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200547 if (EVALCMD_BUSY)
548 {
549 redir_lval = NULL;
550 return;
551 }
552
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000553 if (redir_lval != NULL)
554 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000555 /* If there was no error: assign the text to the variable. */
556 if (redir_endp != NULL)
557 {
558 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
559 tv.v_type = VAR_STRING;
560 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200561 /* Call get_lval() again, if it's inside a Dict or List it may
562 * have changed. */
563 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100564 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200565 if (redir_endp != NULL && redir_lval->ll_name != NULL)
566 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
567 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000568 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000570 /* free the collected output */
571 vim_free(redir_ga.ga_data);
572 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000573
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000574 vim_free(redir_lval);
575 redir_lval = NULL;
576 }
577 vim_free(redir_varname);
578 redir_varname = NULL;
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# if defined(FEAT_MBYTE) || defined(PROTO)
582 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100583eval_charconvert(
584 char_u *enc_from,
585 char_u *enc_to,
586 char_u *fname_from,
587 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 int err = FALSE;
590
591 set_vim_var_string(VV_CC_FROM, enc_from, -1);
592 set_vim_var_string(VV_CC_TO, enc_to, -1);
593 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
594 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
595 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
596 err = TRUE;
597 set_vim_var_string(VV_CC_FROM, NULL, -1);
598 set_vim_var_string(VV_CC_TO, NULL, -1);
599 set_vim_var_string(VV_FNAME_IN, NULL, -1);
600 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
601
602 if (err)
603 return FAIL;
604 return OK;
605}
606# endif
607
608# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100610eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 int err = FALSE;
613
614 set_vim_var_string(VV_FNAME_IN, fname, -1);
615 set_vim_var_string(VV_CMDARG, args, -1);
616 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
617 err = TRUE;
618 set_vim_var_string(VV_FNAME_IN, NULL, -1);
619 set_vim_var_string(VV_CMDARG, NULL, -1);
620
621 if (err)
622 {
623 mch_remove(fname);
624 return FAIL;
625 }
626 return OK;
627}
628# endif
629
630# if defined(FEAT_DIFF) || defined(PROTO)
631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100632eval_diff(
633 char_u *origfile,
634 char_u *newfile,
635 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 int err = FALSE;
638
639 set_vim_var_string(VV_FNAME_IN, origfile, -1);
640 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
641 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
642 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
643 set_vim_var_string(VV_FNAME_IN, NULL, -1);
644 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
645 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
646}
647
648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649eval_patch(
650 char_u *origfile,
651 char_u *difffile,
652 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 int err;
655
656 set_vim_var_string(VV_FNAME_IN, origfile, -1);
657 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
658 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
659 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
660 set_vim_var_string(VV_FNAME_IN, NULL, -1);
661 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
662 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
663}
664# endif
665
666/*
667 * Top level evaluation function, returning a boolean.
668 * Sets "error" to TRUE if there was an error.
669 * Return TRUE or FALSE.
670 */
671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100672eval_to_bool(
673 char_u *arg,
674 int *error,
675 char_u **nextcmd,
676 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
Bram Moolenaar33570922005-01-25 22:26:29 +0000678 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200679 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 if (skip)
682 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000683 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 else
686 {
687 *error = FALSE;
688 if (!skip)
689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000690 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000691 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 }
694 if (skip)
695 --emsg_skip;
696
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200697 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
701 * Top level evaluation function, returning a string. If "skip" is TRUE,
702 * only parsing to "nextcmd" is done, without reporting errors. Return
703 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
704 */
705 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100706eval_to_string_skip(
707 char_u *arg,
708 char_u **nextcmd,
709 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
Bram Moolenaar33570922005-01-25 22:26:29 +0000711 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 char_u *retval;
713
714 if (skip)
715 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 retval = NULL;
718 else
719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000720 retval = vim_strsave(get_tv_string(&tv));
721 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723 if (skip)
724 --emsg_skip;
725
726 return retval;
727}
728
729/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000730 * Skip over an expression at "*pp".
731 * Return FAIL for an error, OK otherwise.
732 */
733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100734skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000735{
Bram Moolenaar33570922005-01-25 22:26:29 +0000736 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000737
738 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000740}
741
742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000744 * When "convert" is TRUE convert a List into a sequence of lines and convert
745 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * Return pointer to allocated memory, or NULL for failure.
747 */
748 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100749eval_to_string(
750 char_u *arg,
751 char_u **nextcmd,
752 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000756 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000757#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000758 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000761 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 retval = NULL;
763 else
764 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000765 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000766 {
767 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000768 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200769 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200770 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200771 if (tv.vval.v_list->lv_len > 0)
772 ga_append(&ga, NL);
773 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000774 ga_append(&ga, NUL);
775 retval = (char_u *)ga.ga_data;
776 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000777#ifdef FEAT_FLOAT
778 else if (convert && tv.v_type == VAR_FLOAT)
779 {
780 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
781 retval = vim_strsave(numbuf);
782 }
783#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000784 else
785 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000786 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788
789 return retval;
790}
791
792/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000793 * Call eval_to_string() without using current local variables and using
794 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 */
796 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797eval_to_string_safe(
798 char_u *arg,
799 char_u **nextcmd,
800 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 char_u *retval;
803 void *save_funccalp;
804
805 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000806 if (use_sandbox)
807 ++sandbox;
808 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000809 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000810 if (use_sandbox)
811 --sandbox;
812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 restore_funccal(save_funccalp);
814 return retval;
815}
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817/*
818 * Top level evaluation function, returning a number.
819 * Evaluates "expr" silently.
820 * Returns -1 for an error.
821 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200822 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200826 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000827 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 ++emsg_off;
830
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 retval = -1;
833 else
834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000835 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 --emsg_off;
839
840 return retval;
841}
842
Bram Moolenaara40058a2005-07-11 22:42:07 +0000843/*
844 * Prepare v: variable "idx" to be used.
845 * Save the current typeval in "save_tv".
846 * When not used yet add the variable to the v: hashtable.
847 */
848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100849prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000850{
851 *save_tv = vimvars[idx].vv_tv;
852 if (vimvars[idx].vv_type == VAR_UNKNOWN)
853 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
854}
855
856/*
857 * Restore v: variable "idx" to typeval "save_tv".
858 * When no longer defined, remove the variable from the v: hashtable.
859 */
860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100861restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000862{
863 hashitem_T *hi;
864
Bram Moolenaara40058a2005-07-11 22:42:07 +0000865 vimvars[idx].vv_tv = *save_tv;
866 if (vimvars[idx].vv_type == VAR_UNKNOWN)
867 {
868 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
869 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100870 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000871 else
872 hash_remove(&vimvarht, hi);
873 }
874}
875
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000876#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000877/*
878 * Evaluate an expression to a list with suggestions.
879 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000880 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000881 */
882 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100883eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000884{
885 typval_T save_val;
886 typval_T rettv;
887 list_T *list = NULL;
888 char_u *p = skipwhite(expr);
889
890 /* Set "v:val" to the bad word. */
891 prepare_vimvar(VV_VAL, &save_val);
892 vimvars[VV_VAL].vv_type = VAR_STRING;
893 vimvars[VV_VAL].vv_str = badword;
894 if (p_verbose == 0)
895 ++emsg_off;
896
897 if (eval1(&p, &rettv, TRUE) == OK)
898 {
899 if (rettv.v_type != VAR_LIST)
900 clear_tv(&rettv);
901 else
902 list = rettv.vval.v_list;
903 }
904
905 if (p_verbose == 0)
906 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000907 restore_vimvar(VV_VAL, &save_val);
908
909 return list;
910}
911
912/*
913 * "list" is supposed to contain two items: a word and a number. Return the
914 * word in "pp" and the number as the return value.
915 * Return -1 if anything isn't right.
916 * Used to get the good word and score from the eval_spell_expr() result.
917 */
918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100919get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000920{
921 listitem_T *li;
922
923 li = list->lv_first;
924 if (li == NULL)
925 return -1;
926 *pp = get_tv_string(&li->li_tv);
927
928 li = li->li_next;
929 if (li == NULL)
930 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200931 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000932}
933#endif
934
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000935/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000936 * Top level evaluation function.
937 * Returns an allocated typval_T with the result.
938 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000939 */
940 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000942{
943 typval_T *tv;
944
945 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000946 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000947 {
948 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000949 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000950 }
951
952 return tv;
953}
954
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100957 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000958 * Uses argv[argc] for the function arguments. Only Number and String
959 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000960 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963call_vim_function(
964 char_u *func,
965 int argc,
966 char_u **argv,
967 int safe, /* use the sandbox */
968 int str_arg_only, /* all arguments are strings */
969 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaar33570922005-01-25 22:26:29 +0000971 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200972 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 int len;
974 int i;
975 int doesrange;
976 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000977 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000979 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000981 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 for (i = 0; i < argc; i++)
984 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000985 /* Pass a NULL or empty argument as an empty string */
986 if (argv[i] == NULL || *argv[i] == NUL)
987 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000988 argvars[i].v_type = VAR_STRING;
989 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000990 continue;
991 }
992
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200993 if (str_arg_only)
994 len = 0;
995 else
996 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100997 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (len != 0 && len == (int)STRLEN(argv[i]))
999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001000 argvars[i].v_type = VAR_NUMBER;
1001 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003 else
1004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 argvars[i].v_type = VAR_STRING;
1006 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 }
1009
1010 if (safe)
1011 {
1012 save_funccalp = save_funccal();
1013 ++sandbox;
1014 }
1015
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001016 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001017 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001019 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (safe)
1021 {
1022 --sandbox;
1023 restore_funccal(save_funccalp);
1024 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001025 vim_free(argvars);
1026
1027 if (ret == FAIL)
1028 clear_tv(rettv);
1029
1030 return ret;
1031}
1032
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001034 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001035 * Returns -1 when calling the function fails.
1036 * Uses argv[argc] for the function arguments.
1037 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001038 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001039call_func_retnr(
1040 char_u *func,
1041 int argc,
1042 char_u **argv,
1043 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001044{
1045 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001046 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001047
1048 /* All arguments are passed as strings, no conversion to number. */
1049 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1050 return -1;
1051
1052 retval = get_tv_number_chk(&rettv, NULL);
1053 clear_tv(&rettv);
1054 return retval;
1055}
1056
1057#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1058 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1059
Bram Moolenaar4f688582007-07-24 12:34:30 +00001060# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001061/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001062 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001063 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001064 * Uses argv[argc] for the function arguments.
1065 */
1066 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067call_func_retstr(
1068 char_u *func,
1069 int argc,
1070 char_u **argv,
1071 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001072{
1073 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001074 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001075
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001076 /* All arguments are passed as strings, no conversion to number. */
1077 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001078 return NULL;
1079
1080 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 return retval;
1083}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001084# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001085
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001086/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001087 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001089 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001090 */
1091 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001092call_func_retlist(
1093 char_u *func,
1094 int argc,
1095 char_u **argv,
1096 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001097{
1098 typval_T rettv;
1099
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001100 /* All arguments are passed as strings, no conversion to number. */
1101 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001102 return NULL;
1103
1104 if (rettv.v_type != VAR_LIST)
1105 {
1106 clear_tv(&rettv);
1107 return NULL;
1108 }
1109
1110 return rettv.vval.v_list;
1111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#endif
1113
Bram Moolenaar05159a02005-02-26 23:04:13 +00001114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#ifdef FEAT_FOLDING
1116/*
1117 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1118 * it in "*cp". Doesn't give error messages.
1119 */
1120 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001121eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
Bram Moolenaar33570922005-01-25 22:26:29 +00001123 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001124 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001126 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1127 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001130 if (use_sandbox)
1131 ++sandbox;
1132 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001134 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 retval = 0;
1136 else
1137 {
1138 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 if (tv.v_type == VAR_NUMBER)
1140 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001141 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 retval = 0;
1143 else
1144 {
1145 /* If the result is a string, check if there is a non-digit before
1146 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (!VIM_ISDIGIT(*s) && *s != '-')
1149 *cp = *s++;
1150 retval = atol((char *)s);
1151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001155 if (use_sandbox)
1156 --sandbox;
1157 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001159 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160}
1161#endif
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 * ":let" list all variable values
1165 * ":let var1 var2" list variable values
1166 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001167 * ":let var += expr" assignment command.
1168 * ":let var -= expr" assignment command.
1169 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 */
1172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001173ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001176 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001177 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 int var_count = 0;
1180 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001181 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001182 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001183 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaardb552d602006-03-23 22:59:57 +00001185 argend = skip_var_list(arg, &var_count, &semicolon);
1186 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001187 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001188 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1189 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001190 expr = skipwhite(argend);
1191 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1192 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001194 /*
1195 * ":let" without "=": list variables
1196 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001197 if (*arg == '[')
1198 EMSG(_(e_invarg));
1199 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001201 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001205 list_glob_vars(&first);
1206 list_buf_vars(&first);
1207 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001208#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001209 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001210#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001211 list_script_vars(&first);
1212 list_func_vars(&first);
1213 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 eap->nextcmd = check_nextcmd(arg);
1216 }
1217 else
1218 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001219 op[0] = '=';
1220 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001221 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001223 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1224 op[0] = *expr; /* +=, -= or .= */
1225 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001226 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001227 else
1228 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (eap->skip)
1231 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001232 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (eap->skip)
1234 {
1235 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001236 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 --emsg_skip;
1238 }
1239 else if (i != FAIL)
1240 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001241 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001242 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245 }
1246}
1247
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001248/*
1249 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1250 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251 * When "nextchars" is not NULL it points to a string with characters that
1252 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1253 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001254 * Returns OK or FAIL;
1255 */
1256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001257ex_let_vars(
1258 char_u *arg_start,
1259 typval_T *tv,
1260 int copy, /* copy values from "tv", don't move */
1261 int semicolon, /* from skip_var_list() */
1262 int var_count, /* from skip_var_list() */
1263 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001264{
1265 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001267 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 listitem_T *item;
1269 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001270
1271 if (*arg != '[')
1272 {
1273 /*
1274 * ":let var = expr" or ":for var in list"
1275 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001277 return FAIL;
1278 return OK;
1279 }
1280
1281 /*
1282 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1283 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001284 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001285 {
1286 EMSG(_(e_listreq));
1287 return FAIL;
1288 }
1289
1290 i = list_len(l);
1291 if (semicolon == 0 && var_count < i)
1292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001293 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001294 return FAIL;
1295 }
1296 if (var_count - semicolon > i)
1297 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001298 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 return FAIL;
1300 }
1301
1302 item = l->lv_first;
1303 while (*arg != ']')
1304 {
1305 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001306 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307 item = item->li_next;
1308 if (arg == NULL)
1309 return FAIL;
1310
1311 arg = skipwhite(arg);
1312 if (*arg == ';')
1313 {
1314 /* Put the rest of the list (may be empty) in the var after ';'.
1315 * Create a new list for this. */
1316 l = list_alloc();
1317 if (l == NULL)
1318 return FAIL;
1319 while (item != NULL)
1320 {
1321 list_append_tv(l, &item->li_tv);
1322 item = item->li_next;
1323 }
1324
1325 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001326 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001327 ltv.vval.v_list = l;
1328 l->lv_refcount = 1;
1329
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1331 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332 clear_tv(&ltv);
1333 if (arg == NULL)
1334 return FAIL;
1335 break;
1336 }
1337 else if (*arg != ',' && *arg != ']')
1338 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001339 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001340 return FAIL;
1341 }
1342 }
1343
1344 return OK;
1345}
1346
1347/*
1348 * Skip over assignable variable "var" or list of variables "[var, var]".
1349 * Used for ":let varvar = expr" and ":for varvar in expr".
1350 * For "[var, var]" increment "*var_count" for each variable.
1351 * for "[var, var; var]" set "semicolon".
1352 * Return NULL for an error.
1353 */
1354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355skip_var_list(
1356 char_u *arg,
1357 int *var_count,
1358 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001359{
1360 char_u *p, *s;
1361
1362 if (*arg == '[')
1363 {
1364 /* "[var, var]": find the matching ']'. */
1365 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001367 {
1368 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1369 s = skip_var_one(p);
1370 if (s == p)
1371 {
1372 EMSG2(_(e_invarg2), p);
1373 return NULL;
1374 }
1375 ++*var_count;
1376
1377 p = skipwhite(s);
1378 if (*p == ']')
1379 break;
1380 else if (*p == ';')
1381 {
1382 if (*semicolon == 1)
1383 {
1384 EMSG(_("Double ; in list of variables"));
1385 return NULL;
1386 }
1387 *semicolon = 1;
1388 }
1389 else if (*p != ',')
1390 {
1391 EMSG2(_(e_invarg2), p);
1392 return NULL;
1393 }
1394 }
1395 return p + 1;
1396 }
1397 else
1398 return skip_var_one(arg);
1399}
1400
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001401/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001402 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001403 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001404 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001407{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001408 if (*arg == '@' && arg[1] != NUL)
1409 return arg + 2;
1410 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1411 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412}
1413
Bram Moolenaara7043832005-01-21 11:56:39 +00001414/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 * List variables for hashtab "ht" with prefix "prefix".
1416 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001417 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001419list_hashtable_vars(
1420 hashtab_T *ht,
1421 char_u *prefix,
1422 int empty,
1423 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001424{
Bram Moolenaar33570922005-01-25 22:26:29 +00001425 hashitem_T *hi;
1426 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001427 int todo;
1428
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001429 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001430 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1431 {
1432 if (!HASHITEM_EMPTY(hi))
1433 {
1434 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001435 di = HI2DI(hi);
1436 if (empty || di->di_tv.v_type != VAR_STRING
1437 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001438 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001439 }
1440 }
1441}
1442
1443/*
1444 * List global variables.
1445 */
1446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001448{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001449 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001450}
1451
1452/*
1453 * List buffer variables.
1454 */
1455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001457{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001458 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001459 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001460}
1461
1462/*
1463 * List window variables.
1464 */
1465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001466list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001467{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001468 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001469 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001470}
1471
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001472#ifdef FEAT_WINDOWS
1473/*
1474 * List tab page variables.
1475 */
1476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001478{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001479 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001480 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001481}
1482#endif
1483
Bram Moolenaara7043832005-01-21 11:56:39 +00001484/*
1485 * List Vim variables.
1486 */
1487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001488list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001490 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491}
1492
1493/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001494 * List script-local variables, if there is a script.
1495 */
1496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001497list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001498{
1499 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001500 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1501 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001502}
1503
1504/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001505 * List variables in "arg".
1506 */
1507 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509{
1510 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001511 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001513 char_u *name_start;
1514 char_u *arg_subsc;
1515 char_u *tofree;
1516 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517
1518 while (!ends_excmd(*arg) && !got_int)
1519 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001520 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001522 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001523 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524 {
1525 emsg_severe = TRUE;
1526 EMSG(_(e_trailing));
1527 break;
1528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001529 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001530 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001532 /* get_name_len() takes care of expanding curly braces */
1533 name_start = name = arg;
1534 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1535 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001537 /* This is mainly to keep test 49 working: when expanding
1538 * curly braces fails overrule the exception error message. */
1539 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 emsg_severe = TRUE;
1542 EMSG2(_(e_invarg2), arg);
1543 break;
1544 }
1545 error = TRUE;
1546 }
1547 else
1548 {
1549 if (tofree != NULL)
1550 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001551 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001552 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 else
1554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 /* handle d.key, l[idx], f(expr) */
1556 arg_subsc = arg;
1557 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001559 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001561 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001564 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001565 case 'g': list_glob_vars(first); break;
1566 case 'b': list_buf_vars(first); break;
1567 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001568#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001569 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001570#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001571 case 'v': list_vim_vars(first); break;
1572 case 's': list_script_vars(first); break;
1573 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001574 default:
1575 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001576 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 }
1578 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 {
1580 char_u numbuf[NUMBUFLEN];
1581 char_u *tf;
1582 int c;
1583 char_u *s;
1584
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001585 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001586 c = *arg;
1587 *arg = NUL;
1588 list_one_var_a((char_u *)"",
1589 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001590 tv.v_type,
1591 s == NULL ? (char_u *)"" : s,
1592 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001593 *arg = c;
1594 vim_free(tf);
1595 }
1596 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 }
1599 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600
1601 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001603
1604 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 }
1606
1607 return arg;
1608}
1609
1610/*
1611 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1612 * Returns a pointer to the char just after the var name.
1613 * Returns NULL if there is an error.
1614 */
1615 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616ex_let_one(
1617 char_u *arg, /* points to variable name */
1618 typval_T *tv, /* value to assign to variable */
1619 int copy, /* copy value from "tv" */
1620 char_u *endchars, /* valid chars after variable name or NULL */
1621 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622{
1623 int c1;
1624 char_u *name;
1625 char_u *p;
1626 char_u *arg_end = NULL;
1627 int len;
1628 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630
1631 /*
1632 * ":let $VAR = expr": Set environment variable.
1633 */
1634 if (*arg == '$')
1635 {
1636 /* Find the end of the name. */
1637 ++arg;
1638 name = arg;
1639 len = get_env_len(&arg);
1640 if (len == 0)
1641 EMSG2(_(e_invarg2), name - 1);
1642 else
1643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 if (op != NULL && (*op == '+' || *op == '-'))
1645 EMSG2(_(e_letwrong), op);
1646 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001649 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 {
1651 c1 = name[len];
1652 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001653 p = get_tv_string_chk(tv);
1654 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 {
1656 int mustfree = FALSE;
1657 char_u *s = vim_getenv(name, &mustfree);
1658
1659 if (s != NULL)
1660 {
1661 p = tofree = concat_str(s, p);
1662 if (mustfree)
1663 vim_free(s);
1664 }
1665 }
1666 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001667 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001669 if (STRICMP(name, "HOME") == 0)
1670 init_homedir();
1671 else if (didset_vim && STRICMP(name, "VIM") == 0)
1672 didset_vim = FALSE;
1673 else if (didset_vimruntime
1674 && STRICMP(name, "VIMRUNTIME") == 0)
1675 didset_vimruntime = FALSE;
1676 arg_end = arg;
1677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 }
1681 }
1682 }
1683
1684 /*
1685 * ":let &option = expr": Set option value.
1686 * ":let &l:option = expr": Set local option value.
1687 * ":let &g:option = expr": Set global option value.
1688 */
1689 else if (*arg == '&')
1690 {
1691 /* Find the end of the name. */
1692 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 if (p == NULL || (endchars != NULL
1694 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 EMSG(_(e_letunexp));
1696 else
1697 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 long n;
1699 int opt_type;
1700 long numval;
1701 char_u *stringval = NULL;
1702 char_u *s;
1703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 c1 = *p;
1705 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001707 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001708 s = get_tv_string_chk(tv); /* != NULL if number or string */
1709 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 {
1711 opt_type = get_option_value(arg, &numval,
1712 &stringval, opt_flags);
1713 if ((opt_type == 1 && *op == '.')
1714 || (opt_type == 0 && *op != '.'))
1715 EMSG2(_(e_letwrong), op);
1716 else
1717 {
1718 if (opt_type == 1) /* number */
1719 {
1720 if (*op == '+')
1721 n = numval + n;
1722 else
1723 n = numval - n;
1724 }
1725 else if (opt_type == 0 && stringval != NULL) /* string */
1726 {
1727 s = concat_str(stringval, s);
1728 vim_free(stringval);
1729 stringval = s;
1730 }
1731 }
1732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733 if (s != NULL)
1734 {
1735 set_option_value(arg, n, s, opt_flags);
1736 arg_end = p;
1737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 }
1741 }
1742
1743 /*
1744 * ":let @r = expr": Set register contents.
1745 */
1746 else if (*arg == '@')
1747 {
1748 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 if (op != NULL && (*op == '+' || *op == '-'))
1750 EMSG2(_(e_letwrong), op);
1751 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 EMSG(_(e_letunexp));
1754 else
1755 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 char_u *s;
1758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001759 p = get_tv_string_chk(tv);
1760 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001762 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 if (s != NULL)
1764 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001765 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 vim_free(s);
1767 }
1768 }
1769 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772 arg_end = arg + 1;
1773 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001774 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 }
1776 }
1777
1778 /*
1779 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001780 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001782 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001786 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001787 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001789 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1790 EMSG(_(e_letunexp));
1791 else
1792 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001794 arg_end = p;
1795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001797 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 }
1799
1800 else
1801 EMSG2(_(e_invarg2), arg);
1802
1803 return arg_end;
1804}
1805
1806/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001807 * Get an lval: variable, Dict item or List item that can be assigned a value
1808 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1809 * "name.key", "name.key[expr]" etc.
1810 * Indexing only works if "name" is an existing List or Dictionary.
1811 * "name" points to the start of the name.
1812 * If "rettv" is not NULL it points to the value to be assigned.
1813 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1814 * wrong; must end in space or cmd separator.
1815 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001816 * flags:
1817 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001818 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001819 * GLV_NO_AUTOLOAD: do not use script autoloading
1820 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001821 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001822 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001823 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826get_lval(
1827 char_u *name,
1828 typval_T *rettv,
1829 lval_T *lp,
1830 int unlet,
1831 int skip,
1832 int flags, /* GLV_ values */
1833 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001836 char_u *expr_start, *expr_end;
1837 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 dictitem_T *v;
1839 typval_T var1;
1840 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001843 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001844 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001846 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001850
1851 if (skip)
1852 {
1853 /* When skipping just find the end of the name. */
1854 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001855 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 }
1857
1858 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001859 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 if (expr_start != NULL)
1861 {
1862 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001863 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 && *p != '[' && *p != '.')
1865 {
1866 EMSG(_(e_trailing));
1867 return NULL;
1868 }
1869
1870 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1871 if (lp->ll_exp_name == NULL)
1872 {
1873 /* Report an invalid expression in braces, unless the
1874 * expression evaluation has been cancelled due to an
1875 * aborting error, an interrupt, or an exception. */
1876 if (!aborting() && !quiet)
1877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001878 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001879 EMSG2(_(e_invarg2), name);
1880 return NULL;
1881 }
1882 }
1883 lp->ll_name = lp->ll_exp_name;
1884 }
1885 else
1886 lp->ll_name = name;
1887
1888 /* Without [idx] or .key we are done. */
1889 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1890 return p;
1891
1892 cc = *p;
1893 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001894 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 if (v == NULL && !quiet)
1896 EMSG2(_(e_undefvar), lp->ll_name);
1897 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 if (v == NULL)
1899 return NULL;
1900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 /*
1902 * Loop until no more [idx] or .key is following.
1903 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001905 var1.v_type = VAR_UNKNOWN;
1906 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1910 && !(lp->ll_tv->v_type == VAR_DICT
1911 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 if (!quiet)
1914 EMSG(_("E689: Can only index a List or Dictionary"));
1915 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001919 if (!quiet)
1920 EMSG(_("E708: [:] must come last"));
1921 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001923
Bram Moolenaar8c711452005-01-14 21:53:12 +00001924 len = -1;
1925 if (*p == '.')
1926 {
1927 key = p + 1;
1928 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1929 ;
1930 if (len == 0)
1931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 if (!quiet)
1933 EMSG(_(e_emptykey));
1934 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 }
1936 p = key + len;
1937 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001938 else
1939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001940 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001941 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001942 if (*p == ':')
1943 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001944 else
1945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 empty1 = FALSE;
1947 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001949 if (get_tv_string_chk(&var1) == NULL)
1950 {
1951 /* not a number or string */
1952 clear_tv(&var1);
1953 return NULL;
1954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001955 }
1956
1957 /* Optionally get the second index [ :expr]. */
1958 if (*p == ':')
1959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001964 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001966 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 if (rettv != NULL && (rettv->v_type != VAR_LIST
1968 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 if (!quiet)
1971 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001972 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 }
1975 p = skipwhite(p + 1);
1976 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 else
1979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1982 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001983 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001985 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 if (get_tv_string_chk(&var2) == NULL)
1987 {
1988 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001989 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001990 clear_tv(&var2);
1991 return NULL;
1992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001998
Bram Moolenaar8c711452005-01-14 21:53:12 +00001999 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 if (!quiet)
2002 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002003 clear_tv(&var1);
2004 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 }
2007
2008 /* Skip to past ']'. */
2009 ++p;
2010 }
2011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 {
2014 if (len == -1)
2015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002017 key = get_tv_string_chk(&var1); /* is number or string */
2018 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 }
2023 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 lp->ll_list = NULL;
2025 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002026 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002027
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002028 /* When assigning to a scope dictionary check that a function and
2029 * variable name is valid (only variable name unless it is l: or
2030 * g: dictionary). Disallow overwriting a builtin function. */
2031 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002032 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002033 int prevval;
2034 int wrong;
2035
2036 if (len != -1)
2037 {
2038 prevval = key[len];
2039 key[len] = NUL;
2040 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002041 else
2042 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002043 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2044 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002045 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002046 || !valid_varname(key);
2047 if (len != -1)
2048 key[len] = prevval;
2049 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002050 return NULL;
2051 }
2052
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002055 /* Can't add "v:" variable. */
2056 if (lp->ll_dict == &vimvardict)
2057 {
2058 EMSG2(_(e_illvar), name);
2059 return NULL;
2060 }
2061
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002062 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002067 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002069 }
2070 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002074 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002076 p = NULL;
2077 break;
2078 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002079 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002080 else if ((flags & GLV_READ_ONLY) == 0
2081 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002082 {
2083 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002084 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002085 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002086
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002087 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 }
2090 else
2091 {
2092 /*
2093 * Get the number and item for the only or first index of the List.
2094 */
2095 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002097 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002098 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002099 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002100 clear_tv(&var1);
2101
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002103 lp->ll_list = lp->ll_tv->vval.v_list;
2104 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2105 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002107 if (lp->ll_n1 < 0)
2108 {
2109 lp->ll_n1 = 0;
2110 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2111 }
2112 }
2113 if (lp->ll_li == NULL)
2114 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002115 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002116 if (!quiet)
2117 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 }
2120
2121 /*
2122 * May need to find the item or absolute index for the second
2123 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 * When no index given: "lp->ll_empty2" is TRUE.
2125 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002129 lp->ll_n2 = (long)get_tv_number(&var2);
2130 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002135 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 {
2137 if (!quiet)
2138 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002140 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002142 }
2143
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2145 if (lp->ll_n1 < 0)
2146 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2147 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002148 {
2149 if (!quiet)
2150 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002153 }
2154
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 }
2158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002159 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 return p;
2161}
2162
2163/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002164 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168{
2169 vim_free(lp->ll_exp_name);
2170 vim_free(lp->ll_newkey);
2171}
2172
2173/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002174 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 */
2178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002179set_var_lval(
2180 lval_T *lp,
2181 char_u *endp,
2182 typval_T *rettv,
2183 int copy,
2184 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185{
2186 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002187 listitem_T *ri;
2188 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189
2190 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002192 cc = *endp;
2193 *endp = NUL;
2194 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002196 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197
Bram Moolenaar79518e22017-02-17 16:31:35 +01002198 /* handle +=, -= and .= */
2199 di = NULL;
2200 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2201 &tv, &di, TRUE, FALSE) == OK)
2202 {
2203 if ((di == NULL
2204 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2205 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2206 FALSE)))
2207 && tv_op(&tv, rettv, op) == OK)
2208 set_var(lp->ll_name, &tv, FALSE);
2209 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002212 else
2213 set_var(lp->ll_name, rettv, copy);
2214 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002216 else if (tv_check_lock(lp->ll_newkey == NULL
2217 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002218 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002219 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 else if (lp->ll_range)
2221 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002222 listitem_T *ll_li = lp->ll_li;
2223 int ll_n1 = lp->ll_n1;
2224
2225 /*
2226 * Check whether any of the list items is locked
2227 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002228 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002229 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002230 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002231 return;
2232 ri = ri->li_next;
2233 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2234 break;
2235 ll_li = ll_li->li_next;
2236 ++ll_n1;
2237 }
2238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 /*
2240 * Assign the List values to the list items.
2241 */
2242 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 if (op != NULL && *op != '=')
2245 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2246 else
2247 {
2248 clear_tv(&lp->ll_li->li_tv);
2249 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 ri = ri->li_next;
2252 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2253 break;
2254 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002257 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 ri = NULL;
2260 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002262 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 lp->ll_li = lp->ll_li->li_next;
2264 ++lp->ll_n1;
2265 }
2266 if (ri != NULL)
2267 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 else if (lp->ll_empty2
2269 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 : lp->ll_n1 != lp->ll_n2)
2271 EMSG(_("E711: List value has not enough items"));
2272 }
2273 else
2274 {
2275 /*
2276 * Assign to a List or Dictionary item.
2277 */
2278 if (lp->ll_newkey != NULL)
2279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 if (op != NULL && *op != '=')
2281 {
2282 EMSG2(_(e_letwrong), op);
2283 return;
2284 }
2285
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002287 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (di == NULL)
2289 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002290 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2291 {
2292 vim_free(di);
2293 return;
2294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297 else if (op != NULL && *op != '=')
2298 {
2299 tv_op(lp->ll_tv, rettv, op);
2300 return;
2301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 /*
2306 * Assign the value to the variable or list item.
2307 */
2308 if (copy)
2309 copy_tv(rettv, lp->ll_tv);
2310 else
2311 {
2312 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002313 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317}
2318
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2321 * Returns OK or FAIL.
2322 */
2323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002324tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002326 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 char_u numbuf[NUMBUFLEN];
2328 char_u *s;
2329
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002330 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2331 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2332 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 {
2334 switch (tv1->v_type)
2335 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002336 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 case VAR_DICT:
2338 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002339 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002340 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002341 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002342 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 break;
2344
2345 case VAR_LIST:
2346 if (*op != '+' || tv2->v_type != VAR_LIST)
2347 break;
2348 /* List += List */
2349 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2350 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2351 return OK;
2352
2353 case VAR_NUMBER:
2354 case VAR_STRING:
2355 if (tv2->v_type == VAR_LIST)
2356 break;
2357 if (*op == '+' || *op == '-')
2358 {
2359 /* nr += nr or nr -= nr*/
2360 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002361#ifdef FEAT_FLOAT
2362 if (tv2->v_type == VAR_FLOAT)
2363 {
2364 float_T f = n;
2365
2366 if (*op == '+')
2367 f += tv2->vval.v_float;
2368 else
2369 f -= tv2->vval.v_float;
2370 clear_tv(tv1);
2371 tv1->v_type = VAR_FLOAT;
2372 tv1->vval.v_float = f;
2373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002375#endif
2376 {
2377 if (*op == '+')
2378 n += get_tv_number(tv2);
2379 else
2380 n -= get_tv_number(tv2);
2381 clear_tv(tv1);
2382 tv1->v_type = VAR_NUMBER;
2383 tv1->vval.v_number = n;
2384 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 }
2386 else
2387 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002388 if (tv2->v_type == VAR_FLOAT)
2389 break;
2390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 /* str .= str */
2392 s = get_tv_string(tv1);
2393 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2394 clear_tv(tv1);
2395 tv1->v_type = VAR_STRING;
2396 tv1->vval.v_string = s;
2397 }
2398 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002400 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002401#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402 {
2403 float_T f;
2404
2405 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2406 && tv2->v_type != VAR_NUMBER
2407 && tv2->v_type != VAR_STRING))
2408 break;
2409 if (tv2->v_type == VAR_FLOAT)
2410 f = tv2->vval.v_float;
2411 else
2412 f = get_tv_number(tv2);
2413 if (*op == '+')
2414 tv1->vval.v_float += f;
2415 else
2416 tv1->vval.v_float -= f;
2417 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002418#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002419 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 }
2421 }
2422
2423 EMSG2(_(e_letwrong), op);
2424 return FAIL;
2425}
2426
2427/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428 * Evaluate the expression used in a ":for var in expr" command.
2429 * "arg" points to "var".
2430 * Set "*errp" to TRUE for an error, FALSE otherwise;
2431 * Return a pointer that holds the info. Null when there is an error.
2432 */
2433 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434eval_for_line(
2435 char_u *arg,
2436 int *errp,
2437 char_u **nextcmdp,
2438 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439{
Bram Moolenaar33570922005-01-25 22:26:29 +00002440 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002442 typval_T tv;
2443 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444
2445 *errp = TRUE; /* default: there is an error */
2446
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002448 if (fi == NULL)
2449 return NULL;
2450
2451 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2452 if (expr == NULL)
2453 return fi;
2454
2455 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002456 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002457 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002458 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459 return fi;
2460 }
2461
2462 if (skip)
2463 ++emsg_skip;
2464 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2465 {
2466 *errp = FALSE;
2467 if (!skip)
2468 {
2469 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002470 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002473 clear_tv(&tv);
2474 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002475 else if (l == NULL)
2476 {
2477 /* a null list is like an empty list: do nothing */
2478 clear_tv(&tv);
2479 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 else
2481 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002482 /* No need to increment the refcount, it's already set for the
2483 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002484 fi->fi_list = l;
2485 list_add_watch(l, &fi->fi_lw);
2486 fi->fi_lw.lw_item = l->lv_first;
2487 }
2488 }
2489 }
2490 if (skip)
2491 --emsg_skip;
2492
2493 return fi;
2494}
2495
2496/*
2497 * Use the first item in a ":for" list. Advance to the next.
2498 * Assign the values to the variable (list). "arg" points to the first one.
2499 * Return TRUE when a valid item was found, FALSE when at end of list or
2500 * something wrong.
2501 */
2502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002503next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002505 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002508
2509 item = fi->fi_lw.lw_item;
2510 if (item == NULL)
2511 result = FALSE;
2512 else
2513 {
2514 fi->fi_lw.lw_item = item->li_next;
2515 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2516 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2517 }
2518 return result;
2519}
2520
2521/*
2522 * Free the structure used to store info used by ":for".
2523 */
2524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002525free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526{
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002529 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002530 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002532 list_unref(fi->fi_list);
2533 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 vim_free(fi);
2535}
2536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2538
2539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002540set_context_for_expression(
2541 expand_T *xp,
2542 char_u *arg,
2543 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 int got_eq = FALSE;
2546 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 if (cmdidx == CMD_let)
2550 {
2551 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002552 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002553 {
2554 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002555 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 {
2557 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002558 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002559 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002560 break;
2561 }
2562 return;
2563 }
2564 }
2565 else
2566 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2567 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 while ((xp->xp_pattern = vim_strpbrk(arg,
2569 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2570 {
2571 c = *xp->xp_pattern;
2572 if (c == '&')
2573 {
2574 c = xp->xp_pattern[1];
2575 if (c == '&')
2576 {
2577 ++xp->xp_pattern;
2578 xp->xp_context = cmdidx != CMD_let || got_eq
2579 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2580 }
2581 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002584 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2585 xp->xp_pattern += 2;
2586
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 else if (c == '$')
2590 {
2591 /* environment variable */
2592 xp->xp_context = EXPAND_ENV_VARS;
2593 }
2594 else if (c == '=')
2595 {
2596 got_eq = TRUE;
2597 xp->xp_context = EXPAND_EXPRESSION;
2598 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002599 else if (c == '#'
2600 && xp->xp_context == EXPAND_EXPRESSION)
2601 {
2602 /* Autoload function/variable contains '#'. */
2603 break;
2604 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002605 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 && xp->xp_context == EXPAND_FUNCTIONS
2607 && vim_strchr(xp->xp_pattern, '(') == NULL)
2608 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002609 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
2611 }
2612 else if (cmdidx != CMD_let || got_eq)
2613 {
2614 if (c == '"') /* string */
2615 {
2616 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2617 if (c == '\\' && xp->xp_pattern[1] != NUL)
2618 ++xp->xp_pattern;
2619 xp->xp_context = EXPAND_NOTHING;
2620 }
2621 else if (c == '\'') /* literal string */
2622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2625 /* skip */ ;
2626 xp->xp_context = EXPAND_NOTHING;
2627 }
2628 else if (c == '|')
2629 {
2630 if (xp->xp_pattern[1] == '|')
2631 {
2632 ++xp->xp_pattern;
2633 xp->xp_context = EXPAND_EXPRESSION;
2634 }
2635 else
2636 xp->xp_context = EXPAND_COMMANDS;
2637 }
2638 else
2639 xp->xp_context = EXPAND_EXPRESSION;
2640 }
2641 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002642 /* Doesn't look like something valid, expand as an expression
2643 * anyway. */
2644 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 arg = xp->xp_pattern;
2646 if (*arg != NUL)
2647 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2648 /* skip */ ;
2649 }
2650 xp->xp_pattern = arg;
2651}
2652
2653#endif /* FEAT_CMDL_COMPL */
2654
2655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 * ":unlet[!] var1 ... " command.
2657 */
2658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002659ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002661 ex_unletlock(eap, eap->arg, 0);
2662}
2663
2664/*
2665 * ":lockvar" and ":unlockvar" commands
2666 */
2667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002668ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002671 int deep = 2;
2672
2673 if (eap->forceit)
2674 deep = -1;
2675 else if (vim_isdigit(*arg))
2676 {
2677 deep = getdigits(&arg);
2678 arg = skipwhite(arg);
2679 }
2680
2681 ex_unletlock(eap, arg, deep);
2682}
2683
2684/*
2685 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2686 */
2687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002688ex_unletlock(
2689 exarg_T *eap,
2690 char_u *argstart,
2691 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002692{
2693 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002696 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 do
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002701 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002702 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lv.ll_name == NULL)
2704 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002705 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (name_end != NULL)
2709 {
2710 emsg_severe = TRUE;
2711 EMSG(_(e_trailing));
2712 }
2713 if (!(eap->skip || error))
2714 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 break;
2716 }
2717
2718 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002719 {
2720 if (eap->cmdidx == CMD_unlet)
2721 {
2722 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2723 error = TRUE;
2724 }
2725 else
2726 {
2727 if (do_lock_var(&lv, name_end, deep,
2728 eap->cmdidx == CMD_lockvar) == FAIL)
2729 error = TRUE;
2730 }
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!eap->skip)
2734 clear_lval(&lv);
2735
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 arg = skipwhite(name_end);
2737 } while (!ends_excmd(*arg));
2738
2739 eap->nextcmd = check_nextcmd(arg);
2740}
2741
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002743do_unlet_var(
2744 lval_T *lp,
2745 char_u *name_end,
2746 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 int ret = OK;
2749 int cc;
2750
2751 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 cc = *name_end;
2754 *name_end = NUL;
2755
2756 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002757 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002761 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002762 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002763 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002764 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002765 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 else if (lp->ll_range)
2767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002769 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002770 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002771
2772 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2773 {
2774 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002775 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002776 return FAIL;
2777 ll_li = li;
2778 ++ll_n1;
2779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780
2781 /* Delete a range of List items. */
2782 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2783 {
2784 li = lp->ll_li->li_next;
2785 listitem_remove(lp->ll_list, lp->ll_li);
2786 lp->ll_li = li;
2787 ++lp->ll_n1;
2788 }
2789 }
2790 else
2791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 /* unlet a List item. */
2794 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002797 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 }
2799
2800 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801}
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803/*
2804 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002805 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
2807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002808do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
Bram Moolenaar33570922005-01-25 22:26:29 +00002810 hashtab_T *ht;
2811 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002812 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002813 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002814 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 ht = find_var_ht(name, &varname);
2817 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002820 if (d == NULL)
2821 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 if (ht == &globvarht)
2823 d = &globvardict;
2824 else if (ht == &compat_hashtab)
2825 d = &vimvardict;
2826 else
2827 {
2828 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2829 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2830 }
2831 if (d == NULL)
2832 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002833 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834 return FAIL;
2835 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002836 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002837 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002838 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002839 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002840 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002841 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002842 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002844 || var_check_ro(di->di_flags, name, FALSE)
2845 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002846 return FAIL;
2847
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 delete_var(ht, hi);
2849 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002852 if (forceit)
2853 return OK;
2854 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 return FAIL;
2856}
2857
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002858/*
2859 * Lock or unlock variable indicated by "lp".
2860 * "deep" is the levels to go (-1 for unlimited);
2861 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2862 */
2863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002864do_lock_var(
2865 lval_T *lp,
2866 char_u *name_end,
2867 int deep,
2868 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869{
2870 int ret = OK;
2871 int cc;
2872 dictitem_T *di;
2873
2874 if (deep == 0) /* nothing to do */
2875 return OK;
2876
2877 if (lp->ll_tv == NULL)
2878 {
2879 cc = *name_end;
2880 *name_end = NUL;
2881
2882 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002883 di = find_var(lp->ll_name, NULL, TRUE);
2884 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002886 else if ((di->di_flags & DI_FLAGS_FIX)
2887 && di->di_tv.v_type != VAR_DICT
2888 && di->di_tv.v_type != VAR_LIST)
2889 /* For historic reasons this error is not given for a list or dict.
2890 * E.g., the b: dict could be locked/unlocked. */
2891 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 else
2893 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002894 if (lock)
2895 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002897 di->di_flags &= ~DI_FLAGS_LOCK;
2898 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002899 }
2900 *name_end = cc;
2901 }
2902 else if (lp->ll_range)
2903 {
2904 listitem_T *li = lp->ll_li;
2905
2906 /* (un)lock a range of List items. */
2907 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2908 {
2909 item_lock(&li->li_tv, deep, lock);
2910 li = li->li_next;
2911 ++lp->ll_n1;
2912 }
2913 }
2914 else if (lp->ll_list != NULL)
2915 /* (un)lock a List item. */
2916 item_lock(&lp->ll_li->li_tv, deep, lock);
2917 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002918 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919 item_lock(&lp->ll_di->di_tv, deep, lock);
2920
2921 return ret;
2922}
2923
2924/*
2925 * Lock or unlock an item. "deep" is nr of levels to go.
2926 */
2927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002928item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002929{
2930 static int recurse = 0;
2931 list_T *l;
2932 listitem_T *li;
2933 dict_T *d;
2934 hashitem_T *hi;
2935 int todo;
2936
2937 if (recurse >= DICT_MAXNEST)
2938 {
2939 EMSG(_("E743: variable nested too deep for (un)lock"));
2940 return;
2941 }
2942 if (deep == 0)
2943 return;
2944 ++recurse;
2945
2946 /* lock/unlock the item itself */
2947 if (lock)
2948 tv->v_lock |= VAR_LOCKED;
2949 else
2950 tv->v_lock &= ~VAR_LOCKED;
2951
2952 switch (tv->v_type)
2953 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002954 case VAR_UNKNOWN:
2955 case VAR_NUMBER:
2956 case VAR_STRING:
2957 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002958 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002959 case VAR_FLOAT:
2960 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002961 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002962 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002963 break;
2964
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 case VAR_LIST:
2966 if ((l = tv->vval.v_list) != NULL)
2967 {
2968 if (lock)
2969 l->lv_lock |= VAR_LOCKED;
2970 else
2971 l->lv_lock &= ~VAR_LOCKED;
2972 if (deep < 0 || deep > 1)
2973 /* recursive: lock/unlock the items the List contains */
2974 for (li = l->lv_first; li != NULL; li = li->li_next)
2975 item_lock(&li->li_tv, deep - 1, lock);
2976 }
2977 break;
2978 case VAR_DICT:
2979 if ((d = tv->vval.v_dict) != NULL)
2980 {
2981 if (lock)
2982 d->dv_lock |= VAR_LOCKED;
2983 else
2984 d->dv_lock &= ~VAR_LOCKED;
2985 if (deep < 0 || deep > 1)
2986 {
2987 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002988 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002989 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2990 {
2991 if (!HASHITEM_EMPTY(hi))
2992 {
2993 --todo;
2994 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2995 }
2996 }
2997 }
2998 }
2999 }
3000 --recurse;
3001}
3002
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3004/*
3005 * Delete all "menutrans_" variables.
3006 */
3007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003008del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
Bram Moolenaar33570922005-01-25 22:26:29 +00003010 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003011 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
Bram Moolenaar33570922005-01-25 22:26:29 +00003013 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003014 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003015 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003016 {
3017 if (!HASHITEM_EMPTY(hi))
3018 {
3019 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3021 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003022 }
3023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026#endif
3027
3028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3029
3030/*
3031 * Local string buffer for the next two functions to store a variable name
3032 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3033 * get_user_var_name().
3034 */
3035
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003036static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038static char_u *varnamebuf = NULL;
3039static int varnamebuflen = 0;
3040
3041/*
3042 * Function to concatenate a prefix and a variable name.
3043 */
3044 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003045cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 int len;
3048
3049 len = (int)STRLEN(name) + 3;
3050 if (len > varnamebuflen)
3051 {
3052 vim_free(varnamebuf);
3053 len += 10; /* some additional space */
3054 varnamebuf = alloc(len);
3055 if (varnamebuf == NULL)
3056 {
3057 varnamebuflen = 0;
3058 return NULL;
3059 }
3060 varnamebuflen = len;
3061 }
3062 *varnamebuf = prefix;
3063 varnamebuf[1] = ':';
3064 STRCPY(varnamebuf + 2, name);
3065 return varnamebuf;
3066}
3067
3068/*
3069 * Function given to ExpandGeneric() to obtain the list of user defined
3070 * (global/buffer/window/built-in) variable names.
3071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003075 static long_u gdone;
3076 static long_u bdone;
3077 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003078#ifdef FEAT_WINDOWS
3079 static long_u tdone;
3080#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003081 static int vidx;
3082 static hashitem_T *hi;
3083 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003086 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003087 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003088#ifdef FEAT_WINDOWS
3089 tdone = 0;
3090#endif
3091 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003092
3093 /* Global variables */
3094 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003096 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003097 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003098 else
3099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003100 while (HASHITEM_EMPTY(hi))
3101 ++hi;
3102 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3103 return cat_prefix_varname('g', hi->hi_key);
3104 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003106
3107 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003108 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003111 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003113 else
3114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003115 while (HASHITEM_EMPTY(hi))
3116 ++hi;
3117 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003119
3120 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003121 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003124 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003126 else
3127 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003128 while (HASHITEM_EMPTY(hi))
3129 ++hi;
3130 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003132
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003133#ifdef FEAT_WINDOWS
3134 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003135 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003136 if (tdone < ht->ht_used)
3137 {
3138 if (tdone++ == 0)
3139 hi = ht->ht_array;
3140 else
3141 ++hi;
3142 while (HASHITEM_EMPTY(hi))
3143 ++hi;
3144 return cat_prefix_varname('t', hi->hi_key);
3145 }
3146#endif
3147
Bram Moolenaar33570922005-01-25 22:26:29 +00003148 /* v: variables */
3149 if (vidx < VV_LEN)
3150 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 vim_free(varnamebuf);
3153 varnamebuf = NULL;
3154 varnamebuflen = 0;
3155 return NULL;
3156}
3157
3158#endif /* FEAT_CMDL_COMPL */
3159
3160/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003161 * Return TRUE if "pat" matches "text".
3162 * Does not use 'cpo' and always uses 'magic'.
3163 */
3164 static int
3165pattern_match(char_u *pat, char_u *text, int ic)
3166{
3167 int matches = FALSE;
3168 char_u *save_cpo;
3169 regmatch_T regmatch;
3170
3171 /* avoid 'l' flag in 'cpoptions' */
3172 save_cpo = p_cpo;
3173 p_cpo = (char_u *)"";
3174 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3175 if (regmatch.regprog != NULL)
3176 {
3177 regmatch.rm_ic = ic;
3178 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3179 vim_regfree(regmatch.regprog);
3180 }
3181 p_cpo = save_cpo;
3182 return matches;
3183}
3184
3185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 * types for expressions.
3187 */
3188typedef enum
3189{
3190 TYPE_UNKNOWN = 0
3191 , TYPE_EQUAL /* == */
3192 , TYPE_NEQUAL /* != */
3193 , TYPE_GREATER /* > */
3194 , TYPE_GEQUAL /* >= */
3195 , TYPE_SMALLER /* < */
3196 , TYPE_SEQUAL /* <= */
3197 , TYPE_MATCH /* =~ */
3198 , TYPE_NOMATCH /* !~ */
3199} exptype_T;
3200
3201/*
3202 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003203 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3205 */
3206
3207/*
3208 * Handle zero level expression.
3209 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003211 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 * Return OK or FAIL.
3213 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003215eval0(
3216 char_u *arg,
3217 typval_T *rettv,
3218 char_u **nextcmd,
3219 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 int ret;
3222 char_u *p;
3223
3224 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003225 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (ret == FAIL || !ends_excmd(*p))
3227 {
3228 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /*
3231 * Report the invalid expression unless the expression evaluation has
3232 * been cancelled due to an aborting error, an interrupt, or an
3233 * exception.
3234 */
3235 if (!aborting())
3236 EMSG2(_(e_invexpr2), arg);
3237 ret = FAIL;
3238 }
3239 if (nextcmd != NULL)
3240 *nextcmd = check_nextcmd(p);
3241
3242 return ret;
3243}
3244
3245/*
3246 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003247 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 *
3249 * "arg" must point to the first non-white of the expression.
3250 * "arg" is advanced to the next non-white after the recognized expression.
3251 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003252 * Note: "rettv.v_lock" is not set.
3253 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * Return OK or FAIL.
3255 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003257eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003260 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 /*
3263 * Get the first variable.
3264 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003265 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 return FAIL;
3267
3268 if ((*arg)[0] == '?')
3269 {
3270 result = FALSE;
3271 if (evaluate)
3272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003273 int error = FALSE;
3274
3275 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003277 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003278 if (error)
3279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281
3282 /*
3283 * Get the second variable.
3284 */
3285 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003286 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return FAIL;
3288
3289 /*
3290 * Check for the ":".
3291 */
3292 if ((*arg)[0] != ':')
3293 {
3294 EMSG(_("E109: Missing ':' after '?'"));
3295 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003296 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return FAIL;
3298 }
3299
3300 /*
3301 * Get the third variable.
3302 */
3303 *arg = skipwhite(*arg + 1);
3304 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3305 {
3306 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003307 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return FAIL;
3309 }
3310 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003311 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313
3314 return OK;
3315}
3316
3317/*
3318 * Handle first level expression:
3319 * expr2 || expr2 || expr2 logical OR
3320 *
3321 * "arg" must point to the first non-white of the expression.
3322 * "arg" is advanced to the next non-white after the recognized expression.
3323 *
3324 * Return OK or FAIL.
3325 */
3326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 long result;
3331 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
3334 /*
3335 * Get the first variable.
3336 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003337 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 return FAIL;
3339
3340 /*
3341 * Repeat until there is no following "||".
3342 */
3343 first = TRUE;
3344 result = FALSE;
3345 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3346 {
3347 if (evaluate && first)
3348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003351 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 if (error)
3353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 first = FALSE;
3355 }
3356
3357 /*
3358 * Get the second variable.
3359 */
3360 *arg = skipwhite(*arg + 2);
3361 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3362 return FAIL;
3363
3364 /*
3365 * Compute the result.
3366 */
3367 if (evaluate && !result)
3368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003369 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003371 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003372 if (error)
3373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 if (evaluate)
3376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 rettv->v_type = VAR_NUMBER;
3378 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 }
3381
3382 return OK;
3383}
3384
3385/*
3386 * Handle second level expression:
3387 * expr3 && expr3 && expr3 logical AND
3388 *
3389 * "arg" must point to the first non-white of the expression.
3390 * "arg" is advanced to the next non-white after the recognized expression.
3391 *
3392 * Return OK or FAIL.
3393 */
3394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003395eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 long result;
3399 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003400 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
3402 /*
3403 * Get the first variable.
3404 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return FAIL;
3407
3408 /*
3409 * Repeat until there is no following "&&".
3410 */
3411 first = TRUE;
3412 result = TRUE;
3413 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3414 {
3415 if (evaluate && first)
3416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 if (error)
3421 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 first = FALSE;
3423 }
3424
3425 /*
3426 * Get the second variable.
3427 */
3428 *arg = skipwhite(*arg + 2);
3429 if (eval4(arg, &var2, evaluate && result) == FAIL)
3430 return FAIL;
3431
3432 /*
3433 * Compute the result.
3434 */
3435 if (evaluate && result)
3436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003437 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003440 if (error)
3441 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443 if (evaluate)
3444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 rettv->v_type = VAR_NUMBER;
3446 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 }
3449
3450 return OK;
3451}
3452
3453/*
3454 * Handle third level expression:
3455 * var1 == var2
3456 * var1 =~ var2
3457 * var1 != var2
3458 * var1 !~ var2
3459 * var1 > var2
3460 * var1 >= var2
3461 * var1 < var2
3462 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003463 * var1 is var2
3464 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 *
3466 * "arg" must point to the first non-white of the expression.
3467 * "arg" is advanced to the next non-white after the recognized expression.
3468 *
3469 * Return OK or FAIL.
3470 */
3471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar33570922005-01-25 22:26:29 +00003474 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 char_u *p;
3476 int i;
3477 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003478 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003480 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 char_u *s1, *s2;
3482 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 /*
3486 * Get the first variable.
3487 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return FAIL;
3490
3491 p = *arg;
3492 switch (p[0])
3493 {
3494 case '=': if (p[1] == '=')
3495 type = TYPE_EQUAL;
3496 else if (p[1] == '~')
3497 type = TYPE_MATCH;
3498 break;
3499 case '!': if (p[1] == '=')
3500 type = TYPE_NEQUAL;
3501 else if (p[1] == '~')
3502 type = TYPE_NOMATCH;
3503 break;
3504 case '>': if (p[1] != '=')
3505 {
3506 type = TYPE_GREATER;
3507 len = 1;
3508 }
3509 else
3510 type = TYPE_GEQUAL;
3511 break;
3512 case '<': if (p[1] != '=')
3513 {
3514 type = TYPE_SMALLER;
3515 len = 1;
3516 }
3517 else
3518 type = TYPE_SEQUAL;
3519 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003520 case 'i': if (p[1] == 's')
3521 {
3522 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3523 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003524 i = p[len];
3525 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003526 {
3527 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3528 type_is = TRUE;
3529 }
3530 }
3531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533
3534 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003535 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 */
3537 if (type != TYPE_UNKNOWN)
3538 {
3539 /* extra question mark appended: ignore case */
3540 if (p[len] == '?')
3541 {
3542 ic = TRUE;
3543 ++len;
3544 }
3545 /* extra '#' appended: match case */
3546 else if (p[len] == '#')
3547 {
3548 ic = FALSE;
3549 ++len;
3550 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003551 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
3553 ic = p_ic;
3554
3555 /*
3556 * Get the second variable.
3557 */
3558 *arg = skipwhite(p + len);
3559 if (eval5(arg, &var2, evaluate) == FAIL)
3560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003561 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 return FAIL;
3563 }
3564
3565 if (evaluate)
3566 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003567 if (type_is && rettv->v_type != var2.v_type)
3568 {
3569 /* For "is" a different type always means FALSE, for "notis"
3570 * it means TRUE. */
3571 n1 = (type == TYPE_NEQUAL);
3572 }
3573 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3574 {
3575 if (type_is)
3576 {
3577 n1 = (rettv->v_type == var2.v_type
3578 && rettv->vval.v_list == var2.vval.v_list);
3579 if (type == TYPE_NEQUAL)
3580 n1 = !n1;
3581 }
3582 else if (rettv->v_type != var2.v_type
3583 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3584 {
3585 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003586 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003587 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003588 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003589 clear_tv(rettv);
3590 clear_tv(&var2);
3591 return FAIL;
3592 }
3593 else
3594 {
3595 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003596 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3597 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003598 if (type == TYPE_NEQUAL)
3599 n1 = !n1;
3600 }
3601 }
3602
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003603 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3604 {
3605 if (type_is)
3606 {
3607 n1 = (rettv->v_type == var2.v_type
3608 && rettv->vval.v_dict == var2.vval.v_dict);
3609 if (type == TYPE_NEQUAL)
3610 n1 = !n1;
3611 }
3612 else if (rettv->v_type != var2.v_type
3613 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3614 {
3615 if (rettv->v_type != var2.v_type)
3616 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3617 else
3618 EMSG(_("E736: Invalid operation for Dictionary"));
3619 clear_tv(rettv);
3620 clear_tv(&var2);
3621 return FAIL;
3622 }
3623 else
3624 {
3625 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003626 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3627 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003628 if (type == TYPE_NEQUAL)
3629 n1 = !n1;
3630 }
3631 }
3632
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003633 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3634 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003635 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003636 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003637 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003638 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003639 clear_tv(rettv);
3640 clear_tv(&var2);
3641 return FAIL;
3642 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003643 if ((rettv->v_type == VAR_PARTIAL
3644 && rettv->vval.v_partial == NULL)
3645 || (var2.v_type == VAR_PARTIAL
3646 && var2.vval.v_partial == NULL))
3647 /* when a partial is NULL assume not equal */
3648 n1 = FALSE;
3649 else if (type_is)
3650 {
3651 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3652 /* strings are considered the same if their value is
3653 * the same */
3654 n1 = tv_equal(rettv, &var2, ic, FALSE);
3655 else if (rettv->v_type == VAR_PARTIAL
3656 && var2.v_type == VAR_PARTIAL)
3657 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3658 else
3659 n1 = FALSE;
3660 }
3661 else
3662 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003663 if (type == TYPE_NEQUAL)
3664 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003665 }
3666
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003667#ifdef FEAT_FLOAT
3668 /*
3669 * If one of the two variables is a float, compare as a float.
3670 * When using "=~" or "!~", always compare as string.
3671 */
3672 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3673 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3674 {
3675 float_T f1, f2;
3676
3677 if (rettv->v_type == VAR_FLOAT)
3678 f1 = rettv->vval.v_float;
3679 else
3680 f1 = get_tv_number(rettv);
3681 if (var2.v_type == VAR_FLOAT)
3682 f2 = var2.vval.v_float;
3683 else
3684 f2 = get_tv_number(&var2);
3685 n1 = FALSE;
3686 switch (type)
3687 {
3688 case TYPE_EQUAL: n1 = (f1 == f2); break;
3689 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3690 case TYPE_GREATER: n1 = (f1 > f2); break;
3691 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3692 case TYPE_SMALLER: n1 = (f1 < f2); break;
3693 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3694 case TYPE_UNKNOWN:
3695 case TYPE_MATCH:
3696 case TYPE_NOMATCH: break; /* avoid gcc warning */
3697 }
3698 }
3699#endif
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 /*
3702 * If one of the two variables is a number, compare as a number.
3703 * When using "=~" or "!~", always compare as string.
3704 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003705 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 n1 = get_tv_number(rettv);
3709 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 switch (type)
3711 {
3712 case TYPE_EQUAL: n1 = (n1 == n2); break;
3713 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3714 case TYPE_GREATER: n1 = (n1 > n2); break;
3715 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3716 case TYPE_SMALLER: n1 = (n1 < n2); break;
3717 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3718 case TYPE_UNKNOWN:
3719 case TYPE_MATCH:
3720 case TYPE_NOMATCH: break; /* avoid gcc warning */
3721 }
3722 }
3723 else
3724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 s1 = get_tv_string_buf(rettv, buf1);
3726 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3728 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3729 else
3730 i = 0;
3731 n1 = FALSE;
3732 switch (type)
3733 {
3734 case TYPE_EQUAL: n1 = (i == 0); break;
3735 case TYPE_NEQUAL: n1 = (i != 0); break;
3736 case TYPE_GREATER: n1 = (i > 0); break;
3737 case TYPE_GEQUAL: n1 = (i >= 0); break;
3738 case TYPE_SMALLER: n1 = (i < 0); break;
3739 case TYPE_SEQUAL: n1 = (i <= 0); break;
3740
3741 case TYPE_MATCH:
3742 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003743 n1 = pattern_match(s2, s1, ic);
3744 if (type == TYPE_NOMATCH)
3745 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 break;
3747
3748 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3749 }
3750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 clear_tv(rettv);
3752 clear_tv(&var2);
3753 rettv->v_type = VAR_NUMBER;
3754 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 }
3757
3758 return OK;
3759}
3760
3761/*
3762 * Handle fourth level expression:
3763 * + number addition
3764 * - number subtraction
3765 * . string concatenation
3766 *
3767 * "arg" must point to the first non-white of the expression.
3768 * "arg" is advanced to the next non-white after the recognized expression.
3769 *
3770 * Return OK or FAIL.
3771 */
3772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003773eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 typval_T var2;
3776 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003778 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003779#ifdef FEAT_FLOAT
3780 float_T f1 = 0, f2 = 0;
3781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 char_u *s1, *s2;
3783 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3784 char_u *p;
3785
3786 /*
3787 * Get the first variable.
3788 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003789 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return FAIL;
3791
3792 /*
3793 * Repeat computing, until no '+', '-' or '.' is following.
3794 */
3795 for (;;)
3796 {
3797 op = **arg;
3798 if (op != '+' && op != '-' && op != '.')
3799 break;
3800
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003801 if ((op != '+' || rettv->v_type != VAR_LIST)
3802#ifdef FEAT_FLOAT
3803 && (op == '.' || rettv->v_type != VAR_FLOAT)
3804#endif
3805 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003806 {
3807 /* For "list + ...", an illegal use of the first operand as
3808 * a number cannot be determined before evaluating the 2nd
3809 * operand: if this is also a list, all is ok.
3810 * For "something . ...", "something - ..." or "non-list + ...",
3811 * we know that the first operand needs to be a string or number
3812 * without evaluating the 2nd operand. So check before to avoid
3813 * side effects after an error. */
3814 if (evaluate && get_tv_string_chk(rettv) == NULL)
3815 {
3816 clear_tv(rettv);
3817 return FAIL;
3818 }
3819 }
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 /*
3822 * Get the second variable.
3823 */
3824 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003825 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003827 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return FAIL;
3829 }
3830
3831 if (evaluate)
3832 {
3833 /*
3834 * Compute the result.
3835 */
3836 if (op == '.')
3837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003838 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3839 s2 = get_tv_string_buf_chk(&var2, buf2);
3840 if (s2 == NULL) /* type error ? */
3841 {
3842 clear_tv(rettv);
3843 clear_tv(&var2);
3844 return FAIL;
3845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003846 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 clear_tv(rettv);
3848 rettv->v_type = VAR_STRING;
3849 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003851 else if (op == '+' && rettv->v_type == VAR_LIST
3852 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003853 {
3854 /* concatenate Lists */
3855 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3856 &var3) == FAIL)
3857 {
3858 clear_tv(rettv);
3859 clear_tv(&var2);
3860 return FAIL;
3861 }
3862 clear_tv(rettv);
3863 *rettv = var3;
3864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 else
3866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 int error = FALSE;
3868
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003869#ifdef FEAT_FLOAT
3870 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003872 f1 = rettv->vval.v_float;
3873 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003875 else
3876#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003877 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003878 n1 = get_tv_number_chk(rettv, &error);
3879 if (error)
3880 {
3881 /* This can only happen for "list + non-list". For
3882 * "non-list + ..." or "something - ...", we returned
3883 * before evaluating the 2nd operand. */
3884 clear_tv(rettv);
3885 return FAIL;
3886 }
3887#ifdef FEAT_FLOAT
3888 if (var2.v_type == VAR_FLOAT)
3889 f1 = n1;
3890#endif
3891 }
3892#ifdef FEAT_FLOAT
3893 if (var2.v_type == VAR_FLOAT)
3894 {
3895 f2 = var2.vval.v_float;
3896 n2 = 0;
3897 }
3898 else
3899#endif
3900 {
3901 n2 = get_tv_number_chk(&var2, &error);
3902 if (error)
3903 {
3904 clear_tv(rettv);
3905 clear_tv(&var2);
3906 return FAIL;
3907 }
3908#ifdef FEAT_FLOAT
3909 if (rettv->v_type == VAR_FLOAT)
3910 f2 = n2;
3911#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003914
3915#ifdef FEAT_FLOAT
3916 /* If there is a float on either side the result is a float. */
3917 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3918 {
3919 if (op == '+')
3920 f1 = f1 + f2;
3921 else
3922 f1 = f1 - f2;
3923 rettv->v_type = VAR_FLOAT;
3924 rettv->vval.v_float = f1;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003927#endif
3928 {
3929 if (op == '+')
3930 n1 = n1 + n2;
3931 else
3932 n1 = n1 - n2;
3933 rettv->v_type = VAR_NUMBER;
3934 rettv->vval.v_number = n1;
3935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003937 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 }
3940 return OK;
3941}
3942
3943/*
3944 * Handle fifth level expression:
3945 * * number multiplication
3946 * / number division
3947 * % number modulo
3948 *
3949 * "arg" must point to the first non-white of the expression.
3950 * "arg" is advanced to the next non-white after the recognized expression.
3951 *
3952 * Return OK or FAIL.
3953 */
3954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003955eval6(
3956 char_u **arg,
3957 typval_T *rettv,
3958 int evaluate,
3959 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003963 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003964#ifdef FEAT_FLOAT
3965 int use_float = FALSE;
3966 float_T f1 = 0, f2;
3967#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003968 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 /*
3971 * Get the first variable.
3972 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003973 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return FAIL;
3975
3976 /*
3977 * Repeat computing, until no '*', '/' or '%' is following.
3978 */
3979 for (;;)
3980 {
3981 op = **arg;
3982 if (op != '*' && op != '/' && op != '%')
3983 break;
3984
3985 if (evaluate)
3986 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003987#ifdef FEAT_FLOAT
3988 if (rettv->v_type == VAR_FLOAT)
3989 {
3990 f1 = rettv->vval.v_float;
3991 use_float = TRUE;
3992 n1 = 0;
3993 }
3994 else
3995#endif
3996 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 if (error)
3999 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 else
4002 n1 = 0;
4003
4004 /*
4005 * Get the second variable.
4006 */
4007 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004008 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 return FAIL;
4010
4011 if (evaluate)
4012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004013#ifdef FEAT_FLOAT
4014 if (var2.v_type == VAR_FLOAT)
4015 {
4016 if (!use_float)
4017 {
4018 f1 = n1;
4019 use_float = TRUE;
4020 }
4021 f2 = var2.vval.v_float;
4022 n2 = 0;
4023 }
4024 else
4025#endif
4026 {
4027 n2 = get_tv_number_chk(&var2, &error);
4028 clear_tv(&var2);
4029 if (error)
4030 return FAIL;
4031#ifdef FEAT_FLOAT
4032 if (use_float)
4033 f2 = n2;
4034#endif
4035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037 /*
4038 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004039 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004041#ifdef FEAT_FLOAT
4042 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004044 if (op == '*')
4045 f1 = f1 * f2;
4046 else if (op == '/')
4047 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048# ifdef VMS
4049 /* VMS crashes on divide by zero, work around it */
4050 if (f2 == 0.0)
4051 {
4052 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004053 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004054 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004055 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004056 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004057 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004058 }
4059 else
4060 f1 = f1 / f2;
4061# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 /* We rely on the floating point library to handle divide
4063 * by zero to result in "inf" and not a crash. */
4064 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004065# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004069 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070 return FAIL;
4071 }
4072 rettv->v_type = VAR_FLOAT;
4073 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004078 if (op == '*')
4079 n1 = n1 * n2;
4080 else if (op == '/')
4081 {
4082 if (n2 == 0) /* give an error message? */
4083 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004084 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004085 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004086 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004087 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004088 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004089 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004090 }
4091 else
4092 n1 = n1 / n2;
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004095 {
4096 if (n2 == 0) /* give an error message? */
4097 n1 = 0;
4098 else
4099 n1 = n1 % n2;
4100 }
4101 rettv->v_type = VAR_NUMBER;
4102 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
4105 }
4106
4107 return OK;
4108}
4109
4110/*
4111 * Handle sixth level expression:
4112 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004113 * "string" string constant
4114 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * &option-name option value
4116 * @r register contents
4117 * identifier variable value
4118 * function() function call
4119 * $VAR environment variable
4120 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004121 * [expr, expr] List
4122 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * Also handle:
4125 * ! in front logical NOT
4126 * - in front unary minus
4127 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128 * trailing [] subscript in String or List
4129 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 *
4131 * "arg" must point to the first non-white of the expression.
4132 * "arg" is advanced to the next non-white after the recognized expression.
4133 *
4134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137eval7(
4138 char_u **arg,
4139 typval_T *rettv,
4140 int evaluate,
4141 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004143 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int len;
4145 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u *start_leader, *end_leader;
4147 int ret = OK;
4148 char_u *alias;
4149
4150 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004152 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004157 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 */
4159 start_leader = *arg;
4160 while (**arg == '!' || **arg == '-' || **arg == '+')
4161 *arg = skipwhite(*arg + 1);
4162 end_leader = *arg;
4163
4164 switch (**arg)
4165 {
4166 /*
4167 * Number constant.
4168 */
4169 case '0':
4170 case '1':
4171 case '2':
4172 case '3':
4173 case '4':
4174 case '5':
4175 case '6':
4176 case '7':
4177 case '8':
4178 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004179 {
4180#ifdef FEAT_FLOAT
4181 char_u *p = skipdigits(*arg + 1);
4182 int get_float = FALSE;
4183
4184 /* We accept a float when the format matches
4185 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004186 * strict to avoid backwards compatibility problems.
4187 * Don't look for a float after the "." operator, so that
4188 * ":let vers = 1.2.3" doesn't fail. */
4189 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004191 get_float = TRUE;
4192 p = skipdigits(p + 2);
4193 if (*p == 'e' || *p == 'E')
4194 {
4195 ++p;
4196 if (*p == '-' || *p == '+')
4197 ++p;
4198 if (!vim_isdigit(*p))
4199 get_float = FALSE;
4200 else
4201 p = skipdigits(p + 1);
4202 }
4203 if (ASCII_ISALPHA(*p) || *p == '.')
4204 get_float = FALSE;
4205 }
4206 if (get_float)
4207 {
4208 float_T f;
4209
4210 *arg += string2float(*arg, &f);
4211 if (evaluate)
4212 {
4213 rettv->v_type = VAR_FLOAT;
4214 rettv->vval.v_float = f;
4215 }
4216 }
4217 else
4218#endif
4219 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004220 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004221 *arg += len;
4222 if (evaluate)
4223 {
4224 rettv->v_type = VAR_NUMBER;
4225 rettv->vval.v_number = n;
4226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /*
4232 * String constant: "string".
4233 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 break;
4236
4237 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004238 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 break;
4242
4243 /*
4244 * List: [expr, expr]
4245 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 break;
4248
4249 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004250 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251 * Dictionary: {key: val, key: val}
4252 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004253 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4254 if (ret == NOTDONE)
4255 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004256 break;
4257
4258 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004259 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004261 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 break;
4263
4264 /*
4265 * Environment variable: $VAR.
4266 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 break;
4269
4270 /*
4271 * Register contents: @r.
4272 */
4273 case '@': ++*arg;
4274 if (evaluate)
4275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004277 rettv->vval.v_string = get_reg_contents(**arg,
4278 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 if (**arg != NUL)
4281 ++*arg;
4282 break;
4283
4284 /*
4285 * nested expression: (expression).
4286 */
4287 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (**arg == ')')
4290 ++*arg;
4291 else if (ret == OK)
4292 {
4293 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 ret = FAIL;
4296 }
4297 break;
4298
Bram Moolenaar8c711452005-01-14 21:53:12 +00004299 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 break;
4301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302
4303 if (ret == NOTDONE)
4304 {
4305 /*
4306 * Must be a variable or function name.
4307 * Can also be a curly-braces kind of name: {expr}.
4308 */
4309 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004310 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 if (alias != NULL)
4312 s = alias;
4313
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004314 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 ret = FAIL;
4316 else
4317 {
4318 if (**arg == '(') /* recursive! */
4319 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004320 partial_T *partial;
4321
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004322 if (!evaluate)
4323 check_vars(s, len);
4324
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 /* If "s" is the name of a variable of type VAR_FUNC
4326 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004327 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004328
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004329 /* Need to make a copy, in case evaluating the arguments makes
4330 * the name invalid. */
4331 s = vim_strsave(s);
4332 if (s == NULL)
4333 ret = FAIL;
4334 else
4335 /* Invoke the function. */
4336 ret = get_func_tv(s, len, rettv, arg,
4337 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4338 &len, evaluate, partial, NULL);
4339 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004340
4341 /* If evaluate is FALSE rettv->v_type was not set in
4342 * get_func_tv, but it's needed in handle_subscript() to parse
4343 * what follows. So set it here. */
4344 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4345 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004346 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004347 rettv->v_type = VAR_FUNC;
4348 }
4349
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350 /* Stop the expression evaluation when immediately
4351 * aborting on error, or when an interrupt occurred or
4352 * an exception was thrown but not caught. */
4353 if (aborting())
4354 {
4355 if (ret == OK)
4356 clear_tv(rettv);
4357 ret = FAIL;
4358 }
4359 }
4360 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004361 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004362 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004363 {
4364 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004365 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004368 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 }
4370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *arg = skipwhite(*arg);
4372
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004373 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4374 * expr(expr). */
4375 if (ret == OK)
4376 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 /*
4379 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4380 */
4381 if (ret == OK && evaluate && end_leader > start_leader)
4382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004384 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004385#ifdef FEAT_FLOAT
4386 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 if (rettv->v_type == VAR_FLOAT)
4389 f = rettv->vval.v_float;
4390 else
4391#endif
4392 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395 clear_tv(rettv);
4396 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 else
4399 {
4400 while (end_leader > start_leader)
4401 {
4402 --end_leader;
4403 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004404 {
4405#ifdef FEAT_FLOAT
4406 if (rettv->v_type == VAR_FLOAT)
4407 f = !f;
4408 else
4409#endif
4410 val = !val;
4411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004413 {
4414#ifdef FEAT_FLOAT
4415 if (rettv->v_type == VAR_FLOAT)
4416 f = -f;
4417 else
4418#endif
4419 val = -val;
4420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004422#ifdef FEAT_FLOAT
4423 if (rettv->v_type == VAR_FLOAT)
4424 {
4425 clear_tv(rettv);
4426 rettv->vval.v_float = f;
4427 }
4428 else
4429#endif
4430 {
4431 clear_tv(rettv);
4432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = val;
4434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437
4438 return ret;
4439}
4440
4441/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004442 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4443 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4445 */
4446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004447eval_index(
4448 char_u **arg,
4449 typval_T *rettv,
4450 int evaluate,
4451 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452{
4453 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004454 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004456 long len = -1;
4457 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460
Bram Moolenaara03f2332016-02-06 18:09:59 +01004461 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004463 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004464 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004465 if (verbose)
4466 EMSG(_("E695: Cannot index a Funcref"));
4467 return FAIL;
4468 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004469#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004470 if (verbose)
4471 EMSG(_(e_float_as_string));
4472 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004473#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004474 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004475 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004476 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004477 if (verbose)
4478 EMSG(_("E909: Cannot index a special variable"));
4479 return FAIL;
4480 case VAR_UNKNOWN:
4481 if (evaluate)
4482 return FAIL;
4483 /* FALLTHROUGH */
4484
4485 case VAR_STRING:
4486 case VAR_NUMBER:
4487 case VAR_LIST:
4488 case VAR_DICT:
4489 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004492 init_tv(&var1);
4493 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 /*
4497 * dict.name
4498 */
4499 key = *arg + 1;
4500 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4501 ;
4502 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 }
4506 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 /*
4509 * something[idx]
4510 *
4511 * Get the (first) variable from inside the [].
4512 */
4513 *arg = skipwhite(*arg + 1);
4514 if (**arg == ':')
4515 empty1 = TRUE;
4516 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4517 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004518 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4519 {
4520 /* not a number or string */
4521 clear_tv(&var1);
4522 return FAIL;
4523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524
4525 /*
4526 * Get the second variable from inside the [:].
4527 */
4528 if (**arg == ':')
4529 {
4530 range = TRUE;
4531 *arg = skipwhite(*arg + 1);
4532 if (**arg == ']')
4533 empty2 = TRUE;
4534 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004536 if (!empty1)
4537 clear_tv(&var1);
4538 return FAIL;
4539 }
4540 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4541 {
4542 /* not a number or string */
4543 if (!empty1)
4544 clear_tv(&var1);
4545 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 return FAIL;
4547 }
4548 }
4549
4550 /* Check for the ']'. */
4551 if (**arg != ']')
4552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 if (verbose)
4554 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 clear_tv(&var1);
4556 if (range)
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 }
4562
4563 if (evaluate)
4564 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 n1 = 0;
4566 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 n1 = get_tv_number(&var1);
4569 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 }
4571 if (range)
4572 {
4573 if (empty2)
4574 n2 = -1;
4575 else
4576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 n2 = get_tv_number(&var2);
4578 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 }
4580 }
4581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004584 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004585 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004586 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004587 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004588 case VAR_SPECIAL:
4589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004590 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004591 break; /* not evaluating, skipping over subscript */
4592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 case VAR_NUMBER:
4594 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 len = (long)STRLEN(s);
4597 if (range)
4598 {
4599 /* The resulting variable is a substring. If the indexes
4600 * are out of range the result is empty. */
4601 if (n1 < 0)
4602 {
4603 n1 = len + n1;
4604 if (n1 < 0)
4605 n1 = 0;
4606 }
4607 if (n2 < 0)
4608 n2 = len + n2;
4609 else if (n2 >= len)
4610 n2 = len;
4611 if (n1 >= len || n2 < 0 || n1 > n2)
4612 s = NULL;
4613 else
4614 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4615 }
4616 else
4617 {
4618 /* The resulting variable is a string of a single
4619 * character. If the index is too big or negative the
4620 * result is empty. */
4621 if (n1 >= len || n1 < 0)
4622 s = NULL;
4623 else
4624 s = vim_strnsave(s + n1, 1);
4625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 clear_tv(rettv);
4627 rettv->v_type = VAR_STRING;
4628 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 break;
4630
4631 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 if (n1 < 0)
4634 n1 = len + n1;
4635 if (!empty1 && (n1 < 0 || n1 >= len))
4636 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004637 /* For a range we allow invalid values and return an empty
4638 * list. A list index out of range is an error. */
4639 if (!range)
4640 {
4641 if (verbose)
4642 EMSGN(_(e_listidx), n1);
4643 return FAIL;
4644 }
4645 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 }
4647 if (range)
4648 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004649 list_T *l;
4650 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651
4652 if (n2 < 0)
4653 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004654 else if (n2 >= len)
4655 n2 = len - 1;
4656 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004657 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 l = list_alloc();
4659 if (l == NULL)
4660 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 n1 <= n2; ++n1)
4663 {
4664 if (list_append_tv(l, &item->li_tv) == FAIL)
4665 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004666 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 return FAIL;
4668 }
4669 item = item->li_next;
4670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004672 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 }
4674 else
4675 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004676 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
4678 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 }
4680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681
4682 case VAR_DICT:
4683 if (range)
4684 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004685 if (verbose)
4686 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 if (len == -1)
4688 clear_tv(&var1);
4689 return FAIL;
4690 }
4691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693
4694 if (len == -1)
4695 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004696 key = get_tv_string_chk(&var1);
4697 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 clear_tv(&var1);
4700 return FAIL;
4701 }
4702 }
4703
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004704 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004706 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004707 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708 if (len == -1)
4709 clear_tv(&var1);
4710 if (item == NULL)
4711 return FAIL;
4712
4713 copy_tv(&item->di_tv, &var1);
4714 clear_tv(rettv);
4715 *rettv = var1;
4716 }
4717 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 }
4719 }
4720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 return OK;
4722}
4723
4724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 * Get an option value.
4726 * "arg" points to the '&' or '+' before the option name.
4727 * "arg" is advanced to character after the option name.
4728 * Return OK or FAIL.
4729 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004730 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004731get_option_tv(
4732 char_u **arg,
4733 typval_T *rettv, /* when NULL, only check if option exists */
4734 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 char_u *option_end;
4737 long numval;
4738 char_u *stringval;
4739 int opt_type;
4740 int c;
4741 int working = (**arg == '+'); /* has("+option") */
4742 int ret = OK;
4743 int opt_flags;
4744
4745 /*
4746 * Isolate the option name and find its value.
4747 */
4748 option_end = find_option_end(arg, &opt_flags);
4749 if (option_end == NULL)
4750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 EMSG2(_("E112: Option name missing: %s"), *arg);
4753 return FAIL;
4754 }
4755
4756 if (!evaluate)
4757 {
4758 *arg = option_end;
4759 return OK;
4760 }
4761
4762 c = *option_end;
4763 *option_end = NUL;
4764 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766
4767 if (opt_type == -3) /* invalid name */
4768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 EMSG2(_("E113: Unknown option: %s"), *arg);
4771 ret = FAIL;
4772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 if (opt_type == -2) /* hidden string option */
4776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 rettv->v_type = VAR_STRING;
4778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 else if (opt_type == -1) /* hidden number option */
4781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 rettv->v_type = VAR_NUMBER;
4783 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 else if (opt_type == 1) /* number option */
4786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 rettv->v_type = VAR_NUMBER;
4788 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 else /* string option */
4791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 rettv->v_type = VAR_STRING;
4793 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 }
4796 else if (working && (opt_type == -2 || opt_type == -1))
4797 ret = FAIL;
4798
4799 *option_end = c; /* put back for error messages */
4800 *arg = option_end;
4801
4802 return ret;
4803}
4804
4805/*
4806 * Allocate a variable for a string constant.
4807 * Return OK or FAIL.
4808 */
4809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004810get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 char_u *p;
4813 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 int extra = 0;
4815
4816 /*
4817 * Find the end of the string, skipping backslashed characters.
4818 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004819 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 if (*p == '\\' && p[1] != NUL)
4822 {
4823 ++p;
4824 /* A "\<x>" form occupies at least 4 characters, and produces up
4825 * to 6 characters: reserve space for 2 extra */
4826 if (*p == '<')
4827 extra += 2;
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830
4831 if (*p != '"')
4832 {
4833 EMSG2(_("E114: Missing quote: %s"), *arg);
4834 return FAIL;
4835 }
4836
4837 /* If only parsing, set *arg and return here */
4838 if (!evaluate)
4839 {
4840 *arg = p + 1;
4841 return OK;
4842 }
4843
4844 /*
4845 * Copy the string into allocated memory, handling backslashed
4846 * characters.
4847 */
4848 name = alloc((unsigned)(p - *arg + extra));
4849 if (name == NULL)
4850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 rettv->v_type = VAR_STRING;
4852 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
Bram Moolenaar8c711452005-01-14 21:53:12 +00004854 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
4856 if (*p == '\\')
4857 {
4858 switch (*++p)
4859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 case 'b': *name++ = BS; ++p; break;
4861 case 'e': *name++ = ESC; ++p; break;
4862 case 'f': *name++ = FF; ++p; break;
4863 case 'n': *name++ = NL; ++p; break;
4864 case 'r': *name++ = CAR; ++p; break;
4865 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 case 'X': /* hex: "\x1", "\x12" */
4868 case 'x':
4869 case 'u': /* Unicode: "\u0023" */
4870 case 'U':
4871 if (vim_isxdigit(p[1]))
4872 {
4873 int n, nr;
4874 int c = toupper(*p);
4875
4876 if (c == 'X')
4877 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004878 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004880 else
4881 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 nr = 0;
4883 while (--n >= 0 && vim_isxdigit(p[1]))
4884 {
4885 ++p;
4886 nr = (nr << 4) + hex2nr(*p);
4887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004888 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889#ifdef FEAT_MBYTE
4890 /* For "\u" store the number according to
4891 * 'encoding'. */
4892 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004893 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
4895#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004896 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 break;
4899
4900 /* octal: "\1", "\12", "\123" */
4901 case '0':
4902 case '1':
4903 case '2':
4904 case '3':
4905 case '4':
4906 case '5':
4907 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 case '7': *name = *p++ - '0';
4909 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004911 *name = (*name << 3) + *p++ - '0';
4912 if (*p >= '0' && *p <= '7')
4913 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917
4918 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004919 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (extra != 0)
4921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004922 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 break;
4924 }
4925 /* FALLTHROUGH */
4926
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 break;
4929 }
4930 }
4931 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004932 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004936 if (*p != NUL) /* just in case */
4937 ++p;
4938 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return OK;
4941}
4942
4943/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * Return OK or FAIL.
4946 */
4947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004951 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004952 int reduce = 0;
4953
4954 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004957 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004962 break;
4963 ++reduce;
4964 ++p;
4965 }
4966 }
4967
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004971 return FAIL;
4972 }
4973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004975 if (!evaluate)
4976 {
4977 *arg = p + 1;
4978 return OK;
4979 }
4980
4981 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004983 */
4984 str = alloc((unsigned)((p - *arg) - reduce));
4985 if (str == NULL)
4986 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 rettv->v_type = VAR_STRING;
4988 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004992 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 break;
4996 ++p;
4997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005001 *arg = p + 1;
5002
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005003 return OK;
5004}
5005
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005006/*
5007 * Return the function name of the partial.
5008 */
5009 char_u *
5010partial_name(partial_T *pt)
5011{
5012 if (pt->pt_name != NULL)
5013 return pt->pt_name;
5014 return pt->pt_func->uf_name;
5015}
5016
Bram Moolenaarddecc252016-04-06 22:59:37 +02005017 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005018partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005019{
5020 int i;
5021
5022 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005023 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005024 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005025 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005026 if (pt->pt_name != NULL)
5027 {
5028 func_unref(pt->pt_name);
5029 vim_free(pt->pt_name);
5030 }
5031 else
5032 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005033 vim_free(pt);
5034}
5035
5036/*
5037 * Unreference a closure: decrement the reference count and free it when it
5038 * becomes zero.
5039 */
5040 void
5041partial_unref(partial_T *pt)
5042{
5043 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005044 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005045}
5046
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005047static int tv_equal_recurse_limit;
5048
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005049 static int
5050func_equal(
5051 typval_T *tv1,
5052 typval_T *tv2,
5053 int ic) /* ignore case */
5054{
5055 char_u *s1, *s2;
5056 dict_T *d1, *d2;
5057 int a1, a2;
5058 int i;
5059
5060 /* empty and NULL function name considered the same */
5061 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005062 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005063 if (s1 != NULL && *s1 == NUL)
5064 s1 = NULL;
5065 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005066 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005067 if (s2 != NULL && *s2 == NUL)
5068 s2 = NULL;
5069 if (s1 == NULL || s2 == NULL)
5070 {
5071 if (s1 != s2)
5072 return FALSE;
5073 }
5074 else if (STRCMP(s1, s2) != 0)
5075 return FALSE;
5076
5077 /* empty dict and NULL dict is different */
5078 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5079 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5080 if (d1 == NULL || d2 == NULL)
5081 {
5082 if (d1 != d2)
5083 return FALSE;
5084 }
5085 else if (!dict_equal(d1, d2, ic, TRUE))
5086 return FALSE;
5087
5088 /* empty list and no list considered the same */
5089 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5090 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5091 if (a1 != a2)
5092 return FALSE;
5093 for (i = 0; i < a1; ++i)
5094 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5095 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5096 return FALSE;
5097
5098 return TRUE;
5099}
5100
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005101/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005102 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005103 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005105 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005107tv_equal(
5108 typval_T *tv1,
5109 typval_T *tv2,
5110 int ic, /* ignore case */
5111 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005112{
5113 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005114 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005115 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005116 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005117
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005118 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005119 * recursiveness to a limit. We guess they are equal then.
5120 * A fixed limit has the problem of still taking an awful long time.
5121 * Reduce the limit every time running into it. That should work fine for
5122 * deeply linked structures that are not recursively linked and catch
5123 * recursiveness quickly. */
5124 if (!recursive)
5125 tv_equal_recurse_limit = 1000;
5126 if (recursive_cnt >= tv_equal_recurse_limit)
5127 {
5128 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005129 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005130 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005131
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005132 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5133 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005134 if ((tv1->v_type == VAR_FUNC
5135 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5136 && (tv2->v_type == VAR_FUNC
5137 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5138 {
5139 ++recursive_cnt;
5140 r = func_equal(tv1, tv2, ic);
5141 --recursive_cnt;
5142 return r;
5143 }
5144
5145 if (tv1->v_type != tv2->v_type)
5146 return FALSE;
5147
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005148 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005149 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005150 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005151 ++recursive_cnt;
5152 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5153 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005154 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005155
5156 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005157 ++recursive_cnt;
5158 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5159 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005160 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005161
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005162 case VAR_NUMBER:
5163 return tv1->vval.v_number == tv2->vval.v_number;
5164
5165 case VAR_STRING:
5166 s1 = get_tv_string_buf(tv1, buf1);
5167 s2 = get_tv_string_buf(tv2, buf2);
5168 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005169
5170 case VAR_SPECIAL:
5171 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005172
5173 case VAR_FLOAT:
5174#ifdef FEAT_FLOAT
5175 return tv1->vval.v_float == tv2->vval.v_float;
5176#endif
5177 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005178#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005179 return tv1->vval.v_job == tv2->vval.v_job;
5180#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005181 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005182#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005183 return tv1->vval.v_channel == tv2->vval.v_channel;
5184#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005185 case VAR_FUNC:
5186 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005187 case VAR_UNKNOWN:
5188 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005190
Bram Moolenaara03f2332016-02-06 18:09:59 +01005191 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5192 * does not equal anything, not even itself. */
5193 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005194}
5195
5196/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005197 * Return the next (unique) copy ID.
5198 * Used for serializing nested structures.
5199 */
5200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005201get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005202{
5203 current_copyID += COPYID_INC;
5204 return current_copyID;
5205}
5206
5207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005208 * Garbage collection for lists and dictionaries.
5209 *
5210 * We use reference counts to be able to free most items right away when they
5211 * are no longer used. But for composite items it's possible that it becomes
5212 * unused while the reference count is > 0: When there is a recursive
5213 * reference. Example:
5214 * :let l = [1, 2, 3]
5215 * :let d = {9: l}
5216 * :let l[1] = d
5217 *
5218 * Since this is quite unusual we handle this with garbage collection: every
5219 * once in a while find out which lists and dicts are not referenced from any
5220 * variable.
5221 *
5222 * Here is a good reference text about garbage collection (refers to Python
5223 * but it applies to all reference-counting mechanisms):
5224 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005225 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226
5227/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005229 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005231 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005233garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005234{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005235 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005236 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005237 buf_T *buf;
5238 win_T *wp;
5239 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005240 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005241#ifdef FEAT_WINDOWS
5242 tabpage_T *tp;
5243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005245 if (!testing)
5246 {
5247 /* Only do this once. */
5248 want_garbage_collect = FALSE;
5249 may_garbage_collect = FALSE;
5250 garbage_collect_at_exit = FALSE;
5251 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005252
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005253 /* We advance by two because we add one for items referenced through
5254 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005255 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005256
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 /*
5258 * 1. Go through all accessible variables and mark all lists and dicts
5259 * with copyID.
5260 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005261
5262 /* Don't free variables in the previous_funccal list unless they are only
5263 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005264 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005265 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005266
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005267 /* script-local variables */
5268 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270
5271 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005272 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5274 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275
5276 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005277 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5279 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005280#ifdef FEAT_AUTOCMD
5281 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005282 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5283 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005284#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005285
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005286#ifdef FEAT_WINDOWS
5287 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005288 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005289 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5290 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005291#endif
5292
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005295
5296 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005297 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005299 /* named functions (matters for closures) */
5300 abort = abort || set_ref_in_functions(copyID);
5301
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005302 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005304
Bram Moolenaard812df62008-11-09 12:46:09 +00005305 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005306 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005307
Bram Moolenaar1dced572012-04-05 16:54:08 +02005308#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005309 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005310#endif
5311
Bram Moolenaardb913952012-06-29 12:54:53 +02005312#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005313 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005314#endif
5315
5316#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005317 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005318#endif
5319
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005320#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005321 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005322 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005323#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005324#ifdef FEAT_NETBEANS_INTG
5325 abort = abort || set_ref_in_nb_channel(copyID);
5326#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005327
Bram Moolenaare3188e22016-05-31 21:13:04 +02005328#ifdef FEAT_TIMERS
5329 abort = abort || set_ref_in_timer(copyID);
5330#endif
5331
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005332#ifdef FEAT_QUICKFIX
5333 abort = abort || set_ref_in_quickfix(copyID);
5334#endif
5335
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005336#ifdef FEAT_TERMINAL
5337 abort = abort || set_ref_in_term(copyID);
5338#endif
5339
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005340 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005341 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005342 /*
5343 * 2. Free lists and dictionaries that are not referenced.
5344 */
5345 did_free = free_unref_items(copyID);
5346
5347 /*
5348 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005350 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005351 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005352 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 else if (p_verbose > 0)
5354 {
5355 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5356 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005357
5358 return did_free;
5359}
5360
5361/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005363 */
5364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005365free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005366{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005367 int did_free = FALSE;
5368
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369 /* Let all "free" functions know that we are here. This means no
5370 * dictionaries, lists, channels or jobs are to be freed, because we will
5371 * do that here. */
5372 in_free_unref_items = TRUE;
5373
5374 /*
5375 * PASS 1: free the contents of the items. We don't free the items
5376 * themselves yet, so that it is possible to decrement refcount counters
5377 */
5378
Bram Moolenaarcd524592016-07-17 14:57:05 +02005379 /* Go through the list of dicts and free items without the copyID. */
5380 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381
Bram Moolenaarda861d62016-07-17 15:46:27 +02005382 /* Go through the list of lists and free items without the copyID. */
5383 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384
5385#ifdef FEAT_JOB_CHANNEL
5386 /* Go through the list of jobs and free items without the copyID. This
5387 * must happen before doing channels, because jobs refer to channels, but
5388 * the reference from the channel to the job isn't tracked. */
5389 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5390
5391 /* Go through the list of channels and free items without the copyID. */
5392 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5393#endif
5394
5395 /*
5396 * PASS 2: free the items themselves.
5397 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005398 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005399 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005400
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005401#ifdef FEAT_JOB_CHANNEL
5402 /* Go through the list of jobs and free items without the copyID. This
5403 * must happen before doing channels, because jobs refer to channels, but
5404 * the reference from the channel to the job isn't tracked. */
5405 free_unused_jobs(copyID, COPYID_MASK);
5406
5407 /* Go through the list of channels and free items without the copyID. */
5408 free_unused_channels(copyID, COPYID_MASK);
5409#endif
5410
5411 in_free_unref_items = FALSE;
5412
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005413 return did_free;
5414}
5415
5416/*
5417 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 * "list_stack" is used to add lists to be marked. Can be NULL.
5419 *
5420 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005424{
5425 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005426 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005427 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005428 hashtab_T *cur_ht;
5429 ht_stack_T *ht_stack = NULL;
5430 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005431
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005432 cur_ht = ht;
5433 for (;;)
5434 {
5435 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005436 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005437 /* Mark each item in the hashtab. If the item contains a hashtab
5438 * it is added to ht_stack, if it contains a list it is added to
5439 * list_stack. */
5440 todo = (int)cur_ht->ht_used;
5441 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5442 if (!HASHITEM_EMPTY(hi))
5443 {
5444 --todo;
5445 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5446 &ht_stack, list_stack);
5447 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005448 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005449
5450 if (ht_stack == NULL)
5451 break;
5452
5453 /* take an item from the stack */
5454 cur_ht = ht_stack->ht;
5455 tempitem = ht_stack;
5456 ht_stack = ht_stack->prev;
5457 free(tempitem);
5458 }
5459
5460 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005461}
5462
5463/*
5464 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5466 *
5467 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005468 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005470set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005471{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 listitem_T *li;
5473 int abort = FALSE;
5474 list_T *cur_l;
5475 list_stack_T *list_stack = NULL;
5476 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005477
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005478 cur_l = l;
5479 for (;;)
5480 {
5481 if (!abort)
5482 /* Mark each item in the list. If the item contains a hashtab
5483 * it is added to ht_stack, if it contains a list it is added to
5484 * list_stack. */
5485 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5486 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5487 ht_stack, &list_stack);
5488 if (list_stack == NULL)
5489 break;
5490
5491 /* take an item from the stack */
5492 cur_l = list_stack->list;
5493 tempitem = list_stack;
5494 list_stack = list_stack->prev;
5495 free(tempitem);
5496 }
5497
5498 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005499}
5500
5501/*
5502 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005503 * "list_stack" is used to add lists to be marked. Can be NULL.
5504 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5505 *
5506 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005507 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509set_ref_in_item(
5510 typval_T *tv,
5511 int copyID,
5512 ht_stack_T **ht_stack,
5513 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005514{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005516
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005517 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005518 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005519 dict_T *dd = tv->vval.v_dict;
5520
Bram Moolenaara03f2332016-02-06 18:09:59 +01005521 if (dd != NULL && dd->dv_copyID != copyID)
5522 {
5523 /* Didn't see this dict yet. */
5524 dd->dv_copyID = copyID;
5525 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005526 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005527 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5528 }
5529 else
5530 {
5531 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5532 if (newitem == NULL)
5533 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005534 else
5535 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005536 newitem->ht = &dd->dv_hashtab;
5537 newitem->prev = *ht_stack;
5538 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005539 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005540 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005541 }
5542 }
5543 else if (tv->v_type == VAR_LIST)
5544 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005545 list_T *ll = tv->vval.v_list;
5546
Bram Moolenaara03f2332016-02-06 18:09:59 +01005547 if (ll != NULL && ll->lv_copyID != copyID)
5548 {
5549 /* Didn't see this list yet. */
5550 ll->lv_copyID = copyID;
5551 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005552 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005553 abort = set_ref_in_list(ll, copyID, ht_stack);
5554 }
5555 else
5556 {
5557 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005558 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005559 if (newitem == NULL)
5560 abort = TRUE;
5561 else
5562 {
5563 newitem->list = ll;
5564 newitem->prev = *list_stack;
5565 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005566 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005567 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005568 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005569 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005570 else if (tv->v_type == VAR_FUNC)
5571 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005572 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005573 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005574 else if (tv->v_type == VAR_PARTIAL)
5575 {
5576 partial_T *pt = tv->vval.v_partial;
5577 int i;
5578
5579 /* A partial does not have a copyID, because it cannot contain itself.
5580 */
5581 if (pt != NULL)
5582 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005583 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005584
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005585 if (pt->pt_dict != NULL)
5586 {
5587 typval_T dtv;
5588
5589 dtv.v_type = VAR_DICT;
5590 dtv.vval.v_dict = pt->pt_dict;
5591 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5592 }
5593
5594 for (i = 0; i < pt->pt_argc; ++i)
5595 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5596 ht_stack, list_stack);
5597 }
5598 }
5599#ifdef FEAT_JOB_CHANNEL
5600 else if (tv->v_type == VAR_JOB)
5601 {
5602 job_T *job = tv->vval.v_job;
5603 typval_T dtv;
5604
5605 if (job != NULL && job->jv_copyID != copyID)
5606 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005607 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005608 if (job->jv_channel != NULL)
5609 {
5610 dtv.v_type = VAR_CHANNEL;
5611 dtv.vval.v_channel = job->jv_channel;
5612 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5613 }
5614 if (job->jv_exit_partial != NULL)
5615 {
5616 dtv.v_type = VAR_PARTIAL;
5617 dtv.vval.v_partial = job->jv_exit_partial;
5618 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5619 }
5620 }
5621 }
5622 else if (tv->v_type == VAR_CHANNEL)
5623 {
5624 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005625 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005626 typval_T dtv;
5627 jsonq_T *jq;
5628 cbq_T *cq;
5629
5630 if (ch != NULL && ch->ch_copyID != copyID)
5631 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005632 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005633 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005634 {
5635 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5636 jq = jq->jq_next)
5637 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5638 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5639 cq = cq->cq_next)
5640 if (cq->cq_partial != NULL)
5641 {
5642 dtv.v_type = VAR_PARTIAL;
5643 dtv.vval.v_partial = cq->cq_partial;
5644 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5645 }
5646 if (ch->ch_part[part].ch_partial != NULL)
5647 {
5648 dtv.v_type = VAR_PARTIAL;
5649 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5650 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5651 }
5652 }
5653 if (ch->ch_partial != NULL)
5654 {
5655 dtv.v_type = VAR_PARTIAL;
5656 dtv.vval.v_partial = ch->ch_partial;
5657 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5658 }
5659 if (ch->ch_close_partial != NULL)
5660 {
5661 dtv.v_type = VAR_PARTIAL;
5662 dtv.vval.v_partial = ch->ch_close_partial;
5663 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5664 }
5665 }
5666 }
5667#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005668 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005669}
5670
Bram Moolenaar17a13432016-01-24 14:22:10 +01005671 static char *
5672get_var_special_name(int nr)
5673{
5674 switch (nr)
5675 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005676 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005677 case VVAL_TRUE: return "v:true";
5678 case VVAL_NONE: return "v:none";
5679 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005680 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005681 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005682 return "42";
5683}
5684
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 * Return a string with the string representation of a variable.
5687 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005688 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005689 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005690 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5691 * stings as "string()", otherwise does not put quotes around strings, as
5692 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005693 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5694 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005695 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005697 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005698echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005699 typval_T *tv,
5700 char_u **tofree,
5701 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005702 int copyID,
5703 int echo_style,
5704 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005705 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005707 static int recurse = 0;
5708 char_u *r = NULL;
5709
Bram Moolenaar33570922005-01-25 22:26:29 +00005710 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005711 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005712 if (!did_echo_string_emsg)
5713 {
5714 /* Only give this message once for a recursive call to avoid
5715 * flooding the user with errors. And stop iterating over lists
5716 * and dicts. */
5717 did_echo_string_emsg = TRUE;
5718 EMSG(_("E724: variable nested too deep for displaying"));
5719 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005720 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005721 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005722 }
5723 ++recurse;
5724
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 switch (tv->v_type)
5726 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005727 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005728 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005729 {
5730 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005731 r = tv->vval.v_string;
5732 if (r == NULL)
5733 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005734 }
5735 else
5736 {
5737 *tofree = string_quote(tv->vval.v_string, FALSE);
5738 r = *tofree;
5739 }
5740 break;
5741
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005743 if (echo_style)
5744 {
5745 *tofree = NULL;
5746 r = tv->vval.v_string;
5747 }
5748 else
5749 {
5750 *tofree = string_quote(tv->vval.v_string, TRUE);
5751 r = *tofree;
5752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005753 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005754
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005755 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005756 {
5757 partial_T *pt = tv->vval.v_partial;
5758 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005759 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005760 garray_T ga;
5761 int i;
5762 char_u *tf;
5763
5764 ga_init2(&ga, 1, 100);
5765 ga_concat(&ga, (char_u *)"function(");
5766 if (fname != NULL)
5767 {
5768 ga_concat(&ga, fname);
5769 vim_free(fname);
5770 }
5771 if (pt != NULL && pt->pt_argc > 0)
5772 {
5773 ga_concat(&ga, (char_u *)", [");
5774 for (i = 0; i < pt->pt_argc; ++i)
5775 {
5776 if (i > 0)
5777 ga_concat(&ga, (char_u *)", ");
5778 ga_concat(&ga,
5779 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5780 vim_free(tf);
5781 }
5782 ga_concat(&ga, (char_u *)"]");
5783 }
5784 if (pt != NULL && pt->pt_dict != NULL)
5785 {
5786 typval_T dtv;
5787
5788 ga_concat(&ga, (char_u *)", ");
5789 dtv.v_type = VAR_DICT;
5790 dtv.vval.v_dict = pt->pt_dict;
5791 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5792 vim_free(tf);
5793 }
5794 ga_concat(&ga, (char_u *)")");
5795
5796 *tofree = ga.ga_data;
5797 r = *tofree;
5798 break;
5799 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005800
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005802 if (tv->vval.v_list == NULL)
5803 {
5804 *tofree = NULL;
5805 r = NULL;
5806 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005807 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5808 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005809 {
5810 *tofree = NULL;
5811 r = (char_u *)"[...]";
5812 }
5813 else
5814 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005815 int old_copyID = tv->vval.v_list->lv_copyID;
5816
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005817 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005818 *tofree = list2string(tv, copyID, restore_copyID);
5819 if (restore_copyID)
5820 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005821 r = *tofree;
5822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005823 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005824
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005826 if (tv->vval.v_dict == NULL)
5827 {
5828 *tofree = NULL;
5829 r = NULL;
5830 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005831 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5832 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005833 {
5834 *tofree = NULL;
5835 r = (char_u *)"{...}";
5836 }
5837 else
5838 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005839 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005840 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005841 *tofree = dict2string(tv, copyID, restore_copyID);
5842 if (restore_copyID)
5843 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005844 r = *tofree;
5845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005846 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005848 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005849 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005850 *tofree = NULL;
5851 r = get_tv_string_buf(tv, numbuf);
5852 break;
5853
Bram Moolenaar835dc632016-02-07 14:27:38 +01005854 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005855 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005856 *tofree = NULL;
5857 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005858 if (composite_val)
5859 {
5860 *tofree = string_quote(r, FALSE);
5861 r = *tofree;
5862 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005865 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005866#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005867 *tofree = NULL;
5868 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5869 r = numbuf;
5870 break;
5871#endif
5872
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005873 case VAR_SPECIAL:
5874 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005875 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005876 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005878
Bram Moolenaar8502c702014-06-17 12:51:16 +02005879 if (--recurse == 0)
5880 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005881 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882}
5883
5884/*
5885 * Return a string with the string representation of a variable.
5886 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5887 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 * Does not put quotes around strings, as ":echo" displays values.
5889 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5890 * May return NULL.
5891 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005892 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005893echo_string(
5894 typval_T *tv,
5895 char_u **tofree,
5896 char_u *numbuf,
5897 int copyID)
5898{
5899 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5900}
5901
5902/*
5903 * Return a string with the string representation of a variable.
5904 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5905 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005907 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910tv2string(
5911 typval_T *tv,
5912 char_u **tofree,
5913 char_u *numbuf,
5914 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005916 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917}
5918
5919/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 * Return string "str" in ' quotes, doubling ' characters.
5921 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005924 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926{
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 char_u *p, *r, *s;
5929
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 len = (function ? 13 : 3);
5931 if (str != NULL)
5932 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005933 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005934 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005935 if (*p == '\'')
5936 ++len;
5937 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 s = r = alloc(len);
5939 if (r != NULL)
5940 {
5941 if (function)
5942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 r += 10;
5945 }
5946 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 if (str != NULL)
5949 for (p = str; *p != NUL; )
5950 {
5951 if (*p == '\'')
5952 *r++ = '\'';
5953 MB_COPY_CHAR(p, r);
5954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956 if (function)
5957 *r++ = ')';
5958 *r++ = NUL;
5959 }
5960 return s;
5961}
5962
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005963#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005964/*
5965 * Convert the string "text" to a floating point number.
5966 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5967 * this always uses a decimal point.
5968 * Returns the length of the text that was consumed.
5969 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971string2float(
5972 char_u *text,
5973 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005974{
5975 char *s = (char *)text;
5976 float_T f;
5977
Bram Moolenaar62473612017-01-08 19:25:40 +01005978 /* MS-Windows does not deal with "inf" and "nan" properly. */
5979 if (STRNICMP(text, "inf", 3) == 0)
5980 {
5981 *value = INFINITY;
5982 return 3;
5983 }
5984 if (STRNICMP(text, "-inf", 3) == 0)
5985 {
5986 *value = -INFINITY;
5987 return 4;
5988 }
5989 if (STRNICMP(text, "nan", 3) == 0)
5990 {
5991 *value = NAN;
5992 return 3;
5993 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005994 f = strtod(s, &s);
5995 *value = f;
5996 return (int)((char_u *)s - text);
5997}
5998#endif
5999
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 * Get the value of an environment variable.
6002 * "arg" is pointing to the '$'. It is advanced to after the name.
6003 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006004 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 */
6006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 char_u *string = NULL;
6010 int len;
6011 int cc;
6012 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006013 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014
6015 ++*arg;
6016 name = *arg;
6017 len = get_env_len(arg);
6018 if (evaluate)
6019 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006020 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006021 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006022
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006023 cc = name[len];
6024 name[len] = NUL;
6025 /* first try vim_getenv(), fast for normal environment vars */
6026 string = vim_getenv(name, &mustfree);
6027 if (string != NULL && *string != NUL)
6028 {
6029 if (!mustfree)
6030 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006032 else
6033 {
6034 if (mustfree)
6035 vim_free(string);
6036
6037 /* next try expanding things like $VIM and ${HOME} */
6038 string = expand_env_save(name - 1);
6039 if (string != NULL && *string == '$')
6040 {
6041 vim_free(string);
6042 string = NULL;
6043 }
6044 }
6045 name[len] = cc;
6046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006047 rettv->v_type = VAR_STRING;
6048 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050
6051 return OK;
6052}
6053
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006054
6055
6056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006058 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061var2fpos(
6062 typval_T *varp,
6063 int dollar_lnum, /* TRUE when $ is last line */
6064 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006066 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006068 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
Bram Moolenaara5525202006-03-02 22:52:09 +00006070 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006071 if (varp->v_type == VAR_LIST)
6072 {
6073 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006074 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006075 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006076 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006077
6078 l = varp->vval.v_list;
6079 if (l == NULL)
6080 return NULL;
6081
6082 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006083 pos.lnum = list_find_nr(l, 0L, &error);
6084 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006085 return NULL; /* invalid line number */
6086
6087 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006088 pos.col = list_find_nr(l, 1L, &error);
6089 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006090 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006091 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006092
6093 /* We accept "$" for the column number: last column. */
6094 li = list_find(l, 1L);
6095 if (li != NULL && li->li_tv.v_type == VAR_STRING
6096 && li->li_tv.vval.v_string != NULL
6097 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6098 pos.col = len + 1;
6099
Bram Moolenaara5525202006-03-02 22:52:09 +00006100 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006101 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006102 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006103 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006104
Bram Moolenaara5525202006-03-02 22:52:09 +00006105#ifdef FEAT_VIRTUALEDIT
6106 /* Get the virtual offset. Defaults to zero. */
6107 pos.coladd = list_find_nr(l, 2L, &error);
6108 if (error)
6109 pos.coladd = 0;
6110#endif
6111
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006112 return &pos;
6113 }
6114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006115 name = get_tv_string_chk(varp);
6116 if (name == NULL)
6117 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006118 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006120 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6121 {
6122 if (VIsual_active)
6123 return &VIsual;
6124 return &curwin->w_cursor;
6125 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006126 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006128 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6130 return NULL;
6131 return pp;
6132 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006133
6134#ifdef FEAT_VIRTUALEDIT
6135 pos.coladd = 0;
6136#endif
6137
Bram Moolenaar477933c2007-07-17 14:32:23 +00006138 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006139 {
6140 pos.col = 0;
6141 if (name[1] == '0') /* "w0": first visible line */
6142 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006143 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006144 /* In silent Ex mode topline is zero, but that's not a valid line
6145 * number; use one instead. */
6146 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006147 return &pos;
6148 }
6149 else if (name[1] == '$') /* "w$": last visible line */
6150 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006151 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006152 /* In silent Ex mode botline is zero, return zero then. */
6153 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006154 return &pos;
6155 }
6156 }
6157 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006159 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
6161 pos.lnum = curbuf->b_ml.ml_line_count;
6162 pos.col = 0;
6163 }
6164 else
6165 {
6166 pos.lnum = curwin->w_cursor.lnum;
6167 pos.col = (colnr_T)STRLEN(ml_get_curline());
6168 }
6169 return &pos;
6170 }
6171 return NULL;
6172}
6173
6174/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006175 * Convert list in "arg" into a position and optional file number.
6176 * When "fnump" is NULL there is no file number, only 3 items.
6177 * Note that the column is passed on as-is, the caller may want to decrement
6178 * it to use 1 for the first column.
6179 * Return FAIL when conversion is not possible, doesn't check the position for
6180 * validity.
6181 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006183list2fpos(
6184 typval_T *arg,
6185 pos_T *posp,
6186 int *fnump,
6187 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006188{
6189 list_T *l = arg->vval.v_list;
6190 long i = 0;
6191 long n;
6192
Bram Moolenaar493c1782014-05-28 14:34:46 +02006193 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6194 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006195 if (arg->v_type != VAR_LIST
6196 || l == NULL
6197 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006198 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006199 return FAIL;
6200
6201 if (fnump != NULL)
6202 {
6203 n = list_find_nr(l, i++, NULL); /* fnum */
6204 if (n < 0)
6205 return FAIL;
6206 if (n == 0)
6207 n = curbuf->b_fnum; /* current buffer */
6208 *fnump = n;
6209 }
6210
6211 n = list_find_nr(l, i++, NULL); /* lnum */
6212 if (n < 0)
6213 return FAIL;
6214 posp->lnum = n;
6215
6216 n = list_find_nr(l, i++, NULL); /* col */
6217 if (n < 0)
6218 return FAIL;
6219 posp->col = n;
6220
6221#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006222 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006223 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006224 posp->coladd = 0;
6225 else
6226 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006227#endif
6228
Bram Moolenaar493c1782014-05-28 14:34:46 +02006229 if (curswantp != NULL)
6230 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6231
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006232 return OK;
6233}
6234
6235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 * Get the length of an environment variable name.
6237 * Advance "arg" to the first character after the name.
6238 * Return 0 for error.
6239 */
6240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006241get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 char_u *p;
6244 int len;
6245
6246 for (p = *arg; vim_isIDc(*p); ++p)
6247 ;
6248 if (p == *arg) /* no name found */
6249 return 0;
6250
6251 len = (int)(p - *arg);
6252 *arg = p;
6253 return len;
6254}
6255
6256/*
6257 * Get the length of the name of a function or internal variable.
6258 * "arg" is advanced to the first non-white character after the name.
6259 * Return 0 if something is wrong.
6260 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006262get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 char_u *p;
6265 int len;
6266
6267 /* Find the end of the name. */
6268 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006269 {
6270 if (*p == ':')
6271 {
6272 /* "s:" is start of "s:var", but "n:" is not and can be used in
6273 * slice "[n:]". Also "xx:" is not a namespace. */
6274 len = (int)(p - *arg);
6275 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6276 || len > 1)
6277 break;
6278 }
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (p == *arg) /* no name found */
6281 return 0;
6282
6283 len = (int)(p - *arg);
6284 *arg = skipwhite(p);
6285
6286 return len;
6287}
6288
6289/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006290 * Get the length of the name of a variable or function.
6291 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006293 * Return -1 if curly braces expansion failed.
6294 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * If the name contains 'magic' {}'s, expand them and return the
6296 * expanded name in an allocated string via 'alias' - caller must free.
6297 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006299get_name_len(
6300 char_u **arg,
6301 char_u **alias,
6302 int evaluate,
6303 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 char_u *p;
6307 char_u *expr_start;
6308 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 *alias = NULL; /* default to no alias */
6311
6312 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6313 && (*arg)[2] == (int)KE_SNR)
6314 {
6315 /* hard coded <SNR>, already translated */
6316 *arg += 3;
6317 return get_id_len(arg) + 3;
6318 }
6319 len = eval_fname_script(*arg);
6320 if (len > 0)
6321 {
6322 /* literal "<SID>", "s:" or "<SNR>" */
6323 *arg += len;
6324 }
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006327 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006329 p = find_name_end(*arg, &expr_start, &expr_end,
6330 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 if (expr_start != NULL)
6332 {
6333 char_u *temp_string;
6334
6335 if (!evaluate)
6336 {
6337 len += (int)(p - *arg);
6338 *arg = skipwhite(p);
6339 return len;
6340 }
6341
6342 /*
6343 * Include any <SID> etc in the expanded string:
6344 * Thus the -len here.
6345 */
6346 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6347 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006348 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 *alias = temp_string;
6350 *arg = skipwhite(p);
6351 return (int)STRLEN(temp_string);
6352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006355 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 EMSG2(_(e_invexpr2), *arg);
6357
6358 return len;
6359}
6360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006361/*
6362 * Find the end of a variable or function name, taking care of magic braces.
6363 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6364 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006365 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006366 * Return a pointer to just after the name. Equal to "arg" if there is no
6367 * valid name.
6368 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006369 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370find_name_end(
6371 char_u *arg,
6372 char_u **expr_start,
6373 char_u **expr_end,
6374 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 int mb_nest = 0;
6377 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006379 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006381 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006383 *expr_start = NULL;
6384 *expr_end = NULL;
6385 }
6386
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006387 /* Quick check for valid starting character. */
6388 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6389 return arg;
6390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 for (p = arg; *p != NUL
6392 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006394 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006395 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006396 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006397 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006398 if (*p == '\'')
6399 {
6400 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006401 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006402 ;
6403 if (*p == NUL)
6404 break;
6405 }
6406 else if (*p == '"')
6407 {
6408 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006409 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006410 if (*p == '\\' && p[1] != NUL)
6411 ++p;
6412 if (*p == NUL)
6413 break;
6414 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006415 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6416 {
6417 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006418 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006419 len = (int)(p - arg);
6420 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006421 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006422 break;
6423 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427 if (*p == '[')
6428 ++br_nest;
6429 else if (*p == ']')
6430 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006433 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 if (*p == '{')
6436 {
6437 mb_nest++;
6438 if (expr_start != NULL && *expr_start == NULL)
6439 *expr_start = p;
6440 }
6441 else if (*p == '}')
6442 {
6443 mb_nest--;
6444 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6445 *expr_end = p;
6446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449
6450 return p;
6451}
6452
6453/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006454 * Expands out the 'magic' {}'s in a variable/function name.
6455 * Note that this can call itself recursively, to deal with
6456 * constructs like foo{bar}{baz}{bam}
6457 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6458 * "in_start" ^
6459 * "expr_start" ^
6460 * "expr_end" ^
6461 * "in_end" ^
6462 *
6463 * Returns a new allocated string, which the caller must free.
6464 * Returns NULL for failure.
6465 */
6466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467make_expanded_name(
6468 char_u *in_start,
6469 char_u *expr_start,
6470 char_u *expr_end,
6471 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006472{
6473 char_u c1;
6474 char_u *retval = NULL;
6475 char_u *temp_result;
6476 char_u *nextcmd = NULL;
6477
6478 if (expr_end == NULL || in_end == NULL)
6479 return NULL;
6480 *expr_start = NUL;
6481 *expr_end = NUL;
6482 c1 = *in_end;
6483 *in_end = NUL;
6484
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006485 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006486 if (temp_result != NULL && nextcmd == NULL)
6487 {
6488 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6489 + (in_end - expr_end) + 1));
6490 if (retval != NULL)
6491 {
6492 STRCPY(retval, in_start);
6493 STRCAT(retval, temp_result);
6494 STRCAT(retval, expr_end + 1);
6495 }
6496 }
6497 vim_free(temp_result);
6498
6499 *in_end = c1; /* put char back for error messages */
6500 *expr_start = '{';
6501 *expr_end = '}';
6502
6503 if (retval != NULL)
6504 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006505 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006506 if (expr_start != NULL)
6507 {
6508 /* Further expansion! */
6509 temp_result = make_expanded_name(retval, expr_start,
6510 expr_end, temp_result);
6511 vim_free(retval);
6512 retval = temp_result;
6513 }
6514 }
6515
6516 return retval;
6517}
6518
6519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006524eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006526 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6527}
6528
6529/*
6530 * Return TRUE if character "c" can be used as the first character in a
6531 * variable or function name (excluding '{' and '}').
6532 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006535{
6536 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537}
6538
6539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 * Set number v: variable to "val".
6541 */
6542 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006543set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546}
6547
6548/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006549 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006551 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555}
6556
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006557/*
6558 * Get string v: variable value. Uses a static buffer, can only be used once.
6559 */
6560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006561get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006562{
6563 return get_tv_string(&vimvars[idx].vv_tv);
6564}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006565
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006567 * Get List v: variable value. Caller must take care of reference count when
6568 * needed.
6569 */
6570 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006571get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006572{
6573 return vimvars[idx].vv_list;
6574}
6575
6576/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006577 * Set v:char to character "c".
6578 */
6579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006581{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006582 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006583
6584#ifdef FEAT_MBYTE
6585 if (has_mbyte)
6586 buf[(*mb_char2bytes)(c, buf)] = NUL;
6587 else
6588#endif
6589 {
6590 buf[0] = c;
6591 buf[1] = NUL;
6592 }
6593 set_vim_var_string(VV_CHAR, buf, -1);
6594}
6595
6596/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006597 * Set v:count to "count" and v:count1 to "count1".
6598 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 */
6600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601set_vcount(
6602 long count,
6603 long count1,
6604 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006606 if (set_prevcount)
6607 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 vimvars[VV_COUNT].vv_nr = count;
6609 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610}
6611
6612/*
6613 * Set string v: variable to a copy of "val".
6614 */
6615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616set_vim_var_string(
6617 int idx,
6618 char_u *val,
6619 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
Bram Moolenaara542c682016-01-31 16:28:04 +01006621 clear_tv(&vimvars[idx].vv_di.di_tv);
6622 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006624 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006626 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630
6631/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006632 * Set List v: variable to "val".
6633 */
6634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006636{
Bram Moolenaara542c682016-01-31 16:28:04 +01006637 clear_tv(&vimvars[idx].vv_di.di_tv);
6638 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006639 vimvars[idx].vv_list = val;
6640 if (val != NULL)
6641 ++val->lv_refcount;
6642}
6643
6644/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006645 * Set Dictionary v: variable to "val".
6646 */
6647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006648set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006649{
6650 int todo;
6651 hashitem_T *hi;
6652
Bram Moolenaara542c682016-01-31 16:28:04 +01006653 clear_tv(&vimvars[idx].vv_di.di_tv);
6654 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006655 vimvars[idx].vv_dict = val;
6656 if (val != NULL)
6657 {
6658 ++val->dv_refcount;
6659
6660 /* Set readonly */
6661 todo = (int)val->dv_hashtab.ht_used;
6662 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6663 {
6664 if (HASHITEM_EMPTY(hi))
6665 continue;
6666 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006667 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006668 }
6669 }
6670}
6671
6672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 * Set v:register if needed.
6674 */
6675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 char_u regname;
6679
6680 if (c == 0 || c == ' ')
6681 regname = '"';
6682 else
6683 regname = c;
6684 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 set_vim_var_string(VV_REG, &regname, 1);
6687}
6688
6689/*
6690 * Get or set v:exception. If "oldval" == NULL, return the current value.
6691 * Otherwise, restore the value to "oldval" and return NULL.
6692 * Must always be called in pairs to save and restore v:exception! Does not
6693 * take care of memory allocations.
6694 */
6695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006696v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 return NULL;
6703}
6704
6705/*
6706 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6707 * Otherwise, restore the value to "oldval" and return NULL.
6708 * Must always be called in pairs to save and restore v:throwpoint! Does not
6709 * take care of memory allocations.
6710 */
6711 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006715 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
Bram Moolenaare9a41262005-01-15 22:18:47 +00006717 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 return NULL;
6719}
6720
6721#if defined(FEAT_AUTOCMD) || defined(PROTO)
6722/*
6723 * Set v:cmdarg.
6724 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6725 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6726 * Must always be called in pairs!
6727 */
6728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006729set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 char_u *oldval;
6732 char_u *newval;
6733 unsigned len;
6734
Bram Moolenaare9a41262005-01-15 22:18:47 +00006735 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006736 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006738 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006740 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006743 if (eap->force_bin == FORCE_BIN)
6744 len = 6;
6745 else if (eap->force_bin == FORCE_NOBIN)
6746 len = 8;
6747 else
6748 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006749
6750 if (eap->read_edit)
6751 len += 7;
6752
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006753 if (eap->force_ff != 0)
6754 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6755# ifdef FEAT_MBYTE
6756 if (eap->force_enc != 0)
6757 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006758 if (eap->bad_char != 0)
6759 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006760# endif
6761
6762 newval = alloc(len + 1);
6763 if (newval == NULL)
6764 return NULL;
6765
6766 if (eap->force_bin == FORCE_BIN)
6767 sprintf((char *)newval, " ++bin");
6768 else if (eap->force_bin == FORCE_NOBIN)
6769 sprintf((char *)newval, " ++nobin");
6770 else
6771 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006772
6773 if (eap->read_edit)
6774 STRCAT(newval, " ++edit");
6775
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006776 if (eap->force_ff != 0)
6777 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6778 eap->cmd + eap->force_ff);
6779# ifdef FEAT_MBYTE
6780 if (eap->force_enc != 0)
6781 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6782 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006783 if (eap->bad_char == BAD_KEEP)
6784 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6785 else if (eap->bad_char == BAD_DROP)
6786 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6787 else if (eap->bad_char != 0)
6788 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006789# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006790 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006791 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792}
6793#endif
6794
6795/*
6796 * Get the value of internal variable "name".
6797 * Return OK or FAIL.
6798 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800get_var_tv(
6801 char_u *name,
6802 int len, /* length of "name" */
6803 typval_T *rettv, /* NULL when only checking existence */
6804 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6805 int verbose, /* may give error message */
6806 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813 /* truncate the name, so that we can use strcmp() */
6814 cc = name[len];
6815 name[len] = NUL;
6816
6817 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * Check for user-defined variables.
6819 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006820 v = find_var(name, NULL, no_autoload);
6821 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006823 tv = &v->di_tv;
6824 if (dip != NULL)
6825 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827
Bram Moolenaare9a41262005-01-15 22:18:47 +00006828 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006830 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006831 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 ret = FAIL;
6833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
6837 name[len] = cc;
6838
6839 return ret;
6840}
6841
6842/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006843 * Check if variable "name[len]" is a local variable or an argument.
6844 * If so, "*eval_lavars_used" is set to TRUE.
6845 */
6846 static void
6847check_vars(char_u *name, int len)
6848{
6849 int cc;
6850 char_u *varname;
6851 hashtab_T *ht;
6852
6853 if (eval_lavars_used == NULL)
6854 return;
6855
6856 /* truncate the name, so that we can use strcmp() */
6857 cc = name[len];
6858 name[len] = NUL;
6859
6860 ht = find_var_ht(name, &varname);
6861 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6862 {
6863 if (find_var(name, NULL, TRUE) != NULL)
6864 *eval_lavars_used = TRUE;
6865 }
6866
6867 name[len] = cc;
6868}
6869
6870/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006871 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6872 * Also handle function call with Funcref variable: func(expr)
6873 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6874 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006876handle_subscript(
6877 char_u **arg,
6878 typval_T *rettv,
6879 int evaluate, /* do more than finding the end */
6880 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006881{
6882 int ret = OK;
6883 dict_T *selfdict = NULL;
6884 char_u *s;
6885 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006886 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006887
6888 while (ret == OK
6889 && (**arg == '['
6890 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006891 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6892 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006893 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006894 {
6895 if (**arg == '(')
6896 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006897 partial_T *pt = NULL;
6898
Bram Moolenaard9fba312005-06-26 22:34:35 +00006899 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006900 if (evaluate)
6901 {
6902 functv = *rettv;
6903 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006904
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006905 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006906 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006907 {
6908 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006909 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006910 }
6911 else
6912 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006913 }
6914 else
6915 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006916 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006918 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006919
6920 /* Clear the funcref afterwards, so that deleting it while
6921 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006922 if (evaluate)
6923 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006924
6925 /* Stop the expression evaluation when immediately aborting on
6926 * error, or when an interrupt occurred or an exception was thrown
6927 * but not caught. */
6928 if (aborting())
6929 {
6930 if (ret == OK)
6931 clear_tv(rettv);
6932 ret = FAIL;
6933 }
6934 dict_unref(selfdict);
6935 selfdict = NULL;
6936 }
6937 else /* **arg == '[' || **arg == '.' */
6938 {
6939 dict_unref(selfdict);
6940 if (rettv->v_type == VAR_DICT)
6941 {
6942 selfdict = rettv->vval.v_dict;
6943 if (selfdict != NULL)
6944 ++selfdict->dv_refcount;
6945 }
6946 else
6947 selfdict = NULL;
6948 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6949 {
6950 clear_tv(rettv);
6951 ret = FAIL;
6952 }
6953 }
6954 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006955
Bram Moolenaar1d429612016-05-24 15:44:17 +02006956 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6957 * Don't do this when "Func" is already a partial that was bound
6958 * explicitly (pt_auto is FALSE). */
6959 if (selfdict != NULL
6960 && (rettv->v_type == VAR_FUNC
6961 || (rettv->v_type == VAR_PARTIAL
6962 && (rettv->vval.v_partial->pt_auto
6963 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006964 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006965
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006966 dict_unref(selfdict);
6967 return ret;
6968}
6969
6970/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006971 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006972 * value).
6973 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006974 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006975alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976{
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978}
6979
6980/*
6981 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 * The string "s" must have been allocated, it is consumed.
6983 * Return NULL for out of memory, the variable otherwise.
6984 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
Bram Moolenaar33570922005-01-25 22:26:29 +00006988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990 rettv = alloc_tv();
6991 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006993 rettv->v_type = VAR_STRING;
6994 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 else
6997 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006998 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999}
7000
7001/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007002 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 if (varp != NULL)
7008 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007009 switch (varp->v_type)
7010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007012 func_unref(varp->vval.v_string);
7013 /*FALLTHROUGH*/
7014 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007015 vim_free(varp->vval.v_string);
7016 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007017 case VAR_PARTIAL:
7018 partial_unref(varp->vval.v_partial);
7019 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007020 case VAR_LIST:
7021 list_unref(varp->vval.v_list);
7022 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007023 case VAR_DICT:
7024 dict_unref(varp->vval.v_dict);
7025 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007026 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007027#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007028 job_unref(varp->vval.v_job);
7029 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007030#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007031 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007032#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007033 channel_unref(varp->vval.v_channel);
7034 break;
7035#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007036 case VAR_NUMBER:
7037 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007038 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007039 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007040 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 vim_free(varp);
7043 }
7044}
7045
7046/*
7047 * Free the memory for a variable value and set the value to NULL or 0.
7048 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007050clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 if (varp != NULL)
7053 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007054 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007056 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007057 func_unref(varp->vval.v_string);
7058 /*FALLTHROUGH*/
7059 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007060 vim_free(varp->vval.v_string);
7061 varp->vval.v_string = NULL;
7062 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007063 case VAR_PARTIAL:
7064 partial_unref(varp->vval.v_partial);
7065 varp->vval.v_partial = NULL;
7066 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007067 case VAR_LIST:
7068 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007070 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071 case VAR_DICT:
7072 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007076 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007077 varp->vval.v_number = 0;
7078 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007079 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007080#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007081 varp->vval.v_float = 0.0;
7082 break;
7083#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007084 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007085#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007086 job_unref(varp->vval.v_job);
7087 varp->vval.v_job = NULL;
7088#endif
7089 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007090 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007091#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007092 channel_unref(varp->vval.v_channel);
7093 varp->vval.v_channel = NULL;
7094#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007095 case VAR_UNKNOWN:
7096 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007098 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100}
7101
7102/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007103 * Set the value of a variable to NULL without freeing items.
7104 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007107{
7108 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007110}
7111
7112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 * Get the number value of a variable.
7114 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007115 * For incompatible types, return 0.
7116 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7117 * caller of incompatible types: it sets *denote to TRUE if "denote"
7118 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007120 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007121get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 int error = FALSE;
7124
7125 return get_tv_number_chk(varp, &error); /* return 0L on error */
7126}
7127
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007128 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007129get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007130{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007131 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007133 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007135 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007136 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007137 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007138#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007139 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007140 break;
7141#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007142 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007143 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007144 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007145 break;
7146 case VAR_STRING:
7147 if (varp->vval.v_string != NULL)
7148 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007149 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007151 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007152 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007153 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007154 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007155 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007156 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007157 case VAR_SPECIAL:
7158 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7159 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007160 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007161#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007162 EMSG(_("E910: Using a Job as a Number"));
7163 break;
7164#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007165 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007166#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007167 EMSG(_("E913: Using a Channel as a Number"));
7168 break;
7169#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007171 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 if (denote == NULL) /* useful for values that must be unsigned */
7175 n = -1;
7176 else
7177 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007178 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007181#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007182 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007183get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007184{
7185 switch (varp->v_type)
7186 {
7187 case VAR_NUMBER:
7188 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007189 case VAR_FLOAT:
7190 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007191 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007192 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007193 EMSG(_("E891: Using a Funcref as a Float"));
7194 break;
7195 case VAR_STRING:
7196 EMSG(_("E892: Using a String as a Float"));
7197 break;
7198 case VAR_LIST:
7199 EMSG(_("E893: Using a List as a Float"));
7200 break;
7201 case VAR_DICT:
7202 EMSG(_("E894: Using a Dictionary as a Float"));
7203 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007204 case VAR_SPECIAL:
7205 EMSG(_("E907: Using a special value as a Float"));
7206 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007207 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007208# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007209 EMSG(_("E911: Using a Job as a Float"));
7210 break;
7211# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007212 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007213# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007214 EMSG(_("E914: Using a Channel as a Float"));
7215 break;
7216# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007217 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007218 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007219 break;
7220 }
7221 return 0;
7222}
7223#endif
7224
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 * Get the string value of a variable.
7227 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007228 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7229 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 * If the String variable has never been set, return an empty string.
7231 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007232 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7233 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007235 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 static char_u mybuf[NUMBUFLEN];
7239
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007240 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007243 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007246 char_u *res = get_tv_string_buf_chk(varp, buf);
7247
7248 return res != NULL ? res : (char_u *)"";
7249}
7250
Bram Moolenaar7d647822014-04-05 21:28:56 +02007251/*
7252 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7253 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007254 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007255get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256{
7257 static char_u mybuf[NUMBUFLEN];
7258
7259 return get_tv_string_buf_chk(varp, mybuf);
7260}
7261
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007262 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007263get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007264{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007265 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007267 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007268 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7269 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007270 return buf;
7271 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007272 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007273 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 break;
7275 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007276 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 break;
7278 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007279 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007281 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007282#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007283 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007284 break;
7285#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007286 case VAR_STRING:
7287 if (varp->vval.v_string != NULL)
7288 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007289 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007290 case VAR_SPECIAL:
7291 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7292 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007293 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007294#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007295 {
7296 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007297 char *status;
7298
7299 if (job == NULL)
7300 return (char_u *)"no process";
7301 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007302 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007303 : "run";
7304# ifdef UNIX
7305 vim_snprintf((char *)buf, NUMBUFLEN,
7306 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007307# elif defined(WIN32)
7308 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007309 "process %ld %s",
7310 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007311 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007312# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007313 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007314 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7315# endif
7316 return buf;
7317 }
7318#endif
7319 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007320 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007322 {
7323 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007324 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007325
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007326 if (channel == NULL)
7327 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7328 else
7329 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007330 "channel %d %s", channel->ch_id, status);
7331 return buf;
7332 }
7333#endif
7334 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007335 case VAR_UNKNOWN:
7336 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007339 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
7342/*
7343 * Find variable "name" in the list of variables.
7344 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007346 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007349 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007350find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007354 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
Bram Moolenaara7043832005-01-21 11:56:39 +00007356 ht = find_var_ht(name, &varname);
7357 if (htp != NULL)
7358 *htp = ht;
7359 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007361 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7362 if (ret != NULL)
7363 return ret;
7364
7365 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007366 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
7369/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007370 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007371 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007374find_var_in_ht(
7375 hashtab_T *ht,
7376 int htname,
7377 char_u *varname,
7378 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 hashitem_T *hi;
7381
7382 if (*varname == NUL)
7383 {
7384 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007385 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007387 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 case 'g': return &globvars_var;
7389 case 'v': return &vimvars_var;
7390 case 'b': return &curbuf->b_bufvar;
7391 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007392#ifdef FEAT_WINDOWS
7393 case 't': return &curtab->tp_winvar;
7394#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007395 case 'l': return get_funccal_local_var();
7396 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 }
7398 return NULL;
7399 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007400
7401 hi = hash_find(ht, varname);
7402 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007403 {
7404 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007405 * worked find the variable again. Don't auto-load a script if it was
7406 * loaded already, otherwise it would be loaded every time when
7407 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007408 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007409 {
7410 /* Note: script_autoload() may make "hi" invalid. It must either
7411 * be obtained again or not used. */
7412 if (!script_autoload(varname, FALSE) || aborting())
7413 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007414 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007415 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007416 if (HASHITEM_EMPTY(hi))
7417 return NULL;
7418 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007420}
7421
7422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007424 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007425 * Set "varname" to the start of name without ':'.
7426 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007427 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007430 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007431 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007432
Bram Moolenaar73627d02015-08-11 15:46:09 +02007433 if (name[0] == NUL)
7434 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (name[1] != ':')
7436 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007437 /* The name must not start with a colon or #. */
7438 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 return NULL;
7440 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007441
7442 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007443 hi = hash_find(&compat_hashtab, name);
7444 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007445 return &compat_hashtab;
7446
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007447 ht = get_funccal_local_ht();
7448 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007450 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007453 if (*name == 'g') /* global variable */
7454 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007455 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7456 */
7457 if (vim_strchr(name + 2, ':') != NULL
7458 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007459 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007461 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007463 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007464#ifdef FEAT_WINDOWS
7465 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007466 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 if (*name == 'v') /* v: variable */
7469 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007470 if (*name == 'a') /* a: function argument */
7471 return get_funccal_args_ht();
7472 if (*name == 'l') /* l: local function variable */
7473 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (*name == 's' /* script variable */
7475 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7476 return &SCRIPT_VARS(current_SID);
7477 return NULL;
7478}
7479
7480/*
7481 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007482 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 * Returns NULL when it doesn't exist.
7484 */
7485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007490 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (v == NULL)
7492 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494}
7495
7496/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 * sourcing this script and when executing functions defined in the script.
7499 */
7500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007501new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
Bram Moolenaara7043832005-01-21 11:56:39 +00007503 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 hashtab_T *ht;
7505 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7508 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007509 /* Re-allocating ga_data means that an ht_array pointing to
7510 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007512 for (i = 1; i <= ga_scripts.ga_len; ++i)
7513 {
7514 ht = &SCRIPT_VARS(i);
7515 if (ht->ht_mask == HT_INIT_SIZE - 1)
7516 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007517 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007519 }
7520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 while (ga_scripts.ga_len < id)
7522 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007523 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007524 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007525 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 }
7528 }
7529}
7530
7531/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7533 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 */
7535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007539 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007540 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007541 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007542 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 dict_var->di_tv.vval.v_dict = dict;
7544 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007545 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7547 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007551 * Unreference a dictionary initialized by init_var_dict().
7552 */
7553 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007555{
7556 /* Now the dict needs to be freed if no one else is using it, go back to
7557 * normal reference counting. */
7558 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7559 dict_unref(dict);
7560}
7561
7562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 * Frees all allocated variables and the value they contain.
7565 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 */
7567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007569{
7570 vars_clear_ext(ht, TRUE);
7571}
7572
7573/*
7574 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7575 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007577vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaara7043832005-01-21 11:56:39 +00007579 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 hashitem_T *hi;
7581 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007584 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007585 for (hi = ht->ht_array; todo > 0; ++hi)
7586 {
7587 if (!HASHITEM_EMPTY(hi))
7588 {
7589 --todo;
7590
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007592 * ht_array might change then. hash_clear() takes care of it
7593 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 v = HI2DI(hi);
7595 if (free_val)
7596 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007597 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007599 }
7600 }
7601 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007602 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
Bram Moolenaara7043832005-01-21 11:56:39 +00007605/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 * Delete a variable from hashtab "ht" at item "hi".
7607 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007610delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007613
7614 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 clear_tv(&di->di_tv);
7616 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617}
7618
7619/*
7620 * List the value of one internal variable.
7621 */
7622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007623list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007625 char_u *tofree;
7626 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007627 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007629 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007631 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633}
7634
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007636list_one_var_a(
7637 char_u *prefix,
7638 char_u *name,
7639 int type,
7640 char_u *string,
7641 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
Bram Moolenaar31859182007-08-14 20:41:13 +00007643 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7644 msg_start();
7645 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (name != NULL) /* "a:" vars don't have a name stored */
7647 msg_puts(name);
7648 msg_putchar(' ');
7649 msg_advance(22);
7650 if (type == VAR_NUMBER)
7651 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007652 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007653 msg_putchar('*');
7654 else if (type == VAR_LIST)
7655 {
7656 msg_putchar('[');
7657 if (*string == '[')
7658 ++string;
7659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 else if (type == VAR_DICT)
7661 {
7662 msg_putchar('{');
7663 if (*string == '{')
7664 ++string;
7665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 else
7667 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007668
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007671 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007673 if (*first)
7674 {
7675 msg_clr_eos();
7676 *first = FALSE;
7677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678}
7679
7680/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 * If the variable already exists, the value is updated.
7683 * Otherwise the variable is created.
7684 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686set_var(
7687 char_u *name,
7688 typval_T *tv,
7689 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007695 ht = find_var_ht(name, &varname);
7696 if (ht == NULL || *varname == NUL)
7697 {
7698 EMSG2(_(e_illvar), name);
7699 return;
7700 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007701 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007702
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007703 /* Search in parent scope which is possible to reference from lambda */
7704 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007705 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007706
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007707 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7708 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007709 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007710
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007714 if (var_check_ro(v->di_flags, name, FALSE)
7715 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007717
7718 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007719 * Handle setting internal v: variables separately where needed to
7720 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 */
7722 if (ht == &vimvarht)
7723 {
7724 if (v->di_tv.v_type == VAR_STRING)
7725 {
7726 vim_free(v->di_tv.vval.v_string);
7727 if (copy || tv->v_type != VAR_STRING)
7728 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7729 else
7730 {
7731 /* Take over the string to avoid an extra alloc/free. */
7732 v->di_tv.vval.v_string = tv->vval.v_string;
7733 tv->vval.v_string = NULL;
7734 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007735 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007737 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007740 if (STRCMP(varname, "searchforward") == 0)
7741 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007742#ifdef FEAT_SEARCH_EXTRA
7743 else if (STRCMP(varname, "hlsearch") == 0)
7744 {
7745 no_hlsearch = !v->di_tv.vval.v_number;
7746 redraw_all_later(SOME_VALID);
7747 }
7748#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007749 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007750 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007751 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007752 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 }
7754
7755 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757 else /* add a new variable */
7758 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007759 /* Can't add "v:" variable. */
7760 if (ht == &vimvarht)
7761 {
7762 EMSG2(_(e_illvar), name);
7763 return;
7764 }
7765
Bram Moolenaar92124a32005-06-17 22:03:40 +00007766 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007767 if (!valid_varname(varname))
7768 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007769
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007770 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7771 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007772 if (v == NULL)
7773 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007777 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 return;
7779 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007780 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007785 else
7786 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007788 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791}
7792
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007794 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007795 * Also give an error message.
7796 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007799{
7800 if (flags & DI_FLAGS_RO)
7801 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007802 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 return TRUE;
7804 }
7805 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7806 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007807 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 return TRUE;
7809 }
7810 return FALSE;
7811}
7812
7813/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007814 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7815 * Also give an error message.
7816 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007818var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007819{
7820 if (flags & DI_FLAGS_FIX)
7821 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007822 EMSG2(_("E795: Cannot delete variable %s"),
7823 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007824 return TRUE;
7825 }
7826 return FALSE;
7827}
7828
7829/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007830 * Check if a funcref is assigned to a valid variable name.
7831 * Return TRUE and give an error if not.
7832 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007834var_check_func_name(
7835 char_u *name, /* points to start of variable name */
7836 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007837{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007838 /* Allow for w: b: s: and t:. */
7839 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007840 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7841 ? name[2] : name[0]))
7842 {
7843 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7844 name);
7845 return TRUE;
7846 }
7847 /* Don't allow hiding a function. When "v" is not NULL we might be
7848 * assigning another function to the same var, the type is checked
7849 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007850 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007851 {
7852 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7853 name);
7854 return TRUE;
7855 }
7856 return FALSE;
7857}
7858
7859/*
7860 * Check if a variable name is valid.
7861 * Return FALSE and give an error if not.
7862 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007864valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007865{
7866 char_u *p;
7867
7868 for (p = varname; *p != NUL; ++p)
7869 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7870 && *p != AUTOLOAD_CHAR)
7871 {
7872 EMSG2(_(e_illvar), varname);
7873 return FALSE;
7874 }
7875 return TRUE;
7876}
7877
7878/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007879 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007880 * Also give an error message, using "name" or _("name") when use_gettext is
7881 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007882 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007885{
7886 if (lock & VAR_LOCKED)
7887 {
7888 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007889 name == NULL ? (char_u *)_("Unknown")
7890 : use_gettext ? (char_u *)_(name)
7891 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007892 return TRUE;
7893 }
7894 if (lock & VAR_FIXED)
7895 {
7896 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007897 name == NULL ? (char_u *)_("Unknown")
7898 : use_gettext ? (char_u *)_(name)
7899 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007900 return TRUE;
7901 }
7902 return FALSE;
7903}
7904
7905/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007907 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007909 * It is OK for "from" and "to" to point to the same item. This is used to
7910 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007913copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007916 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 switch (from->v_type)
7918 {
7919 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007920 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007921 to->vval.v_number = from->vval.v_number;
7922 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007923 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007924#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007925 to->vval.v_float = from->vval.v_float;
7926 break;
7927#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007928 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007929#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007930 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007931 if (to->vval.v_job != NULL)
7932 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007933 break;
7934#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007935 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007936#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007937 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007938 if (to->vval.v_channel != NULL)
7939 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007940 break;
7941#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942 case VAR_STRING:
7943 case VAR_FUNC:
7944 if (from->vval.v_string == NULL)
7945 to->vval.v_string = NULL;
7946 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007949 if (from->v_type == VAR_FUNC)
7950 func_ref(to->vval.v_string);
7951 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007952 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007953 case VAR_PARTIAL:
7954 if (from->vval.v_partial == NULL)
7955 to->vval.v_partial = NULL;
7956 else
7957 {
7958 to->vval.v_partial = from->vval.v_partial;
7959 ++to->vval.v_partial->pt_refcount;
7960 }
7961 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 case VAR_LIST:
7963 if (from->vval.v_list == NULL)
7964 to->vval.v_list = NULL;
7965 else
7966 {
7967 to->vval.v_list = from->vval.v_list;
7968 ++to->vval.v_list->lv_refcount;
7969 }
7970 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 case VAR_DICT:
7972 if (from->vval.v_dict == NULL)
7973 to->vval.v_dict = NULL;
7974 else
7975 {
7976 to->vval.v_dict = from->vval.v_dict;
7977 ++to->vval.v_dict->dv_refcount;
7978 }
7979 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007980 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007981 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007982 break;
7983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984}
7985
7986/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 * Make a copy of an item.
7988 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007989 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7990 * reference to an already copied list/dict can be used.
7991 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007992 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007993 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007994item_copy(
7995 typval_T *from,
7996 typval_T *to,
7997 int deep,
7998 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007999{
8000 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008001 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002
Bram Moolenaar33570922005-01-25 22:26:29 +00008003 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 {
8005 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008006 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008007 }
8008 ++recurse;
8009
8010 switch (from->v_type)
8011 {
8012 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 case VAR_STRING:
8015 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008016 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008017 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008018 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008019 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 copy_tv(from, to);
8021 break;
8022 case VAR_LIST:
8023 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008024 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008025 if (from->vval.v_list == NULL)
8026 to->vval.v_list = NULL;
8027 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8028 {
8029 /* use the copy made earlier */
8030 to->vval.v_list = from->vval.v_list->lv_copylist;
8031 ++to->vval.v_list->lv_refcount;
8032 }
8033 else
8034 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8035 if (to->vval.v_list == NULL)
8036 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 break;
8038 case VAR_DICT:
8039 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008040 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008041 if (from->vval.v_dict == NULL)
8042 to->vval.v_dict = NULL;
8043 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8044 {
8045 /* use the copy made earlier */
8046 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8047 ++to->vval.v_dict->dv_refcount;
8048 }
8049 else
8050 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8051 if (to->vval.v_dict == NULL)
8052 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008054 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008055 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008057 }
8058 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008059 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008060}
8061
8062/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 * This function is used by f_input() and f_inputdialog() functions. The third
8064 * argument to f_input() specifies the type of completion to use at the
8065 * prompt. The third argument to f_inputdialog() specifies the value to return
8066 * when the user cancels the prompt.
8067 */
8068 void
8069get_user_input(
8070 typval_T *argvars,
8071 typval_T *rettv,
8072 int inputdialog,
8073 int secret)
8074{
8075 char_u *prompt = get_tv_string_chk(&argvars[0]);
8076 char_u *p = NULL;
8077 int c;
8078 char_u buf[NUMBUFLEN];
8079 int cmd_silent_save = cmd_silent;
8080 char_u *defstr = (char_u *)"";
8081 int xp_type = EXPAND_NOTHING;
8082 char_u *xp_arg = NULL;
8083
8084 rettv->v_type = VAR_STRING;
8085 rettv->vval.v_string = NULL;
8086
8087#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008088 /* While starting up, there is no place to enter text. When running tests
8089 * with --not-a-term we assume feedkeys() will be used. */
8090 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 return;
8092#endif
8093
8094 cmd_silent = FALSE; /* Want to see the prompt. */
8095 if (prompt != NULL)
8096 {
8097 /* Only the part of the message after the last NL is considered as
8098 * prompt for the command line */
8099 p = vim_strrchr(prompt, '\n');
8100 if (p == NULL)
8101 p = prompt;
8102 else
8103 {
8104 ++p;
8105 c = *p;
8106 *p = NUL;
8107 msg_start();
8108 msg_clr_eos();
8109 msg_puts_attr(prompt, echo_attr);
8110 msg_didout = FALSE;
8111 msg_starthere();
8112 *p = c;
8113 }
8114 cmdline_row = msg_row;
8115
8116 if (argvars[1].v_type != VAR_UNKNOWN)
8117 {
8118 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8119 if (defstr != NULL)
8120 stuffReadbuffSpec(defstr);
8121
8122 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8123 {
8124 char_u *xp_name;
8125 int xp_namelen;
8126 long argt;
8127
8128 /* input() with a third argument: completion */
8129 rettv->vval.v_string = NULL;
8130
8131 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8132 if (xp_name == NULL)
8133 return;
8134
8135 xp_namelen = (int)STRLEN(xp_name);
8136
8137 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8138 &xp_arg) == FAIL)
8139 return;
8140 }
8141 }
8142
8143 if (defstr != NULL)
8144 {
8145 int save_ex_normal_busy = ex_normal_busy;
8146 ex_normal_busy = 0;
8147 rettv->vval.v_string =
8148 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8149 xp_type, xp_arg);
8150 ex_normal_busy = save_ex_normal_busy;
8151 }
8152 if (inputdialog && rettv->vval.v_string == NULL
8153 && argvars[1].v_type != VAR_UNKNOWN
8154 && argvars[2].v_type != VAR_UNKNOWN)
8155 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8156 &argvars[2], buf));
8157
8158 vim_free(xp_arg);
8159
8160 /* since the user typed this, no need to wait for return */
8161 need_wait_return = FALSE;
8162 msg_didout = FALSE;
8163 }
8164 cmd_silent = cmd_silent_save;
8165}
8166
8167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 * ":echo expr1 ..." print each argument separated with a space, add a
8169 * newline at the end.
8170 * ":echon expr1 ..." print each argument plain.
8171 */
8172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008173ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008177 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 char_u *p;
8179 int needclr = TRUE;
8180 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008181 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182
8183 if (eap->skip)
8184 ++emsg_skip;
8185 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8186 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187 /* If eval1() causes an error message the text from the command may
8188 * still need to be cleared. E.g., "echo 22,44". */
8189 need_clr_eos = needclr;
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {
8194 /*
8195 * Report the invalid expression unless the expression evaluation
8196 * has been cancelled due to an aborting error, an interrupt, or an
8197 * exception.
8198 */
8199 if (!aborting())
8200 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 break;
8203 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008204 need_clr_eos = FALSE;
8205
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 if (!eap->skip)
8207 {
8208 if (atstart)
8209 {
8210 atstart = FALSE;
8211 /* Call msg_start() after eval1(), evaluating the expression
8212 * may cause a message to appear. */
8213 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008214 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008215 /* Mark the saved text as finishing the line, so that what
8216 * follows is displayed on a new line when scrolling back
8217 * at the more prompt. */
8218 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
8222 else if (eap->cmdidx == CMD_echo)
8223 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008224 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008225 if (p != NULL)
8226 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008228 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008230 if (*p != TAB && needclr)
8231 {
8232 /* remove any text still there from the command */
8233 msg_clr_eos();
8234 needclr = FALSE;
8235 }
8236 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
8238 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008239 {
8240#ifdef FEAT_MBYTE
8241 if (has_mbyte)
8242 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008243 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244
8245 (void)msg_outtrans_len_attr(p, i, echo_attr);
8246 p += i - 1;
8247 }
8248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008250 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008253 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 arg = skipwhite(arg);
8257 }
8258 eap->nextcmd = check_nextcmd(arg);
8259
8260 if (eap->skip)
8261 --emsg_skip;
8262 else
8263 {
8264 /* remove text that may still be there from the command */
8265 if (needclr)
8266 msg_clr_eos();
8267 if (eap->cmdidx == CMD_echo)
8268 msg_end();
8269 }
8270}
8271
8272/*
8273 * ":echohl {name}".
8274 */
8275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008276ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 int id;
8279
8280 id = syn_name2id(eap->arg);
8281 if (id == 0)
8282 echo_attr = 0;
8283 else
8284 echo_attr = syn_id2attr(id);
8285}
8286
8287/*
8288 * ":execute expr1 ..." execute the result of an expression.
8289 * ":echomsg expr1 ..." Print a message
8290 * ":echoerr expr1 ..." Print an error
8291 * Each gets spaces around each argument and a newline at the end for
8292 * echo commands
8293 */
8294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008295ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
8297 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 int ret = OK;
8300 char_u *p;
8301 garray_T ga;
8302 int len;
8303 int save_did_emsg;
8304
8305 ga_init2(&ga, 1, 80);
8306
8307 if (eap->skip)
8308 ++emsg_skip;
8309 while (*arg != NUL && *arg != '|' && *arg != '\n')
8310 {
8311 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
8314 /*
8315 * Report the invalid expression unless the expression evaluation
8316 * has been cancelled due to an aborting error, an interrupt, or an
8317 * exception.
8318 */
8319 if (!aborting())
8320 EMSG2(_(e_invexpr2), p);
8321 ret = FAIL;
8322 break;
8323 }
8324
8325 if (!eap->skip)
8326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008327 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 len = (int)STRLEN(p);
8329 if (ga_grow(&ga, len + 2) == FAIL)
8330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 ret = FAIL;
8333 break;
8334 }
8335 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 ga.ga_len += len;
8339 }
8340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 arg = skipwhite(arg);
8343 }
8344
8345 if (ret != FAIL && ga.ga_data != NULL)
8346 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008347 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8348 {
8349 /* Mark the already saved text as finishing the line, so that what
8350 * follows is displayed on a new line when scrolling back at the
8351 * more prompt. */
8352 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008353 }
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008358 out_flush();
8359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 else if (eap->cmdidx == CMD_echoerr)
8361 {
8362 /* We don't want to abort following commands, restore did_emsg. */
8363 save_did_emsg = did_emsg;
8364 EMSG((char_u *)ga.ga_data);
8365 if (!force_abort)
8366 did_emsg = save_did_emsg;
8367 }
8368 else if (eap->cmdidx == CMD_execute)
8369 do_cmdline((char_u *)ga.ga_data,
8370 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8371 }
8372
8373 ga_clear(&ga);
8374
8375 if (eap->skip)
8376 --emsg_skip;
8377
8378 eap->nextcmd = check_nextcmd(arg);
8379}
8380
8381/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 * Find window specified by "vp" in tabpage "tp".
8383 */
8384 win_T *
8385find_win_by_nr(
8386 typval_T *vp,
8387 tabpage_T *tp UNUSED) /* NULL for current tab page */
8388{
8389#ifdef FEAT_WINDOWS
8390 win_T *wp;
8391#endif
8392 int nr;
8393
8394 nr = (int)get_tv_number_chk(vp, NULL);
8395
8396#ifdef FEAT_WINDOWS
8397 if (nr < 0)
8398 return NULL;
8399 if (nr == 0)
8400 return curwin;
8401
Bram Moolenaar29323592016-07-24 22:04:11 +02008402 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8403 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 if (nr >= LOWEST_WIN_ID)
8405 {
8406 if (wp->w_id == nr)
8407 return wp;
8408 }
8409 else if (--nr <= 0)
8410 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008411 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 if (nr >= LOWEST_WIN_ID)
8413 return NULL;
8414 return wp;
8415#else
8416 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8417 return curwin;
8418 return NULL;
8419#endif
8420}
8421
8422/*
8423 * Find window specified by "wvp" in tabpage "tvp".
8424 */
8425 win_T *
8426find_tabwin(
8427 typval_T *wvp, /* VAR_UNKNOWN for current window */
8428 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8429{
8430 win_T *wp = NULL;
8431 tabpage_T *tp = NULL;
8432 long n;
8433
8434 if (wvp->v_type != VAR_UNKNOWN)
8435 {
8436 if (tvp->v_type != VAR_UNKNOWN)
8437 {
8438 n = (long)get_tv_number(tvp);
8439 if (n >= 0)
8440 tp = find_tabpage(n);
8441 }
8442 else
8443 tp = curtab;
8444
8445 if (tp != NULL)
8446 wp = find_win_by_nr(wvp, tp);
8447 }
8448 else
8449 wp = curwin;
8450
8451 return wp;
8452}
8453
8454/*
8455 * getwinvar() and gettabwinvar()
8456 */
8457 void
8458getwinvar(
8459 typval_T *argvars,
8460 typval_T *rettv,
8461 int off) /* 1 for gettabwinvar() */
8462{
8463 win_T *win;
8464 char_u *varname;
8465 dictitem_T *v;
8466 tabpage_T *tp = NULL;
8467 int done = FALSE;
8468#ifdef FEAT_WINDOWS
8469 win_T *oldcurwin;
8470 tabpage_T *oldtabpage;
8471 int need_switch_win;
8472#endif
8473
8474#ifdef FEAT_WINDOWS
8475 if (off == 1)
8476 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8477 else
8478 tp = curtab;
8479#endif
8480 win = find_win_by_nr(&argvars[off], tp);
8481 varname = get_tv_string_chk(&argvars[off + 1]);
8482 ++emsg_off;
8483
8484 rettv->v_type = VAR_STRING;
8485 rettv->vval.v_string = NULL;
8486
8487 if (win != NULL && varname != NULL)
8488 {
8489#ifdef FEAT_WINDOWS
8490 /* Set curwin to be our win, temporarily. Also set the tabpage,
8491 * otherwise the window is not valid. Only do this when needed,
8492 * autocommands get blocked. */
8493 need_switch_win = !(tp == curtab && win == curwin);
8494 if (!need_switch_win
8495 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8496#endif
8497 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008498 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008500 if (varname[1] == NUL)
8501 {
8502 /* get all window-local options in a dict */
8503 dict_T *opts = get_winbuf_options(FALSE);
8504
8505 if (opts != NULL)
8506 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008507 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008508 done = TRUE;
8509 }
8510 }
8511 else if (get_option_tv(&varname, rettv, 1) == OK)
8512 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 done = TRUE;
8514 }
8515 else
8516 {
8517 /* Look up the variable. */
8518 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8519 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8520 varname, FALSE);
8521 if (v != NULL)
8522 {
8523 copy_tv(&v->di_tv, rettv);
8524 done = TRUE;
8525 }
8526 }
8527 }
8528
8529#ifdef FEAT_WINDOWS
8530 if (need_switch_win)
8531 /* restore previous notion of curwin */
8532 restore_win(oldcurwin, oldtabpage, TRUE);
8533#endif
8534 }
8535
8536 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8537 /* use the default return value */
8538 copy_tv(&argvars[off + 2], rettv);
8539
8540 --emsg_off;
8541}
8542
8543/*
8544 * "setwinvar()" and "settabwinvar()" functions
8545 */
8546 void
8547setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8548{
8549 win_T *win;
8550#ifdef FEAT_WINDOWS
8551 win_T *save_curwin;
8552 tabpage_T *save_curtab;
8553 int need_switch_win;
8554#endif
8555 char_u *varname, *winvarname;
8556 typval_T *varp;
8557 char_u nbuf[NUMBUFLEN];
8558 tabpage_T *tp = NULL;
8559
8560 if (check_restricted() || check_secure())
8561 return;
8562
8563#ifdef FEAT_WINDOWS
8564 if (off == 1)
8565 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8566 else
8567 tp = curtab;
8568#endif
8569 win = find_win_by_nr(&argvars[off], tp);
8570 varname = get_tv_string_chk(&argvars[off + 1]);
8571 varp = &argvars[off + 2];
8572
8573 if (win != NULL && varname != NULL && varp != NULL)
8574 {
8575#ifdef FEAT_WINDOWS
8576 need_switch_win = !(tp == curtab && win == curwin);
8577 if (!need_switch_win
8578 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8579#endif
8580 {
8581 if (*varname == '&')
8582 {
8583 long numval;
8584 char_u *strval;
8585 int error = FALSE;
8586
8587 ++varname;
8588 numval = (long)get_tv_number_chk(varp, &error);
8589 strval = get_tv_string_buf_chk(varp, nbuf);
8590 if (!error && strval != NULL)
8591 set_option_value(varname, numval, strval, OPT_LOCAL);
8592 }
8593 else
8594 {
8595 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8596 if (winvarname != NULL)
8597 {
8598 STRCPY(winvarname, "w:");
8599 STRCPY(winvarname + 2, varname);
8600 set_var(winvarname, varp, TRUE);
8601 vim_free(winvarname);
8602 }
8603 }
8604 }
8605#ifdef FEAT_WINDOWS
8606 if (need_switch_win)
8607 restore_win(save_curwin, save_curtab, TRUE);
8608#endif
8609 }
8610}
8611
8612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8614 * "arg" points to the "&" or '+' when called, to "option" when returning.
8615 * Returns NULL when no option name found. Otherwise pointer to the char
8616 * after the option name.
8617 */
8618 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008619find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 char_u *p = *arg;
8622
8623 ++p;
8624 if (*p == 'g' && p[1] == ':')
8625 {
8626 *opt_flags = OPT_GLOBAL;
8627 p += 2;
8628 }
8629 else if (*p == 'l' && p[1] == ':')
8630 {
8631 *opt_flags = OPT_LOCAL;
8632 p += 2;
8633 }
8634 else
8635 *opt_flags = 0;
8636
8637 if (!ASCII_ISALPHA(*p))
8638 return NULL;
8639 *arg = p;
8640
8641 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8642 p += 4; /* termcap option */
8643 else
8644 while (ASCII_ISALPHA(*p))
8645 ++p;
8646 return p;
8647}
8648
8649/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008650 * Return the autoload script name for a function or variable name.
8651 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008653 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008654autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008655{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008656 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008657 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008658
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008659 /* Get the script file name: replace '#' with '/', append ".vim". */
8660 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8661 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008662 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008663 STRCPY(scriptname, "autoload/");
8664 STRCAT(scriptname, name);
8665 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8666 STRCAT(scriptname, ".vim");
8667 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8668 *p = '/';
8669 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008670}
8671
8672/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008673 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008674 * Return TRUE if a package was loaded.
8675 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008677script_autoload(
8678 char_u *name,
8679 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008680{
8681 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008682 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008683 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008684 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008686 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008687 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008688 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008689 return FALSE;
8690
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008691 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008692
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008693 /* Find the name in the list of previously loaded package names. Skip
8694 * "autoload/", it's always the same. */
8695 for (i = 0; i < ga_loaded.ga_len; ++i)
8696 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8697 break;
8698 if (!reload && i < ga_loaded.ga_len)
8699 ret = FALSE; /* was loaded already */
8700 else
8701 {
8702 /* Remember the name if it wasn't loaded already. */
8703 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8704 {
8705 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8706 tofree = NULL;
8707 }
8708
8709 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008710 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008711 ret = TRUE;
8712 }
8713
8714 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008715 return ret;
8716}
8717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8719typedef enum
8720{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008721 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8722 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8723 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724} var_flavour_T;
8725
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008726static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008729var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 char_u *p = varname;
8732
8733 if (ASCII_ISUPPER(*p))
8734 {
8735 while (*(++p))
8736 if (ASCII_ISLOWER(*p))
8737 return VAR_FLAVOUR_SESSION;
8738 return VAR_FLAVOUR_VIMINFO;
8739 }
8740 else
8741 return VAR_FLAVOUR_DEFAULT;
8742}
8743#endif
8744
8745#if defined(FEAT_VIMINFO) || defined(PROTO)
8746/*
8747 * Restore global vars that start with a capital from the viminfo file
8748 */
8749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008753 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008755 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
8757 if (!writing && (find_viminfo_parameter('!') != NULL))
8758 {
8759 tab = vim_strchr(virp->vir_line + 1, '\t');
8760 if (tab != NULL)
8761 {
8762 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008763 switch (*tab)
8764 {
8765 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008766#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008767 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008768#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008769 case 'D': type = VAR_DICT; break;
8770 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008771 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
8774 tab = vim_strchr(tab, '\t');
8775 if (tab != NULL)
8776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008777 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008778 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008779 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782 else if (type == VAR_FLOAT)
8783 (void)string2float(tab + 1, &tv.vval.v_float);
8784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008786 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008787 if (type == VAR_DICT || type == VAR_LIST)
8788 {
8789 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8790
8791 if (etv == NULL)
8792 /* Failed to parse back the dict or list, use it as a
8793 * string. */
8794 tv.v_type = VAR_STRING;
8795 else
8796 {
8797 vim_free(tv.vval.v_string);
8798 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008799 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008800 }
8801 }
8802
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008803 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008804 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008805 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008806 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008807
8808 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008809 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008810 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
8813 }
8814 }
8815
8816 return viminfo_readline(virp);
8817}
8818
8819/*
8820 * Write global vars that start with a capital to the viminfo file
8821 */
8822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008823write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 hashitem_T *hi;
8826 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008827 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008828 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008831 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 if (find_viminfo_parameter('!') == NULL)
8834 return;
8835
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008836 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008837
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008838 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008841 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008843 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 this_var = HI2DI(hi);
8845 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008848 {
8849 case VAR_STRING: s = "STR"; break;
8850 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008851 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008852 case VAR_DICT: s = "DIC"; break;
8853 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008854 case VAR_SPECIAL: s = "XPL"; break;
8855
8856 case VAR_UNKNOWN:
8857 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008858 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008859 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008860 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008861 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008864 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 if (p != NULL)
8866 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008867 vim_free(tofree);
8868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870 }
8871}
8872#endif
8873
8874#if defined(FEAT_SESSION) || defined(PROTO)
8875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 hashitem_T *hi;
8879 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008880 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 char_u *p, *t;
8882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008883 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008886 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008888 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008889 this_var = HI2DI(hi);
8890 if ((this_var->di_tv.v_type == VAR_NUMBER
8891 || this_var->di_tv.v_type == VAR_STRING)
8892 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008893 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008894 /* Escape special characters with a backslash. Turn a LF and
8895 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008897 (char_u *)"\\\"\n\r");
8898 if (p == NULL) /* out of memory */
8899 break;
8900 for (t = p; *t != NUL; ++t)
8901 if (*t == '\n')
8902 *t = 'n';
8903 else if (*t == '\r')
8904 *t = 'r';
8905 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008906 this_var->di_key,
8907 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8908 : ' ',
8909 p,
8910 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8911 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008912 || put_eol(fd) == FAIL)
8913 {
8914 vim_free(p);
8915 return FAIL;
8916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 vim_free(p);
8918 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008919#ifdef FEAT_FLOAT
8920 else if (this_var->di_tv.v_type == VAR_FLOAT
8921 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8922 {
8923 float_T f = this_var->di_tv.vval.v_float;
8924 int sign = ' ';
8925
8926 if (f < 0)
8927 {
8928 f = -f;
8929 sign = '-';
8930 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008931 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008932 this_var->di_key, sign, f) < 0)
8933 || put_eol(fd) == FAIL)
8934 return FAIL;
8935 }
8936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 }
8938 }
8939 return OK;
8940}
8941#endif
8942
Bram Moolenaar661b1822005-07-28 22:36:45 +00008943/*
8944 * Display script name where an item was last set.
8945 * Should only be invoked when 'verbose' is non-zero.
8946 */
8947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008948last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008949{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008950 char_u *p;
8951
Bram Moolenaar661b1822005-07-28 22:36:45 +00008952 if (scriptID != 0)
8953 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008954 p = home_replace_save(NULL, get_scriptname(scriptID));
8955 if (p != NULL)
8956 {
8957 verbose_enter();
8958 MSG_PUTS(_("\n\tLast set from "));
8959 MSG_PUTS(p);
8960 vim_free(p);
8961 verbose_leave();
8962 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008963 }
8964}
8965
Bram Moolenaar53744302015-07-17 17:38:22 +02008966/* reset v:option_new, v:option_old and v:option_type */
8967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008969{
8970 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8971 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8972 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8973}
8974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975/*
8976 * Prepare "gap" for an assert error and add the sourcing position.
8977 */
8978 void
8979prepare_assert_error(garray_T *gap)
8980{
8981 char buf[NUMBUFLEN];
8982
8983 ga_init2(gap, 1, 100);
8984 if (sourcing_name != NULL)
8985 {
8986 ga_concat(gap, sourcing_name);
8987 if (sourcing_lnum > 0)
8988 ga_concat(gap, (char_u *)" ");
8989 }
8990 if (sourcing_lnum > 0)
8991 {
8992 sprintf(buf, "line %ld", (long)sourcing_lnum);
8993 ga_concat(gap, (char_u *)buf);
8994 }
8995 if (sourcing_name != NULL || sourcing_lnum > 0)
8996 ga_concat(gap, (char_u *)": ");
8997}
8998
8999/*
9000 * Add an assert error to v:errors.
9001 */
9002 void
9003assert_error(garray_T *gap)
9004{
9005 struct vimvar *vp = &vimvars[VV_ERRORS];
9006
9007 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9008 /* Make sure v:errors is a list. */
9009 set_vim_var_list(VV_ERRORS, list_alloc());
9010 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9011}
9012
9013 void
9014assert_equal_common(typval_T *argvars, assert_type_T atype)
9015{
9016 garray_T ga;
9017
9018 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9019 != (atype == ASSERT_EQUAL))
9020 {
9021 prepare_assert_error(&ga);
9022 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9023 atype);
9024 assert_error(&ga);
9025 ga_clear(&ga);
9026 }
9027}
9028
9029 void
9030assert_match_common(typval_T *argvars, assert_type_T atype)
9031{
9032 garray_T ga;
9033 char_u buf1[NUMBUFLEN];
9034 char_u buf2[NUMBUFLEN];
9035 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9036 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9037
9038 if (pat == NULL || text == NULL)
9039 EMSG(_(e_invarg));
9040 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9041 {
9042 prepare_assert_error(&ga);
9043 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9044 atype);
9045 assert_error(&ga);
9046 ga_clear(&ga);
9047 }
9048}
9049
Bram Moolenaar61c04492016-07-23 15:35:35 +02009050 void
9051assert_inrange(typval_T *argvars)
9052{
9053 garray_T ga;
9054 int error = FALSE;
9055 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9056 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9057 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9058 char_u *tofree;
9059 char msg[200];
9060 char_u numbuf[NUMBUFLEN];
9061
9062 if (error)
9063 return;
9064 if (actual < lower || actual > upper)
9065 {
9066 prepare_assert_error(&ga);
9067 if (argvars[3].v_type != VAR_UNKNOWN)
9068 {
9069 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9070 vim_free(tofree);
9071 }
9072 else
9073 {
9074 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9075 (long)lower, (long)upper, (long)actual);
9076 ga_concat(&ga, (char_u *)msg);
9077 }
9078 assert_error(&ga);
9079 ga_clear(&ga);
9080 }
9081}
9082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083/*
9084 * Common for assert_true() and assert_false().
9085 */
9086 void
9087assert_bool(typval_T *argvars, int isTrue)
9088{
9089 int error = FALSE;
9090 garray_T ga;
9091
9092 if (argvars[0].v_type == VAR_SPECIAL
9093 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9094 return;
9095 if (argvars[0].v_type != VAR_NUMBER
9096 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9097 || error)
9098 {
9099 prepare_assert_error(&ga);
9100 fill_assert_error(&ga, &argvars[1],
9101 (char_u *)(isTrue ? "True" : "False"),
9102 NULL, &argvars[0], ASSERT_OTHER);
9103 assert_error(&ga);
9104 ga_clear(&ga);
9105 }
9106}
9107
9108 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009109assert_report(typval_T *argvars)
9110{
9111 garray_T ga;
9112
9113 prepare_assert_error(&ga);
9114 ga_concat(&ga, get_tv_string(&argvars[0]));
9115 assert_error(&ga);
9116 ga_clear(&ga);
9117}
9118
9119 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120assert_exception(typval_T *argvars)
9121{
9122 garray_T ga;
9123 char_u *error = get_tv_string_chk(&argvars[0]);
9124
9125 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9126 {
9127 prepare_assert_error(&ga);
9128 ga_concat(&ga, (char_u *)"v:exception is not set");
9129 assert_error(&ga);
9130 ga_clear(&ga);
9131 }
9132 else if (error != NULL
9133 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9134 {
9135 prepare_assert_error(&ga);
9136 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9137 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9138 assert_error(&ga);
9139 ga_clear(&ga);
9140 }
9141}
9142
9143 void
9144assert_fails(typval_T *argvars)
9145{
9146 char_u *cmd = get_tv_string_chk(&argvars[0]);
9147 garray_T ga;
9148
9149 called_emsg = FALSE;
9150 suppress_errthrow = TRUE;
9151 emsg_silent = TRUE;
9152 do_cmdline_cmd(cmd);
9153 if (!called_emsg)
9154 {
9155 prepare_assert_error(&ga);
9156 ga_concat(&ga, (char_u *)"command did not fail: ");
9157 ga_concat(&ga, cmd);
9158 assert_error(&ga);
9159 ga_clear(&ga);
9160 }
9161 else if (argvars[1].v_type != VAR_UNKNOWN)
9162 {
9163 char_u buf[NUMBUFLEN];
9164 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9165
9166 if (error == NULL
9167 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9168 {
9169 prepare_assert_error(&ga);
9170 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9171 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9172 assert_error(&ga);
9173 ga_clear(&ga);
9174 }
9175 }
9176
9177 called_emsg = FALSE;
9178 suppress_errthrow = FALSE;
9179 emsg_silent = FALSE;
9180 emsg_on_display = FALSE;
9181 set_vim_var_string(VV_ERRMSG, NULL, 0);
9182}
9183
9184/*
9185 * Append "str" to "gap", escaping unprintable characters.
9186 * Changes NL to \n, CR to \r, etc.
9187 */
9188 static void
9189ga_concat_esc(garray_T *gap, char_u *str)
9190{
9191 char_u *p;
9192 char_u buf[NUMBUFLEN];
9193
9194 if (str == NULL)
9195 {
9196 ga_concat(gap, (char_u *)"NULL");
9197 return;
9198 }
9199
9200 for (p = str; *p != NUL; ++p)
9201 switch (*p)
9202 {
9203 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9204 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9205 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9206 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9207 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9208 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9209 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9210 default:
9211 if (*p < ' ')
9212 {
9213 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9214 ga_concat(gap, buf);
9215 }
9216 else
9217 ga_append(gap, *p);
9218 break;
9219 }
9220}
9221
9222/*
9223 * Fill "gap" with information about an assert error.
9224 */
9225 void
9226fill_assert_error(
9227 garray_T *gap,
9228 typval_T *opt_msg_tv,
9229 char_u *exp_str,
9230 typval_T *exp_tv,
9231 typval_T *got_tv,
9232 assert_type_T atype)
9233{
9234 char_u numbuf[NUMBUFLEN];
9235 char_u *tofree;
9236
9237 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9238 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009239 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9240 vim_free(tofree);
9241 ga_concat(gap, (char_u *)": ");
9242 }
9243
9244 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9245 ga_concat(gap, (char_u *)"Pattern ");
9246 else if (atype == ASSERT_NOTEQUAL)
9247 ga_concat(gap, (char_u *)"Expected not equal to ");
9248 else
9249 ga_concat(gap, (char_u *)"Expected ");
9250 if (exp_str == NULL)
9251 {
9252 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009253 vim_free(tofree);
9254 }
9255 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009256 ga_concat_esc(gap, exp_str);
9257 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009259 if (atype == ASSERT_MATCH)
9260 ga_concat(gap, (char_u *)" does not match ");
9261 else if (atype == ASSERT_NOTMATCH)
9262 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009264 ga_concat(gap, (char_u *)" but got ");
9265 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9266 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 }
9268}
9269
Bram Moolenaar53744302015-07-17 17:38:22 +02009270
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271#endif /* FEAT_EVAL */
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275
9276#ifdef WIN3264
9277/*
9278 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9279 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009280static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9281static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9282static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
9284/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009285 * Get the short path (8.3) for the filename in "fnamep".
9286 * Only works for a valid file name.
9287 * When the path gets longer "fnamep" is changed and the allocated buffer
9288 * is put in "bufp".
9289 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9290 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 */
9292 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009293get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009295 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 char_u *newbuf;
9297
9298 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009299 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (l > len - 1)
9301 {
9302 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009303 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 newbuf = vim_strnsave(*fnamep, l);
9305 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009306 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307
9308 vim_free(*bufp);
9309 *fnamep = *bufp = newbuf;
9310
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009311 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009312 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 }
9314
9315 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009316 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317}
9318
9319/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009320 * Get the short path (8.3) for the filename in "fname". The converted
9321 * path is returned in "bufp".
9322 *
9323 * Some of the directories specified in "fname" may not exist. This function
9324 * will shorten the existing directories at the beginning of the path and then
9325 * append the remaining non-existing path.
9326 *
9327 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009328 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009329 * bufp - Pointer to an allocated buffer for the filename.
9330 * fnamelen - Length of the filename pointed to by fname
9331 *
9332 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 */
9334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009335shortpath_for_invalid_fname(
9336 char_u **fname,
9337 char_u **bufp,
9338 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009340 char_u *short_fname, *save_fname, *pbuf_unused;
9341 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009343 int old_len, len;
9344 int new_len, sfx_len;
9345 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
9347 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009348 old_len = *fnamelen;
9349 save_fname = vim_strnsave(*fname, old_len);
9350 pbuf_unused = NULL;
9351 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009353 endp = save_fname + old_len - 1; /* Find the end of the copy */
9354 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009356 /*
9357 * Try shortening the supplied path till it succeeds by removing one
9358 * directory at a time from the tail of the path.
9359 */
9360 len = 0;
9361 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009363 /* go back one path-separator */
9364 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9365 --endp;
9366 if (endp <= save_fname)
9367 break; /* processed the complete path */
9368
9369 /*
9370 * Replace the path separator with a NUL and try to shorten the
9371 * resulting path.
9372 */
9373 ch = *endp;
9374 *endp = 0;
9375 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009376 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009377 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9378 {
9379 retval = FAIL;
9380 goto theend;
9381 }
9382 *endp = ch; /* preserve the string */
9383
9384 if (len > 0)
9385 break; /* successfully shortened the path */
9386
9387 /* failed to shorten the path. Skip the path separator */
9388 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 }
9390
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009391 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009393 /*
9394 * Succeeded in shortening the path. Now concatenate the shortened
9395 * path with the remaining path at the tail.
9396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009398 /* Compute the length of the new path. */
9399 sfx_len = (int)(save_endp - endp) + 1;
9400 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009402 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009404 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009406 /* There is not enough space in the currently allocated string,
9407 * copy it to a buffer big enough. */
9408 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009410 {
9411 retval = FAIL;
9412 goto theend;
9413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 }
9415 else
9416 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009417 /* Transfer short_fname to the main buffer (it's big enough),
9418 * unless get_short_pathname() did its work in-place. */
9419 *fname = *bufp = save_fname;
9420 if (short_fname != save_fname)
9421 vim_strncpy(save_fname, short_fname, len);
9422 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009424
9425 /* concat the not-shortened part of the path */
9426 vim_strncpy(*fname + len, endp, sfx_len);
9427 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009429
9430theend:
9431 vim_free(pbuf_unused);
9432 vim_free(save_fname);
9433
9434 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435}
9436
9437/*
9438 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009439 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 */
9441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442shortpath_for_partial(
9443 char_u **fnamep,
9444 char_u **bufp,
9445 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 int sepcount, len, tflen;
9448 char_u *p;
9449 char_u *pbuf, *tfname;
9450 int hasTilde;
9451
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009452 /* Count up the path separators from the RHS.. so we know which part
9453 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009455 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 if (vim_ispathsep(*p))
9457 ++sepcount;
9458
9459 /* Need full path first (use expand_env() to remove a "~/") */
9460 hasTilde = (**fnamep == '~');
9461 if (hasTilde)
9462 pbuf = tfname = expand_env_save(*fnamep);
9463 else
9464 pbuf = tfname = FullName_save(*fnamep, FALSE);
9465
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009466 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009468 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9469 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470
9471 if (len == 0)
9472 {
9473 /* Don't have a valid filename, so shorten the rest of the
9474 * path if we can. This CAN give us invalid 8.3 filenames, but
9475 * there's not a lot of point in guessing what it might be.
9476 */
9477 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009478 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9479 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 }
9481
9482 /* Count the paths backward to find the beginning of the desired string. */
9483 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009484 {
9485#ifdef FEAT_MBYTE
9486 if (has_mbyte)
9487 p -= mb_head_off(tfname, p);
9488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 if (vim_ispathsep(*p))
9490 {
9491 if (sepcount == 0 || (hasTilde && sepcount == 1))
9492 break;
9493 else
9494 sepcount --;
9495 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (hasTilde)
9498 {
9499 --p;
9500 if (p >= tfname)
9501 *p = '~';
9502 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 }
9505 else
9506 ++p;
9507
9508 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9509 vim_free(*bufp);
9510 *fnamelen = (int)STRLEN(p);
9511 *bufp = pbuf;
9512 *fnamep = p;
9513
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009514 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515}
9516#endif /* WIN3264 */
9517
9518/*
9519 * Adjust a filename, according to a string of modifiers.
9520 * *fnamep must be NUL terminated when called. When returning, the length is
9521 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009522 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 * When there is an error, *fnamep is set to NULL.
9524 */
9525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526modify_fname(
9527 char_u *src, /* string with modifiers */
9528 int *usedlen, /* characters after src that are used */
9529 char_u **fnamep, /* file name so far */
9530 char_u **bufp, /* buffer for allocated file name or NULL */
9531 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533 int valid = 0;
9534 char_u *tail;
9535 char_u *s, *p, *pbuf;
9536 char_u dirname[MAXPATHL];
9537 int c;
9538 int has_fullname = 0;
9539#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009540 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 int has_shortname = 0;
9542#endif
9543
9544repeat:
9545 /* ":p" - full path/file_name */
9546 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9547 {
9548 has_fullname = 1;
9549
9550 valid |= VALID_PATH;
9551 *usedlen += 2;
9552
9553 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9554 if ((*fnamep)[0] == '~'
9555#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9556 && ((*fnamep)[1] == '/'
9557# ifdef BACKSLASH_IN_FILENAME
9558 || (*fnamep)[1] == '\\'
9559# endif
9560 || (*fnamep)[1] == NUL)
9561
9562#endif
9563 )
9564 {
9565 *fnamep = expand_env_save(*fnamep);
9566 vim_free(*bufp); /* free any allocated file name */
9567 *bufp = *fnamep;
9568 if (*fnamep == NULL)
9569 return -1;
9570 }
9571
9572 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009573 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
9575 if (vim_ispathsep(*p)
9576 && p[1] == '.'
9577 && (p[2] == NUL
9578 || vim_ispathsep(p[2])
9579 || (p[2] == '.'
9580 && (p[3] == NUL || vim_ispathsep(p[3])))))
9581 break;
9582 }
9583
9584 /* FullName_save() is slow, don't use it when not needed. */
9585 if (*p != NUL || !vim_isAbsName(*fnamep))
9586 {
9587 *fnamep = FullName_save(*fnamep, *p != NUL);
9588 vim_free(*bufp); /* free any allocated file name */
9589 *bufp = *fnamep;
9590 if (*fnamep == NULL)
9591 return -1;
9592 }
9593
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009594#ifdef WIN3264
9595# if _WIN32_WINNT >= 0x0500
9596 if (vim_strchr(*fnamep, '~') != NULL)
9597 {
9598 /* Expand 8.3 filename to full path. Needed to make sure the same
9599 * file does not have two different names.
9600 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9601 p = alloc(_MAX_PATH + 1);
9602 if (p != NULL)
9603 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009604 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009605 {
9606 vim_free(*bufp);
9607 *bufp = *fnamep = p;
9608 }
9609 else
9610 vim_free(p);
9611 }
9612 }
9613# endif
9614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 /* Append a path separator to a directory. */
9616 if (mch_isdir(*fnamep))
9617 {
9618 /* Make room for one or two extra characters. */
9619 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9620 vim_free(*bufp); /* free any allocated file name */
9621 *bufp = *fnamep;
9622 if (*fnamep == NULL)
9623 return -1;
9624 add_pathsep(*fnamep);
9625 }
9626 }
9627
9628 /* ":." - path relative to the current directory */
9629 /* ":~" - path relative to the home directory */
9630 /* ":8" - shortname path - postponed till after */
9631 while (src[*usedlen] == ':'
9632 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9633 {
9634 *usedlen += 2;
9635 if (c == '8')
9636 {
9637#ifdef WIN3264
9638 has_shortname = 1; /* Postpone this. */
9639#endif
9640 continue;
9641 }
9642 pbuf = NULL;
9643 /* Need full path first (use expand_env() to remove a "~/") */
9644 if (!has_fullname)
9645 {
9646 if (c == '.' && **fnamep == '~')
9647 p = pbuf = expand_env_save(*fnamep);
9648 else
9649 p = pbuf = FullName_save(*fnamep, FALSE);
9650 }
9651 else
9652 p = *fnamep;
9653
9654 has_fullname = 0;
9655
9656 if (p != NULL)
9657 {
9658 if (c == '.')
9659 {
9660 mch_dirname(dirname, MAXPATHL);
9661 s = shorten_fname(p, dirname);
9662 if (s != NULL)
9663 {
9664 *fnamep = s;
9665 if (pbuf != NULL)
9666 {
9667 vim_free(*bufp); /* free any allocated file name */
9668 *bufp = pbuf;
9669 pbuf = NULL;
9670 }
9671 }
9672 }
9673 else
9674 {
9675 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9676 /* Only replace it when it starts with '~' */
9677 if (*dirname == '~')
9678 {
9679 s = vim_strsave(dirname);
9680 if (s != NULL)
9681 {
9682 *fnamep = s;
9683 vim_free(*bufp);
9684 *bufp = s;
9685 }
9686 }
9687 }
9688 vim_free(pbuf);
9689 }
9690 }
9691
9692 tail = gettail(*fnamep);
9693 *fnamelen = (int)STRLEN(*fnamep);
9694
9695 /* ":h" - head, remove "/file_name", can be repeated */
9696 /* Don't remove the first "/" or "c:\" */
9697 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9698 {
9699 valid |= VALID_HEAD;
9700 *usedlen += 2;
9701 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009702 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009703 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 *fnamelen = (int)(tail - *fnamep);
9705#ifdef VMS
9706 if (*fnamelen > 0)
9707 *fnamelen += 1; /* the path separator is part of the path */
9708#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009709 if (*fnamelen == 0)
9710 {
9711 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9712 p = vim_strsave((char_u *)".");
9713 if (p == NULL)
9714 return -1;
9715 vim_free(*bufp);
9716 *bufp = *fnamep = tail = p;
9717 *fnamelen = 1;
9718 }
9719 else
9720 {
9721 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009722 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 }
9725
9726 /* ":8" - shortname */
9727 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9728 {
9729 *usedlen += 2;
9730#ifdef WIN3264
9731 has_shortname = 1;
9732#endif
9733 }
9734
9735#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009736 /*
9737 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 */
9739 if (has_shortname)
9740 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009741 /* Copy the string if it is shortened by :h and when it wasn't copied
9742 * yet, because we are going to change it in place. Avoids changing
9743 * the buffer name for "%:8". */
9744 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 {
9746 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009747 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 return -1;
9749 vim_free(*bufp);
9750 *bufp = *fnamep = p;
9751 }
9752
9753 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009754 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (!has_fullname && !vim_isAbsName(*fnamep))
9756 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009757 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 return -1;
9759 }
9760 else
9761 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009762 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
Bram Moolenaardc935552011-08-17 15:23:23 +02009764 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009766 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 return -1;
9768
9769 if (l == 0)
9770 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009771 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009773 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 return -1;
9775 }
9776 *fnamelen = l;
9777 }
9778 }
9779#endif /* WIN3264 */
9780
9781 /* ":t" - tail, just the basename */
9782 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9783 {
9784 *usedlen += 2;
9785 *fnamelen -= (int)(tail - *fnamep);
9786 *fnamep = tail;
9787 }
9788
9789 /* ":e" - extension, can be repeated */
9790 /* ":r" - root, without extension, can be repeated */
9791 while (src[*usedlen] == ':'
9792 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9793 {
9794 /* find a '.' in the tail:
9795 * - for second :e: before the current fname
9796 * - otherwise: The last '.'
9797 */
9798 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9799 s = *fnamep - 2;
9800 else
9801 s = *fnamep + *fnamelen - 1;
9802 for ( ; s > tail; --s)
9803 if (s[0] == '.')
9804 break;
9805 if (src[*usedlen + 1] == 'e') /* :e */
9806 {
9807 if (s > tail)
9808 {
9809 *fnamelen += (int)(*fnamep - (s + 1));
9810 *fnamep = s + 1;
9811#ifdef VMS
9812 /* cut version from the extension */
9813 s = *fnamep + *fnamelen - 1;
9814 for ( ; s > *fnamep; --s)
9815 if (s[0] == ';')
9816 break;
9817 if (s > *fnamep)
9818 *fnamelen = s - *fnamep;
9819#endif
9820 }
9821 else if (*fnamep <= tail)
9822 *fnamelen = 0;
9823 }
9824 else /* :r */
9825 {
9826 if (s > tail) /* remove one extension */
9827 *fnamelen = (int)(s - *fnamep);
9828 }
9829 *usedlen += 2;
9830 }
9831
9832 /* ":s?pat?foo?" - substitute */
9833 /* ":gs?pat?foo?" - global substitute */
9834 if (src[*usedlen] == ':'
9835 && (src[*usedlen + 1] == 's'
9836 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9837 {
9838 char_u *str;
9839 char_u *pat;
9840 char_u *sub;
9841 int sep;
9842 char_u *flags;
9843 int didit = FALSE;
9844
9845 flags = (char_u *)"";
9846 s = src + *usedlen + 2;
9847 if (src[*usedlen + 1] == 'g')
9848 {
9849 flags = (char_u *)"g";
9850 ++s;
9851 }
9852
9853 sep = *s++;
9854 if (sep)
9855 {
9856 /* find end of pattern */
9857 p = vim_strchr(s, sep);
9858 if (p != NULL)
9859 {
9860 pat = vim_strnsave(s, (int)(p - s));
9861 if (pat != NULL)
9862 {
9863 s = p + 1;
9864 /* find end of substitution */
9865 p = vim_strchr(s, sep);
9866 if (p != NULL)
9867 {
9868 sub = vim_strnsave(s, (int)(p - s));
9869 str = vim_strnsave(*fnamep, *fnamelen);
9870 if (sub != NULL && str != NULL)
9871 {
9872 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009873 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 if (s != NULL)
9875 {
9876 *fnamep = s;
9877 *fnamelen = (int)STRLEN(s);
9878 vim_free(*bufp);
9879 *bufp = s;
9880 didit = TRUE;
9881 }
9882 }
9883 vim_free(sub);
9884 vim_free(str);
9885 }
9886 vim_free(pat);
9887 }
9888 }
9889 /* after using ":s", repeat all the modifiers */
9890 if (didit)
9891 goto repeat;
9892 }
9893 }
9894
Bram Moolenaar26df0922014-02-23 23:39:13 +01009895 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9896 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009897 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009898 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009899 if (c != NUL)
9900 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009901 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009902 if (c != NUL)
9903 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009904 if (p == NULL)
9905 return -1;
9906 vim_free(*bufp);
9907 *bufp = *fnamep = p;
9908 *fnamelen = (int)STRLEN(p);
9909 *usedlen += 2;
9910 }
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 return valid;
9913}
9914
9915/*
9916 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009917 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 * "flags" can be "g" to do a global substitute.
9919 * Returns an allocated string, NULL for error.
9920 */
9921 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922do_string_sub(
9923 char_u *str,
9924 char_u *pat,
9925 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009926 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009927 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929 int sublen;
9930 regmatch_T regmatch;
9931 int i;
9932 int do_all;
9933 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009934 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 garray_T ga;
9936 char_u *ret;
9937 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009938 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9941 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009942 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
9944 ga_init2(&ga, 1, 200);
9945
9946 do_all = (flags[0] == 'g');
9947
9948 regmatch.rm_ic = p_ic;
9949 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9950 if (regmatch.regprog != NULL)
9951 {
9952 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009953 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9955 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009956 /* Skip empty match except for first match. */
9957 if (regmatch.startp[0] == regmatch.endp[0])
9958 {
9959 if (zero_width == regmatch.startp[0])
9960 {
9961 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009962 i = MB_PTR2LEN(tail);
9963 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9964 (size_t)i);
9965 ga.ga_len += i;
9966 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009967 continue;
9968 }
9969 zero_width = regmatch.startp[0];
9970 }
9971
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 /*
9973 * Get some space for a temporary buffer to do the substitution
9974 * into. It will contain:
9975 * - The text up to where the match is.
9976 * - The substituted text.
9977 * - The text after the match.
9978 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009979 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009980 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9982 {
9983 ga_clear(&ga);
9984 break;
9985 }
9986
9987 /* copy the text up to where the match is */
9988 i = (int)(regmatch.startp[0] - tail);
9989 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9990 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009991 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 + ga.ga_len + i, TRUE, TRUE, FALSE);
9993 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009994 tail = regmatch.endp[0];
9995 if (*tail == NUL)
9996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 if (!do_all)
9998 break;
9999 }
10000
10001 if (ga.ga_data != NULL)
10002 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10003
Bram Moolenaar473de612013-06-08 18:19:48 +020010004 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 }
10006
10007 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10008 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010009 if (p_cpo == empty_option)
10010 p_cpo = save_cpo;
10011 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010012 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010013 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
10015 return ret;
10016}
10017
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010018 static int
10019filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10020{
10021 typval_T rettv;
10022 typval_T argv[3];
10023 char_u buf[NUMBUFLEN];
10024 char_u *s;
10025 int retval = FAIL;
10026 int dummy;
10027
10028 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10029 argv[0] = vimvars[VV_KEY].vv_tv;
10030 argv[1] = vimvars[VV_VAL].vv_tv;
10031 if (expr->v_type == VAR_FUNC)
10032 {
10033 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010034 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10035 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010036 goto theend;
10037 }
10038 else if (expr->v_type == VAR_PARTIAL)
10039 {
10040 partial_T *partial = expr->vval.v_partial;
10041
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010042 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010043 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10044 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010045 goto theend;
10046 }
10047 else
10048 {
10049 s = get_tv_string_buf_chk(expr, buf);
10050 if (s == NULL)
10051 goto theend;
10052 s = skipwhite(s);
10053 if (eval1(&s, &rettv, TRUE) == FAIL)
10054 goto theend;
10055 if (*s != NUL) /* check for trailing chars after expr */
10056 {
10057 EMSG2(_(e_invexpr2), s);
10058 goto theend;
10059 }
10060 }
10061 if (map)
10062 {
10063 /* map(): replace the list item value */
10064 clear_tv(tv);
10065 rettv.v_lock = 0;
10066 *tv = rettv;
10067 }
10068 else
10069 {
10070 int error = FALSE;
10071
10072 /* filter(): when expr is zero remove the item */
10073 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10074 clear_tv(&rettv);
10075 /* On type error, nothing has been removed; return FAIL to stop the
10076 * loop. The error message was given by get_tv_number_chk(). */
10077 if (error)
10078 goto theend;
10079 }
10080 retval = OK;
10081theend:
10082 clear_tv(&vimvars[VV_VAL].vv_tv);
10083 return retval;
10084}
10085
10086
10087/*
10088 * Implementation of map() and filter().
10089 */
10090 void
10091filter_map(typval_T *argvars, typval_T *rettv, int map)
10092{
10093 typval_T *expr;
10094 listitem_T *li, *nli;
10095 list_T *l = NULL;
10096 dictitem_T *di;
10097 hashtab_T *ht;
10098 hashitem_T *hi;
10099 dict_T *d = NULL;
10100 typval_T save_val;
10101 typval_T save_key;
10102 int rem;
10103 int todo;
10104 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10105 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10106 : N_("filter() argument"));
10107 int save_did_emsg;
10108 int idx = 0;
10109
10110 if (argvars[0].v_type == VAR_LIST)
10111 {
10112 if ((l = argvars[0].vval.v_list) == NULL
10113 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10114 return;
10115 }
10116 else if (argvars[0].v_type == VAR_DICT)
10117 {
10118 if ((d = argvars[0].vval.v_dict) == NULL
10119 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10120 return;
10121 }
10122 else
10123 {
10124 EMSG2(_(e_listdictarg), ermsg);
10125 return;
10126 }
10127
10128 expr = &argvars[1];
10129 /* On type errors, the preceding call has already displayed an error
10130 * message. Avoid a misleading error message for an empty string that
10131 * was not passed as argument. */
10132 if (expr->v_type != VAR_UNKNOWN)
10133 {
10134 prepare_vimvar(VV_VAL, &save_val);
10135
10136 /* We reset "did_emsg" to be able to detect whether an error
10137 * occurred during evaluation of the expression. */
10138 save_did_emsg = did_emsg;
10139 did_emsg = FALSE;
10140
10141 prepare_vimvar(VV_KEY, &save_key);
10142 if (argvars[0].v_type == VAR_DICT)
10143 {
10144 vimvars[VV_KEY].vv_type = VAR_STRING;
10145
10146 ht = &d->dv_hashtab;
10147 hash_lock(ht);
10148 todo = (int)ht->ht_used;
10149 for (hi = ht->ht_array; todo > 0; ++hi)
10150 {
10151 if (!HASHITEM_EMPTY(hi))
10152 {
10153 int r;
10154
10155 --todo;
10156 di = HI2DI(hi);
10157 if (map &&
10158 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10159 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10160 break;
10161 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10162 r = filter_map_one(&di->di_tv, expr, map, &rem);
10163 clear_tv(&vimvars[VV_KEY].vv_tv);
10164 if (r == FAIL || did_emsg)
10165 break;
10166 if (!map && rem)
10167 {
10168 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10169 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10170 break;
10171 dictitem_remove(d, di);
10172 }
10173 }
10174 }
10175 hash_unlock(ht);
10176 }
10177 else
10178 {
10179 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10180
10181 for (li = l->lv_first; li != NULL; li = nli)
10182 {
10183 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10184 break;
10185 nli = li->li_next;
10186 vimvars[VV_KEY].vv_nr = idx;
10187 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10188 || did_emsg)
10189 break;
10190 if (!map && rem)
10191 listitem_remove(l, li);
10192 ++idx;
10193 }
10194 }
10195
10196 restore_vimvar(VV_KEY, &save_key);
10197 restore_vimvar(VV_VAL, &save_val);
10198
10199 did_emsg |= save_did_emsg;
10200 }
10201
10202 copy_tv(&argvars[0], rettv);
10203}
10204
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */