blob: 8670e124032bc7e0516e88d36ac21f565c263f02 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200190 {VV_NAME("termrgbresp", VAR_STRING), VV_RO},
191 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
192 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
193 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000194};
195
196/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000197#define vv_type vv_di.di_tv.v_type
198#define vv_nr vv_di.di_tv.vval.v_number
199#define vv_float vv_di.di_tv.vval.v_float
200#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000201#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200202#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000203#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000204
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200205static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000206#define vimvarht vimvardict.dv_hashtab
207
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100208static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
209static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
210static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_glob_vars(int *first);
212static void list_buf_vars(int *first);
213static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static void list_vim_vars(int *first);
216static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
218static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
220static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
222static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
223static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
224static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000225
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static int eval2(char_u **arg, typval_T *rettv, int evaluate);
227static int eval3(char_u **arg, typval_T *rettv, int evaluate);
228static int eval4(char_u **arg, typval_T *rettv, int evaluate);
229static int eval5(char_u **arg, typval_T *rettv, int evaluate);
230static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
231static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000232
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
235static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200240static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static void delete_var(hashtab_T *ht, hashitem_T *hi);
243static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
244static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247/* for VIM_VERSION_ defines */
248#include "version.h"
249
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100250
251#if defined(EBCDIC) || defined(PROTO)
252/*
253 * Compare struct fst by function name.
254 */
255 static int
256compare_func_name(const void *s1, const void *s2)
257{
258 struct fst *p1 = (struct fst *)s1;
259 struct fst *p2 = (struct fst *)s2;
260
261 return STRCMP(p1->f_name, p2->f_name);
262}
263
264/*
265 * Sort the function table by function name.
266 * The sorting of the table above is ASCII dependant.
267 * On machines using EBCDIC we have to sort it.
268 */
269 static void
270sortFunctions(void)
271{
272 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
273
274 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
275}
276#endif
277
278
Bram Moolenaar33570922005-01-25 22:26:29 +0000279/*
280 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000281 */
282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100283eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000284{
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 int i;
286 struct vimvar *p;
287
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200288 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
289 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200290 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000291 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294 for (i = 0; i < VV_LEN; ++i)
295 {
296 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200297 if (STRLEN(p->vv_name) > 16)
298 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100299 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200300 getout(1);
301 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000302 STRCPY(p->vv_di.di_key, p->vv_name);
303 if (p->vv_flags & VV_RO)
304 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
305 else if (p->vv_flags & VV_RO_SBX)
306 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
307 else
308 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000309
310 /* add to v: scope dict, unless the value is not always available */
311 if (p->vv_type != VAR_UNKNOWN)
312 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000313 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000314 /* add to compat scope dict */
315 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000316 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100317 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
318
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000319 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100320 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200321 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100322 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100323
324 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
325 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
326 set_vim_var_nr(VV_NONE, VVAL_NONE);
327 set_vim_var_nr(VV_NULL, VVAL_NULL);
328
Bram Moolenaarf562e722016-07-19 17:25:25 +0200329 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
330 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
331 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
332 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
333 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
334 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
335 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
336 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
337 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
338 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
339
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200340 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200341
342#ifdef EBCDIC
343 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100344 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200345 */
346 sortFunctions();
347#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000348}
349
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000350#if defined(EXITFREE) || defined(PROTO)
351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100352eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000353{
354 int i;
355 struct vimvar *p;
356
357 for (i = 0; i < VV_LEN; ++i)
358 {
359 p = &vimvars[i];
360 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000361 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000362 vim_free(p->vv_str);
363 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 }
365 else if (p->vv_di.di_tv.v_type == VAR_LIST)
366 {
367 list_unref(p->vv_list);
368 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000369 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000370 }
371 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000372 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000373 hash_clear(&compat_hashtab);
374
Bram Moolenaard9fba312005-06-26 22:34:35 +0000375 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200377 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100378# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000379
380 /* global variables */
381 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000382
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000383 /* autoloaded script names */
384 ga_clear_strings(&ga_loaded);
385
Bram Moolenaarcca74132013-09-25 21:00:28 +0200386 /* Script-local variables. First clear all the variables and in a second
387 * loop free the scriptvar_T, because a variable in one script might hold
388 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 ga_clear(&ga_scripts);
394
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000395 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200396 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000397
398 /* functions */
399 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400}
401#endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 * Set an internal variable to a string value. Creates the variable if it does
406 * not already exist.
407 */
408 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100409set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000411 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000412 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 val = vim_strsave(value);
415 if (val != NULL)
416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000420 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
423 }
424}
425
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000426static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200427#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000428static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000429static char_u *redir_endp = NULL;
430static char_u *redir_varname = NULL;
431
432/*
433 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100434 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000435 * Returns OK if successfully completed the setup. FAIL otherwise.
436 */
437 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439{
440 int save_emsg;
441 int err;
442 typval_T tv;
443
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000444 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000445 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000446 {
447 EMSG(_(e_invarg));
448 return FAIL;
449 }
450
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000451 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 redir_varname = vim_strsave(name);
453 if (redir_varname == NULL)
454 return FAIL;
455
456 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
457 if (redir_lval == NULL)
458 {
459 var_redir_stop();
460 return FAIL;
461 }
462
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000463 /* The output is stored in growarray "redir_ga" until redirection ends. */
464 ga_init2(&redir_ga, (int)sizeof(char), 500);
465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000466 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100467 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000468 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
470 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200471 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 if (redir_endp != NULL && *redir_endp != NUL)
473 /* Trailing characters are present after the variable name */
474 EMSG(_(e_trailing));
475 else
476 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000477 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478 var_redir_stop();
479 return FAIL;
480 }
481
482 /* check if we can write to the variable: set it to or append an empty
483 * string */
484 save_emsg = did_emsg;
485 did_emsg = FALSE;
486 tv.v_type = VAR_STRING;
487 tv.vval.v_string = (char_u *)"";
488 if (append)
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
490 else
491 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200492 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000494 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 if (err)
496 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 var_redir_stop();
499 return FAIL;
500 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000501
502 return OK;
503}
504
505/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000506 * Append "value[value_len]" to the variable set by var_redir_start().
507 * The actual appending is postponed until redirection ends, because the value
508 * appended may in fact be the string we write to, changing it may cause freed
509 * memory to be used:
510 * :redir => foo
511 * :let foo
512 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 */
514 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100515var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000517 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518
519 if (redir_lval == NULL)
520 return;
521
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000522 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000526
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000528 {
529 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000531 }
532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534}
535
536/*
537 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000538 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 */
540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100541var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543 typval_T tv;
544
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200545 if (EVALCMD_BUSY)
546 {
547 redir_lval = NULL;
548 return;
549 }
550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 if (redir_lval != NULL)
552 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000553 /* If there was no error: assign the text to the variable. */
554 if (redir_endp != NULL)
555 {
556 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
557 tv.v_type = VAR_STRING;
558 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200559 /* Call get_lval() again, if it's inside a Dict or List it may
560 * have changed. */
561 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100562 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 if (redir_endp != NULL && redir_lval->ll_name != NULL)
564 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
565 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000568 /* free the collected output */
569 vim_free(redir_ga.ga_data);
570 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572 vim_free(redir_lval);
573 redir_lval = NULL;
574 }
575 vim_free(redir_varname);
576 redir_varname = NULL;
577}
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579# if defined(FEAT_MBYTE) || defined(PROTO)
580 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100581eval_charconvert(
582 char_u *enc_from,
583 char_u *enc_to,
584 char_u *fname_from,
585 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 int err = FALSE;
588
589 set_vim_var_string(VV_CC_FROM, enc_from, -1);
590 set_vim_var_string(VV_CC_TO, enc_to, -1);
591 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
592 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
593 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
594 err = TRUE;
595 set_vim_var_string(VV_CC_FROM, NULL, -1);
596 set_vim_var_string(VV_CC_TO, NULL, -1);
597 set_vim_var_string(VV_FNAME_IN, NULL, -1);
598 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
599
600 if (err)
601 return FAIL;
602 return OK;
603}
604# endif
605
606# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 int err = FALSE;
611
612 set_vim_var_string(VV_FNAME_IN, fname, -1);
613 set_vim_var_string(VV_CMDARG, args, -1);
614 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
615 err = TRUE;
616 set_vim_var_string(VV_FNAME_IN, NULL, -1);
617 set_vim_var_string(VV_CMDARG, NULL, -1);
618
619 if (err)
620 {
621 mch_remove(fname);
622 return FAIL;
623 }
624 return OK;
625}
626# endif
627
628# if defined(FEAT_DIFF) || defined(PROTO)
629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630eval_diff(
631 char_u *origfile,
632 char_u *newfile,
633 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int err = FALSE;
636
637 set_vim_var_string(VV_FNAME_IN, origfile, -1);
638 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
639 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
640 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
641 set_vim_var_string(VV_FNAME_IN, NULL, -1);
642 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
643 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
644}
645
646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100647eval_patch(
648 char_u *origfile,
649 char_u *difffile,
650 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err;
653
654 set_vim_var_string(VV_FNAME_IN, origfile, -1);
655 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
656 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
657 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
660 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
661}
662# endif
663
664/*
665 * Top level evaluation function, returning a boolean.
666 * Sets "error" to TRUE if there was an error.
667 * Return TRUE or FALSE.
668 */
669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100670eval_to_bool(
671 char_u *arg,
672 int *error,
673 char_u **nextcmd,
674 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
Bram Moolenaar33570922005-01-25 22:26:29 +0000676 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 if (skip)
680 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000681 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 else
684 {
685 *error = FALSE;
686 if (!skip)
687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000688 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691 }
692 if (skip)
693 --emsg_skip;
694
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200695 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697
698/*
699 * Top level evaluation function, returning a string. If "skip" is TRUE,
700 * only parsing to "nextcmd" is done, without reporting errors. Return
701 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
702 */
703 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704eval_to_string_skip(
705 char_u *arg,
706 char_u **nextcmd,
707 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 char_u *retval;
711
712 if (skip)
713 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = NULL;
716 else
717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000718 retval = vim_strsave(get_tv_string(&tv));
719 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 }
721 if (skip)
722 --emsg_skip;
723
724 return retval;
725}
726
727/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000728 * Skip over an expression at "*pp".
729 * Return FAIL for an error, OK otherwise.
730 */
731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100732skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000733{
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000735
736 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000737 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000738}
739
740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000742 * When "convert" is TRUE convert a List into a sequence of lines and convert
743 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * Return pointer to allocated memory, or NULL for failure.
745 */
746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100747eval_to_string(
748 char_u *arg,
749 char_u **nextcmd,
750 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
Bram Moolenaar33570922005-01-25 22:26:29 +0000752 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000754 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000755#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000756 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000759 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 retval = NULL;
761 else
762 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000763 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000764 {
765 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000766 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200767 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200768 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200769 if (tv.vval.v_list->lv_len > 0)
770 ga_append(&ga, NL);
771 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000772 ga_append(&ga, NUL);
773 retval = (char_u *)ga.ga_data;
774 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000775#ifdef FEAT_FLOAT
776 else if (convert && tv.v_type == VAR_FLOAT)
777 {
778 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
779 retval = vim_strsave(numbuf);
780 }
781#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000782 else
783 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786
787 return retval;
788}
789
790/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000791 * Call eval_to_string() without using current local variables and using
792 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 */
794 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100795eval_to_string_safe(
796 char_u *arg,
797 char_u **nextcmd,
798 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
800 char_u *retval;
801 void *save_funccalp;
802
803 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000804 if (use_sandbox)
805 ++sandbox;
806 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000807 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000808 if (use_sandbox)
809 --sandbox;
810 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 restore_funccal(save_funccalp);
812 return retval;
813}
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815/*
816 * Top level evaluation function, returning a number.
817 * Evaluates "expr" silently.
818 * Returns -1 for an error.
819 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200820 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar33570922005-01-25 22:26:29 +0000823 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200824 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000825 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 ++emsg_off;
828
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 retval = -1;
831 else
832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000833 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836 --emsg_off;
837
838 return retval;
839}
840
Bram Moolenaara40058a2005-07-11 22:42:07 +0000841/*
842 * Prepare v: variable "idx" to be used.
843 * Save the current typeval in "save_tv".
844 * When not used yet add the variable to the v: hashtable.
845 */
846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000848{
849 *save_tv = vimvars[idx].vv_tv;
850 if (vimvars[idx].vv_type == VAR_UNKNOWN)
851 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
852}
853
854/*
855 * Restore v: variable "idx" to typeval "save_tv".
856 * When no longer defined, remove the variable from the v: hashtable.
857 */
858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100859restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000860{
861 hashitem_T *hi;
862
Bram Moolenaara40058a2005-07-11 22:42:07 +0000863 vimvars[idx].vv_tv = *save_tv;
864 if (vimvars[idx].vv_type == VAR_UNKNOWN)
865 {
866 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
867 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100868 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000869 else
870 hash_remove(&vimvarht, hi);
871 }
872}
873
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000874#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000875/*
876 * Evaluate an expression to a list with suggestions.
877 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000878 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000879 */
880 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100881eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000882{
883 typval_T save_val;
884 typval_T rettv;
885 list_T *list = NULL;
886 char_u *p = skipwhite(expr);
887
888 /* Set "v:val" to the bad word. */
889 prepare_vimvar(VV_VAL, &save_val);
890 vimvars[VV_VAL].vv_type = VAR_STRING;
891 vimvars[VV_VAL].vv_str = badword;
892 if (p_verbose == 0)
893 ++emsg_off;
894
895 if (eval1(&p, &rettv, TRUE) == OK)
896 {
897 if (rettv.v_type != VAR_LIST)
898 clear_tv(&rettv);
899 else
900 list = rettv.vval.v_list;
901 }
902
903 if (p_verbose == 0)
904 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000905 restore_vimvar(VV_VAL, &save_val);
906
907 return list;
908}
909
910/*
911 * "list" is supposed to contain two items: a word and a number. Return the
912 * word in "pp" and the number as the return value.
913 * Return -1 if anything isn't right.
914 * Used to get the good word and score from the eval_spell_expr() result.
915 */
916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100917get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000918{
919 listitem_T *li;
920
921 li = list->lv_first;
922 if (li == NULL)
923 return -1;
924 *pp = get_tv_string(&li->li_tv);
925
926 li = li->li_next;
927 if (li == NULL)
928 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200929 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000930}
931#endif
932
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000933/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000934 * Top level evaluation function.
935 * Returns an allocated typval_T with the result.
936 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000937 */
938 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100939eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000940{
941 typval_T *tv;
942
943 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000944 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000945 {
946 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000947 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000948 }
949
950 return tv;
951}
952
953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100955 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000956 * Uses argv[argc] for the function arguments. Only Number and String
957 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000958 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200960 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100961call_vim_function(
962 char_u *func,
963 int argc,
964 char_u **argv,
965 int safe, /* use the sandbox */
966 int str_arg_only, /* all arguments are strings */
967 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200970 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 int len;
972 int i;
973 int doesrange;
974 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000975 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000977 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 for (i = 0; i < argc; i++)
982 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000983 /* Pass a NULL or empty argument as an empty string */
984 if (argv[i] == NULL || *argv[i] == NUL)
985 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000986 argvars[i].v_type = VAR_STRING;
987 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000988 continue;
989 }
990
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200991 if (str_arg_only)
992 len = 0;
993 else
994 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100995 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (len != 0 && len == (int)STRLEN(argv[i]))
997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000998 argvars[i].v_type = VAR_NUMBER;
999 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
1001 else
1002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 argvars[i].v_type = VAR_STRING;
1004 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 }
1007
1008 if (safe)
1009 {
1010 save_funccalp = save_funccal();
1011 ++sandbox;
1012 }
1013
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001014 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001015 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001017 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (safe)
1019 {
1020 --sandbox;
1021 restore_funccal(save_funccalp);
1022 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001023 vim_free(argvars);
1024
1025 if (ret == FAIL)
1026 clear_tv(rettv);
1027
1028 return ret;
1029}
1030
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001031/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001032 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033 * Returns -1 when calling the function fails.
1034 * Uses argv[argc] for the function arguments.
1035 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001036 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037call_func_retnr(
1038 char_u *func,
1039 int argc,
1040 char_u **argv,
1041 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001042{
1043 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001044 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001045
1046 /* All arguments are passed as strings, no conversion to number. */
1047 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1048 return -1;
1049
1050 retval = get_tv_number_chk(&rettv, NULL);
1051 clear_tv(&rettv);
1052 return retval;
1053}
1054
1055#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1056 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1057
Bram Moolenaar4f688582007-07-24 12:34:30 +00001058# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001059/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001060 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001061 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 * Uses argv[argc] for the function arguments.
1063 */
1064 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001065call_func_retstr(
1066 char_u *func,
1067 int argc,
1068 char_u **argv,
1069 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001070{
1071 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001072 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001073
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001074 /* All arguments are passed as strings, no conversion to number. */
1075 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001076 return NULL;
1077
1078 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 return retval;
1081}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001082# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001083
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001084/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001085 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001087 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088 */
1089 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090call_func_retlist(
1091 char_u *func,
1092 int argc,
1093 char_u **argv,
1094 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095{
1096 typval_T rettv;
1097
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001098 /* All arguments are passed as strings, no conversion to number. */
1099 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 return NULL;
1101
1102 if (rettv.v_type != VAR_LIST)
1103 {
1104 clear_tv(&rettv);
1105 return NULL;
1106 }
1107
1108 return rettv.vval.v_list;
1109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110#endif
1111
Bram Moolenaar05159a02005-02-26 23:04:13 +00001112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_FOLDING
1114/*
1115 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1116 * it in "*cp". Doesn't give error messages.
1117 */
1118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001119eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
Bram Moolenaar33570922005-01-25 22:26:29 +00001121 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001122 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001124 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1125 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001128 if (use_sandbox)
1129 ++sandbox;
1130 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 retval = 0;
1134 else
1135 {
1136 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 if (tv.v_type == VAR_NUMBER)
1138 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001139 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 retval = 0;
1141 else
1142 {
1143 /* If the result is a string, check if there is a non-digit before
1144 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (!VIM_ISDIGIT(*s) && *s != '-')
1147 *cp = *s++;
1148 retval = atol((char *)s);
1149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 }
1152 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001153 if (use_sandbox)
1154 --sandbox;
1155 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001157 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158}
1159#endif
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 * ":let" list all variable values
1163 * ":let var1 var2" list variable values
1164 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001165 * ":let var += expr" assignment command.
1166 * ":let var -= expr" assignment command.
1167 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
1170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
1173 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001175 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 int var_count = 0;
1178 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001180 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001181 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
Bram Moolenaardb552d602006-03-23 22:59:57 +00001183 argend = skip_var_list(arg, &var_count, &semicolon);
1184 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001185 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001186 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1187 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001188 expr = skipwhite(argend);
1189 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1190 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001192 /*
1193 * ":let" without "=": list variables
1194 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001195 if (*arg == '[')
1196 EMSG(_(e_invarg));
1197 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001199 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001203 list_glob_vars(&first);
1204 list_buf_vars(&first);
1205 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001206 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001207 list_script_vars(&first);
1208 list_func_vars(&first);
1209 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 eap->nextcmd = check_nextcmd(arg);
1212 }
1213 else
1214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001215 op[0] = '=';
1216 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001217 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001219 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1220 op[0] = *expr; /* +=, -= or .= */
1221 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001223 else
1224 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (eap->skip)
1227 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (eap->skip)
1230 {
1231 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 --emsg_skip;
1234 }
1235 else if (i != FAIL)
1236 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
1242}
1243
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244/*
1245 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1246 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 * When "nextchars" is not NULL it points to a string with characters that
1248 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1249 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001250 * Returns OK or FAIL;
1251 */
1252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253ex_let_vars(
1254 char_u *arg_start,
1255 typval_T *tv,
1256 int copy, /* copy values from "tv", don't move */
1257 int semicolon, /* from skip_var_list() */
1258 int var_count, /* from skip_var_list() */
1259 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260{
1261 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001263 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 listitem_T *item;
1265 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266
1267 if (*arg != '[')
1268 {
1269 /*
1270 * ":let var = expr" or ":for var in list"
1271 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 return OK;
1275 }
1276
1277 /*
1278 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1279 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001280 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281 {
1282 EMSG(_(e_listreq));
1283 return FAIL;
1284 }
1285
1286 i = list_len(l);
1287 if (semicolon == 0 && var_count < i)
1288 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001289 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 return FAIL;
1291 }
1292 if (var_count - semicolon > i)
1293 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001294 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 return FAIL;
1296 }
1297
1298 item = l->lv_first;
1299 while (*arg != ']')
1300 {
1301 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001303 item = item->li_next;
1304 if (arg == NULL)
1305 return FAIL;
1306
1307 arg = skipwhite(arg);
1308 if (*arg == ';')
1309 {
1310 /* Put the rest of the list (may be empty) in the var after ';'.
1311 * Create a new list for this. */
1312 l = list_alloc();
1313 if (l == NULL)
1314 return FAIL;
1315 while (item != NULL)
1316 {
1317 list_append_tv(l, &item->li_tv);
1318 item = item->li_next;
1319 }
1320
1321 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001322 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001323 ltv.vval.v_list = l;
1324 l->lv_refcount = 1;
1325
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001326 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1327 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 clear_tv(&ltv);
1329 if (arg == NULL)
1330 return FAIL;
1331 break;
1332 }
1333 else if (*arg != ',' && *arg != ']')
1334 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001335 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336 return FAIL;
1337 }
1338 }
1339
1340 return OK;
1341}
1342
1343/*
1344 * Skip over assignable variable "var" or list of variables "[var, var]".
1345 * Used for ":let varvar = expr" and ":for varvar in expr".
1346 * For "[var, var]" increment "*var_count" for each variable.
1347 * for "[var, var; var]" set "semicolon".
1348 * Return NULL for an error.
1349 */
1350 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001351skip_var_list(
1352 char_u *arg,
1353 int *var_count,
1354 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355{
1356 char_u *p, *s;
1357
1358 if (*arg == '[')
1359 {
1360 /* "[var, var]": find the matching ']'. */
1361 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001363 {
1364 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1365 s = skip_var_one(p);
1366 if (s == p)
1367 {
1368 EMSG2(_(e_invarg2), p);
1369 return NULL;
1370 }
1371 ++*var_count;
1372
1373 p = skipwhite(s);
1374 if (*p == ']')
1375 break;
1376 else if (*p == ';')
1377 {
1378 if (*semicolon == 1)
1379 {
1380 EMSG(_("Double ; in list of variables"));
1381 return NULL;
1382 }
1383 *semicolon = 1;
1384 }
1385 else if (*p != ',')
1386 {
1387 EMSG2(_(e_invarg2), p);
1388 return NULL;
1389 }
1390 }
1391 return p + 1;
1392 }
1393 else
1394 return skip_var_one(arg);
1395}
1396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001397/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001398 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001399 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001400 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001404 if (*arg == '@' && arg[1] != NUL)
1405 return arg + 2;
1406 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1407 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408}
1409
Bram Moolenaara7043832005-01-21 11:56:39 +00001410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 * List variables for hashtab "ht" with prefix "prefix".
1412 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415list_hashtable_vars(
1416 hashtab_T *ht,
1417 char_u *prefix,
1418 int empty,
1419 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 hashitem_T *hi;
1422 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001423 int todo;
1424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001425 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001426 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1427 {
1428 if (!HASHITEM_EMPTY(hi))
1429 {
1430 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 di = HI2DI(hi);
1432 if (empty || di->di_tv.v_type != VAR_STRING
1433 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001434 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001435 }
1436 }
1437}
1438
1439/*
1440 * List global variables.
1441 */
1442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001443list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001444{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001445 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001446}
1447
1448/*
1449 * List buffer variables.
1450 */
1451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001453{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001454 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001455 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001456}
1457
1458/*
1459 * List window variables.
1460 */
1461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001462list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001463{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001464 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001465 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001466}
1467
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001468/*
1469 * List tab page variables.
1470 */
1471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001473{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001474 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001475 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001476}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477
Bram Moolenaara7043832005-01-21 11:56:39 +00001478/*
1479 * List Vim variables.
1480 */
1481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001484 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485}
1486
1487/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001488 * List script-local variables, if there is a script.
1489 */
1490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001492{
1493 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001494 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1495 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001496}
1497
1498/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 * List variables in "arg".
1500 */
1501 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503{
1504 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001505 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001507 char_u *name_start;
1508 char_u *arg_subsc;
1509 char_u *tofree;
1510 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511
1512 while (!ends_excmd(*arg) && !got_int)
1513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001514 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001515 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001516 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001517 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001518 {
1519 emsg_severe = TRUE;
1520 EMSG(_(e_trailing));
1521 break;
1522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 /* get_name_len() takes care of expanding curly braces */
1527 name_start = name = arg;
1528 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1529 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 /* This is mainly to keep test 49 working: when expanding
1532 * curly braces fails overrule the exception error message. */
1533 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 emsg_severe = TRUE;
1536 EMSG2(_(e_invarg2), arg);
1537 break;
1538 }
1539 error = TRUE;
1540 }
1541 else
1542 {
1543 if (tofree != NULL)
1544 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001545 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 else
1548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001549 /* handle d.key, l[idx], f(expr) */
1550 arg_subsc = arg;
1551 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001552 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001559 case 'g': list_glob_vars(first); break;
1560 case 'b': list_buf_vars(first); break;
1561 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001562 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001563 case 'v': list_vim_vars(first); break;
1564 case 's': list_script_vars(first); break;
1565 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 default:
1567 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001568 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 }
1570 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001571 {
1572 char_u numbuf[NUMBUFLEN];
1573 char_u *tf;
1574 int c;
1575 char_u *s;
1576
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001577 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 c = *arg;
1579 *arg = NUL;
1580 list_one_var_a((char_u *)"",
1581 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001582 tv.v_type,
1583 s == NULL ? (char_u *)"" : s,
1584 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001585 *arg = c;
1586 vim_free(tf);
1587 }
1588 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 }
1591 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592
1593 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595
1596 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 }
1598
1599 return arg;
1600}
1601
1602/*
1603 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1604 * Returns a pointer to the char just after the var name.
1605 * Returns NULL if there is an error.
1606 */
1607 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608ex_let_one(
1609 char_u *arg, /* points to variable name */
1610 typval_T *tv, /* value to assign to variable */
1611 int copy, /* copy value from "tv" */
1612 char_u *endchars, /* valid chars after variable name or NULL */
1613 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614{
1615 int c1;
1616 char_u *name;
1617 char_u *p;
1618 char_u *arg_end = NULL;
1619 int len;
1620 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622
1623 /*
1624 * ":let $VAR = expr": Set environment variable.
1625 */
1626 if (*arg == '$')
1627 {
1628 /* Find the end of the name. */
1629 ++arg;
1630 name = arg;
1631 len = get_env_len(&arg);
1632 if (len == 0)
1633 EMSG2(_(e_invarg2), name - 1);
1634 else
1635 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 if (op != NULL && (*op == '+' || *op == '-'))
1637 EMSG2(_(e_letwrong), op);
1638 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001640 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001641 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 {
1643 c1 = name[len];
1644 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001645 p = get_tv_string_chk(tv);
1646 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 {
1648 int mustfree = FALSE;
1649 char_u *s = vim_getenv(name, &mustfree);
1650
1651 if (s != NULL)
1652 {
1653 p = tofree = concat_str(s, p);
1654 if (mustfree)
1655 vim_free(s);
1656 }
1657 }
1658 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001661 if (STRICMP(name, "HOME") == 0)
1662 init_homedir();
1663 else if (didset_vim && STRICMP(name, "VIM") == 0)
1664 didset_vim = FALSE;
1665 else if (didset_vimruntime
1666 && STRICMP(name, "VIMRUNTIME") == 0)
1667 didset_vimruntime = FALSE;
1668 arg_end = arg;
1669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 }
1673 }
1674 }
1675
1676 /*
1677 * ":let &option = expr": Set option value.
1678 * ":let &l:option = expr": Set local option value.
1679 * ":let &g:option = expr": Set global option value.
1680 */
1681 else if (*arg == '&')
1682 {
1683 /* Find the end of the name. */
1684 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 if (p == NULL || (endchars != NULL
1686 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 EMSG(_(e_letunexp));
1688 else
1689 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 long n;
1691 int opt_type;
1692 long numval;
1693 char_u *stringval = NULL;
1694 char_u *s;
1695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696 c1 = *p;
1697 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001699 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001700 s = get_tv_string_chk(tv); /* != NULL if number or string */
1701 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 {
1703 opt_type = get_option_value(arg, &numval,
1704 &stringval, opt_flags);
1705 if ((opt_type == 1 && *op == '.')
1706 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001709 s = NULL; /* don't set the value */
1710 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001711 else
1712 {
1713 if (opt_type == 1) /* number */
1714 {
1715 if (*op == '+')
1716 n = numval + n;
1717 else
1718 n = numval - n;
1719 }
1720 else if (opt_type == 0 && stringval != NULL) /* string */
1721 {
1722 s = concat_str(stringval, s);
1723 vim_free(stringval);
1724 stringval = s;
1725 }
1726 }
1727 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001728 if (s != NULL)
1729 {
1730 set_option_value(arg, n, s, opt_flags);
1731 arg_end = p;
1732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 }
1736 }
1737
1738 /*
1739 * ":let @r = expr": Set register contents.
1740 */
1741 else if (*arg == '@')
1742 {
1743 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001744 if (op != NULL && (*op == '+' || *op == '-'))
1745 EMSG2(_(e_letwrong), op);
1746 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001748 EMSG(_(e_letunexp));
1749 else
1750 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001751 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 char_u *s;
1753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001754 p = get_tv_string_chk(tv);
1755 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001757 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 if (s != NULL)
1759 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 vim_free(s);
1762 }
1763 }
1764 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001765 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001767 arg_end = arg + 1;
1768 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001769 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 }
1771 }
1772
1773 /*
1774 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001775 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001777 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001779 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001781 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001782 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001784 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1785 EMSG(_(e_letunexp));
1786 else
1787 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001788 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001789 arg_end = p;
1790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 }
1794
1795 else
1796 EMSG2(_(e_invarg2), arg);
1797
1798 return arg_end;
1799}
1800
1801/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001802 * Get an lval: variable, Dict item or List item that can be assigned a value
1803 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1804 * "name.key", "name.key[expr]" etc.
1805 * Indexing only works if "name" is an existing List or Dictionary.
1806 * "name" points to the start of the name.
1807 * If "rettv" is not NULL it points to the value to be assigned.
1808 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1809 * wrong; must end in space or cmd separator.
1810 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001811 * flags:
1812 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001813 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001814 * GLV_NO_AUTOLOAD: do not use script autoloading
1815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001821get_lval(
1822 char_u *name,
1823 typval_T *rettv,
1824 lval_T *lp,
1825 int unlet,
1826 int skip,
1827 int flags, /* GLV_ values */
1828 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 char_u *expr_start, *expr_end;
1832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 dictitem_T *v;
1834 typval_T var1;
1835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001839 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001841 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845
1846 if (skip)
1847 {
1848 /* When skipping just find the end of the name. */
1849 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001850 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001851 }
1852
1853 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001854 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 if (expr_start != NULL)
1856 {
1857 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001858 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001859 && *p != '[' && *p != '.')
1860 {
1861 EMSG(_(e_trailing));
1862 return NULL;
1863 }
1864
1865 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1866 if (lp->ll_exp_name == NULL)
1867 {
1868 /* Report an invalid expression in braces, unless the
1869 * expression evaluation has been cancelled due to an
1870 * aborting error, an interrupt, or an exception. */
1871 if (!aborting() && !quiet)
1872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001873 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 EMSG2(_(e_invarg2), name);
1875 return NULL;
1876 }
1877 }
1878 lp->ll_name = lp->ll_exp_name;
1879 }
1880 else
1881 lp->ll_name = name;
1882
1883 /* Without [idx] or .key we are done. */
1884 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1885 return p;
1886
1887 cc = *p;
1888 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001889 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001890 if (v == NULL && !quiet)
1891 EMSG2(_(e_undefvar), lp->ll_name);
1892 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (v == NULL)
1894 return NULL;
1895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 /*
1897 * Loop until no more [idx] or .key is following.
1898 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001900 var1.v_type = VAR_UNKNOWN;
1901 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1905 && !(lp->ll_tv->v_type == VAR_DICT
1906 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 if (!quiet)
1909 EMSG(_("E689: Can only index a List or Dictionary"));
1910 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 if (!quiet)
1915 EMSG(_("E708: [:] must come last"));
1916 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001918
Bram Moolenaar8c711452005-01-14 21:53:12 +00001919 len = -1;
1920 if (*p == '.')
1921 {
1922 key = p + 1;
1923 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1924 ;
1925 if (len == 0)
1926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001927 if (!quiet)
1928 EMSG(_(e_emptykey));
1929 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001930 }
1931 p = key + len;
1932 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001933 else
1934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001936 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 if (*p == ':')
1938 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001939 else
1940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 empty1 = FALSE;
1942 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001944 if (get_tv_string_chk(&var1) == NULL)
1945 {
1946 /* not a number or string */
1947 clear_tv(&var1);
1948 return NULL;
1949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001950 }
1951
1952 /* Optionally get the second index [ :expr]. */
1953 if (*p == ':')
1954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001959 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (rettv != NULL && (rettv->v_type != VAR_LIST
1963 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 if (!quiet)
1966 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001967 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 }
1970 p = skipwhite(p + 1);
1971 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 else
1974 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1977 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001978 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001981 if (get_tv_string_chk(&var2) == NULL)
1982 {
1983 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001984 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001985 clear_tv(&var2);
1986 return NULL;
1987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001990 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001993
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 if (!quiet)
1997 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001998 clear_tv(&var1);
1999 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 }
2002
2003 /* Skip to past ']'. */
2004 ++p;
2005 }
2006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002008 {
2009 if (len == -1)
2010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002012 key = get_tv_string_chk(&var1); /* is number or string */
2013 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 }
2018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 lp->ll_list = NULL;
2020 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002021 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002022
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002023 /* When assigning to a scope dictionary check that a function and
2024 * variable name is valid (only variable name unless it is l: or
2025 * g: dictionary). Disallow overwriting a builtin function. */
2026 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002027 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002028 int prevval;
2029 int wrong;
2030
2031 if (len != -1)
2032 {
2033 prevval = key[len];
2034 key[len] = NUL;
2035 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002036 else
2037 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002038 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2039 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002040 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002041 || !valid_varname(key);
2042 if (len != -1)
2043 key[len] = prevval;
2044 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002045 return NULL;
2046 }
2047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002050 /* Can't add "v:" variable. */
2051 if (lp->ll_dict == &vimvardict)
2052 {
2053 EMSG2(_(e_illvar), name);
2054 return NULL;
2055 }
2056
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002057 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002062 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 }
2065 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002071 p = NULL;
2072 break;
2073 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002074 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002075 else if ((flags & GLV_READ_ONLY) == 0
2076 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002077 {
2078 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002079 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002080 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002081
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002082 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 }
2085 else
2086 {
2087 /*
2088 * Get the number and item for the only or first index of the List.
2089 */
2090 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002093 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002094 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002095 clear_tv(&var1);
2096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 lp->ll_list = lp->ll_tv->vval.v_list;
2099 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2100 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002102 if (lp->ll_n1 < 0)
2103 {
2104 lp->ll_n1 = 0;
2105 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2106 }
2107 }
2108 if (lp->ll_li == NULL)
2109 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002110 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002111 if (!quiet)
2112 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 }
2115
2116 /*
2117 * May need to find the item or absolute index for the second
2118 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 * When no index given: "lp->ll_empty2" is TRUE.
2120 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002121 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002123 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002124 lp->ll_n2 = (long)get_tv_number(&var2);
2125 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002130 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002131 {
2132 if (!quiet)
2133 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002135 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 }
2138
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2140 if (lp->ll_n1 < 0)
2141 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2142 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002143 {
2144 if (!quiet)
2145 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002147 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002148 }
2149
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 }
2153
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002154 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 return p;
2156}
2157
2158/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002159 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163{
2164 vim_free(lp->ll_exp_name);
2165 vim_free(lp->ll_newkey);
2166}
2167
2168/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002169 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 */
2173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174set_var_lval(
2175 lval_T *lp,
2176 char_u *endp,
2177 typval_T *rettv,
2178 int copy,
2179 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180{
2181 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 listitem_T *ri;
2183 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184
2185 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002187 cc = *endp;
2188 *endp = NUL;
2189 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002191 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002192
Bram Moolenaar79518e22017-02-17 16:31:35 +01002193 /* handle +=, -= and .= */
2194 di = NULL;
2195 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2196 &tv, &di, TRUE, FALSE) == OK)
2197 {
2198 if ((di == NULL
2199 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2200 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2201 FALSE)))
2202 && tv_op(&tv, rettv, op) == OK)
2203 set_var(lp->ll_name, &tv, FALSE);
2204 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002207 else
2208 set_var(lp->ll_name, rettv, copy);
2209 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002211 else if (tv_check_lock(lp->ll_newkey == NULL
2212 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002213 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002214 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 else if (lp->ll_range)
2216 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002217 listitem_T *ll_li = lp->ll_li;
2218 int ll_n1 = lp->ll_n1;
2219
2220 /*
2221 * Check whether any of the list items is locked
2222 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002223 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002224 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002225 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002226 return;
2227 ri = ri->li_next;
2228 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2229 break;
2230 ll_li = ll_li->li_next;
2231 ++ll_n1;
2232 }
2233
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 /*
2235 * Assign the List values to the list items.
2236 */
2237 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002238 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239 if (op != NULL && *op != '=')
2240 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2241 else
2242 {
2243 clear_tv(&lp->ll_li->li_tv);
2244 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2245 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 ri = ri->li_next;
2247 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2248 break;
2249 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002252 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 ri = NULL;
2255 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002256 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002257 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 lp->ll_li = lp->ll_li->li_next;
2259 ++lp->ll_n1;
2260 }
2261 if (ri != NULL)
2262 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002263 else if (lp->ll_empty2
2264 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 : lp->ll_n1 != lp->ll_n2)
2266 EMSG(_("E711: List value has not enough items"));
2267 }
2268 else
2269 {
2270 /*
2271 * Assign to a List or Dictionary item.
2272 */
2273 if (lp->ll_newkey != NULL)
2274 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 if (op != NULL && *op != '=')
2276 {
2277 EMSG2(_(e_letwrong), op);
2278 return;
2279 }
2280
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002282 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 if (di == NULL)
2284 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002285 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2286 {
2287 vim_free(di);
2288 return;
2289 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002291 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002292 else if (op != NULL && *op != '=')
2293 {
2294 tv_op(lp->ll_tv, rettv, op);
2295 return;
2296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002299
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 /*
2301 * Assign the value to the variable or list item.
2302 */
2303 if (copy)
2304 copy_tv(rettv, lp->ll_tv);
2305 else
2306 {
2307 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002308 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 }
2311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312}
2313
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002314/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2316 * Returns OK or FAIL.
2317 */
2318 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002319tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002321 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u numbuf[NUMBUFLEN];
2323 char_u *s;
2324
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002325 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2326 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2327 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 {
2329 switch (tv1->v_type)
2330 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002331 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 case VAR_DICT:
2333 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002334 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002335 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002336 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002337 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 break;
2339
2340 case VAR_LIST:
2341 if (*op != '+' || tv2->v_type != VAR_LIST)
2342 break;
2343 /* List += List */
2344 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2345 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2346 return OK;
2347
2348 case VAR_NUMBER:
2349 case VAR_STRING:
2350 if (tv2->v_type == VAR_LIST)
2351 break;
2352 if (*op == '+' || *op == '-')
2353 {
2354 /* nr += nr or nr -= nr*/
2355 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002356#ifdef FEAT_FLOAT
2357 if (tv2->v_type == VAR_FLOAT)
2358 {
2359 float_T f = n;
2360
2361 if (*op == '+')
2362 f += tv2->vval.v_float;
2363 else
2364 f -= tv2->vval.v_float;
2365 clear_tv(tv1);
2366 tv1->v_type = VAR_FLOAT;
2367 tv1->vval.v_float = f;
2368 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002370#endif
2371 {
2372 if (*op == '+')
2373 n += get_tv_number(tv2);
2374 else
2375 n -= get_tv_number(tv2);
2376 clear_tv(tv1);
2377 tv1->v_type = VAR_NUMBER;
2378 tv1->vval.v_number = n;
2379 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 }
2381 else
2382 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002383 if (tv2->v_type == VAR_FLOAT)
2384 break;
2385
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 /* str .= str */
2387 s = get_tv_string(tv1);
2388 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2389 clear_tv(tv1);
2390 tv1->v_type = VAR_STRING;
2391 tv1->vval.v_string = s;
2392 }
2393 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002394
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002395 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002396#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002397 {
2398 float_T f;
2399
2400 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2401 && tv2->v_type != VAR_NUMBER
2402 && tv2->v_type != VAR_STRING))
2403 break;
2404 if (tv2->v_type == VAR_FLOAT)
2405 f = tv2->vval.v_float;
2406 else
2407 f = get_tv_number(tv2);
2408 if (*op == '+')
2409 tv1->vval.v_float += f;
2410 else
2411 tv1->vval.v_float -= f;
2412 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002413#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002414 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 }
2416 }
2417
2418 EMSG2(_(e_letwrong), op);
2419 return FAIL;
2420}
2421
2422/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002423 * Evaluate the expression used in a ":for var in expr" command.
2424 * "arg" points to "var".
2425 * Set "*errp" to TRUE for an error, FALSE otherwise;
2426 * Return a pointer that holds the info. Null when there is an error.
2427 */
2428 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002429eval_for_line(
2430 char_u *arg,
2431 int *errp,
2432 char_u **nextcmdp,
2433 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434{
Bram Moolenaar33570922005-01-25 22:26:29 +00002435 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002436 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002437 typval_T tv;
2438 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439
2440 *errp = TRUE; /* default: there is an error */
2441
Bram Moolenaar33570922005-01-25 22:26:29 +00002442 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443 if (fi == NULL)
2444 return NULL;
2445
2446 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2447 if (expr == NULL)
2448 return fi;
2449
2450 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002451 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002452 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002453 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454 return fi;
2455 }
2456
2457 if (skip)
2458 ++emsg_skip;
2459 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2460 {
2461 *errp = FALSE;
2462 if (!skip)
2463 {
2464 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002465 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002466 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002468 clear_tv(&tv);
2469 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002470 else if (l == NULL)
2471 {
2472 /* a null list is like an empty list: do nothing */
2473 clear_tv(&tv);
2474 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475 else
2476 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002477 /* No need to increment the refcount, it's already set for the
2478 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002479 fi->fi_list = l;
2480 list_add_watch(l, &fi->fi_lw);
2481 fi->fi_lw.lw_item = l->lv_first;
2482 }
2483 }
2484 }
2485 if (skip)
2486 --emsg_skip;
2487
2488 return fi;
2489}
2490
2491/*
2492 * Use the first item in a ":for" list. Advance to the next.
2493 * Assign the values to the variable (list). "arg" points to the first one.
2494 * Return TRUE when a valid item was found, FALSE when at end of list or
2495 * something wrong.
2496 */
2497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002498next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002499{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002500 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002501 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002502 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503
2504 item = fi->fi_lw.lw_item;
2505 if (item == NULL)
2506 result = FALSE;
2507 else
2508 {
2509 fi->fi_lw.lw_item = item->li_next;
2510 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2511 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2512 }
2513 return result;
2514}
2515
2516/*
2517 * Free the structure used to store info used by ":for".
2518 */
2519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002520free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002521{
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002524 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002525 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002527 list_unref(fi->fi_list);
2528 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529 vim_free(fi);
2530}
2531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2533
2534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002535set_context_for_expression(
2536 expand_T *xp,
2537 char_u *arg,
2538 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 int got_eq = FALSE;
2541 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002542 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002544 if (cmdidx == CMD_let)
2545 {
2546 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002547 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002548 {
2549 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002550 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002551 {
2552 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002553 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002554 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 break;
2556 }
2557 return;
2558 }
2559 }
2560 else
2561 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2562 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 while ((xp->xp_pattern = vim_strpbrk(arg,
2564 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2565 {
2566 c = *xp->xp_pattern;
2567 if (c == '&')
2568 {
2569 c = xp->xp_pattern[1];
2570 if (c == '&')
2571 {
2572 ++xp->xp_pattern;
2573 xp->xp_context = cmdidx != CMD_let || got_eq
2574 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2575 }
2576 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002579 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2580 xp->xp_pattern += 2;
2581
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 else if (c == '$')
2585 {
2586 /* environment variable */
2587 xp->xp_context = EXPAND_ENV_VARS;
2588 }
2589 else if (c == '=')
2590 {
2591 got_eq = TRUE;
2592 xp->xp_context = EXPAND_EXPRESSION;
2593 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002594 else if (c == '#'
2595 && xp->xp_context == EXPAND_EXPRESSION)
2596 {
2597 /* Autoload function/variable contains '#'. */
2598 break;
2599 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002600 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 && xp->xp_context == EXPAND_FUNCTIONS
2602 && vim_strchr(xp->xp_pattern, '(') == NULL)
2603 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002604 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 break;
2606 }
2607 else if (cmdidx != CMD_let || got_eq)
2608 {
2609 if (c == '"') /* string */
2610 {
2611 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2612 if (c == '\\' && xp->xp_pattern[1] != NUL)
2613 ++xp->xp_pattern;
2614 xp->xp_context = EXPAND_NOTHING;
2615 }
2616 else if (c == '\'') /* literal string */
2617 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2620 /* skip */ ;
2621 xp->xp_context = EXPAND_NOTHING;
2622 }
2623 else if (c == '|')
2624 {
2625 if (xp->xp_pattern[1] == '|')
2626 {
2627 ++xp->xp_pattern;
2628 xp->xp_context = EXPAND_EXPRESSION;
2629 }
2630 else
2631 xp->xp_context = EXPAND_COMMANDS;
2632 }
2633 else
2634 xp->xp_context = EXPAND_EXPRESSION;
2635 }
2636 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002637 /* Doesn't look like something valid, expand as an expression
2638 * anyway. */
2639 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 arg = xp->xp_pattern;
2641 if (*arg != NUL)
2642 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2643 /* skip */ ;
2644 }
2645 xp->xp_pattern = arg;
2646}
2647
2648#endif /* FEAT_CMDL_COMPL */
2649
2650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 * ":unlet[!] var1 ... " command.
2652 */
2653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002654ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002656 ex_unletlock(eap, eap->arg, 0);
2657}
2658
2659/*
2660 * ":lockvar" and ":unlockvar" commands
2661 */
2662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002663ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002664{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002666 int deep = 2;
2667
2668 if (eap->forceit)
2669 deep = -1;
2670 else if (vim_isdigit(*arg))
2671 {
2672 deep = getdigits(&arg);
2673 arg = skipwhite(arg);
2674 }
2675
2676 ex_unletlock(eap, arg, deep);
2677}
2678
2679/*
2680 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2681 */
2682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002683ex_unletlock(
2684 exarg_T *eap,
2685 char_u *argstart,
2686 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002687{
2688 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002691 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 do
2694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002696 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002697 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (lv.ll_name == NULL)
2699 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002700 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (name_end != NULL)
2704 {
2705 emsg_severe = TRUE;
2706 EMSG(_(e_trailing));
2707 }
2708 if (!(eap->skip || error))
2709 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 break;
2711 }
2712
2713 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002714 {
2715 if (eap->cmdidx == CMD_unlet)
2716 {
2717 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2718 error = TRUE;
2719 }
2720 else
2721 {
2722 if (do_lock_var(&lv, name_end, deep,
2723 eap->cmdidx == CMD_lockvar) == FAIL)
2724 error = TRUE;
2725 }
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (!eap->skip)
2729 clear_lval(&lv);
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 arg = skipwhite(name_end);
2732 } while (!ends_excmd(*arg));
2733
2734 eap->nextcmd = check_nextcmd(arg);
2735}
2736
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002738do_unlet_var(
2739 lval_T *lp,
2740 char_u *name_end,
2741 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 int ret = OK;
2744 int cc;
2745
2746 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 cc = *name_end;
2749 *name_end = NUL;
2750
2751 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002752 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002756 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002757 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002758 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002759 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002760 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 else if (lp->ll_range)
2762 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002763 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002764 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002765 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002766
2767 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2768 {
2769 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002770 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002771 return FAIL;
2772 ll_li = li;
2773 ++ll_n1;
2774 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775
2776 /* Delete a range of List items. */
2777 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2778 {
2779 li = lp->ll_li->li_next;
2780 listitem_remove(lp->ll_list, lp->ll_li);
2781 lp->ll_li = li;
2782 ++lp->ll_n1;
2783 }
2784 }
2785 else
2786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 /* unlet a List item. */
2789 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002792 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 }
2794
2795 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796}
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798/*
2799 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002800 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 */
2802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002803do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 hashtab_T *ht;
2806 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002807 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002808 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002809 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 ht = find_var_ht(name, &varname);
2812 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002815 if (d == NULL)
2816 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 if (ht == &globvarht)
2818 d = &globvardict;
2819 else if (ht == &compat_hashtab)
2820 d = &vimvardict;
2821 else
2822 {
2823 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2824 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2825 }
2826 if (d == NULL)
2827 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002828 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002829 return FAIL;
2830 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002831 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002832 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002833 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002834 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002835 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002836 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002837 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002838 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002839 || var_check_ro(di->di_flags, name, FALSE)
2840 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002841 return FAIL;
2842
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002843 delete_var(ht, hi);
2844 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 if (forceit)
2848 return OK;
2849 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 return FAIL;
2851}
2852
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002853/*
2854 * Lock or unlock variable indicated by "lp".
2855 * "deep" is the levels to go (-1 for unlimited);
2856 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2857 */
2858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002859do_lock_var(
2860 lval_T *lp,
2861 char_u *name_end,
2862 int deep,
2863 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002864{
2865 int ret = OK;
2866 int cc;
2867 dictitem_T *di;
2868
2869 if (deep == 0) /* nothing to do */
2870 return OK;
2871
2872 if (lp->ll_tv == NULL)
2873 {
2874 cc = *name_end;
2875 *name_end = NUL;
2876
2877 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002878 di = find_var(lp->ll_name, NULL, TRUE);
2879 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002880 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002881 else if ((di->di_flags & DI_FLAGS_FIX)
2882 && di->di_tv.v_type != VAR_DICT
2883 && di->di_tv.v_type != VAR_LIST)
2884 /* For historic reasons this error is not given for a list or dict.
2885 * E.g., the b: dict could be locked/unlocked. */
2886 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002887 else
2888 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002889 if (lock)
2890 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002891 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002892 di->di_flags &= ~DI_FLAGS_LOCK;
2893 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002894 }
2895 *name_end = cc;
2896 }
2897 else if (lp->ll_range)
2898 {
2899 listitem_T *li = lp->ll_li;
2900
2901 /* (un)lock a range of List items. */
2902 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2903 {
2904 item_lock(&li->li_tv, deep, lock);
2905 li = li->li_next;
2906 ++lp->ll_n1;
2907 }
2908 }
2909 else if (lp->ll_list != NULL)
2910 /* (un)lock a List item. */
2911 item_lock(&lp->ll_li->li_tv, deep, lock);
2912 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002913 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 item_lock(&lp->ll_di->di_tv, deep, lock);
2915
2916 return ret;
2917}
2918
2919/*
2920 * Lock or unlock an item. "deep" is nr of levels to go.
2921 */
2922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002924{
2925 static int recurse = 0;
2926 list_T *l;
2927 listitem_T *li;
2928 dict_T *d;
2929 hashitem_T *hi;
2930 int todo;
2931
2932 if (recurse >= DICT_MAXNEST)
2933 {
2934 EMSG(_("E743: variable nested too deep for (un)lock"));
2935 return;
2936 }
2937 if (deep == 0)
2938 return;
2939 ++recurse;
2940
2941 /* lock/unlock the item itself */
2942 if (lock)
2943 tv->v_lock |= VAR_LOCKED;
2944 else
2945 tv->v_lock &= ~VAR_LOCKED;
2946
2947 switch (tv->v_type)
2948 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002949 case VAR_UNKNOWN:
2950 case VAR_NUMBER:
2951 case VAR_STRING:
2952 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002953 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002954 case VAR_FLOAT:
2955 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002956 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002957 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002958 break;
2959
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 case VAR_LIST:
2961 if ((l = tv->vval.v_list) != NULL)
2962 {
2963 if (lock)
2964 l->lv_lock |= VAR_LOCKED;
2965 else
2966 l->lv_lock &= ~VAR_LOCKED;
2967 if (deep < 0 || deep > 1)
2968 /* recursive: lock/unlock the items the List contains */
2969 for (li = l->lv_first; li != NULL; li = li->li_next)
2970 item_lock(&li->li_tv, deep - 1, lock);
2971 }
2972 break;
2973 case VAR_DICT:
2974 if ((d = tv->vval.v_dict) != NULL)
2975 {
2976 if (lock)
2977 d->dv_lock |= VAR_LOCKED;
2978 else
2979 d->dv_lock &= ~VAR_LOCKED;
2980 if (deep < 0 || deep > 1)
2981 {
2982 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002983 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002984 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2985 {
2986 if (!HASHITEM_EMPTY(hi))
2987 {
2988 --todo;
2989 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2990 }
2991 }
2992 }
2993 }
2994 }
2995 --recurse;
2996}
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2999/*
3000 * Delete all "menutrans_" variables.
3001 */
3002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003003del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003006 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003009 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003010 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003011 {
3012 if (!HASHITEM_EMPTY(hi))
3013 {
3014 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003015 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3016 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003017 }
3018 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021#endif
3022
3023#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3024
3025/*
3026 * Local string buffer for the next two functions to store a variable name
3027 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3028 * get_user_var_name().
3029 */
3030
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003031static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033static char_u *varnamebuf = NULL;
3034static int varnamebuflen = 0;
3035
3036/*
3037 * Function to concatenate a prefix and a variable name.
3038 */
3039 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003040cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 int len;
3043
3044 len = (int)STRLEN(name) + 3;
3045 if (len > varnamebuflen)
3046 {
3047 vim_free(varnamebuf);
3048 len += 10; /* some additional space */
3049 varnamebuf = alloc(len);
3050 if (varnamebuf == NULL)
3051 {
3052 varnamebuflen = 0;
3053 return NULL;
3054 }
3055 varnamebuflen = len;
3056 }
3057 *varnamebuf = prefix;
3058 varnamebuf[1] = ':';
3059 STRCPY(varnamebuf + 2, name);
3060 return varnamebuf;
3061}
3062
3063/*
3064 * Function given to ExpandGeneric() to obtain the list of user defined
3065 * (global/buffer/window/built-in) variable names.
3066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003070 static long_u gdone;
3071 static long_u bdone;
3072 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003073 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003074 static int vidx;
3075 static hashitem_T *hi;
3076 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003079 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003080 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003081 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003082 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003083
3084 /* Global variables */
3085 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003087 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003089 else
3090 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003091 while (HASHITEM_EMPTY(hi))
3092 ++hi;
3093 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3094 return cat_prefix_varname('g', hi->hi_key);
3095 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003097
3098 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003099 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003100 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003102 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003104 else
3105 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003106 while (HASHITEM_EMPTY(hi))
3107 ++hi;
3108 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003110
3111 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003112 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003115 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003117 else
3118 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003119 while (HASHITEM_EMPTY(hi))
3120 ++hi;
3121 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003123
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003124 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003125 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003126 if (tdone < ht->ht_used)
3127 {
3128 if (tdone++ == 0)
3129 hi = ht->ht_array;
3130 else
3131 ++hi;
3132 while (HASHITEM_EMPTY(hi))
3133 ++hi;
3134 return cat_prefix_varname('t', hi->hi_key);
3135 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003136
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 /* v: variables */
3138 if (vidx < VV_LEN)
3139 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 vim_free(varnamebuf);
3142 varnamebuf = NULL;
3143 varnamebuflen = 0;
3144 return NULL;
3145}
3146
3147#endif /* FEAT_CMDL_COMPL */
3148
3149/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003150 * Return TRUE if "pat" matches "text".
3151 * Does not use 'cpo' and always uses 'magic'.
3152 */
3153 static int
3154pattern_match(char_u *pat, char_u *text, int ic)
3155{
3156 int matches = FALSE;
3157 char_u *save_cpo;
3158 regmatch_T regmatch;
3159
3160 /* avoid 'l' flag in 'cpoptions' */
3161 save_cpo = p_cpo;
3162 p_cpo = (char_u *)"";
3163 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3164 if (regmatch.regprog != NULL)
3165 {
3166 regmatch.rm_ic = ic;
3167 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3168 vim_regfree(regmatch.regprog);
3169 }
3170 p_cpo = save_cpo;
3171 return matches;
3172}
3173
3174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 * types for expressions.
3176 */
3177typedef enum
3178{
3179 TYPE_UNKNOWN = 0
3180 , TYPE_EQUAL /* == */
3181 , TYPE_NEQUAL /* != */
3182 , TYPE_GREATER /* > */
3183 , TYPE_GEQUAL /* >= */
3184 , TYPE_SMALLER /* < */
3185 , TYPE_SEQUAL /* <= */
3186 , TYPE_MATCH /* =~ */
3187 , TYPE_NOMATCH /* !~ */
3188} exptype_T;
3189
3190/*
3191 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003192 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3194 */
3195
3196/*
3197 * Handle zero level expression.
3198 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003199 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003200 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 * Return OK or FAIL.
3202 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204eval0(
3205 char_u *arg,
3206 typval_T *rettv,
3207 char_u **nextcmd,
3208 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int ret;
3211 char_u *p;
3212
3213 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003214 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (ret == FAIL || !ends_excmd(*p))
3216 {
3217 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /*
3220 * Report the invalid expression unless the expression evaluation has
3221 * been cancelled due to an aborting error, an interrupt, or an
3222 * exception.
3223 */
3224 if (!aborting())
3225 EMSG2(_(e_invexpr2), arg);
3226 ret = FAIL;
3227 }
3228 if (nextcmd != NULL)
3229 *nextcmd = check_nextcmd(p);
3230
3231 return ret;
3232}
3233
3234/*
3235 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003236 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 *
3238 * "arg" must point to the first non-white of the expression.
3239 * "arg" is advanced to the next non-white after the recognized expression.
3240 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003241 * Note: "rettv.v_lock" is not set.
3242 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 * Return OK or FAIL.
3244 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003246eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 /*
3252 * Get the first variable.
3253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003254 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 return FAIL;
3256
3257 if ((*arg)[0] == '?')
3258 {
3259 result = FALSE;
3260 if (evaluate)
3261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003262 int error = FALSE;
3263
3264 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003266 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 if (error)
3268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270
3271 /*
3272 * Get the second variable.
3273 */
3274 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003275 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return FAIL;
3277
3278 /*
3279 * Check for the ":".
3280 */
3281 if ((*arg)[0] != ':')
3282 {
3283 EMSG(_("E109: Missing ':' after '?'"));
3284 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003285 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return FAIL;
3287 }
3288
3289 /*
3290 * Get the third variable.
3291 */
3292 *arg = skipwhite(*arg + 1);
3293 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3294 {
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 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302
3303 return OK;
3304}
3305
3306/*
3307 * Handle first level expression:
3308 * expr2 || expr2 || expr2 logical OR
3309 *
3310 * "arg" must point to the first non-white of the expression.
3311 * "arg" is advanced to the next non-white after the recognized expression.
3312 *
3313 * Return OK or FAIL.
3314 */
3315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003316eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 long result;
3320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
3323 /*
3324 * Get the first variable.
3325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003326 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return FAIL;
3328
3329 /*
3330 * Repeat until there is no following "||".
3331 */
3332 first = TRUE;
3333 result = FALSE;
3334 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3335 {
3336 if (evaluate && first)
3337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003341 if (error)
3342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 first = FALSE;
3344 }
3345
3346 /*
3347 * Get the second variable.
3348 */
3349 *arg = skipwhite(*arg + 2);
3350 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3351 return FAIL;
3352
3353 /*
3354 * Compute the result.
3355 */
3356 if (evaluate && !result)
3357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003361 if (error)
3362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364 if (evaluate)
3365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 rettv->v_type = VAR_NUMBER;
3367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 }
3370
3371 return OK;
3372}
3373
3374/*
3375 * Handle second level expression:
3376 * expr3 && expr3 && expr3 logical AND
3377 *
3378 * "arg" must point to the first non-white of the expression.
3379 * "arg" is advanced to the next non-white after the recognized expression.
3380 *
3381 * Return OK or FAIL.
3382 */
3383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003384eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaar33570922005-01-25 22:26:29 +00003386 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 long result;
3388 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003389 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 /*
3392 * Get the first variable.
3393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return FAIL;
3396
3397 /*
3398 * Repeat until there is no following "&&".
3399 */
3400 first = TRUE;
3401 result = TRUE;
3402 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3403 {
3404 if (evaluate && first)
3405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003409 if (error)
3410 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 first = FALSE;
3412 }
3413
3414 /*
3415 * Get the second variable.
3416 */
3417 *arg = skipwhite(*arg + 2);
3418 if (eval4(arg, &var2, evaluate && result) == FAIL)
3419 return FAIL;
3420
3421 /*
3422 * Compute the result.
3423 */
3424 if (evaluate && result)
3425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 if (error)
3430 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
3432 if (evaluate)
3433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003434 rettv->v_type = VAR_NUMBER;
3435 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 }
3438
3439 return OK;
3440}
3441
3442/*
3443 * Handle third level expression:
3444 * var1 == var2
3445 * var1 =~ var2
3446 * var1 != var2
3447 * var1 !~ var2
3448 * var1 > var2
3449 * var1 >= var2
3450 * var1 < var2
3451 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003452 * var1 is var2
3453 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 *
3455 * "arg" must point to the first non-white of the expression.
3456 * "arg" is advanced to the next non-white after the recognized expression.
3457 *
3458 * Return OK or FAIL.
3459 */
3460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 char_u *p;
3465 int i;
3466 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003467 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003469 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 char_u *s1, *s2;
3471 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474 /*
3475 * Get the first variable.
3476 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003477 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return FAIL;
3479
3480 p = *arg;
3481 switch (p[0])
3482 {
3483 case '=': if (p[1] == '=')
3484 type = TYPE_EQUAL;
3485 else if (p[1] == '~')
3486 type = TYPE_MATCH;
3487 break;
3488 case '!': if (p[1] == '=')
3489 type = TYPE_NEQUAL;
3490 else if (p[1] == '~')
3491 type = TYPE_NOMATCH;
3492 break;
3493 case '>': if (p[1] != '=')
3494 {
3495 type = TYPE_GREATER;
3496 len = 1;
3497 }
3498 else
3499 type = TYPE_GEQUAL;
3500 break;
3501 case '<': if (p[1] != '=')
3502 {
3503 type = TYPE_SMALLER;
3504 len = 1;
3505 }
3506 else
3507 type = TYPE_SEQUAL;
3508 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003509 case 'i': if (p[1] == 's')
3510 {
3511 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3512 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003513 i = p[len];
3514 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003515 {
3516 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3517 type_is = TRUE;
3518 }
3519 }
3520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522
3523 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003524 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
3526 if (type != TYPE_UNKNOWN)
3527 {
3528 /* extra question mark appended: ignore case */
3529 if (p[len] == '?')
3530 {
3531 ic = TRUE;
3532 ++len;
3533 }
3534 /* extra '#' appended: match case */
3535 else if (p[len] == '#')
3536 {
3537 ic = FALSE;
3538 ++len;
3539 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003540 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 else
3542 ic = p_ic;
3543
3544 /*
3545 * Get the second variable.
3546 */
3547 *arg = skipwhite(p + len);
3548 if (eval5(arg, &var2, evaluate) == FAIL)
3549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return FAIL;
3552 }
3553
3554 if (evaluate)
3555 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003556 if (type_is && rettv->v_type != var2.v_type)
3557 {
3558 /* For "is" a different type always means FALSE, for "notis"
3559 * it means TRUE. */
3560 n1 = (type == TYPE_NEQUAL);
3561 }
3562 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3563 {
3564 if (type_is)
3565 {
3566 n1 = (rettv->v_type == var2.v_type
3567 && rettv->vval.v_list == var2.vval.v_list);
3568 if (type == TYPE_NEQUAL)
3569 n1 = !n1;
3570 }
3571 else if (rettv->v_type != var2.v_type
3572 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3573 {
3574 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003575 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003576 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003577 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003578 clear_tv(rettv);
3579 clear_tv(&var2);
3580 return FAIL;
3581 }
3582 else
3583 {
3584 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003585 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3586 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003587 if (type == TYPE_NEQUAL)
3588 n1 = !n1;
3589 }
3590 }
3591
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003592 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3593 {
3594 if (type_is)
3595 {
3596 n1 = (rettv->v_type == var2.v_type
3597 && rettv->vval.v_dict == var2.vval.v_dict);
3598 if (type == TYPE_NEQUAL)
3599 n1 = !n1;
3600 }
3601 else if (rettv->v_type != var2.v_type
3602 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3603 {
3604 if (rettv->v_type != var2.v_type)
3605 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3606 else
3607 EMSG(_("E736: Invalid operation for Dictionary"));
3608 clear_tv(rettv);
3609 clear_tv(&var2);
3610 return FAIL;
3611 }
3612 else
3613 {
3614 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003615 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3616 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003617 if (type == TYPE_NEQUAL)
3618 n1 = !n1;
3619 }
3620 }
3621
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003622 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3623 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003624 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003625 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003626 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003627 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003628 clear_tv(rettv);
3629 clear_tv(&var2);
3630 return FAIL;
3631 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003632 if ((rettv->v_type == VAR_PARTIAL
3633 && rettv->vval.v_partial == NULL)
3634 || (var2.v_type == VAR_PARTIAL
3635 && var2.vval.v_partial == NULL))
3636 /* when a partial is NULL assume not equal */
3637 n1 = FALSE;
3638 else if (type_is)
3639 {
3640 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3641 /* strings are considered the same if their value is
3642 * the same */
3643 n1 = tv_equal(rettv, &var2, ic, FALSE);
3644 else if (rettv->v_type == VAR_PARTIAL
3645 && var2.v_type == VAR_PARTIAL)
3646 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3647 else
3648 n1 = FALSE;
3649 }
3650 else
3651 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003652 if (type == TYPE_NEQUAL)
3653 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003654 }
3655
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003656#ifdef FEAT_FLOAT
3657 /*
3658 * If one of the two variables is a float, compare as a float.
3659 * When using "=~" or "!~", always compare as string.
3660 */
3661 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3662 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3663 {
3664 float_T f1, f2;
3665
3666 if (rettv->v_type == VAR_FLOAT)
3667 f1 = rettv->vval.v_float;
3668 else
3669 f1 = get_tv_number(rettv);
3670 if (var2.v_type == VAR_FLOAT)
3671 f2 = var2.vval.v_float;
3672 else
3673 f2 = get_tv_number(&var2);
3674 n1 = FALSE;
3675 switch (type)
3676 {
3677 case TYPE_EQUAL: n1 = (f1 == f2); break;
3678 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3679 case TYPE_GREATER: n1 = (f1 > f2); break;
3680 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3681 case TYPE_SMALLER: n1 = (f1 < f2); break;
3682 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3683 case TYPE_UNKNOWN:
3684 case TYPE_MATCH:
3685 case TYPE_NOMATCH: break; /* avoid gcc warning */
3686 }
3687 }
3688#endif
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 /*
3691 * If one of the two variables is a number, compare as a number.
3692 * When using "=~" or "!~", always compare as string.
3693 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003694 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 n1 = get_tv_number(rettv);
3698 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 switch (type)
3700 {
3701 case TYPE_EQUAL: n1 = (n1 == n2); break;
3702 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3703 case TYPE_GREATER: n1 = (n1 > n2); break;
3704 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3705 case TYPE_SMALLER: n1 = (n1 < n2); break;
3706 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3707 case TYPE_UNKNOWN:
3708 case TYPE_MATCH:
3709 case TYPE_NOMATCH: break; /* avoid gcc warning */
3710 }
3711 }
3712 else
3713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 s1 = get_tv_string_buf(rettv, buf1);
3715 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3717 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3718 else
3719 i = 0;
3720 n1 = FALSE;
3721 switch (type)
3722 {
3723 case TYPE_EQUAL: n1 = (i == 0); break;
3724 case TYPE_NEQUAL: n1 = (i != 0); break;
3725 case TYPE_GREATER: n1 = (i > 0); break;
3726 case TYPE_GEQUAL: n1 = (i >= 0); break;
3727 case TYPE_SMALLER: n1 = (i < 0); break;
3728 case TYPE_SEQUAL: n1 = (i <= 0); break;
3729
3730 case TYPE_MATCH:
3731 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003732 n1 = pattern_match(s2, s1, ic);
3733 if (type == TYPE_NOMATCH)
3734 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 break;
3736
3737 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3738 }
3739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003740 clear_tv(rettv);
3741 clear_tv(&var2);
3742 rettv->v_type = VAR_NUMBER;
3743 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746
3747 return OK;
3748}
3749
3750/*
3751 * Handle fourth level expression:
3752 * + number addition
3753 * - number subtraction
3754 * . string concatenation
3755 *
3756 * "arg" must point to the first non-white of the expression.
3757 * "arg" is advanced to the next non-white after the recognized expression.
3758 *
3759 * Return OK or FAIL.
3760 */
3761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003762eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 typval_T var2;
3765 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003767 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003768#ifdef FEAT_FLOAT
3769 float_T f1 = 0, f2 = 0;
3770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 char_u *s1, *s2;
3772 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3773 char_u *p;
3774
3775 /*
3776 * Get the first variable.
3777 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003778 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return FAIL;
3780
3781 /*
3782 * Repeat computing, until no '+', '-' or '.' is following.
3783 */
3784 for (;;)
3785 {
3786 op = **arg;
3787 if (op != '+' && op != '-' && op != '.')
3788 break;
3789
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003790 if ((op != '+' || rettv->v_type != VAR_LIST)
3791#ifdef FEAT_FLOAT
3792 && (op == '.' || rettv->v_type != VAR_FLOAT)
3793#endif
3794 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 {
3796 /* For "list + ...", an illegal use of the first operand as
3797 * a number cannot be determined before evaluating the 2nd
3798 * operand: if this is also a list, all is ok.
3799 * For "something . ...", "something - ..." or "non-list + ...",
3800 * we know that the first operand needs to be a string or number
3801 * without evaluating the 2nd operand. So check before to avoid
3802 * side effects after an error. */
3803 if (evaluate && get_tv_string_chk(rettv) == NULL)
3804 {
3805 clear_tv(rettv);
3806 return FAIL;
3807 }
3808 }
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 /*
3811 * Get the second variable.
3812 */
3813 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003814 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return FAIL;
3818 }
3819
3820 if (evaluate)
3821 {
3822 /*
3823 * Compute the result.
3824 */
3825 if (op == '.')
3826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003827 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3828 s2 = get_tv_string_buf_chk(&var2, buf2);
3829 if (s2 == NULL) /* type error ? */
3830 {
3831 clear_tv(rettv);
3832 clear_tv(&var2);
3833 return FAIL;
3834 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003835 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 clear_tv(rettv);
3837 rettv->v_type = VAR_STRING;
3838 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003840 else if (op == '+' && rettv->v_type == VAR_LIST
3841 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003842 {
3843 /* concatenate Lists */
3844 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3845 &var3) == FAIL)
3846 {
3847 clear_tv(rettv);
3848 clear_tv(&var2);
3849 return FAIL;
3850 }
3851 clear_tv(rettv);
3852 *rettv = var3;
3853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 else
3855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 int error = FALSE;
3857
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003858#ifdef FEAT_FLOAT
3859 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861 f1 = rettv->vval.v_float;
3862 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864 else
3865#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 n1 = get_tv_number_chk(rettv, &error);
3868 if (error)
3869 {
3870 /* This can only happen for "list + non-list". For
3871 * "non-list + ..." or "something - ...", we returned
3872 * before evaluating the 2nd operand. */
3873 clear_tv(rettv);
3874 return FAIL;
3875 }
3876#ifdef FEAT_FLOAT
3877 if (var2.v_type == VAR_FLOAT)
3878 f1 = n1;
3879#endif
3880 }
3881#ifdef FEAT_FLOAT
3882 if (var2.v_type == VAR_FLOAT)
3883 {
3884 f2 = var2.vval.v_float;
3885 n2 = 0;
3886 }
3887 else
3888#endif
3889 {
3890 n2 = get_tv_number_chk(&var2, &error);
3891 if (error)
3892 {
3893 clear_tv(rettv);
3894 clear_tv(&var2);
3895 return FAIL;
3896 }
3897#ifdef FEAT_FLOAT
3898 if (rettv->v_type == VAR_FLOAT)
3899 f2 = n2;
3900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003902 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003903
3904#ifdef FEAT_FLOAT
3905 /* If there is a float on either side the result is a float. */
3906 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3907 {
3908 if (op == '+')
3909 f1 = f1 + f2;
3910 else
3911 f1 = f1 - f2;
3912 rettv->v_type = VAR_FLOAT;
3913 rettv->vval.v_float = f1;
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003916#endif
3917 {
3918 if (op == '+')
3919 n1 = n1 + n2;
3920 else
3921 n1 = n1 - n2;
3922 rettv->v_type = VAR_NUMBER;
3923 rettv->vval.v_number = n1;
3924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 }
3929 return OK;
3930}
3931
3932/*
3933 * Handle fifth level expression:
3934 * * number multiplication
3935 * / number division
3936 * % number modulo
3937 *
3938 * "arg" must point to the first non-white of the expression.
3939 * "arg" is advanced to the next non-white after the recognized expression.
3940 *
3941 * Return OK or FAIL.
3942 */
3943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003944eval6(
3945 char_u **arg,
3946 typval_T *rettv,
3947 int evaluate,
3948 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003952 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003953#ifdef FEAT_FLOAT
3954 int use_float = FALSE;
3955 float_T f1 = 0, f2;
3956#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003957 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 /*
3960 * Get the first variable.
3961 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003962 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 return FAIL;
3964
3965 /*
3966 * Repeat computing, until no '*', '/' or '%' is following.
3967 */
3968 for (;;)
3969 {
3970 op = **arg;
3971 if (op != '*' && op != '/' && op != '%')
3972 break;
3973
3974 if (evaluate)
3975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003976#ifdef FEAT_FLOAT
3977 if (rettv->v_type == VAR_FLOAT)
3978 {
3979 f1 = rettv->vval.v_float;
3980 use_float = TRUE;
3981 n1 = 0;
3982 }
3983 else
3984#endif
3985 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003986 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 if (error)
3988 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 else
3991 n1 = 0;
3992
3993 /*
3994 * Get the second variable.
3995 */
3996 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003997 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 return FAIL;
3999
4000 if (evaluate)
4001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004002#ifdef FEAT_FLOAT
4003 if (var2.v_type == VAR_FLOAT)
4004 {
4005 if (!use_float)
4006 {
4007 f1 = n1;
4008 use_float = TRUE;
4009 }
4010 f2 = var2.vval.v_float;
4011 n2 = 0;
4012 }
4013 else
4014#endif
4015 {
4016 n2 = get_tv_number_chk(&var2, &error);
4017 clear_tv(&var2);
4018 if (error)
4019 return FAIL;
4020#ifdef FEAT_FLOAT
4021 if (use_float)
4022 f2 = n2;
4023#endif
4024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
4026 /*
4027 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004028 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004030#ifdef FEAT_FLOAT
4031 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004033 if (op == '*')
4034 f1 = f1 * f2;
4035 else if (op == '/')
4036 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004037# ifdef VMS
4038 /* VMS crashes on divide by zero, work around it */
4039 if (f2 == 0.0)
4040 {
4041 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004042 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004043 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004044 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004045 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004046 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004047 }
4048 else
4049 f1 = f1 / f2;
4050# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004051 /* We rely on the floating point library to handle divide
4052 * by zero to result in "inf" and not a crash. */
4053 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004054# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004057 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004058 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004059 return FAIL;
4060 }
4061 rettv->v_type = VAR_FLOAT;
4062 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004067 if (op == '*')
4068 n1 = n1 * n2;
4069 else if (op == '/')
4070 {
4071 if (n2 == 0) /* give an error message? */
4072 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004073 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004074 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004075 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004076 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004077 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004078 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004079 }
4080 else
4081 n1 = n1 / n2;
4082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004084 {
4085 if (n2 == 0) /* give an error message? */
4086 n1 = 0;
4087 else
4088 n1 = n1 % n2;
4089 }
4090 rettv->v_type = VAR_NUMBER;
4091 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094 }
4095
4096 return OK;
4097}
4098
4099/*
4100 * Handle sixth level expression:
4101 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004102 * "string" string constant
4103 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * &option-name option value
4105 * @r register contents
4106 * identifier variable value
4107 * function() function call
4108 * $VAR environment variable
4109 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004110 * [expr, expr] List
4111 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 *
4113 * Also handle:
4114 * ! in front logical NOT
4115 * - in front unary minus
4116 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 * trailing [] subscript in String or List
4118 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * "arg" must point to the first non-white of the expression.
4121 * "arg" is advanced to the next non-white after the recognized expression.
4122 *
4123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126eval7(
4127 char_u **arg,
4128 typval_T *rettv,
4129 int evaluate,
4130 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004132 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 int len;
4134 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 char_u *start_leader, *end_leader;
4136 int ret = OK;
4137 char_u *alias;
4138
4139 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004146 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 start_leader = *arg;
4149 while (**arg == '!' || **arg == '-' || **arg == '+')
4150 *arg = skipwhite(*arg + 1);
4151 end_leader = *arg;
4152
4153 switch (**arg)
4154 {
4155 /*
4156 * Number constant.
4157 */
4158 case '0':
4159 case '1':
4160 case '2':
4161 case '3':
4162 case '4':
4163 case '5':
4164 case '6':
4165 case '7':
4166 case '8':
4167 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004168 {
4169#ifdef FEAT_FLOAT
4170 char_u *p = skipdigits(*arg + 1);
4171 int get_float = FALSE;
4172
4173 /* We accept a float when the format matches
4174 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004175 * strict to avoid backwards compatibility problems.
4176 * Don't look for a float after the "." operator, so that
4177 * ":let vers = 1.2.3" doesn't fail. */
4178 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004180 get_float = TRUE;
4181 p = skipdigits(p + 2);
4182 if (*p == 'e' || *p == 'E')
4183 {
4184 ++p;
4185 if (*p == '-' || *p == '+')
4186 ++p;
4187 if (!vim_isdigit(*p))
4188 get_float = FALSE;
4189 else
4190 p = skipdigits(p + 1);
4191 }
4192 if (ASCII_ISALPHA(*p) || *p == '.')
4193 get_float = FALSE;
4194 }
4195 if (get_float)
4196 {
4197 float_T f;
4198
4199 *arg += string2float(*arg, &f);
4200 if (evaluate)
4201 {
4202 rettv->v_type = VAR_FLOAT;
4203 rettv->vval.v_float = f;
4204 }
4205 }
4206 else
4207#endif
4208 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004209 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004210 *arg += len;
4211 if (evaluate)
4212 {
4213 rettv->v_type = VAR_NUMBER;
4214 rettv->vval.v_number = n;
4215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /*
4221 * String constant: "string".
4222 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 break;
4225
4226 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004227 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004230 break;
4231
4232 /*
4233 * List: [expr, expr]
4234 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 break;
4237
4238 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004239 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240 * Dictionary: {key: val, key: val}
4241 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004242 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4243 if (ret == NOTDONE)
4244 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004245 break;
4246
4247 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004248 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004250 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 break;
4252
4253 /*
4254 * Environment variable: $VAR.
4255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 break;
4258
4259 /*
4260 * Register contents: @r.
4261 */
4262 case '@': ++*arg;
4263 if (evaluate)
4264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004266 rettv->vval.v_string = get_reg_contents(**arg,
4267 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 if (**arg != NUL)
4270 ++*arg;
4271 break;
4272
4273 /*
4274 * nested expression: (expression).
4275 */
4276 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (**arg == ')')
4279 ++*arg;
4280 else if (ret == OK)
4281 {
4282 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 ret = FAIL;
4285 }
4286 break;
4287
Bram Moolenaar8c711452005-01-14 21:53:12 +00004288 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 break;
4290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004291
4292 if (ret == NOTDONE)
4293 {
4294 /*
4295 * Must be a variable or function name.
4296 * Can also be a curly-braces kind of name: {expr}.
4297 */
4298 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004299 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300 if (alias != NULL)
4301 s = alias;
4302
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004303 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 ret = FAIL;
4305 else
4306 {
4307 if (**arg == '(') /* recursive! */
4308 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004309 partial_T *partial;
4310
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004311 if (!evaluate)
4312 check_vars(s, len);
4313
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 /* If "s" is the name of a variable of type VAR_FUNC
4315 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004316 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004317
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004318 /* Need to make a copy, in case evaluating the arguments makes
4319 * the name invalid. */
4320 s = vim_strsave(s);
4321 if (s == NULL)
4322 ret = FAIL;
4323 else
4324 /* Invoke the function. */
4325 ret = get_func_tv(s, len, rettv, arg,
4326 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4327 &len, evaluate, partial, NULL);
4328 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004329
4330 /* If evaluate is FALSE rettv->v_type was not set in
4331 * get_func_tv, but it's needed in handle_subscript() to parse
4332 * what follows. So set it here. */
4333 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4334 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004335 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004336 rettv->v_type = VAR_FUNC;
4337 }
4338
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 /* Stop the expression evaluation when immediately
4340 * aborting on error, or when an interrupt occurred or
4341 * an exception was thrown but not caught. */
4342 if (aborting())
4343 {
4344 if (ret == OK)
4345 clear_tv(rettv);
4346 ret = FAIL;
4347 }
4348 }
4349 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004350 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004351 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004352 {
4353 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004354 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004356 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004357 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 }
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 *arg = skipwhite(*arg);
4361
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004362 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4363 * expr(expr). */
4364 if (ret == OK)
4365 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 /*
4368 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4369 */
4370 if (ret == OK && evaluate && end_leader > start_leader)
4371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004373 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004374#ifdef FEAT_FLOAT
4375 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004377 if (rettv->v_type == VAR_FLOAT)
4378 f = rettv->vval.v_float;
4379 else
4380#endif
4381 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004382 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 clear_tv(rettv);
4385 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 else
4388 {
4389 while (end_leader > start_leader)
4390 {
4391 --end_leader;
4392 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004393 {
4394#ifdef FEAT_FLOAT
4395 if (rettv->v_type == VAR_FLOAT)
4396 f = !f;
4397 else
4398#endif
4399 val = !val;
4400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004402 {
4403#ifdef FEAT_FLOAT
4404 if (rettv->v_type == VAR_FLOAT)
4405 f = -f;
4406 else
4407#endif
4408 val = -val;
4409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004411#ifdef FEAT_FLOAT
4412 if (rettv->v_type == VAR_FLOAT)
4413 {
4414 clear_tv(rettv);
4415 rettv->vval.v_float = f;
4416 }
4417 else
4418#endif
4419 {
4420 clear_tv(rettv);
4421 rettv->v_type = VAR_NUMBER;
4422 rettv->vval.v_number = val;
4423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426
4427 return ret;
4428}
4429
4430/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004431 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4432 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4434 */
4435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004436eval_index(
4437 char_u **arg,
4438 typval_T *rettv,
4439 int evaluate,
4440 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441{
4442 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004443 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004445 long len = -1;
4446 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004447 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004448 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449
Bram Moolenaara03f2332016-02-06 18:09:59 +01004450 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004452 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004453 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004454 if (verbose)
4455 EMSG(_("E695: Cannot index a Funcref"));
4456 return FAIL;
4457 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004458#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004459 if (verbose)
4460 EMSG(_(e_float_as_string));
4461 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004462#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004463 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004464 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004465 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004466 if (verbose)
4467 EMSG(_("E909: Cannot index a special variable"));
4468 return FAIL;
4469 case VAR_UNKNOWN:
4470 if (evaluate)
4471 return FAIL;
4472 /* FALLTHROUGH */
4473
4474 case VAR_STRING:
4475 case VAR_NUMBER:
4476 case VAR_LIST:
4477 case VAR_DICT:
4478 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004479 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004481 init_tv(&var1);
4482 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 /*
4486 * dict.name
4487 */
4488 key = *arg + 1;
4489 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4490 ;
4491 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004492 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004494 }
4495 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004496 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004497 /*
4498 * something[idx]
4499 *
4500 * Get the (first) variable from inside the [].
4501 */
4502 *arg = skipwhite(*arg + 1);
4503 if (**arg == ':')
4504 empty1 = TRUE;
4505 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4506 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004507 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4508 {
4509 /* not a number or string */
4510 clear_tv(&var1);
4511 return FAIL;
4512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004513
4514 /*
4515 * Get the second variable from inside the [:].
4516 */
4517 if (**arg == ':')
4518 {
4519 range = TRUE;
4520 *arg = skipwhite(*arg + 1);
4521 if (**arg == ']')
4522 empty2 = TRUE;
4523 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004525 if (!empty1)
4526 clear_tv(&var1);
4527 return FAIL;
4528 }
4529 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4530 {
4531 /* not a number or string */
4532 if (!empty1)
4533 clear_tv(&var1);
4534 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 return FAIL;
4536 }
4537 }
4538
4539 /* Check for the ']'. */
4540 if (**arg != ']')
4541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004542 if (verbose)
4543 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 clear_tv(&var1);
4545 if (range)
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
4549 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550 }
4551
4552 if (evaluate)
4553 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 n1 = 0;
4555 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004557 n1 = get_tv_number(&var1);
4558 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 }
4560 if (range)
4561 {
4562 if (empty2)
4563 n2 = -1;
4564 else
4565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 n2 = get_tv_number(&var2);
4567 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004568 }
4569 }
4570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004572 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004573 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004574 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004575 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004576 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004577 case VAR_SPECIAL:
4578 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004579 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004580 break; /* not evaluating, skipping over subscript */
4581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 case VAR_NUMBER:
4583 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 len = (long)STRLEN(s);
4586 if (range)
4587 {
4588 /* The resulting variable is a substring. If the indexes
4589 * are out of range the result is empty. */
4590 if (n1 < 0)
4591 {
4592 n1 = len + n1;
4593 if (n1 < 0)
4594 n1 = 0;
4595 }
4596 if (n2 < 0)
4597 n2 = len + n2;
4598 else if (n2 >= len)
4599 n2 = len;
4600 if (n1 >= len || n2 < 0 || n1 > n2)
4601 s = NULL;
4602 else
4603 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4604 }
4605 else
4606 {
4607 /* The resulting variable is a string of a single
4608 * character. If the index is too big or negative the
4609 * result is empty. */
4610 if (n1 >= len || n1 < 0)
4611 s = NULL;
4612 else
4613 s = vim_strnsave(s + n1, 1);
4614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 clear_tv(rettv);
4616 rettv->v_type = VAR_STRING;
4617 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 break;
4619
4620 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 if (n1 < 0)
4623 n1 = len + n1;
4624 if (!empty1 && (n1 < 0 || n1 >= len))
4625 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004626 /* For a range we allow invalid values and return an empty
4627 * list. A list index out of range is an error. */
4628 if (!range)
4629 {
4630 if (verbose)
4631 EMSGN(_(e_listidx), n1);
4632 return FAIL;
4633 }
4634 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635 }
4636 if (range)
4637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004638 list_T *l;
4639 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640
4641 if (n2 < 0)
4642 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004643 else if (n2 >= len)
4644 n2 = len - 1;
4645 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004646 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 l = list_alloc();
4648 if (l == NULL)
4649 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651 n1 <= n2; ++n1)
4652 {
4653 if (list_append_tv(l, &item->li_tv) == FAIL)
4654 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004655 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656 return FAIL;
4657 }
4658 item = item->li_next;
4659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004661 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 }
4663 else
4664 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004665 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
4667 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 }
4669 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670
4671 case VAR_DICT:
4672 if (range)
4673 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004674 if (verbose)
4675 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 if (len == -1)
4677 clear_tv(&var1);
4678 return FAIL;
4679 }
4680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004681 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682
4683 if (len == -1)
4684 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004685 key = get_tv_string_chk(&var1);
4686 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 clear_tv(&var1);
4689 return FAIL;
4690 }
4691 }
4692
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004693 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004695 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004696 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 if (len == -1)
4698 clear_tv(&var1);
4699 if (item == NULL)
4700 return FAIL;
4701
4702 copy_tv(&item->di_tv, &var1);
4703 clear_tv(rettv);
4704 *rettv = var1;
4705 }
4706 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 }
4708 }
4709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 return OK;
4711}
4712
4713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 * Get an option value.
4715 * "arg" points to the '&' or '+' before the option name.
4716 * "arg" is advanced to character after the option name.
4717 * Return OK or FAIL.
4718 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004720get_option_tv(
4721 char_u **arg,
4722 typval_T *rettv, /* when NULL, only check if option exists */
4723 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 char_u *option_end;
4726 long numval;
4727 char_u *stringval;
4728 int opt_type;
4729 int c;
4730 int working = (**arg == '+'); /* has("+option") */
4731 int ret = OK;
4732 int opt_flags;
4733
4734 /*
4735 * Isolate the option name and find its value.
4736 */
4737 option_end = find_option_end(arg, &opt_flags);
4738 if (option_end == NULL)
4739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004740 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 EMSG2(_("E112: Option name missing: %s"), *arg);
4742 return FAIL;
4743 }
4744
4745 if (!evaluate)
4746 {
4747 *arg = option_end;
4748 return OK;
4749 }
4750
4751 c = *option_end;
4752 *option_end = NUL;
4753 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 if (opt_type == -3) /* invalid name */
4757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 EMSG2(_("E113: Unknown option: %s"), *arg);
4760 ret = FAIL;
4761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 if (opt_type == -2) /* hidden string option */
4765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 rettv->v_type = VAR_STRING;
4767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 else if (opt_type == -1) /* hidden number option */
4770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 rettv->v_type = VAR_NUMBER;
4772 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774 else if (opt_type == 1) /* number option */
4775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004776 rettv->v_type = VAR_NUMBER;
4777 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
4779 else /* string option */
4780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 }
4785 else if (working && (opt_type == -2 || opt_type == -1))
4786 ret = FAIL;
4787
4788 *option_end = c; /* put back for error messages */
4789 *arg = option_end;
4790
4791 return ret;
4792}
4793
4794/*
4795 * Allocate a variable for a string constant.
4796 * Return OK or FAIL.
4797 */
4798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004799get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
4801 char_u *p;
4802 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 int extra = 0;
4804
4805 /*
4806 * Find the end of the string, skipping backslashed characters.
4807 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004808 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
4810 if (*p == '\\' && p[1] != NUL)
4811 {
4812 ++p;
4813 /* A "\<x>" form occupies at least 4 characters, and produces up
4814 * to 6 characters: reserve space for 2 extra */
4815 if (*p == '<')
4816 extra += 2;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819
4820 if (*p != '"')
4821 {
4822 EMSG2(_("E114: Missing quote: %s"), *arg);
4823 return FAIL;
4824 }
4825
4826 /* If only parsing, set *arg and return here */
4827 if (!evaluate)
4828 {
4829 *arg = p + 1;
4830 return OK;
4831 }
4832
4833 /*
4834 * Copy the string into allocated memory, handling backslashed
4835 * characters.
4836 */
4837 name = alloc((unsigned)(p - *arg + extra));
4838 if (name == NULL)
4839 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 rettv->v_type = VAR_STRING;
4841 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 if (*p == '\\')
4846 {
4847 switch (*++p)
4848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 case 'b': *name++ = BS; ++p; break;
4850 case 'e': *name++ = ESC; ++p; break;
4851 case 'f': *name++ = FF; ++p; break;
4852 case 'n': *name++ = NL; ++p; break;
4853 case 'r': *name++ = CAR; ++p; break;
4854 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 case 'X': /* hex: "\x1", "\x12" */
4857 case 'x':
4858 case 'u': /* Unicode: "\u0023" */
4859 case 'U':
4860 if (vim_isxdigit(p[1]))
4861 {
4862 int n, nr;
4863 int c = toupper(*p);
4864
4865 if (c == 'X')
4866 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004867 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004869 else
4870 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 nr = 0;
4872 while (--n >= 0 && vim_isxdigit(p[1]))
4873 {
4874 ++p;
4875 nr = (nr << 4) + hex2nr(*p);
4876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004877 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#ifdef FEAT_MBYTE
4879 /* For "\u" store the number according to
4880 * 'encoding'. */
4881 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004882 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 else
4884#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004885 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 break;
4888
4889 /* octal: "\1", "\12", "\123" */
4890 case '0':
4891 case '1':
4892 case '2':
4893 case '3':
4894 case '4':
4895 case '5':
4896 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 case '7': *name = *p++ - '0';
4898 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900 *name = (*name << 3) + *p++ - '0';
4901 if (*p >= '0' && *p <= '7')
4902 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 break;
4906
4907 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004908 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (extra != 0)
4910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004911 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 break;
4913 }
4914 /* FALLTHROUGH */
4915
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 break;
4918 }
4919 }
4920 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004925 if (*p != NUL) /* just in case */
4926 ++p;
4927 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return OK;
4930}
4931
4932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 * Return OK or FAIL.
4935 */
4936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004937get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004940 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004941 int reduce = 0;
4942
4943 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004945 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004946 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004951 break;
4952 ++reduce;
4953 ++p;
4954 }
4955 }
4956
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004960 return FAIL;
4961 }
4962
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004964 if (!evaluate)
4965 {
4966 *arg = p + 1;
4967 return OK;
4968 }
4969
4970 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004972 */
4973 str = alloc((unsigned)((p - *arg) - reduce));
4974 if (str == NULL)
4975 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 rettv->v_type = VAR_STRING;
4977 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004978
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004980 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004984 break;
4985 ++p;
4986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004990 *arg = p + 1;
4991
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004992 return OK;
4993}
4994
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004995/*
4996 * Return the function name of the partial.
4997 */
4998 char_u *
4999partial_name(partial_T *pt)
5000{
5001 if (pt->pt_name != NULL)
5002 return pt->pt_name;
5003 return pt->pt_func->uf_name;
5004}
5005
Bram Moolenaarddecc252016-04-06 22:59:37 +02005006 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005008{
5009 int i;
5010
5011 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005012 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005013 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005014 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005015 if (pt->pt_name != NULL)
5016 {
5017 func_unref(pt->pt_name);
5018 vim_free(pt->pt_name);
5019 }
5020 else
5021 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005022 vim_free(pt);
5023}
5024
5025/*
5026 * Unreference a closure: decrement the reference count and free it when it
5027 * becomes zero.
5028 */
5029 void
5030partial_unref(partial_T *pt)
5031{
5032 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005033 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005034}
5035
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005036static int tv_equal_recurse_limit;
5037
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005038 static int
5039func_equal(
5040 typval_T *tv1,
5041 typval_T *tv2,
5042 int ic) /* ignore case */
5043{
5044 char_u *s1, *s2;
5045 dict_T *d1, *d2;
5046 int a1, a2;
5047 int i;
5048
5049 /* empty and NULL function name considered the same */
5050 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005051 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005052 if (s1 != NULL && *s1 == NUL)
5053 s1 = NULL;
5054 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005055 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005056 if (s2 != NULL && *s2 == NUL)
5057 s2 = NULL;
5058 if (s1 == NULL || s2 == NULL)
5059 {
5060 if (s1 != s2)
5061 return FALSE;
5062 }
5063 else if (STRCMP(s1, s2) != 0)
5064 return FALSE;
5065
5066 /* empty dict and NULL dict is different */
5067 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5068 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5069 if (d1 == NULL || d2 == NULL)
5070 {
5071 if (d1 != d2)
5072 return FALSE;
5073 }
5074 else if (!dict_equal(d1, d2, ic, TRUE))
5075 return FALSE;
5076
5077 /* empty list and no list considered the same */
5078 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5079 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5080 if (a1 != a2)
5081 return FALSE;
5082 for (i = 0; i < a1; ++i)
5083 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5084 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5085 return FALSE;
5086
5087 return TRUE;
5088}
5089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005090/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005091 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005092 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005094 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005096tv_equal(
5097 typval_T *tv1,
5098 typval_T *tv2,
5099 int ic, /* ignore case */
5100 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005101{
5102 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005103 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005104 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005105 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005106
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005107 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005108 * recursiveness to a limit. We guess they are equal then.
5109 * A fixed limit has the problem of still taking an awful long time.
5110 * Reduce the limit every time running into it. That should work fine for
5111 * deeply linked structures that are not recursively linked and catch
5112 * recursiveness quickly. */
5113 if (!recursive)
5114 tv_equal_recurse_limit = 1000;
5115 if (recursive_cnt >= tv_equal_recurse_limit)
5116 {
5117 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005118 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005119 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005120
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005121 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5122 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005123 if ((tv1->v_type == VAR_FUNC
5124 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5125 && (tv2->v_type == VAR_FUNC
5126 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5127 {
5128 ++recursive_cnt;
5129 r = func_equal(tv1, tv2, ic);
5130 --recursive_cnt;
5131 return r;
5132 }
5133
5134 if (tv1->v_type != tv2->v_type)
5135 return FALSE;
5136
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005137 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005138 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005139 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005140 ++recursive_cnt;
5141 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5142 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005143 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005144
5145 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005146 ++recursive_cnt;
5147 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5148 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005149 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005150
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005151 case VAR_NUMBER:
5152 return tv1->vval.v_number == tv2->vval.v_number;
5153
5154 case VAR_STRING:
5155 s1 = get_tv_string_buf(tv1, buf1);
5156 s2 = get_tv_string_buf(tv2, buf2);
5157 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005158
5159 case VAR_SPECIAL:
5160 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005161
5162 case VAR_FLOAT:
5163#ifdef FEAT_FLOAT
5164 return tv1->vval.v_float == tv2->vval.v_float;
5165#endif
5166 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005167#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005168 return tv1->vval.v_job == tv2->vval.v_job;
5169#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005170 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005172 return tv1->vval.v_channel == tv2->vval.v_channel;
5173#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005174 case VAR_FUNC:
5175 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005176 case VAR_UNKNOWN:
5177 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005178 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005179
Bram Moolenaara03f2332016-02-06 18:09:59 +01005180 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5181 * does not equal anything, not even itself. */
5182 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005183}
5184
5185/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005186 * Return the next (unique) copy ID.
5187 * Used for serializing nested structures.
5188 */
5189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005190get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005191{
5192 current_copyID += COPYID_INC;
5193 return current_copyID;
5194}
5195
5196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005197 * Garbage collection for lists and dictionaries.
5198 *
5199 * We use reference counts to be able to free most items right away when they
5200 * are no longer used. But for composite items it's possible that it becomes
5201 * unused while the reference count is > 0: When there is a recursive
5202 * reference. Example:
5203 * :let l = [1, 2, 3]
5204 * :let d = {9: l}
5205 * :let l[1] = d
5206 *
5207 * Since this is quite unusual we handle this with garbage collection: every
5208 * once in a while find out which lists and dicts are not referenced from any
5209 * variable.
5210 *
5211 * Here is a good reference text about garbage collection (refers to Python
5212 * but it applies to all reference-counting mechanisms):
5213 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005214 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005215
5216/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005218 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005219 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005220 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005221 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005222garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005223{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005224 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005225 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226 buf_T *buf;
5227 win_T *wp;
5228 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005229 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005230 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005231
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005232 if (!testing)
5233 {
5234 /* Only do this once. */
5235 want_garbage_collect = FALSE;
5236 may_garbage_collect = FALSE;
5237 garbage_collect_at_exit = FALSE;
5238 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005239
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005240 /* We advance by two because we add one for items referenced through
5241 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005242 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005243
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005244 /*
5245 * 1. Go through all accessible variables and mark all lists and dicts
5246 * with copyID.
5247 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005248
5249 /* Don't free variables in the previous_funccal list unless they are only
5250 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005251 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005252 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005253
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005254 /* script-local variables */
5255 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005256 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257
5258 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005259 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005260 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5261 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262
5263 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005264 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5266 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005267#ifdef FEAT_AUTOCMD
5268 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5270 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005271#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005273 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005274 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005275 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5276 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005277 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005279
5280 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005282
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005283 /* named functions (matters for closures) */
5284 abort = abort || set_ref_in_functions(copyID);
5285
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005286 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005288
Bram Moolenaard812df62008-11-09 12:46:09 +00005289 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005291
Bram Moolenaar1dced572012-04-05 16:54:08 +02005292#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005294#endif
5295
Bram Moolenaardb913952012-06-29 12:54:53 +02005296#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005297 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005298#endif
5299
5300#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005301 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005302#endif
5303
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005304#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005305 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005306 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005307#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005308#ifdef FEAT_NETBEANS_INTG
5309 abort = abort || set_ref_in_nb_channel(copyID);
5310#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005311
Bram Moolenaare3188e22016-05-31 21:13:04 +02005312#ifdef FEAT_TIMERS
5313 abort = abort || set_ref_in_timer(copyID);
5314#endif
5315
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005316#ifdef FEAT_QUICKFIX
5317 abort = abort || set_ref_in_quickfix(copyID);
5318#endif
5319
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005320#ifdef FEAT_TERMINAL
5321 abort = abort || set_ref_in_term(copyID);
5322#endif
5323
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005324 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005325 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 /*
5327 * 2. Free lists and dictionaries that are not referenced.
5328 */
5329 did_free = free_unref_items(copyID);
5330
5331 /*
5332 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005334 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005335 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005336 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005337 else if (p_verbose > 0)
5338 {
5339 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5340 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005341
5342 return did_free;
5343}
5344
5345/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005346 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005347 */
5348 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005349free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005350{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005351 int did_free = FALSE;
5352
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005353 /* Let all "free" functions know that we are here. This means no
5354 * dictionaries, lists, channels or jobs are to be freed, because we will
5355 * do that here. */
5356 in_free_unref_items = TRUE;
5357
5358 /*
5359 * PASS 1: free the contents of the items. We don't free the items
5360 * themselves yet, so that it is possible to decrement refcount counters
5361 */
5362
Bram Moolenaarcd524592016-07-17 14:57:05 +02005363 /* Go through the list of dicts and free items without the copyID. */
5364 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365
Bram Moolenaarda861d62016-07-17 15:46:27 +02005366 /* Go through the list of lists and free items without the copyID. */
5367 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005368
5369#ifdef FEAT_JOB_CHANNEL
5370 /* Go through the list of jobs and free items without the copyID. This
5371 * must happen before doing channels, because jobs refer to channels, but
5372 * the reference from the channel to the job isn't tracked. */
5373 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5374
5375 /* Go through the list of channels and free items without the copyID. */
5376 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5377#endif
5378
5379 /*
5380 * PASS 2: free the items themselves.
5381 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005382 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005383 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005384
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005385#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 free_unused_jobs(copyID, COPYID_MASK);
5390
5391 /* Go through the list of channels and free items without the copyID. */
5392 free_unused_channels(copyID, COPYID_MASK);
5393#endif
5394
5395 in_free_unref_items = FALSE;
5396
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 return did_free;
5398}
5399
5400/*
5401 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 * "list_stack" is used to add lists to be marked. Can be NULL.
5403 *
5404 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005407set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408{
5409 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005411 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005412 hashtab_T *cur_ht;
5413 ht_stack_T *ht_stack = NULL;
5414 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005415
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 cur_ht = ht;
5417 for (;;)
5418 {
5419 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 /* Mark each item in the hashtab. If the item contains a hashtab
5422 * it is added to ht_stack, if it contains a list it is added to
5423 * list_stack. */
5424 todo = (int)cur_ht->ht_used;
5425 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5426 if (!HASHITEM_EMPTY(hi))
5427 {
5428 --todo;
5429 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5430 &ht_stack, list_stack);
5431 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433
5434 if (ht_stack == NULL)
5435 break;
5436
5437 /* take an item from the stack */
5438 cur_ht = ht_stack->ht;
5439 tempitem = ht_stack;
5440 ht_stack = ht_stack->prev;
5441 free(tempitem);
5442 }
5443
5444 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445}
5446
5447/*
5448 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005449 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5450 *
5451 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005452 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005455{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005456 listitem_T *li;
5457 int abort = FALSE;
5458 list_T *cur_l;
5459 list_stack_T *list_stack = NULL;
5460 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005461
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005462 cur_l = l;
5463 for (;;)
5464 {
5465 if (!abort)
5466 /* Mark each item in the list. If the item contains a hashtab
5467 * it is added to ht_stack, if it contains a list it is added to
5468 * list_stack. */
5469 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5470 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5471 ht_stack, &list_stack);
5472 if (list_stack == NULL)
5473 break;
5474
5475 /* take an item from the stack */
5476 cur_l = list_stack->list;
5477 tempitem = list_stack;
5478 list_stack = list_stack->prev;
5479 free(tempitem);
5480 }
5481
5482 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005483}
5484
5485/*
5486 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 * "list_stack" is used to add lists to be marked. Can be NULL.
5488 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5489 *
5490 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005491 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005493set_ref_in_item(
5494 typval_T *tv,
5495 int copyID,
5496 ht_stack_T **ht_stack,
5497 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005498{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005499 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005500
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005501 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005502 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005503 dict_T *dd = tv->vval.v_dict;
5504
Bram Moolenaara03f2332016-02-06 18:09:59 +01005505 if (dd != NULL && dd->dv_copyID != copyID)
5506 {
5507 /* Didn't see this dict yet. */
5508 dd->dv_copyID = copyID;
5509 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005510 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5512 }
5513 else
5514 {
5515 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5516 if (newitem == NULL)
5517 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 else
5519 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 newitem->ht = &dd->dv_hashtab;
5521 newitem->prev = *ht_stack;
5522 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005523 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005524 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005525 }
5526 }
5527 else if (tv->v_type == VAR_LIST)
5528 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005529 list_T *ll = tv->vval.v_list;
5530
Bram Moolenaara03f2332016-02-06 18:09:59 +01005531 if (ll != NULL && ll->lv_copyID != copyID)
5532 {
5533 /* Didn't see this list yet. */
5534 ll->lv_copyID = copyID;
5535 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005536 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005537 abort = set_ref_in_list(ll, copyID, ht_stack);
5538 }
5539 else
5540 {
5541 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005542 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005543 if (newitem == NULL)
5544 abort = TRUE;
5545 else
5546 {
5547 newitem->list = ll;
5548 newitem->prev = *list_stack;
5549 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005550 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005551 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005552 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005553 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005554 else if (tv->v_type == VAR_FUNC)
5555 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005556 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005557 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005558 else if (tv->v_type == VAR_PARTIAL)
5559 {
5560 partial_T *pt = tv->vval.v_partial;
5561 int i;
5562
5563 /* A partial does not have a copyID, because it cannot contain itself.
5564 */
5565 if (pt != NULL)
5566 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005567 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005568
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005569 if (pt->pt_dict != NULL)
5570 {
5571 typval_T dtv;
5572
5573 dtv.v_type = VAR_DICT;
5574 dtv.vval.v_dict = pt->pt_dict;
5575 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5576 }
5577
5578 for (i = 0; i < pt->pt_argc; ++i)
5579 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5580 ht_stack, list_stack);
5581 }
5582 }
5583#ifdef FEAT_JOB_CHANNEL
5584 else if (tv->v_type == VAR_JOB)
5585 {
5586 job_T *job = tv->vval.v_job;
5587 typval_T dtv;
5588
5589 if (job != NULL && job->jv_copyID != copyID)
5590 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005591 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005592 if (job->jv_channel != NULL)
5593 {
5594 dtv.v_type = VAR_CHANNEL;
5595 dtv.vval.v_channel = job->jv_channel;
5596 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5597 }
5598 if (job->jv_exit_partial != NULL)
5599 {
5600 dtv.v_type = VAR_PARTIAL;
5601 dtv.vval.v_partial = job->jv_exit_partial;
5602 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5603 }
5604 }
5605 }
5606 else if (tv->v_type == VAR_CHANNEL)
5607 {
5608 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005609 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005610 typval_T dtv;
5611 jsonq_T *jq;
5612 cbq_T *cq;
5613
5614 if (ch != NULL && ch->ch_copyID != copyID)
5615 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005616 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005617 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618 {
5619 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5620 jq = jq->jq_next)
5621 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5622 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5623 cq = cq->cq_next)
5624 if (cq->cq_partial != NULL)
5625 {
5626 dtv.v_type = VAR_PARTIAL;
5627 dtv.vval.v_partial = cq->cq_partial;
5628 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5629 }
5630 if (ch->ch_part[part].ch_partial != NULL)
5631 {
5632 dtv.v_type = VAR_PARTIAL;
5633 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5634 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5635 }
5636 }
5637 if (ch->ch_partial != NULL)
5638 {
5639 dtv.v_type = VAR_PARTIAL;
5640 dtv.vval.v_partial = ch->ch_partial;
5641 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5642 }
5643 if (ch->ch_close_partial != NULL)
5644 {
5645 dtv.v_type = VAR_PARTIAL;
5646 dtv.vval.v_partial = ch->ch_close_partial;
5647 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5648 }
5649 }
5650 }
5651#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005652 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005653}
5654
Bram Moolenaar17a13432016-01-24 14:22:10 +01005655 static char *
5656get_var_special_name(int nr)
5657{
5658 switch (nr)
5659 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005660 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005661 case VVAL_TRUE: return "v:true";
5662 case VVAL_NONE: return "v:none";
5663 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005664 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005665 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005666 return "42";
5667}
5668
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 * Return a string with the string representation of a variable.
5671 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005672 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005673 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005674 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5675 * stings as "string()", otherwise does not put quotes around strings, as
5676 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005677 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5678 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005679 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005681 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005682echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005683 typval_T *tv,
5684 char_u **tofree,
5685 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005686 int copyID,
5687 int echo_style,
5688 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005689 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005691 static int recurse = 0;
5692 char_u *r = NULL;
5693
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005695 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005696 if (!did_echo_string_emsg)
5697 {
5698 /* Only give this message once for a recursive call to avoid
5699 * flooding the user with errors. And stop iterating over lists
5700 * and dicts. */
5701 did_echo_string_emsg = TRUE;
5702 EMSG(_("E724: variable nested too deep for displaying"));
5703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005704 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005705 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005706 }
5707 ++recurse;
5708
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 switch (tv->v_type)
5710 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005711 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005712 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005713 {
5714 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005715 r = tv->vval.v_string;
5716 if (r == NULL)
5717 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005718 }
5719 else
5720 {
5721 *tofree = string_quote(tv->vval.v_string, FALSE);
5722 r = *tofree;
5723 }
5724 break;
5725
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005727 if (echo_style)
5728 {
5729 *tofree = NULL;
5730 r = tv->vval.v_string;
5731 }
5732 else
5733 {
5734 *tofree = string_quote(tv->vval.v_string, TRUE);
5735 r = *tofree;
5736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005737 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005738
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005739 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005740 {
5741 partial_T *pt = tv->vval.v_partial;
5742 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005743 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005744 garray_T ga;
5745 int i;
5746 char_u *tf;
5747
5748 ga_init2(&ga, 1, 100);
5749 ga_concat(&ga, (char_u *)"function(");
5750 if (fname != NULL)
5751 {
5752 ga_concat(&ga, fname);
5753 vim_free(fname);
5754 }
5755 if (pt != NULL && pt->pt_argc > 0)
5756 {
5757 ga_concat(&ga, (char_u *)", [");
5758 for (i = 0; i < pt->pt_argc; ++i)
5759 {
5760 if (i > 0)
5761 ga_concat(&ga, (char_u *)", ");
5762 ga_concat(&ga,
5763 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5764 vim_free(tf);
5765 }
5766 ga_concat(&ga, (char_u *)"]");
5767 }
5768 if (pt != NULL && pt->pt_dict != NULL)
5769 {
5770 typval_T dtv;
5771
5772 ga_concat(&ga, (char_u *)", ");
5773 dtv.v_type = VAR_DICT;
5774 dtv.vval.v_dict = pt->pt_dict;
5775 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5776 vim_free(tf);
5777 }
5778 ga_concat(&ga, (char_u *)")");
5779
5780 *tofree = ga.ga_data;
5781 r = *tofree;
5782 break;
5783 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005784
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005786 if (tv->vval.v_list == NULL)
5787 {
5788 *tofree = NULL;
5789 r = NULL;
5790 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005791 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5792 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005793 {
5794 *tofree = NULL;
5795 r = (char_u *)"[...]";
5796 }
5797 else
5798 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005799 int old_copyID = tv->vval.v_list->lv_copyID;
5800
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005801 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005802 *tofree = list2string(tv, copyID, restore_copyID);
5803 if (restore_copyID)
5804 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805 r = *tofree;
5806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005807 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005808
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005810 if (tv->vval.v_dict == NULL)
5811 {
5812 *tofree = NULL;
5813 r = NULL;
5814 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005815 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5816 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005817 {
5818 *tofree = NULL;
5819 r = (char_u *)"{...}";
5820 }
5821 else
5822 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005823 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005824 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005825 *tofree = dict2string(tv, copyID, restore_copyID);
5826 if (restore_copyID)
5827 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005828 r = *tofree;
5829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005830 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005833 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005834 *tofree = NULL;
5835 r = get_tv_string_buf(tv, numbuf);
5836 break;
5837
Bram Moolenaar835dc632016-02-07 14:27:38 +01005838 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005839 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005840 *tofree = NULL;
5841 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005842 if (composite_val)
5843 {
5844 *tofree = string_quote(r, FALSE);
5845 r = *tofree;
5846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005848
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005849 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005851 *tofree = NULL;
5852 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5853 r = numbuf;
5854 break;
5855#endif
5856
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005857 case VAR_SPECIAL:
5858 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005859 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005860 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862
Bram Moolenaar8502c702014-06-17 12:51:16 +02005863 if (--recurse == 0)
5864 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866}
5867
5868/*
5869 * Return a string with the string representation of a variable.
5870 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5871 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005872 * Does not put quotes around strings, as ":echo" displays values.
5873 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5874 * May return NULL.
5875 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005877echo_string(
5878 typval_T *tv,
5879 char_u **tofree,
5880 char_u *numbuf,
5881 int copyID)
5882{
5883 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5884}
5885
5886/*
5887 * Return a string with the string representation of a variable.
5888 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5889 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005891 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005894tv2string(
5895 typval_T *tv,
5896 char_u **tofree,
5897 char_u *numbuf,
5898 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005900 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901}
5902
5903/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 * Return string "str" in ' quotes, doubling ' characters.
5905 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005908 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005909string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910{
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 char_u *p, *r, *s;
5913
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 len = (function ? 13 : 3);
5915 if (str != NULL)
5916 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005917 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005918 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005919 if (*p == '\'')
5920 ++len;
5921 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 s = r = alloc(len);
5923 if (r != NULL)
5924 {
5925 if (function)
5926 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 r += 10;
5929 }
5930 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 if (str != NULL)
5933 for (p = str; *p != NUL; )
5934 {
5935 if (*p == '\'')
5936 *r++ = '\'';
5937 MB_COPY_CHAR(p, r);
5938 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 if (function)
5941 *r++ = ')';
5942 *r++ = NUL;
5943 }
5944 return s;
5945}
5946
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005947#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005948/*
5949 * Convert the string "text" to a floating point number.
5950 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5951 * this always uses a decimal point.
5952 * Returns the length of the text that was consumed.
5953 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005955string2float(
5956 char_u *text,
5957 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005958{
5959 char *s = (char *)text;
5960 float_T f;
5961
Bram Moolenaar62473612017-01-08 19:25:40 +01005962 /* MS-Windows does not deal with "inf" and "nan" properly. */
5963 if (STRNICMP(text, "inf", 3) == 0)
5964 {
5965 *value = INFINITY;
5966 return 3;
5967 }
5968 if (STRNICMP(text, "-inf", 3) == 0)
5969 {
5970 *value = -INFINITY;
5971 return 4;
5972 }
5973 if (STRNICMP(text, "nan", 3) == 0)
5974 {
5975 *value = NAN;
5976 return 3;
5977 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005978 f = strtod(s, &s);
5979 *value = f;
5980 return (int)((char_u *)s - text);
5981}
5982#endif
5983
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 * Get the value of an environment variable.
5986 * "arg" is pointing to the '$'. It is advanced to after the name.
5987 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005988 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 */
5990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 char_u *string = NULL;
5994 int len;
5995 int cc;
5996 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005997 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
5999 ++*arg;
6000 name = *arg;
6001 len = get_env_len(arg);
6002 if (evaluate)
6003 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006004 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006005 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006006
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006007 cc = name[len];
6008 name[len] = NUL;
6009 /* first try vim_getenv(), fast for normal environment vars */
6010 string = vim_getenv(name, &mustfree);
6011 if (string != NULL && *string != NUL)
6012 {
6013 if (!mustfree)
6014 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006016 else
6017 {
6018 if (mustfree)
6019 vim_free(string);
6020
6021 /* next try expanding things like $VIM and ${HOME} */
6022 string = expand_env_save(name - 1);
6023 if (string != NULL && *string == '$')
6024 {
6025 vim_free(string);
6026 string = NULL;
6027 }
6028 }
6029 name[len] = cc;
6030
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006031 rettv->v_type = VAR_STRING;
6032 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 }
6034
6035 return OK;
6036}
6037
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006038
6039
6040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006042 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045var2fpos(
6046 typval_T *varp,
6047 int dollar_lnum, /* TRUE when $ is last line */
6048 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006050 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006052 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
Bram Moolenaara5525202006-03-02 22:52:09 +00006054 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006055 if (varp->v_type == VAR_LIST)
6056 {
6057 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006058 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006059 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006060 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006061
6062 l = varp->vval.v_list;
6063 if (l == NULL)
6064 return NULL;
6065
6066 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006067 pos.lnum = list_find_nr(l, 0L, &error);
6068 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006069 return NULL; /* invalid line number */
6070
6071 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006072 pos.col = list_find_nr(l, 1L, &error);
6073 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006074 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006075 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006076
6077 /* We accept "$" for the column number: last column. */
6078 li = list_find(l, 1L);
6079 if (li != NULL && li->li_tv.v_type == VAR_STRING
6080 && li->li_tv.vval.v_string != NULL
6081 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6082 pos.col = len + 1;
6083
Bram Moolenaara5525202006-03-02 22:52:09 +00006084 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006085 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006086 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006087 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006088
Bram Moolenaara5525202006-03-02 22:52:09 +00006089#ifdef FEAT_VIRTUALEDIT
6090 /* Get the virtual offset. Defaults to zero. */
6091 pos.coladd = list_find_nr(l, 2L, &error);
6092 if (error)
6093 pos.coladd = 0;
6094#endif
6095
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006096 return &pos;
6097 }
6098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 name = get_tv_string_chk(varp);
6100 if (name == NULL)
6101 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006102 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006104 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6105 {
6106 if (VIsual_active)
6107 return &VIsual;
6108 return &curwin->w_cursor;
6109 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006110 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006112 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6114 return NULL;
6115 return pp;
6116 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006117
6118#ifdef FEAT_VIRTUALEDIT
6119 pos.coladd = 0;
6120#endif
6121
Bram Moolenaar477933c2007-07-17 14:32:23 +00006122 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006123 {
6124 pos.col = 0;
6125 if (name[1] == '0') /* "w0": first visible line */
6126 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006127 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006128 /* In silent Ex mode topline is zero, but that's not a valid line
6129 * number; use one instead. */
6130 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006131 return &pos;
6132 }
6133 else if (name[1] == '$') /* "w$": last visible line */
6134 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006135 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006136 /* In silent Ex mode botline is zero, return zero then. */
6137 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006138 return &pos;
6139 }
6140 }
6141 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006143 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {
6145 pos.lnum = curbuf->b_ml.ml_line_count;
6146 pos.col = 0;
6147 }
6148 else
6149 {
6150 pos.lnum = curwin->w_cursor.lnum;
6151 pos.col = (colnr_T)STRLEN(ml_get_curline());
6152 }
6153 return &pos;
6154 }
6155 return NULL;
6156}
6157
6158/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006159 * Convert list in "arg" into a position and optional file number.
6160 * When "fnump" is NULL there is no file number, only 3 items.
6161 * Note that the column is passed on as-is, the caller may want to decrement
6162 * it to use 1 for the first column.
6163 * Return FAIL when conversion is not possible, doesn't check the position for
6164 * validity.
6165 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167list2fpos(
6168 typval_T *arg,
6169 pos_T *posp,
6170 int *fnump,
6171 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006172{
6173 list_T *l = arg->vval.v_list;
6174 long i = 0;
6175 long n;
6176
Bram Moolenaar493c1782014-05-28 14:34:46 +02006177 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6178 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006179 if (arg->v_type != VAR_LIST
6180 || l == NULL
6181 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006182 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006183 return FAIL;
6184
6185 if (fnump != NULL)
6186 {
6187 n = list_find_nr(l, i++, NULL); /* fnum */
6188 if (n < 0)
6189 return FAIL;
6190 if (n == 0)
6191 n = curbuf->b_fnum; /* current buffer */
6192 *fnump = n;
6193 }
6194
6195 n = list_find_nr(l, i++, NULL); /* lnum */
6196 if (n < 0)
6197 return FAIL;
6198 posp->lnum = n;
6199
6200 n = list_find_nr(l, i++, NULL); /* col */
6201 if (n < 0)
6202 return FAIL;
6203 posp->col = n;
6204
6205#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006206 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006207 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006208 posp->coladd = 0;
6209 else
6210 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006211#endif
6212
Bram Moolenaar493c1782014-05-28 14:34:46 +02006213 if (curswantp != NULL)
6214 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6215
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006216 return OK;
6217}
6218
6219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 * Get the length of an environment variable name.
6221 * Advance "arg" to the first character after the name.
6222 * Return 0 for error.
6223 */
6224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006225get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
6227 char_u *p;
6228 int len;
6229
6230 for (p = *arg; vim_isIDc(*p); ++p)
6231 ;
6232 if (p == *arg) /* no name found */
6233 return 0;
6234
6235 len = (int)(p - *arg);
6236 *arg = p;
6237 return len;
6238}
6239
6240/*
6241 * Get the length of the name of a function or internal variable.
6242 * "arg" is advanced to the first non-white character after the name.
6243 * Return 0 if something is wrong.
6244 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006246get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 char_u *p;
6249 int len;
6250
6251 /* Find the end of the name. */
6252 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006253 {
6254 if (*p == ':')
6255 {
6256 /* "s:" is start of "s:var", but "n:" is not and can be used in
6257 * slice "[n:]". Also "xx:" is not a namespace. */
6258 len = (int)(p - *arg);
6259 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6260 || len > 1)
6261 break;
6262 }
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (p == *arg) /* no name found */
6265 return 0;
6266
6267 len = (int)(p - *arg);
6268 *arg = skipwhite(p);
6269
6270 return len;
6271}
6272
6273/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006274 * Get the length of the name of a variable or function.
6275 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006277 * Return -1 if curly braces expansion failed.
6278 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 * If the name contains 'magic' {}'s, expand them and return the
6280 * expanded name in an allocated string via 'alias' - caller must free.
6281 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006283get_name_len(
6284 char_u **arg,
6285 char_u **alias,
6286 int evaluate,
6287 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 char_u *p;
6291 char_u *expr_start;
6292 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294 *alias = NULL; /* default to no alias */
6295
6296 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6297 && (*arg)[2] == (int)KE_SNR)
6298 {
6299 /* hard coded <SNR>, already translated */
6300 *arg += 3;
6301 return get_id_len(arg) + 3;
6302 }
6303 len = eval_fname_script(*arg);
6304 if (len > 0)
6305 {
6306 /* literal "<SID>", "s:" or "<SNR>" */
6307 *arg += len;
6308 }
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006311 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006313 p = find_name_end(*arg, &expr_start, &expr_end,
6314 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (expr_start != NULL)
6316 {
6317 char_u *temp_string;
6318
6319 if (!evaluate)
6320 {
6321 len += (int)(p - *arg);
6322 *arg = skipwhite(p);
6323 return len;
6324 }
6325
6326 /*
6327 * Include any <SID> etc in the expanded string:
6328 * Thus the -len here.
6329 */
6330 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6331 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006332 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 *alias = temp_string;
6334 *arg = skipwhite(p);
6335 return (int)STRLEN(temp_string);
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006339 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 EMSG2(_(e_invexpr2), *arg);
6341
6342 return len;
6343}
6344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006345/*
6346 * Find the end of a variable or function name, taking care of magic braces.
6347 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6348 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006349 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350 * Return a pointer to just after the name. Equal to "arg" if there is no
6351 * valid name.
6352 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006353 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354find_name_end(
6355 char_u *arg,
6356 char_u **expr_start,
6357 char_u **expr_end,
6358 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006360 int mb_nest = 0;
6361 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006363 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006365 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006367 *expr_start = NULL;
6368 *expr_end = NULL;
6369 }
6370
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006371 /* Quick check for valid starting character. */
6372 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6373 return arg;
6374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006375 for (p = arg; *p != NUL
6376 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006377 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006378 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006379 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006380 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006381 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006382 if (*p == '\'')
6383 {
6384 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006385 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006386 ;
6387 if (*p == NUL)
6388 break;
6389 }
6390 else if (*p == '"')
6391 {
6392 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006393 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006394 if (*p == '\\' && p[1] != NUL)
6395 ++p;
6396 if (*p == NUL)
6397 break;
6398 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006399 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6400 {
6401 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006402 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006403 len = (int)(p - arg);
6404 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006405 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006406 break;
6407 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006408
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006409 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006411 if (*p == '[')
6412 ++br_nest;
6413 else if (*p == ']')
6414 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006417 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006419 if (*p == '{')
6420 {
6421 mb_nest++;
6422 if (expr_start != NULL && *expr_start == NULL)
6423 *expr_start = p;
6424 }
6425 else if (*p == '}')
6426 {
6427 mb_nest--;
6428 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6429 *expr_end = p;
6430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
6433
6434 return p;
6435}
6436
6437/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006438 * Expands out the 'magic' {}'s in a variable/function name.
6439 * Note that this can call itself recursively, to deal with
6440 * constructs like foo{bar}{baz}{bam}
6441 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6442 * "in_start" ^
6443 * "expr_start" ^
6444 * "expr_end" ^
6445 * "in_end" ^
6446 *
6447 * Returns a new allocated string, which the caller must free.
6448 * Returns NULL for failure.
6449 */
6450 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006451make_expanded_name(
6452 char_u *in_start,
6453 char_u *expr_start,
6454 char_u *expr_end,
6455 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006456{
6457 char_u c1;
6458 char_u *retval = NULL;
6459 char_u *temp_result;
6460 char_u *nextcmd = NULL;
6461
6462 if (expr_end == NULL || in_end == NULL)
6463 return NULL;
6464 *expr_start = NUL;
6465 *expr_end = NUL;
6466 c1 = *in_end;
6467 *in_end = NUL;
6468
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006469 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006470 if (temp_result != NULL && nextcmd == NULL)
6471 {
6472 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6473 + (in_end - expr_end) + 1));
6474 if (retval != NULL)
6475 {
6476 STRCPY(retval, in_start);
6477 STRCAT(retval, temp_result);
6478 STRCAT(retval, expr_end + 1);
6479 }
6480 }
6481 vim_free(temp_result);
6482
6483 *in_end = c1; /* put char back for error messages */
6484 *expr_start = '{';
6485 *expr_end = '}';
6486
6487 if (retval != NULL)
6488 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006489 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006490 if (expr_start != NULL)
6491 {
6492 /* Further expansion! */
6493 temp_result = make_expanded_name(retval, expr_start,
6494 expr_end, temp_result);
6495 vim_free(retval);
6496 retval = temp_result;
6497 }
6498 }
6499
6500 return retval;
6501}
6502
6503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006510 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6511}
6512
6513/*
6514 * Return TRUE if character "c" can be used as the first character in a
6515 * variable or function name (excluding '{' and '}').
6516 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006519{
6520 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521}
6522
6523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 * Set number v: variable to "val".
6525 */
6526 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006527set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006529 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006533 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006535 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006538 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539}
6540
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006541/*
6542 * Get string v: variable value. Uses a static buffer, can only be used once.
6543 */
6544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006545get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006546{
6547 return get_tv_string(&vimvars[idx].vv_tv);
6548}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006549
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006551 * Get List v: variable value. Caller must take care of reference count when
6552 * needed.
6553 */
6554 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006556{
6557 return vimvars[idx].vv_list;
6558}
6559
6560/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006561 * Set v:char to character "c".
6562 */
6563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006564set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006565{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006566 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006567
6568#ifdef FEAT_MBYTE
6569 if (has_mbyte)
6570 buf[(*mb_char2bytes)(c, buf)] = NUL;
6571 else
6572#endif
6573 {
6574 buf[0] = c;
6575 buf[1] = NUL;
6576 }
6577 set_vim_var_string(VV_CHAR, buf, -1);
6578}
6579
6580/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006581 * Set v:count to "count" and v:count1 to "count1".
6582 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 */
6584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585set_vcount(
6586 long count,
6587 long count1,
6588 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006590 if (set_prevcount)
6591 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 vimvars[VV_COUNT].vv_nr = count;
6593 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
6596/*
6597 * Set string v: variable to a copy of "val".
6598 */
6599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006600set_vim_var_string(
6601 int idx,
6602 char_u *val,
6603 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
Bram Moolenaara542c682016-01-31 16:28:04 +01006605 clear_tv(&vimvars[idx].vv_di.di_tv);
6606 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006610 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613}
6614
6615/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006616 * Set List v: variable to "val".
6617 */
6618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006619set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006620{
Bram Moolenaara542c682016-01-31 16:28:04 +01006621 clear_tv(&vimvars[idx].vv_di.di_tv);
6622 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006623 vimvars[idx].vv_list = val;
6624 if (val != NULL)
6625 ++val->lv_refcount;
6626}
6627
6628/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006629 * Set Dictionary v: variable to "val".
6630 */
6631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006633{
6634 int todo;
6635 hashitem_T *hi;
6636
Bram Moolenaara542c682016-01-31 16:28:04 +01006637 clear_tv(&vimvars[idx].vv_di.di_tv);
6638 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006639 vimvars[idx].vv_dict = val;
6640 if (val != NULL)
6641 {
6642 ++val->dv_refcount;
6643
6644 /* Set readonly */
6645 todo = (int)val->dv_hashtab.ht_used;
6646 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6647 {
6648 if (HASHITEM_EMPTY(hi))
6649 continue;
6650 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006651 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006652 }
6653 }
6654}
6655
6656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 * Set v:register if needed.
6658 */
6659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006660set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 char_u regname;
6663
6664 if (c == 0 || c == ' ')
6665 regname = '"';
6666 else
6667 regname = c;
6668 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006669 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 set_vim_var_string(VV_REG, &regname, 1);
6671}
6672
6673/*
6674 * Get or set v:exception. If "oldval" == NULL, return the current value.
6675 * Otherwise, restore the value to "oldval" and return NULL.
6676 * Must always be called in pairs to save and restore v:exception! Does not
6677 * take care of memory allocations.
6678 */
6679 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006680v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006683 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 return NULL;
6687}
6688
6689/*
6690 * Get or set v:throwpoint. 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:throwpoint! Does not
6693 * take care of memory allocations.
6694 */
6695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006696v_throwpoint(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_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 return NULL;
6703}
6704
6705#if defined(FEAT_AUTOCMD) || defined(PROTO)
6706/*
6707 * Set v:cmdarg.
6708 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6709 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6710 * Must always be called in pairs!
6711 */
6712 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006713set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 char_u *oldval;
6716 char_u *newval;
6717 unsigned len;
6718
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006720 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006722 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006723 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006724 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
6726
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006727 if (eap->force_bin == FORCE_BIN)
6728 len = 6;
6729 else if (eap->force_bin == FORCE_NOBIN)
6730 len = 8;
6731 else
6732 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006733
6734 if (eap->read_edit)
6735 len += 7;
6736
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006737 if (eap->force_ff != 0)
6738 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6739# ifdef FEAT_MBYTE
6740 if (eap->force_enc != 0)
6741 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006742 if (eap->bad_char != 0)
6743 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006744# endif
6745
6746 newval = alloc(len + 1);
6747 if (newval == NULL)
6748 return NULL;
6749
6750 if (eap->force_bin == FORCE_BIN)
6751 sprintf((char *)newval, " ++bin");
6752 else if (eap->force_bin == FORCE_NOBIN)
6753 sprintf((char *)newval, " ++nobin");
6754 else
6755 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006756
6757 if (eap->read_edit)
6758 STRCAT(newval, " ++edit");
6759
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006760 if (eap->force_ff != 0)
6761 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6762 eap->cmd + eap->force_ff);
6763# ifdef FEAT_MBYTE
6764 if (eap->force_enc != 0)
6765 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6766 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006767 if (eap->bad_char == BAD_KEEP)
6768 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6769 else if (eap->bad_char == BAD_DROP)
6770 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6771 else if (eap->bad_char != 0)
6772 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006773# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006775 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776}
6777#endif
6778
6779/*
6780 * Get the value of internal variable "name".
6781 * Return OK or FAIL.
6782 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006784get_var_tv(
6785 char_u *name,
6786 int len, /* length of "name" */
6787 typval_T *rettv, /* NULL when only checking existence */
6788 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6789 int verbose, /* may give error message */
6790 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006793 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
6797 /* truncate the name, so that we can use strcmp() */
6798 cc = name[len];
6799 name[len] = NUL;
6800
6801 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 * Check for user-defined variables.
6803 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006804 v = find_var(name, NULL, no_autoload);
6805 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006807 tv = &v->di_tv;
6808 if (dip != NULL)
6809 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006814 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 ret = FAIL;
6817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006818 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006819 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
6821 name[len] = cc;
6822
6823 return ret;
6824}
6825
6826/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006827 * Check if variable "name[len]" is a local variable or an argument.
6828 * If so, "*eval_lavars_used" is set to TRUE.
6829 */
6830 static void
6831check_vars(char_u *name, int len)
6832{
6833 int cc;
6834 char_u *varname;
6835 hashtab_T *ht;
6836
6837 if (eval_lavars_used == NULL)
6838 return;
6839
6840 /* truncate the name, so that we can use strcmp() */
6841 cc = name[len];
6842 name[len] = NUL;
6843
6844 ht = find_var_ht(name, &varname);
6845 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6846 {
6847 if (find_var(name, NULL, TRUE) != NULL)
6848 *eval_lavars_used = TRUE;
6849 }
6850
6851 name[len] = cc;
6852}
6853
6854/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006855 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6856 * Also handle function call with Funcref variable: func(expr)
6857 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6858 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006859 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860handle_subscript(
6861 char_u **arg,
6862 typval_T *rettv,
6863 int evaluate, /* do more than finding the end */
6864 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006865{
6866 int ret = OK;
6867 dict_T *selfdict = NULL;
6868 char_u *s;
6869 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006870 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006871
6872 while (ret == OK
6873 && (**arg == '['
6874 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006875 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6876 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006877 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006878 {
6879 if (**arg == '(')
6880 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006881 partial_T *pt = NULL;
6882
Bram Moolenaard9fba312005-06-26 22:34:35 +00006883 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006884 if (evaluate)
6885 {
6886 functv = *rettv;
6887 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006888
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006889 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006890 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006891 {
6892 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006893 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006894 }
6895 else
6896 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006897 }
6898 else
6899 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006900 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006901 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006902 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006903
6904 /* Clear the funcref afterwards, so that deleting it while
6905 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006906 if (evaluate)
6907 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006908
6909 /* Stop the expression evaluation when immediately aborting on
6910 * error, or when an interrupt occurred or an exception was thrown
6911 * but not caught. */
6912 if (aborting())
6913 {
6914 if (ret == OK)
6915 clear_tv(rettv);
6916 ret = FAIL;
6917 }
6918 dict_unref(selfdict);
6919 selfdict = NULL;
6920 }
6921 else /* **arg == '[' || **arg == '.' */
6922 {
6923 dict_unref(selfdict);
6924 if (rettv->v_type == VAR_DICT)
6925 {
6926 selfdict = rettv->vval.v_dict;
6927 if (selfdict != NULL)
6928 ++selfdict->dv_refcount;
6929 }
6930 else
6931 selfdict = NULL;
6932 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6933 {
6934 clear_tv(rettv);
6935 ret = FAIL;
6936 }
6937 }
6938 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006939
Bram Moolenaar1d429612016-05-24 15:44:17 +02006940 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6941 * Don't do this when "Func" is already a partial that was bound
6942 * explicitly (pt_auto is FALSE). */
6943 if (selfdict != NULL
6944 && (rettv->v_type == VAR_FUNC
6945 || (rettv->v_type == VAR_PARTIAL
6946 && (rettv->vval.v_partial->pt_auto
6947 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006948 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006949
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006950 dict_unref(selfdict);
6951 return ret;
6952}
6953
6954/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006955 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006956 * value).
6957 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006958 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960{
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006962}
6963
6964/*
6965 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 * The string "s" must have been allocated, it is consumed.
6967 * Return NULL for out of memory, the variable otherwise.
6968 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006974 rettv = alloc_tv();
6975 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006977 rettv->v_type = VAR_STRING;
6978 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 else
6981 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006982 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983}
6984
6985/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006986 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 if (varp != NULL)
6992 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006993 switch (varp->v_type)
6994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006995 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006996 func_unref(varp->vval.v_string);
6997 /*FALLTHROUGH*/
6998 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006999 vim_free(varp->vval.v_string);
7000 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007001 case VAR_PARTIAL:
7002 partial_unref(varp->vval.v_partial);
7003 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007004 case VAR_LIST:
7005 list_unref(varp->vval.v_list);
7006 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007007 case VAR_DICT:
7008 dict_unref(varp->vval.v_dict);
7009 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007010 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007011#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007012 job_unref(varp->vval.v_job);
7013 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007014#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007015 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007016#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007017 channel_unref(varp->vval.v_channel);
7018 break;
7019#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007020 case VAR_NUMBER:
7021 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007022 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007023 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007024 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 vim_free(varp);
7027 }
7028}
7029
7030/*
7031 * Free the memory for a variable value and set the value to NULL or 0.
7032 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 if (varp != NULL)
7037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007038 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007040 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007041 func_unref(varp->vval.v_string);
7042 /*FALLTHROUGH*/
7043 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007044 vim_free(varp->vval.v_string);
7045 varp->vval.v_string = NULL;
7046 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007047 case VAR_PARTIAL:
7048 partial_unref(varp->vval.v_partial);
7049 varp->vval.v_partial = NULL;
7050 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007051 case VAR_LIST:
7052 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007054 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 case VAR_DICT:
7056 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007059 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007060 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007061 varp->vval.v_number = 0;
7062 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007063 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007064#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007065 varp->vval.v_float = 0.0;
7066 break;
7067#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007068 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007069#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007070 job_unref(varp->vval.v_job);
7071 varp->vval.v_job = NULL;
7072#endif
7073 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007074 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007075#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007076 channel_unref(varp->vval.v_channel);
7077 varp->vval.v_channel = NULL;
7078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007079 case VAR_UNKNOWN:
7080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007082 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
7084}
7085
7086/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007087 * Set the value of a variable to NULL without freeing items.
7088 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007090init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007091{
7092 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007094}
7095
7096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 * Get the number value of a variable.
7098 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099 * For incompatible types, return 0.
7100 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7101 * caller of incompatible types: it sets *denote to TRUE if "denote"
7102 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007104 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007107 int error = FALSE;
7108
7109 return get_tv_number_chk(varp, &error); /* return 0L on error */
7110}
7111
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007112 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007113get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007114{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007115 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007117 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007120 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007121 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007122#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007123 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007124 break;
7125#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007127 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007128 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007129 break;
7130 case VAR_STRING:
7131 if (varp->vval.v_string != NULL)
7132 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007133 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007134 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007135 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007136 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007137 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007138 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007139 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007140 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007141 case VAR_SPECIAL:
7142 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7143 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007144 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007146 EMSG(_("E910: Using a Job as a Number"));
7147 break;
7148#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007149 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007150#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007151 EMSG(_("E913: Using a Channel as a Number"));
7152 break;
7153#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007155 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158 if (denote == NULL) /* useful for values that must be unsigned */
7159 n = -1;
7160 else
7161 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007162 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007165#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007168{
7169 switch (varp->v_type)
7170 {
7171 case VAR_NUMBER:
7172 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007173 case VAR_FLOAT:
7174 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007175 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007176 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007177 EMSG(_("E891: Using a Funcref as a Float"));
7178 break;
7179 case VAR_STRING:
7180 EMSG(_("E892: Using a String as a Float"));
7181 break;
7182 case VAR_LIST:
7183 EMSG(_("E893: Using a List as a Float"));
7184 break;
7185 case VAR_DICT:
7186 EMSG(_("E894: Using a Dictionary as a Float"));
7187 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007188 case VAR_SPECIAL:
7189 EMSG(_("E907: Using a special value as a Float"));
7190 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007191 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007192# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007193 EMSG(_("E911: Using a Job as a Float"));
7194 break;
7195# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007196 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007197# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007198 EMSG(_("E914: Using a Channel as a Float"));
7199 break;
7200# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007201 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007202 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007203 break;
7204 }
7205 return 0;
7206}
7207#endif
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 * Get the string value of a variable.
7211 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007212 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7213 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 * If the String variable has never been set, return an empty string.
7215 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007216 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7217 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007219 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007220get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 static char_u mybuf[NUMBUFLEN];
7223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225}
7226
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007227 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007228get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007230 char_u *res = get_tv_string_buf_chk(varp, buf);
7231
7232 return res != NULL ? res : (char_u *)"";
7233}
7234
Bram Moolenaar7d647822014-04-05 21:28:56 +02007235/*
7236 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7237 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007238 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007239get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007240{
7241 static char_u mybuf[NUMBUFLEN];
7242
7243 return get_tv_string_buf_chk(varp, mybuf);
7244}
7245
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007246 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007247get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007248{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007249 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007251 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007252 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7253 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007254 return buf;
7255 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007256 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007257 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007258 break;
7259 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007260 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 break;
7262 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007263 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007264 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007265 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007266#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007267 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007268 break;
7269#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007270 case VAR_STRING:
7271 if (varp->vval.v_string != NULL)
7272 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007273 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007274 case VAR_SPECIAL:
7275 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7276 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007277 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007278#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007279 {
7280 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007281 char *status;
7282
7283 if (job == NULL)
7284 return (char_u *)"no process";
7285 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007286 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007287 : "run";
7288# ifdef UNIX
7289 vim_snprintf((char *)buf, NUMBUFLEN,
7290 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007291# elif defined(WIN32)
7292 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007293 "process %ld %s",
7294 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007295 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007296# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007297 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007298 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7299# endif
7300 return buf;
7301 }
7302#endif
7303 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007304 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007305#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007306 {
7307 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007308 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007309
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007310 if (channel == NULL)
7311 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7312 else
7313 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007314 "channel %d %s", channel->ch_id, status);
7315 return buf;
7316 }
7317#endif
7318 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007319 case VAR_UNKNOWN:
7320 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007321 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007323 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324}
7325
7326/*
7327 * Find variable "name" in the list of variables.
7328 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007330 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007333 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007338 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
Bram Moolenaara7043832005-01-21 11:56:39 +00007340 ht = find_var_ht(name, &varname);
7341 if (htp != NULL)
7342 *htp = ht;
7343 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007345 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7346 if (ret != NULL)
7347 return ret;
7348
7349 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007350 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
7353/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007354 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007355 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007358find_var_in_ht(
7359 hashtab_T *ht,
7360 int htname,
7361 char_u *varname,
7362 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007363{
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 hashitem_T *hi;
7365
7366 if (*varname == NUL)
7367 {
7368 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007369 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007371 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 case 'g': return &globvars_var;
7373 case 'v': return &vimvars_var;
7374 case 'b': return &curbuf->b_bufvar;
7375 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007376 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007377 case 'l': return get_funccal_local_var();
7378 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 }
7380 return NULL;
7381 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007382
7383 hi = hash_find(ht, varname);
7384 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007385 {
7386 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007387 * worked find the variable again. Don't auto-load a script if it was
7388 * loaded already, otherwise it would be loaded every time when
7389 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007390 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007391 {
7392 /* Note: script_autoload() may make "hi" invalid. It must either
7393 * be obtained again or not used. */
7394 if (!script_autoload(varname, FALSE) || aborting())
7395 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007396 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007397 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007398 if (HASHITEM_EMPTY(hi))
7399 return NULL;
7400 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007402}
7403
7404/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007406 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007407 * Set "varname" to the start of name without ':'.
7408 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007409 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007410find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007412 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007413 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007414
Bram Moolenaar73627d02015-08-11 15:46:09 +02007415 if (name[0] == NUL)
7416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (name[1] != ':')
7418 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007419 /* The name must not start with a colon or #. */
7420 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return NULL;
7422 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007423
7424 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007425 hi = hash_find(&compat_hashtab, name);
7426 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007427 return &compat_hashtab;
7428
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007429 ht = get_funccal_local_ht();
7430 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007432 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007435 if (*name == 'g') /* global variable */
7436 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007437 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7438 */
7439 if (vim_strchr(name + 2, ':') != NULL
7440 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007443 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007445 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007447 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 if (*name == 'v') /* v: variable */
7449 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007450 if (*name == 'a') /* a: function argument */
7451 return get_funccal_args_ht();
7452 if (*name == 'l') /* l: local function variable */
7453 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (*name == 's' /* script variable */
7455 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7456 return &SCRIPT_VARS(current_SID);
7457 return NULL;
7458}
7459
7460/*
7461 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007462 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 * Returns NULL when it doesn't exist.
7464 */
7465 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007466get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007470 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (v == NULL)
7472 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474}
7475
7476/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 * sourcing this script and when executing functions defined in the script.
7479 */
7480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
Bram Moolenaara7043832005-01-21 11:56:39 +00007483 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 hashtab_T *ht;
7485 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7488 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007489 /* Re-allocating ga_data means that an ht_array pointing to
7490 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007492 for (i = 1; i <= ga_scripts.ga_len; ++i)
7493 {
7494 ht = &SCRIPT_VARS(i);
7495 if (ht->ht_mask == HT_INIT_SIZE - 1)
7496 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007497 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007499 }
7500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 while (ga_scripts.ga_len < id)
7502 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007503 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007504 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007505 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
7508 }
7509}
7510
7511/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7513 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 */
7515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007516init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007519 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007520 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007521 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007522 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 dict_var->di_tv.vval.v_dict = dict;
7524 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007525 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7527 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528}
7529
7530/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007531 * Unreference a dictionary initialized by init_var_dict().
7532 */
7533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007534unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007535{
7536 /* Now the dict needs to be freed if no one else is using it, go back to
7537 * normal reference counting. */
7538 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7539 dict_unref(dict);
7540}
7541
7542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 * Frees all allocated variables and the value they contain.
7545 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 */
7547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007549{
7550 vars_clear_ext(ht, TRUE);
7551}
7552
7553/*
7554 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7555 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007557vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
Bram Moolenaara7043832005-01-21 11:56:39 +00007559 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 hashitem_T *hi;
7561 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007564 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007565 for (hi = ht->ht_array; todo > 0; ++hi)
7566 {
7567 if (!HASHITEM_EMPTY(hi))
7568 {
7569 --todo;
7570
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007572 * ht_array might change then. hash_clear() takes care of it
7573 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 v = HI2DI(hi);
7575 if (free_val)
7576 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007577 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007579 }
7580 }
7581 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007582 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583}
7584
Bram Moolenaara7043832005-01-21 11:56:39 +00007585/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 * Delete a variable from hashtab "ht" at item "hi".
7587 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007590delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007593
7594 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 clear_tv(&di->di_tv);
7596 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597}
7598
7599/*
7600 * List the value of one internal variable.
7601 */
7602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 char_u *tofree;
7606 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007607 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007609 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007611 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613}
7614
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007616list_one_var_a(
7617 char_u *prefix,
7618 char_u *name,
7619 int type,
7620 char_u *string,
7621 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
Bram Moolenaar31859182007-08-14 20:41:13 +00007623 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7624 msg_start();
7625 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (name != NULL) /* "a:" vars don't have a name stored */
7627 msg_puts(name);
7628 msg_putchar(' ');
7629 msg_advance(22);
7630 if (type == VAR_NUMBER)
7631 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007632 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 msg_putchar('*');
7634 else if (type == VAR_LIST)
7635 {
7636 msg_putchar('[');
7637 if (*string == '[')
7638 ++string;
7639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 else if (type == VAR_DICT)
7641 {
7642 msg_putchar('{');
7643 if (*string == '{')
7644 ++string;
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 else
7647 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007651 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007653 if (*first)
7654 {
7655 msg_clr_eos();
7656 *first = FALSE;
7657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
7660/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 * If the variable already exists, the value is updated.
7663 * Otherwise the variable is created.
7664 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007666set_var(
7667 char_u *name,
7668 typval_T *tv,
7669 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007675 ht = find_var_ht(name, &varname);
7676 if (ht == NULL || *varname == NUL)
7677 {
7678 EMSG2(_(e_illvar), name);
7679 return;
7680 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007681 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007682
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007683 /* Search in parent scope which is possible to reference from lambda */
7684 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007685 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007686
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007687 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7688 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007689 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007694 if (var_check_ro(v->di_flags, name, FALSE)
7695 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007697
7698 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007699 * Handle setting internal v: variables separately where needed to
7700 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 */
7702 if (ht == &vimvarht)
7703 {
7704 if (v->di_tv.v_type == VAR_STRING)
7705 {
7706 vim_free(v->di_tv.vval.v_string);
7707 if (copy || tv->v_type != VAR_STRING)
7708 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7709 else
7710 {
7711 /* Take over the string to avoid an extra alloc/free. */
7712 v->di_tv.vval.v_string = tv->vval.v_string;
7713 tv->vval.v_string = NULL;
7714 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007715 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007717 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007718 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007720 if (STRCMP(varname, "searchforward") == 0)
7721 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007722#ifdef FEAT_SEARCH_EXTRA
7723 else if (STRCMP(varname, "hlsearch") == 0)
7724 {
7725 no_hlsearch = !v->di_tv.vval.v_number;
7726 redraw_all_later(SOME_VALID);
7727 }
7728#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007729 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007730 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007731 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007732 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 }
7734
7735 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
7737 else /* add a new variable */
7738 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007739 /* Can't add "v:" variable. */
7740 if (ht == &vimvarht)
7741 {
7742 EMSG2(_(e_illvar), name);
7743 return;
7744 }
7745
Bram Moolenaar92124a32005-06-17 22:03:40 +00007746 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007747 if (!valid_varname(varname))
7748 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007750 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7751 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007752 if (v == NULL)
7753 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007757 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 return;
7759 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007760 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007762
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007763 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007765 else
7766 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007768 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007774 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 * Also give an error message.
7776 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007779{
7780 if (flags & DI_FLAGS_RO)
7781 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007782 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 return TRUE;
7784 }
7785 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7786 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007787 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 return TRUE;
7789 }
7790 return FALSE;
7791}
7792
7793/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007794 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7795 * Also give an error message.
7796 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007799{
7800 if (flags & DI_FLAGS_FIX)
7801 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007802 EMSG2(_("E795: Cannot delete variable %s"),
7803 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007804 return TRUE;
7805 }
7806 return FALSE;
7807}
7808
7809/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007810 * Check if a funcref is assigned to a valid variable name.
7811 * Return TRUE and give an error if not.
7812 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814var_check_func_name(
7815 char_u *name, /* points to start of variable name */
7816 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007817{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007818 /* Allow for w: b: s: and t:. */
7819 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007820 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7821 ? name[2] : name[0]))
7822 {
7823 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7824 name);
7825 return TRUE;
7826 }
7827 /* Don't allow hiding a function. When "v" is not NULL we might be
7828 * assigning another function to the same var, the type is checked
7829 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007830 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007831 {
7832 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7833 name);
7834 return TRUE;
7835 }
7836 return FALSE;
7837}
7838
7839/*
7840 * Check if a variable name is valid.
7841 * Return FALSE and give an error if not.
7842 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007844valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007845{
7846 char_u *p;
7847
7848 for (p = varname; *p != NUL; ++p)
7849 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7850 && *p != AUTOLOAD_CHAR)
7851 {
7852 EMSG2(_(e_illvar), varname);
7853 return FALSE;
7854 }
7855 return TRUE;
7856}
7857
7858/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007859 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007860 * Also give an error message, using "name" or _("name") when use_gettext is
7861 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007862 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007864tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007865{
7866 if (lock & VAR_LOCKED)
7867 {
7868 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007869 name == NULL ? (char_u *)_("Unknown")
7870 : use_gettext ? (char_u *)_(name)
7871 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007872 return TRUE;
7873 }
7874 if (lock & VAR_FIXED)
7875 {
7876 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007877 name == NULL ? (char_u *)_("Unknown")
7878 : use_gettext ? (char_u *)_(name)
7879 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007880 return TRUE;
7881 }
7882 return FALSE;
7883}
7884
7885/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007887 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007889 * It is OK for "from" and "to" to point to the same item. This is used to
7890 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007893copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007895 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007896 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007897 switch (from->v_type)
7898 {
7899 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007900 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901 to->vval.v_number = from->vval.v_number;
7902 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007903 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007904#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905 to->vval.v_float = from->vval.v_float;
7906 break;
7907#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007908 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007910 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007911 if (to->vval.v_job != NULL)
7912 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007913 break;
7914#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007915 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007917 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007918 if (to->vval.v_channel != NULL)
7919 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007920 break;
7921#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 case VAR_STRING:
7923 case VAR_FUNC:
7924 if (from->vval.v_string == NULL)
7925 to->vval.v_string = NULL;
7926 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007927 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007929 if (from->v_type == VAR_FUNC)
7930 func_ref(to->vval.v_string);
7931 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007932 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007933 case VAR_PARTIAL:
7934 if (from->vval.v_partial == NULL)
7935 to->vval.v_partial = NULL;
7936 else
7937 {
7938 to->vval.v_partial = from->vval.v_partial;
7939 ++to->vval.v_partial->pt_refcount;
7940 }
7941 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942 case VAR_LIST:
7943 if (from->vval.v_list == NULL)
7944 to->vval.v_list = NULL;
7945 else
7946 {
7947 to->vval.v_list = from->vval.v_list;
7948 ++to->vval.v_list->lv_refcount;
7949 }
7950 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 case VAR_DICT:
7952 if (from->vval.v_dict == NULL)
7953 to->vval.v_dict = NULL;
7954 else
7955 {
7956 to->vval.v_dict = from->vval.v_dict;
7957 ++to->vval.v_dict->dv_refcount;
7958 }
7959 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007960 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007961 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 break;
7963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964}
7965
7966/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007967 * Make a copy of an item.
7968 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007969 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7970 * reference to an already copied list/dict can be used.
7971 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007972 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007974item_copy(
7975 typval_T *from,
7976 typval_T *to,
7977 int deep,
7978 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007979{
7980 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007981 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982
Bram Moolenaar33570922005-01-25 22:26:29 +00007983 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984 {
7985 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007986 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 }
7988 ++recurse;
7989
7990 switch (from->v_type)
7991 {
7992 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007994 case VAR_STRING:
7995 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007996 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007997 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007998 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007999 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000 copy_tv(from, to);
8001 break;
8002 case VAR_LIST:
8003 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008004 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008005 if (from->vval.v_list == NULL)
8006 to->vval.v_list = NULL;
8007 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8008 {
8009 /* use the copy made earlier */
8010 to->vval.v_list = from->vval.v_list->lv_copylist;
8011 ++to->vval.v_list->lv_refcount;
8012 }
8013 else
8014 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8015 if (to->vval.v_list == NULL)
8016 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008017 break;
8018 case VAR_DICT:
8019 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008020 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008021 if (from->vval.v_dict == NULL)
8022 to->vval.v_dict = NULL;
8023 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8024 {
8025 /* use the copy made earlier */
8026 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8027 ++to->vval.v_dict->dv_refcount;
8028 }
8029 else
8030 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8031 if (to->vval.v_dict == NULL)
8032 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008034 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008035 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008036 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 }
8038 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008039 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008040}
8041
8042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 * This function is used by f_input() and f_inputdialog() functions. The third
8044 * argument to f_input() specifies the type of completion to use at the
8045 * prompt. The third argument to f_inputdialog() specifies the value to return
8046 * when the user cancels the prompt.
8047 */
8048 void
8049get_user_input(
8050 typval_T *argvars,
8051 typval_T *rettv,
8052 int inputdialog,
8053 int secret)
8054{
8055 char_u *prompt = get_tv_string_chk(&argvars[0]);
8056 char_u *p = NULL;
8057 int c;
8058 char_u buf[NUMBUFLEN];
8059 int cmd_silent_save = cmd_silent;
8060 char_u *defstr = (char_u *)"";
8061 int xp_type = EXPAND_NOTHING;
8062 char_u *xp_arg = NULL;
8063
8064 rettv->v_type = VAR_STRING;
8065 rettv->vval.v_string = NULL;
8066
8067#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008068 /* While starting up, there is no place to enter text. When running tests
8069 * with --not-a-term we assume feedkeys() will be used. */
8070 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 return;
8072#endif
8073
8074 cmd_silent = FALSE; /* Want to see the prompt. */
8075 if (prompt != NULL)
8076 {
8077 /* Only the part of the message after the last NL is considered as
8078 * prompt for the command line */
8079 p = vim_strrchr(prompt, '\n');
8080 if (p == NULL)
8081 p = prompt;
8082 else
8083 {
8084 ++p;
8085 c = *p;
8086 *p = NUL;
8087 msg_start();
8088 msg_clr_eos();
8089 msg_puts_attr(prompt, echo_attr);
8090 msg_didout = FALSE;
8091 msg_starthere();
8092 *p = c;
8093 }
8094 cmdline_row = msg_row;
8095
8096 if (argvars[1].v_type != VAR_UNKNOWN)
8097 {
8098 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8099 if (defstr != NULL)
8100 stuffReadbuffSpec(defstr);
8101
8102 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8103 {
8104 char_u *xp_name;
8105 int xp_namelen;
8106 long argt;
8107
8108 /* input() with a third argument: completion */
8109 rettv->vval.v_string = NULL;
8110
8111 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8112 if (xp_name == NULL)
8113 return;
8114
8115 xp_namelen = (int)STRLEN(xp_name);
8116
8117 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8118 &xp_arg) == FAIL)
8119 return;
8120 }
8121 }
8122
8123 if (defstr != NULL)
8124 {
8125 int save_ex_normal_busy = ex_normal_busy;
8126 ex_normal_busy = 0;
8127 rettv->vval.v_string =
8128 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8129 xp_type, xp_arg);
8130 ex_normal_busy = save_ex_normal_busy;
8131 }
8132 if (inputdialog && rettv->vval.v_string == NULL
8133 && argvars[1].v_type != VAR_UNKNOWN
8134 && argvars[2].v_type != VAR_UNKNOWN)
8135 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8136 &argvars[2], buf));
8137
8138 vim_free(xp_arg);
8139
8140 /* since the user typed this, no need to wait for return */
8141 need_wait_return = FALSE;
8142 msg_didout = FALSE;
8143 }
8144 cmd_silent = cmd_silent_save;
8145}
8146
8147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 * ":echo expr1 ..." print each argument separated with a space, add a
8149 * newline at the end.
8150 * ":echon expr1 ..." print each argument plain.
8151 */
8152 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008153ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008156 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 char_u *p;
8159 int needclr = TRUE;
8160 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
8163 if (eap->skip)
8164 ++emsg_skip;
8165 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8166 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167 /* If eval1() causes an error message the text from the command may
8168 * still need to be cleared. E.g., "echo 22,44". */
8169 need_clr_eos = needclr;
8170
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008172 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
8174 /*
8175 * Report the invalid expression unless the expression evaluation
8176 * has been cancelled due to an aborting error, an interrupt, or an
8177 * exception.
8178 */
8179 if (!aborting())
8180 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008181 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 break;
8183 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184 need_clr_eos = FALSE;
8185
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 if (!eap->skip)
8187 {
8188 if (atstart)
8189 {
8190 atstart = FALSE;
8191 /* Call msg_start() after eval1(), evaluating the expression
8192 * may cause a message to appear. */
8193 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008194 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008195 /* Mark the saved text as finishing the line, so that what
8196 * follows is displayed on a new line when scrolling back
8197 * at the more prompt. */
8198 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
8202 else if (eap->cmdidx == CMD_echo)
8203 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008204 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008205 if (p != NULL)
8206 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008210 if (*p != TAB && needclr)
8211 {
8212 /* remove any text still there from the command */
8213 msg_clr_eos();
8214 needclr = FALSE;
8215 }
8216 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
8218 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008219 {
8220#ifdef FEAT_MBYTE
8221 if (has_mbyte)
8222 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008223 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008224
8225 (void)msg_outtrans_len_attr(p, i, echo_attr);
8226 p += i - 1;
8227 }
8228 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008230 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008233 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 arg = skipwhite(arg);
8237 }
8238 eap->nextcmd = check_nextcmd(arg);
8239
8240 if (eap->skip)
8241 --emsg_skip;
8242 else
8243 {
8244 /* remove text that may still be there from the command */
8245 if (needclr)
8246 msg_clr_eos();
8247 if (eap->cmdidx == CMD_echo)
8248 msg_end();
8249 }
8250}
8251
8252/*
8253 * ":echohl {name}".
8254 */
8255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008256ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008258 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259}
8260
8261/*
8262 * ":execute expr1 ..." execute the result of an expression.
8263 * ":echomsg expr1 ..." Print a message
8264 * ":echoerr expr1 ..." Print an error
8265 * Each gets spaces around each argument and a newline at the end for
8266 * echo commands
8267 */
8268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008269ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270{
8271 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 int ret = OK;
8274 char_u *p;
8275 garray_T ga;
8276 int len;
8277 int save_did_emsg;
8278
8279 ga_init2(&ga, 1, 80);
8280
8281 if (eap->skip)
8282 ++emsg_skip;
8283 while (*arg != NUL && *arg != '|' && *arg != '\n')
8284 {
8285 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
8288 /*
8289 * Report the invalid expression unless the expression evaluation
8290 * has been cancelled due to an aborting error, an interrupt, or an
8291 * exception.
8292 */
8293 if (!aborting())
8294 EMSG2(_(e_invexpr2), p);
8295 ret = FAIL;
8296 break;
8297 }
8298
8299 if (!eap->skip)
8300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008301 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 len = (int)STRLEN(p);
8303 if (ga_grow(&ga, len + 2) == FAIL)
8304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 ret = FAIL;
8307 break;
8308 }
8309 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 ga.ga_len += len;
8313 }
8314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008315 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 arg = skipwhite(arg);
8317 }
8318
8319 if (ret != FAIL && ga.ga_data != NULL)
8320 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008321 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8322 {
8323 /* Mark the already saved text as finishing the line, so that what
8324 * follows is displayed on a new line when scrolling back at the
8325 * more prompt. */
8326 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008327 }
8328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008332 out_flush();
8333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else if (eap->cmdidx == CMD_echoerr)
8335 {
8336 /* We don't want to abort following commands, restore did_emsg. */
8337 save_did_emsg = did_emsg;
8338 EMSG((char_u *)ga.ga_data);
8339 if (!force_abort)
8340 did_emsg = save_did_emsg;
8341 }
8342 else if (eap->cmdidx == CMD_execute)
8343 do_cmdline((char_u *)ga.ga_data,
8344 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8345 }
8346
8347 ga_clear(&ga);
8348
8349 if (eap->skip)
8350 --emsg_skip;
8351
8352 eap->nextcmd = check_nextcmd(arg);
8353}
8354
8355/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 * Find window specified by "vp" in tabpage "tp".
8357 */
8358 win_T *
8359find_win_by_nr(
8360 typval_T *vp,
8361 tabpage_T *tp UNUSED) /* NULL for current tab page */
8362{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 int nr;
8365
8366 nr = (int)get_tv_number_chk(vp, NULL);
8367
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 if (nr < 0)
8369 return NULL;
8370 if (nr == 0)
8371 return curwin;
8372
Bram Moolenaar29323592016-07-24 22:04:11 +02008373 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8374 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 if (nr >= LOWEST_WIN_ID)
8376 {
8377 if (wp->w_id == nr)
8378 return wp;
8379 }
8380 else if (--nr <= 0)
8381 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008382 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 if (nr >= LOWEST_WIN_ID)
8384 return NULL;
8385 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386}
8387
8388/*
8389 * Find window specified by "wvp" in tabpage "tvp".
8390 */
8391 win_T *
8392find_tabwin(
8393 typval_T *wvp, /* VAR_UNKNOWN for current window */
8394 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8395{
8396 win_T *wp = NULL;
8397 tabpage_T *tp = NULL;
8398 long n;
8399
8400 if (wvp->v_type != VAR_UNKNOWN)
8401 {
8402 if (tvp->v_type != VAR_UNKNOWN)
8403 {
8404 n = (long)get_tv_number(tvp);
8405 if (n >= 0)
8406 tp = find_tabpage(n);
8407 }
8408 else
8409 tp = curtab;
8410
8411 if (tp != NULL)
8412 wp = find_win_by_nr(wvp, tp);
8413 }
8414 else
8415 wp = curwin;
8416
8417 return wp;
8418}
8419
8420/*
8421 * getwinvar() and gettabwinvar()
8422 */
8423 void
8424getwinvar(
8425 typval_T *argvars,
8426 typval_T *rettv,
8427 int off) /* 1 for gettabwinvar() */
8428{
8429 win_T *win;
8430 char_u *varname;
8431 dictitem_T *v;
8432 tabpage_T *tp = NULL;
8433 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 win_T *oldcurwin;
8435 tabpage_T *oldtabpage;
8436 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 if (off == 1)
8439 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8440 else
8441 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442 win = find_win_by_nr(&argvars[off], tp);
8443 varname = get_tv_string_chk(&argvars[off + 1]);
8444 ++emsg_off;
8445
8446 rettv->v_type = VAR_STRING;
8447 rettv->vval.v_string = NULL;
8448
8449 if (win != NULL && varname != NULL)
8450 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451 /* Set curwin to be our win, temporarily. Also set the tabpage,
8452 * otherwise the window is not valid. Only do this when needed,
8453 * autocommands get blocked. */
8454 need_switch_win = !(tp == curtab && win == curwin);
8455 if (!need_switch_win
8456 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008458 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008460 if (varname[1] == NUL)
8461 {
8462 /* get all window-local options in a dict */
8463 dict_T *opts = get_winbuf_options(FALSE);
8464
8465 if (opts != NULL)
8466 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008467 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008468 done = TRUE;
8469 }
8470 }
8471 else if (get_option_tv(&varname, rettv, 1) == OK)
8472 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 done = TRUE;
8474 }
8475 else
8476 {
8477 /* Look up the variable. */
8478 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8479 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8480 varname, FALSE);
8481 if (v != NULL)
8482 {
8483 copy_tv(&v->di_tv, rettv);
8484 done = TRUE;
8485 }
8486 }
8487 }
8488
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 if (need_switch_win)
8490 /* restore previous notion of curwin */
8491 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 }
8493
8494 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8495 /* use the default return value */
8496 copy_tv(&argvars[off + 2], rettv);
8497
8498 --emsg_off;
8499}
8500
8501/*
8502 * "setwinvar()" and "settabwinvar()" functions
8503 */
8504 void
8505setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8506{
8507 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 win_T *save_curwin;
8509 tabpage_T *save_curtab;
8510 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511 char_u *varname, *winvarname;
8512 typval_T *varp;
8513 char_u nbuf[NUMBUFLEN];
8514 tabpage_T *tp = NULL;
8515
8516 if (check_restricted() || check_secure())
8517 return;
8518
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 if (off == 1)
8520 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8521 else
8522 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 win = find_win_by_nr(&argvars[off], tp);
8524 varname = get_tv_string_chk(&argvars[off + 1]);
8525 varp = &argvars[off + 2];
8526
8527 if (win != NULL && varname != NULL && varp != NULL)
8528 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529 need_switch_win = !(tp == curtab && win == curwin);
8530 if (!need_switch_win
8531 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 {
8533 if (*varname == '&')
8534 {
8535 long numval;
8536 char_u *strval;
8537 int error = FALSE;
8538
8539 ++varname;
8540 numval = (long)get_tv_number_chk(varp, &error);
8541 strval = get_tv_string_buf_chk(varp, nbuf);
8542 if (!error && strval != NULL)
8543 set_option_value(varname, numval, strval, OPT_LOCAL);
8544 }
8545 else
8546 {
8547 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8548 if (winvarname != NULL)
8549 {
8550 STRCPY(winvarname, "w:");
8551 STRCPY(winvarname + 2, varname);
8552 set_var(winvarname, varp, TRUE);
8553 vim_free(winvarname);
8554 }
8555 }
8556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 if (need_switch_win)
8558 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 }
8560}
8561
8562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8564 * "arg" points to the "&" or '+' when called, to "option" when returning.
8565 * Returns NULL when no option name found. Otherwise pointer to the char
8566 * after the option name.
8567 */
8568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008569find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571 char_u *p = *arg;
8572
8573 ++p;
8574 if (*p == 'g' && p[1] == ':')
8575 {
8576 *opt_flags = OPT_GLOBAL;
8577 p += 2;
8578 }
8579 else if (*p == 'l' && p[1] == ':')
8580 {
8581 *opt_flags = OPT_LOCAL;
8582 p += 2;
8583 }
8584 else
8585 *opt_flags = 0;
8586
8587 if (!ASCII_ISALPHA(*p))
8588 return NULL;
8589 *arg = p;
8590
8591 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8592 p += 4; /* termcap option */
8593 else
8594 while (ASCII_ISALPHA(*p))
8595 ++p;
8596 return p;
8597}
8598
8599/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008600 * Return the autoload script name for a function or variable name.
8601 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008603 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008604autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008605{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008606 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008607 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008609 /* Get the script file name: replace '#' with '/', append ".vim". */
8610 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8611 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008612 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008613 STRCPY(scriptname, "autoload/");
8614 STRCAT(scriptname, name);
8615 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8616 STRCAT(scriptname, ".vim");
8617 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8618 *p = '/';
8619 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008620}
8621
8622/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008623 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008624 * Return TRUE if a package was loaded.
8625 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008627script_autoload(
8628 char_u *name,
8629 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008630{
8631 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008632 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008633 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008634 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008636 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008637 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008638 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008639 return FALSE;
8640
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008641 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008642
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008643 /* Find the name in the list of previously loaded package names. Skip
8644 * "autoload/", it's always the same. */
8645 for (i = 0; i < ga_loaded.ga_len; ++i)
8646 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8647 break;
8648 if (!reload && i < ga_loaded.ga_len)
8649 ret = FALSE; /* was loaded already */
8650 else
8651 {
8652 /* Remember the name if it wasn't loaded already. */
8653 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8654 {
8655 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8656 tofree = NULL;
8657 }
8658
8659 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008660 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008661 ret = TRUE;
8662 }
8663
8664 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008665 return ret;
8666}
8667
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8669typedef enum
8670{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008671 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8672 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8673 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674} var_flavour_T;
8675
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008676static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
8678 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008679var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 char_u *p = varname;
8682
8683 if (ASCII_ISUPPER(*p))
8684 {
8685 while (*(++p))
8686 if (ASCII_ISLOWER(*p))
8687 return VAR_FLAVOUR_SESSION;
8688 return VAR_FLAVOUR_VIMINFO;
8689 }
8690 else
8691 return VAR_FLAVOUR_DEFAULT;
8692}
8693#endif
8694
8695#if defined(FEAT_VIMINFO) || defined(PROTO)
8696/*
8697 * Restore global vars that start with a capital from the viminfo file
8698 */
8699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008700read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008703 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008705 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
8707 if (!writing && (find_viminfo_parameter('!') != NULL))
8708 {
8709 tab = vim_strchr(virp->vir_line + 1, '\t');
8710 if (tab != NULL)
8711 {
8712 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008713 switch (*tab)
8714 {
8715 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008716#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008717 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008718#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008719 case 'D': type = VAR_DICT; break;
8720 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008721 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 tab = vim_strchr(tab, '\t');
8725 if (tab != NULL)
8726 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008727 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008728 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008729 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008731#ifdef FEAT_FLOAT
8732 else if (type == VAR_FLOAT)
8733 (void)string2float(tab + 1, &tv.vval.v_float);
8734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008736 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008737 if (type == VAR_DICT || type == VAR_LIST)
8738 {
8739 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8740
8741 if (etv == NULL)
8742 /* Failed to parse back the dict or list, use it as a
8743 * string. */
8744 tv.v_type = VAR_STRING;
8745 else
8746 {
8747 vim_free(tv.vval.v_string);
8748 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008749 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008750 }
8751 }
8752
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008753 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008754 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008755 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008756 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008757
8758 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008759 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008760 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8761 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763 }
8764 }
8765
8766 return viminfo_readline(virp);
8767}
8768
8769/*
8770 * Write global vars that start with a capital to the viminfo file
8771 */
8772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 hashitem_T *hi;
8776 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008777 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008778 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008779 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008781 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782
8783 if (find_viminfo_parameter('!') == NULL)
8784 return;
8785
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008786 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008787
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008788 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008789 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008791 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008793 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008794 this_var = HI2DI(hi);
8795 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008798 {
8799 case VAR_STRING: s = "STR"; break;
8800 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008801 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008802 case VAR_DICT: s = "DIC"; break;
8803 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008804 case VAR_SPECIAL: s = "XPL"; break;
8805
8806 case VAR_UNKNOWN:
8807 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008808 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008809 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008810 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008811 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008812 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008814 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008815 if (p != NULL)
8816 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008817 vim_free(tofree);
8818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820 }
8821}
8822#endif
8823
8824#if defined(FEAT_SESSION) || defined(PROTO)
8825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008826store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
Bram Moolenaar33570922005-01-25 22:26:29 +00008828 hashitem_T *hi;
8829 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008830 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 char_u *p, *t;
8832
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008833 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008836 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008838 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 this_var = HI2DI(hi);
8840 if ((this_var->di_tv.v_type == VAR_NUMBER
8841 || this_var->di_tv.v_type == VAR_STRING)
8842 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008843 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008844 /* Escape special characters with a backslash. Turn a LF and
8845 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008847 (char_u *)"\\\"\n\r");
8848 if (p == NULL) /* out of memory */
8849 break;
8850 for (t = p; *t != NUL; ++t)
8851 if (*t == '\n')
8852 *t = 'n';
8853 else if (*t == '\r')
8854 *t = 'r';
8855 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 this_var->di_key,
8857 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8858 : ' ',
8859 p,
8860 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8861 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008862 || put_eol(fd) == FAIL)
8863 {
8864 vim_free(p);
8865 return FAIL;
8866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 vim_free(p);
8868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008869#ifdef FEAT_FLOAT
8870 else if (this_var->di_tv.v_type == VAR_FLOAT
8871 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8872 {
8873 float_T f = this_var->di_tv.vval.v_float;
8874 int sign = ' ';
8875
8876 if (f < 0)
8877 {
8878 f = -f;
8879 sign = '-';
8880 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008881 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008882 this_var->di_key, sign, f) < 0)
8883 || put_eol(fd) == FAIL)
8884 return FAIL;
8885 }
8886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 }
8888 }
8889 return OK;
8890}
8891#endif
8892
Bram Moolenaar661b1822005-07-28 22:36:45 +00008893/*
8894 * Display script name where an item was last set.
8895 * Should only be invoked when 'verbose' is non-zero.
8896 */
8897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008898last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008899{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008900 char_u *p;
8901
Bram Moolenaar661b1822005-07-28 22:36:45 +00008902 if (scriptID != 0)
8903 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008904 p = home_replace_save(NULL, get_scriptname(scriptID));
8905 if (p != NULL)
8906 {
8907 verbose_enter();
8908 MSG_PUTS(_("\n\tLast set from "));
8909 MSG_PUTS(p);
8910 vim_free(p);
8911 verbose_leave();
8912 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008913 }
8914}
8915
Bram Moolenaar53744302015-07-17 17:38:22 +02008916/* reset v:option_new, v:option_old and v:option_type */
8917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008918reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008919{
8920 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8921 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8922 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8923}
8924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925/*
8926 * Prepare "gap" for an assert error and add the sourcing position.
8927 */
8928 void
8929prepare_assert_error(garray_T *gap)
8930{
8931 char buf[NUMBUFLEN];
8932
8933 ga_init2(gap, 1, 100);
8934 if (sourcing_name != NULL)
8935 {
8936 ga_concat(gap, sourcing_name);
8937 if (sourcing_lnum > 0)
8938 ga_concat(gap, (char_u *)" ");
8939 }
8940 if (sourcing_lnum > 0)
8941 {
8942 sprintf(buf, "line %ld", (long)sourcing_lnum);
8943 ga_concat(gap, (char_u *)buf);
8944 }
8945 if (sourcing_name != NULL || sourcing_lnum > 0)
8946 ga_concat(gap, (char_u *)": ");
8947}
8948
8949/*
8950 * Add an assert error to v:errors.
8951 */
8952 void
8953assert_error(garray_T *gap)
8954{
8955 struct vimvar *vp = &vimvars[VV_ERRORS];
8956
8957 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8958 /* Make sure v:errors is a list. */
8959 set_vim_var_list(VV_ERRORS, list_alloc());
8960 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8961}
8962
8963 void
8964assert_equal_common(typval_T *argvars, assert_type_T atype)
8965{
8966 garray_T ga;
8967
8968 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8969 != (atype == ASSERT_EQUAL))
8970 {
8971 prepare_assert_error(&ga);
8972 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8973 atype);
8974 assert_error(&ga);
8975 ga_clear(&ga);
8976 }
8977}
8978
8979 void
8980assert_match_common(typval_T *argvars, assert_type_T atype)
8981{
8982 garray_T ga;
8983 char_u buf1[NUMBUFLEN];
8984 char_u buf2[NUMBUFLEN];
8985 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8986 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8987
8988 if (pat == NULL || text == NULL)
8989 EMSG(_(e_invarg));
8990 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8991 {
8992 prepare_assert_error(&ga);
8993 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8994 atype);
8995 assert_error(&ga);
8996 ga_clear(&ga);
8997 }
8998}
8999
Bram Moolenaar61c04492016-07-23 15:35:35 +02009000 void
9001assert_inrange(typval_T *argvars)
9002{
9003 garray_T ga;
9004 int error = FALSE;
9005 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9006 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9007 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9008 char_u *tofree;
9009 char msg[200];
9010 char_u numbuf[NUMBUFLEN];
9011
9012 if (error)
9013 return;
9014 if (actual < lower || actual > upper)
9015 {
9016 prepare_assert_error(&ga);
9017 if (argvars[3].v_type != VAR_UNKNOWN)
9018 {
9019 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9020 vim_free(tofree);
9021 }
9022 else
9023 {
9024 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9025 (long)lower, (long)upper, (long)actual);
9026 ga_concat(&ga, (char_u *)msg);
9027 }
9028 assert_error(&ga);
9029 ga_clear(&ga);
9030 }
9031}
9032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033/*
9034 * Common for assert_true() and assert_false().
9035 */
9036 void
9037assert_bool(typval_T *argvars, int isTrue)
9038{
9039 int error = FALSE;
9040 garray_T ga;
9041
9042 if (argvars[0].v_type == VAR_SPECIAL
9043 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9044 return;
9045 if (argvars[0].v_type != VAR_NUMBER
9046 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9047 || error)
9048 {
9049 prepare_assert_error(&ga);
9050 fill_assert_error(&ga, &argvars[1],
9051 (char_u *)(isTrue ? "True" : "False"),
9052 NULL, &argvars[0], ASSERT_OTHER);
9053 assert_error(&ga);
9054 ga_clear(&ga);
9055 }
9056}
9057
9058 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009059assert_report(typval_T *argvars)
9060{
9061 garray_T ga;
9062
9063 prepare_assert_error(&ga);
9064 ga_concat(&ga, get_tv_string(&argvars[0]));
9065 assert_error(&ga);
9066 ga_clear(&ga);
9067}
9068
9069 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070assert_exception(typval_T *argvars)
9071{
9072 garray_T ga;
9073 char_u *error = get_tv_string_chk(&argvars[0]);
9074
9075 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9076 {
9077 prepare_assert_error(&ga);
9078 ga_concat(&ga, (char_u *)"v:exception is not set");
9079 assert_error(&ga);
9080 ga_clear(&ga);
9081 }
9082 else if (error != NULL
9083 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9084 {
9085 prepare_assert_error(&ga);
9086 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9087 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9088 assert_error(&ga);
9089 ga_clear(&ga);
9090 }
9091}
9092
9093 void
9094assert_fails(typval_T *argvars)
9095{
9096 char_u *cmd = get_tv_string_chk(&argvars[0]);
9097 garray_T ga;
9098
9099 called_emsg = FALSE;
9100 suppress_errthrow = TRUE;
9101 emsg_silent = TRUE;
9102 do_cmdline_cmd(cmd);
9103 if (!called_emsg)
9104 {
9105 prepare_assert_error(&ga);
9106 ga_concat(&ga, (char_u *)"command did not fail: ");
9107 ga_concat(&ga, cmd);
9108 assert_error(&ga);
9109 ga_clear(&ga);
9110 }
9111 else if (argvars[1].v_type != VAR_UNKNOWN)
9112 {
9113 char_u buf[NUMBUFLEN];
9114 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9115
9116 if (error == NULL
9117 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9118 {
9119 prepare_assert_error(&ga);
9120 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9121 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9122 assert_error(&ga);
9123 ga_clear(&ga);
9124 }
9125 }
9126
9127 called_emsg = FALSE;
9128 suppress_errthrow = FALSE;
9129 emsg_silent = FALSE;
9130 emsg_on_display = FALSE;
9131 set_vim_var_string(VV_ERRMSG, NULL, 0);
9132}
9133
9134/*
9135 * Append "str" to "gap", escaping unprintable characters.
9136 * Changes NL to \n, CR to \r, etc.
9137 */
9138 static void
9139ga_concat_esc(garray_T *gap, char_u *str)
9140{
9141 char_u *p;
9142 char_u buf[NUMBUFLEN];
9143
9144 if (str == NULL)
9145 {
9146 ga_concat(gap, (char_u *)"NULL");
9147 return;
9148 }
9149
9150 for (p = str; *p != NUL; ++p)
9151 switch (*p)
9152 {
9153 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9154 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9155 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9156 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9157 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9158 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9159 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9160 default:
9161 if (*p < ' ')
9162 {
9163 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9164 ga_concat(gap, buf);
9165 }
9166 else
9167 ga_append(gap, *p);
9168 break;
9169 }
9170}
9171
9172/*
9173 * Fill "gap" with information about an assert error.
9174 */
9175 void
9176fill_assert_error(
9177 garray_T *gap,
9178 typval_T *opt_msg_tv,
9179 char_u *exp_str,
9180 typval_T *exp_tv,
9181 typval_T *got_tv,
9182 assert_type_T atype)
9183{
9184 char_u numbuf[NUMBUFLEN];
9185 char_u *tofree;
9186
9187 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9188 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009189 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9190 vim_free(tofree);
9191 ga_concat(gap, (char_u *)": ");
9192 }
9193
9194 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9195 ga_concat(gap, (char_u *)"Pattern ");
9196 else if (atype == ASSERT_NOTEQUAL)
9197 ga_concat(gap, (char_u *)"Expected not equal to ");
9198 else
9199 ga_concat(gap, (char_u *)"Expected ");
9200 if (exp_str == NULL)
9201 {
9202 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203 vim_free(tofree);
9204 }
9205 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009206 ga_concat_esc(gap, exp_str);
9207 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009209 if (atype == ASSERT_MATCH)
9210 ga_concat(gap, (char_u *)" does not match ");
9211 else if (atype == ASSERT_NOTMATCH)
9212 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009214 ga_concat(gap, (char_u *)" but got ");
9215 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9216 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009217 }
9218}
9219
Bram Moolenaar53744302015-07-17 17:38:22 +02009220
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#endif /* FEAT_EVAL */
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009224#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226#ifdef WIN3264
9227/*
9228 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9229 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009230static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9231static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9232static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009235 * Get the short path (8.3) for the filename in "fnamep".
9236 * Only works for a valid file name.
9237 * When the path gets longer "fnamep" is changed and the allocated buffer
9238 * is put in "bufp".
9239 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9240 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 */
9242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009243get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009245 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 char_u *newbuf;
9247
9248 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009249 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 if (l > len - 1)
9251 {
9252 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009253 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 newbuf = vim_strnsave(*fnamep, l);
9255 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
9258 vim_free(*bufp);
9259 *fnamep = *bufp = newbuf;
9260
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009261 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009262 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
9264
9265 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009266 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267}
9268
9269/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009270 * Get the short path (8.3) for the filename in "fname". The converted
9271 * path is returned in "bufp".
9272 *
9273 * Some of the directories specified in "fname" may not exist. This function
9274 * will shorten the existing directories at the beginning of the path and then
9275 * append the remaining non-existing path.
9276 *
9277 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009278 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009279 * bufp - Pointer to an allocated buffer for the filename.
9280 * fnamelen - Length of the filename pointed to by fname
9281 *
9282 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 */
9284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009285shortpath_for_invalid_fname(
9286 char_u **fname,
9287 char_u **bufp,
9288 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009290 char_u *short_fname, *save_fname, *pbuf_unused;
9291 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009293 int old_len, len;
9294 int new_len, sfx_len;
9295 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
9297 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009298 old_len = *fnamelen;
9299 save_fname = vim_strnsave(*fname, old_len);
9300 pbuf_unused = NULL;
9301 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009303 endp = save_fname + old_len - 1; /* Find the end of the copy */
9304 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009306 /*
9307 * Try shortening the supplied path till it succeeds by removing one
9308 * directory at a time from the tail of the path.
9309 */
9310 len = 0;
9311 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009313 /* go back one path-separator */
9314 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9315 --endp;
9316 if (endp <= save_fname)
9317 break; /* processed the complete path */
9318
9319 /*
9320 * Replace the path separator with a NUL and try to shorten the
9321 * resulting path.
9322 */
9323 ch = *endp;
9324 *endp = 0;
9325 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009326 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009327 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9328 {
9329 retval = FAIL;
9330 goto theend;
9331 }
9332 *endp = ch; /* preserve the string */
9333
9334 if (len > 0)
9335 break; /* successfully shortened the path */
9336
9337 /* failed to shorten the path. Skip the path separator */
9338 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
9340
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009341 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009343 /*
9344 * Succeeded in shortening the path. Now concatenate the shortened
9345 * path with the remaining path at the tail.
9346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009348 /* Compute the length of the new path. */
9349 sfx_len = (int)(save_endp - endp) + 1;
9350 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009352 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009354 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009356 /* There is not enough space in the currently allocated string,
9357 * copy it to a buffer big enough. */
9358 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009360 {
9361 retval = FAIL;
9362 goto theend;
9363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 }
9365 else
9366 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009367 /* Transfer short_fname to the main buffer (it's big enough),
9368 * unless get_short_pathname() did its work in-place. */
9369 *fname = *bufp = save_fname;
9370 if (short_fname != save_fname)
9371 vim_strncpy(save_fname, short_fname, len);
9372 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009374
9375 /* concat the not-shortened part of the path */
9376 vim_strncpy(*fname + len, endp, sfx_len);
9377 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009379
9380theend:
9381 vim_free(pbuf_unused);
9382 vim_free(save_fname);
9383
9384 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385}
9386
9387/*
9388 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009389 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 */
9391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392shortpath_for_partial(
9393 char_u **fnamep,
9394 char_u **bufp,
9395 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 int sepcount, len, tflen;
9398 char_u *p;
9399 char_u *pbuf, *tfname;
9400 int hasTilde;
9401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402 /* Count up the path separators from the RHS.. so we know which part
9403 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009405 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 if (vim_ispathsep(*p))
9407 ++sepcount;
9408
9409 /* Need full path first (use expand_env() to remove a "~/") */
9410 hasTilde = (**fnamep == '~');
9411 if (hasTilde)
9412 pbuf = tfname = expand_env_save(*fnamep);
9413 else
9414 pbuf = tfname = FullName_save(*fnamep, FALSE);
9415
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009416 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009418 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9419 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420
9421 if (len == 0)
9422 {
9423 /* Don't have a valid filename, so shorten the rest of the
9424 * path if we can. This CAN give us invalid 8.3 filenames, but
9425 * there's not a lot of point in guessing what it might be.
9426 */
9427 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009428 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9429 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 }
9431
9432 /* Count the paths backward to find the beginning of the desired string. */
9433 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009434 {
9435#ifdef FEAT_MBYTE
9436 if (has_mbyte)
9437 p -= mb_head_off(tfname, p);
9438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 if (vim_ispathsep(*p))
9440 {
9441 if (sepcount == 0 || (hasTilde && sepcount == 1))
9442 break;
9443 else
9444 sepcount --;
9445 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 if (hasTilde)
9448 {
9449 --p;
9450 if (p >= tfname)
9451 *p = '~';
9452 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009453 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
9455 else
9456 ++p;
9457
9458 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9459 vim_free(*bufp);
9460 *fnamelen = (int)STRLEN(p);
9461 *bufp = pbuf;
9462 *fnamep = p;
9463
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009464 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466#endif /* WIN3264 */
9467
9468/*
9469 * Adjust a filename, according to a string of modifiers.
9470 * *fnamep must be NUL terminated when called. When returning, the length is
9471 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009472 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 * When there is an error, *fnamep is set to NULL.
9474 */
9475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476modify_fname(
9477 char_u *src, /* string with modifiers */
9478 int *usedlen, /* characters after src that are used */
9479 char_u **fnamep, /* file name so far */
9480 char_u **bufp, /* buffer for allocated file name or NULL */
9481 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483 int valid = 0;
9484 char_u *tail;
9485 char_u *s, *p, *pbuf;
9486 char_u dirname[MAXPATHL];
9487 int c;
9488 int has_fullname = 0;
9489#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009490 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 int has_shortname = 0;
9492#endif
9493
9494repeat:
9495 /* ":p" - full path/file_name */
9496 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9497 {
9498 has_fullname = 1;
9499
9500 valid |= VALID_PATH;
9501 *usedlen += 2;
9502
9503 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9504 if ((*fnamep)[0] == '~'
9505#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9506 && ((*fnamep)[1] == '/'
9507# ifdef BACKSLASH_IN_FILENAME
9508 || (*fnamep)[1] == '\\'
9509# endif
9510 || (*fnamep)[1] == NUL)
9511
9512#endif
9513 )
9514 {
9515 *fnamep = expand_env_save(*fnamep);
9516 vim_free(*bufp); /* free any allocated file name */
9517 *bufp = *fnamep;
9518 if (*fnamep == NULL)
9519 return -1;
9520 }
9521
9522 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009523 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
9525 if (vim_ispathsep(*p)
9526 && p[1] == '.'
9527 && (p[2] == NUL
9528 || vim_ispathsep(p[2])
9529 || (p[2] == '.'
9530 && (p[3] == NUL || vim_ispathsep(p[3])))))
9531 break;
9532 }
9533
9534 /* FullName_save() is slow, don't use it when not needed. */
9535 if (*p != NUL || !vim_isAbsName(*fnamep))
9536 {
9537 *fnamep = FullName_save(*fnamep, *p != NUL);
9538 vim_free(*bufp); /* free any allocated file name */
9539 *bufp = *fnamep;
9540 if (*fnamep == NULL)
9541 return -1;
9542 }
9543
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009544#ifdef WIN3264
9545# if _WIN32_WINNT >= 0x0500
9546 if (vim_strchr(*fnamep, '~') != NULL)
9547 {
9548 /* Expand 8.3 filename to full path. Needed to make sure the same
9549 * file does not have two different names.
9550 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9551 p = alloc(_MAX_PATH + 1);
9552 if (p != NULL)
9553 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009554 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009555 {
9556 vim_free(*bufp);
9557 *bufp = *fnamep = p;
9558 }
9559 else
9560 vim_free(p);
9561 }
9562 }
9563# endif
9564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 /* Append a path separator to a directory. */
9566 if (mch_isdir(*fnamep))
9567 {
9568 /* Make room for one or two extra characters. */
9569 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9570 vim_free(*bufp); /* free any allocated file name */
9571 *bufp = *fnamep;
9572 if (*fnamep == NULL)
9573 return -1;
9574 add_pathsep(*fnamep);
9575 }
9576 }
9577
9578 /* ":." - path relative to the current directory */
9579 /* ":~" - path relative to the home directory */
9580 /* ":8" - shortname path - postponed till after */
9581 while (src[*usedlen] == ':'
9582 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9583 {
9584 *usedlen += 2;
9585 if (c == '8')
9586 {
9587#ifdef WIN3264
9588 has_shortname = 1; /* Postpone this. */
9589#endif
9590 continue;
9591 }
9592 pbuf = NULL;
9593 /* Need full path first (use expand_env() to remove a "~/") */
9594 if (!has_fullname)
9595 {
9596 if (c == '.' && **fnamep == '~')
9597 p = pbuf = expand_env_save(*fnamep);
9598 else
9599 p = pbuf = FullName_save(*fnamep, FALSE);
9600 }
9601 else
9602 p = *fnamep;
9603
9604 has_fullname = 0;
9605
9606 if (p != NULL)
9607 {
9608 if (c == '.')
9609 {
9610 mch_dirname(dirname, MAXPATHL);
9611 s = shorten_fname(p, dirname);
9612 if (s != NULL)
9613 {
9614 *fnamep = s;
9615 if (pbuf != NULL)
9616 {
9617 vim_free(*bufp); /* free any allocated file name */
9618 *bufp = pbuf;
9619 pbuf = NULL;
9620 }
9621 }
9622 }
9623 else
9624 {
9625 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9626 /* Only replace it when it starts with '~' */
9627 if (*dirname == '~')
9628 {
9629 s = vim_strsave(dirname);
9630 if (s != NULL)
9631 {
9632 *fnamep = s;
9633 vim_free(*bufp);
9634 *bufp = s;
9635 }
9636 }
9637 }
9638 vim_free(pbuf);
9639 }
9640 }
9641
9642 tail = gettail(*fnamep);
9643 *fnamelen = (int)STRLEN(*fnamep);
9644
9645 /* ":h" - head, remove "/file_name", can be repeated */
9646 /* Don't remove the first "/" or "c:\" */
9647 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9648 {
9649 valid |= VALID_HEAD;
9650 *usedlen += 2;
9651 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009652 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009653 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 *fnamelen = (int)(tail - *fnamep);
9655#ifdef VMS
9656 if (*fnamelen > 0)
9657 *fnamelen += 1; /* the path separator is part of the path */
9658#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009659 if (*fnamelen == 0)
9660 {
9661 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9662 p = vim_strsave((char_u *)".");
9663 if (p == NULL)
9664 return -1;
9665 vim_free(*bufp);
9666 *bufp = *fnamep = tail = p;
9667 *fnamelen = 1;
9668 }
9669 else
9670 {
9671 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009672 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 }
9675
9676 /* ":8" - shortname */
9677 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9678 {
9679 *usedlen += 2;
9680#ifdef WIN3264
9681 has_shortname = 1;
9682#endif
9683 }
9684
9685#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009686 /*
9687 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 */
9689 if (has_shortname)
9690 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009691 /* Copy the string if it is shortened by :h and when it wasn't copied
9692 * yet, because we are going to change it in place. Avoids changing
9693 * the buffer name for "%:8". */
9694 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 {
9696 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009697 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 return -1;
9699 vim_free(*bufp);
9700 *bufp = *fnamep = p;
9701 }
9702
9703 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009704 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (!has_fullname && !vim_isAbsName(*fnamep))
9706 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009707 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 return -1;
9709 }
9710 else
9711 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009712 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713
Bram Moolenaardc935552011-08-17 15:23:23 +02009714 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009716 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 return -1;
9718
9719 if (l == 0)
9720 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009721 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009723 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 return -1;
9725 }
9726 *fnamelen = l;
9727 }
9728 }
9729#endif /* WIN3264 */
9730
9731 /* ":t" - tail, just the basename */
9732 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9733 {
9734 *usedlen += 2;
9735 *fnamelen -= (int)(tail - *fnamep);
9736 *fnamep = tail;
9737 }
9738
9739 /* ":e" - extension, can be repeated */
9740 /* ":r" - root, without extension, can be repeated */
9741 while (src[*usedlen] == ':'
9742 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9743 {
9744 /* find a '.' in the tail:
9745 * - for second :e: before the current fname
9746 * - otherwise: The last '.'
9747 */
9748 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9749 s = *fnamep - 2;
9750 else
9751 s = *fnamep + *fnamelen - 1;
9752 for ( ; s > tail; --s)
9753 if (s[0] == '.')
9754 break;
9755 if (src[*usedlen + 1] == 'e') /* :e */
9756 {
9757 if (s > tail)
9758 {
9759 *fnamelen += (int)(*fnamep - (s + 1));
9760 *fnamep = s + 1;
9761#ifdef VMS
9762 /* cut version from the extension */
9763 s = *fnamep + *fnamelen - 1;
9764 for ( ; s > *fnamep; --s)
9765 if (s[0] == ';')
9766 break;
9767 if (s > *fnamep)
9768 *fnamelen = s - *fnamep;
9769#endif
9770 }
9771 else if (*fnamep <= tail)
9772 *fnamelen = 0;
9773 }
9774 else /* :r */
9775 {
9776 if (s > tail) /* remove one extension */
9777 *fnamelen = (int)(s - *fnamep);
9778 }
9779 *usedlen += 2;
9780 }
9781
9782 /* ":s?pat?foo?" - substitute */
9783 /* ":gs?pat?foo?" - global substitute */
9784 if (src[*usedlen] == ':'
9785 && (src[*usedlen + 1] == 's'
9786 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9787 {
9788 char_u *str;
9789 char_u *pat;
9790 char_u *sub;
9791 int sep;
9792 char_u *flags;
9793 int didit = FALSE;
9794
9795 flags = (char_u *)"";
9796 s = src + *usedlen + 2;
9797 if (src[*usedlen + 1] == 'g')
9798 {
9799 flags = (char_u *)"g";
9800 ++s;
9801 }
9802
9803 sep = *s++;
9804 if (sep)
9805 {
9806 /* find end of pattern */
9807 p = vim_strchr(s, sep);
9808 if (p != NULL)
9809 {
9810 pat = vim_strnsave(s, (int)(p - s));
9811 if (pat != NULL)
9812 {
9813 s = p + 1;
9814 /* find end of substitution */
9815 p = vim_strchr(s, sep);
9816 if (p != NULL)
9817 {
9818 sub = vim_strnsave(s, (int)(p - s));
9819 str = vim_strnsave(*fnamep, *fnamelen);
9820 if (sub != NULL && str != NULL)
9821 {
9822 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009823 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 if (s != NULL)
9825 {
9826 *fnamep = s;
9827 *fnamelen = (int)STRLEN(s);
9828 vim_free(*bufp);
9829 *bufp = s;
9830 didit = TRUE;
9831 }
9832 }
9833 vim_free(sub);
9834 vim_free(str);
9835 }
9836 vim_free(pat);
9837 }
9838 }
9839 /* after using ":s", repeat all the modifiers */
9840 if (didit)
9841 goto repeat;
9842 }
9843 }
9844
Bram Moolenaar26df0922014-02-23 23:39:13 +01009845 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9846 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009847 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009848 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009849 if (c != NUL)
9850 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009851 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009852 if (c != NUL)
9853 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009854 if (p == NULL)
9855 return -1;
9856 vim_free(*bufp);
9857 *bufp = *fnamep = p;
9858 *fnamelen = (int)STRLEN(p);
9859 *usedlen += 2;
9860 }
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 return valid;
9863}
9864
9865/*
9866 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009867 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 * "flags" can be "g" to do a global substitute.
9869 * Returns an allocated string, NULL for error.
9870 */
9871 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009872do_string_sub(
9873 char_u *str,
9874 char_u *pat,
9875 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009876 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
9879 int sublen;
9880 regmatch_T regmatch;
9881 int i;
9882 int do_all;
9883 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009884 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 garray_T ga;
9886 char_u *ret;
9887 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009888 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
9890 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9891 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009892 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
9894 ga_init2(&ga, 1, 200);
9895
9896 do_all = (flags[0] == 'g');
9897
9898 regmatch.rm_ic = p_ic;
9899 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9900 if (regmatch.regprog != NULL)
9901 {
9902 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009903 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9905 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009906 /* Skip empty match except for first match. */
9907 if (regmatch.startp[0] == regmatch.endp[0])
9908 {
9909 if (zero_width == regmatch.startp[0])
9910 {
9911 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009912 i = MB_PTR2LEN(tail);
9913 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9914 (size_t)i);
9915 ga.ga_len += i;
9916 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009917 continue;
9918 }
9919 zero_width = regmatch.startp[0];
9920 }
9921
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 /*
9923 * Get some space for a temporary buffer to do the substitution
9924 * into. It will contain:
9925 * - The text up to where the match is.
9926 * - The substituted text.
9927 * - The text after the match.
9928 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009929 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009930 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9932 {
9933 ga_clear(&ga);
9934 break;
9935 }
9936
9937 /* copy the text up to where the match is */
9938 i = (int)(regmatch.startp[0] - tail);
9939 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9940 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009941 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 + ga.ga_len + i, TRUE, TRUE, FALSE);
9943 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009944 tail = regmatch.endp[0];
9945 if (*tail == NUL)
9946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 if (!do_all)
9948 break;
9949 }
9950
9951 if (ga.ga_data != NULL)
9952 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9953
Bram Moolenaar473de612013-06-08 18:19:48 +02009954 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 }
9956
9957 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9958 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009959 if (p_cpo == empty_option)
9960 p_cpo = save_cpo;
9961 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009962 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009963 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
9965 return ret;
9966}
9967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009968 static int
9969filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9970{
9971 typval_T rettv;
9972 typval_T argv[3];
9973 char_u buf[NUMBUFLEN];
9974 char_u *s;
9975 int retval = FAIL;
9976 int dummy;
9977
9978 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9979 argv[0] = vimvars[VV_KEY].vv_tv;
9980 argv[1] = vimvars[VV_VAL].vv_tv;
9981 if (expr->v_type == VAR_FUNC)
9982 {
9983 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009984 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9985 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 goto theend;
9987 }
9988 else if (expr->v_type == VAR_PARTIAL)
9989 {
9990 partial_T *partial = expr->vval.v_partial;
9991
Bram Moolenaar437bafe2016-08-01 15:40:54 +02009992 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009993 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9994 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009995 goto theend;
9996 }
9997 else
9998 {
9999 s = get_tv_string_buf_chk(expr, buf);
10000 if (s == NULL)
10001 goto theend;
10002 s = skipwhite(s);
10003 if (eval1(&s, &rettv, TRUE) == FAIL)
10004 goto theend;
10005 if (*s != NUL) /* check for trailing chars after expr */
10006 {
10007 EMSG2(_(e_invexpr2), s);
10008 goto theend;
10009 }
10010 }
10011 if (map)
10012 {
10013 /* map(): replace the list item value */
10014 clear_tv(tv);
10015 rettv.v_lock = 0;
10016 *tv = rettv;
10017 }
10018 else
10019 {
10020 int error = FALSE;
10021
10022 /* filter(): when expr is zero remove the item */
10023 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10024 clear_tv(&rettv);
10025 /* On type error, nothing has been removed; return FAIL to stop the
10026 * loop. The error message was given by get_tv_number_chk(). */
10027 if (error)
10028 goto theend;
10029 }
10030 retval = OK;
10031theend:
10032 clear_tv(&vimvars[VV_VAL].vv_tv);
10033 return retval;
10034}
10035
10036
10037/*
10038 * Implementation of map() and filter().
10039 */
10040 void
10041filter_map(typval_T *argvars, typval_T *rettv, int map)
10042{
10043 typval_T *expr;
10044 listitem_T *li, *nli;
10045 list_T *l = NULL;
10046 dictitem_T *di;
10047 hashtab_T *ht;
10048 hashitem_T *hi;
10049 dict_T *d = NULL;
10050 typval_T save_val;
10051 typval_T save_key;
10052 int rem;
10053 int todo;
10054 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10055 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10056 : N_("filter() argument"));
10057 int save_did_emsg;
10058 int idx = 0;
10059
10060 if (argvars[0].v_type == VAR_LIST)
10061 {
10062 if ((l = argvars[0].vval.v_list) == NULL
10063 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10064 return;
10065 }
10066 else if (argvars[0].v_type == VAR_DICT)
10067 {
10068 if ((d = argvars[0].vval.v_dict) == NULL
10069 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10070 return;
10071 }
10072 else
10073 {
10074 EMSG2(_(e_listdictarg), ermsg);
10075 return;
10076 }
10077
10078 expr = &argvars[1];
10079 /* On type errors, the preceding call has already displayed an error
10080 * message. Avoid a misleading error message for an empty string that
10081 * was not passed as argument. */
10082 if (expr->v_type != VAR_UNKNOWN)
10083 {
10084 prepare_vimvar(VV_VAL, &save_val);
10085
10086 /* We reset "did_emsg" to be able to detect whether an error
10087 * occurred during evaluation of the expression. */
10088 save_did_emsg = did_emsg;
10089 did_emsg = FALSE;
10090
10091 prepare_vimvar(VV_KEY, &save_key);
10092 if (argvars[0].v_type == VAR_DICT)
10093 {
10094 vimvars[VV_KEY].vv_type = VAR_STRING;
10095
10096 ht = &d->dv_hashtab;
10097 hash_lock(ht);
10098 todo = (int)ht->ht_used;
10099 for (hi = ht->ht_array; todo > 0; ++hi)
10100 {
10101 if (!HASHITEM_EMPTY(hi))
10102 {
10103 int r;
10104
10105 --todo;
10106 di = HI2DI(hi);
10107 if (map &&
10108 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10109 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10110 break;
10111 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10112 r = filter_map_one(&di->di_tv, expr, map, &rem);
10113 clear_tv(&vimvars[VV_KEY].vv_tv);
10114 if (r == FAIL || did_emsg)
10115 break;
10116 if (!map && rem)
10117 {
10118 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10119 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10120 break;
10121 dictitem_remove(d, di);
10122 }
10123 }
10124 }
10125 hash_unlock(ht);
10126 }
10127 else
10128 {
10129 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10130
10131 for (li = l->lv_first; li != NULL; li = nli)
10132 {
10133 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10134 break;
10135 nli = li->li_next;
10136 vimvars[VV_KEY].vv_nr = idx;
10137 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10138 || did_emsg)
10139 break;
10140 if (!map && rem)
10141 listitem_remove(l, li);
10142 ++idx;
10143 }
10144 }
10145
10146 restore_vimvar(VV_KEY, &save_key);
10147 restore_vimvar(VV_VAL, &save_val);
10148
10149 did_emsg |= save_did_emsg;
10150 }
10151
10152 copy_tv(&argvars[0], rettv);
10153}
10154
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */