blob: 5821079a1054e38e30fe7c0af12e921e2c0ffd18 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar33570922005-01-25 22:26:29 +0000180};
181
182/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183#define vv_type vv_di.di_tv.v_type
184#define vv_nr vv_di.di_tv.vval.v_number
185#define vv_float vv_di.di_tv.vval.v_float
186#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000187#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200188#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000189#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000190
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200191static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000192#define vimvarht vimvardict.dv_hashtab
193
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100194static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
195static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
196static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100197static void list_glob_vars(int *first);
198static void list_buf_vars(int *first);
199static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000200#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100201static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000202#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100203static void list_vim_vars(int *first);
204static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100205static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
206static 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 +0100207static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
208static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100209static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
210static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
211static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
212static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int eval2(char_u **arg, typval_T *rettv, int evaluate);
215static int eval3(char_u **arg, typval_T *rettv, int evaluate);
216static int eval4(char_u **arg, typval_T *rettv, int evaluate);
217static int eval5(char_u **arg, typval_T *rettv, int evaluate);
218static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
219static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000220
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
223static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar33570922005-01-25 22:26:29 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100230static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static hashtab_T *find_var_ht(char_u *name, char_u **varname);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static void delete_var(hashtab_T *ht, hashitem_T *hi);
233static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
234static 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 +0100235static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200236
237#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int compare_func_name(const void *s1, const void *s2);
239static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200240#endif
241
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200242/* for VIM_VERSION_ defines */
243#include "version.h"
244
Bram Moolenaar33570922005-01-25 22:26:29 +0000245/*
246 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000247 */
248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100249eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000250{
Bram Moolenaar33570922005-01-25 22:26:29 +0000251 int i;
252 struct vimvar *p;
253
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200254 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
255 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200256 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000257 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200258 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000259
260 for (i = 0; i < VV_LEN; ++i)
261 {
262 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200263 if (STRLEN(p->vv_name) > 16)
264 {
265 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
266 getout(1);
267 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 STRCPY(p->vv_di.di_key, p->vv_name);
269 if (p->vv_flags & VV_RO)
270 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
271 else if (p->vv_flags & VV_RO_SBX)
272 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
273 else
274 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000275
276 /* add to v: scope dict, unless the value is not always available */
277 if (p->vv_type != VAR_UNKNOWN)
278 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 /* add to compat scope dict */
281 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000282 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100283 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
284
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000285 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100286 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200287 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100288 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100289
290 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
291 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
292 set_vim_var_nr(VV_NONE, VVAL_NONE);
293 set_vim_var_nr(VV_NULL, VVAL_NULL);
294
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200295 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200296
297#ifdef EBCDIC
298 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100299 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200300 */
301 sortFunctions();
302#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000303}
304
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000305#if defined(EXITFREE) || defined(PROTO)
306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100307eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000308{
309 int i;
310 struct vimvar *p;
311
312 for (i = 0; i < VV_LEN; ++i)
313 {
314 p = &vimvars[i];
315 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000316 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000317 vim_free(p->vv_str);
318 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000319 }
320 else if (p->vv_di.di_tv.v_type == VAR_LIST)
321 {
322 list_unref(p->vv_list);
323 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000324 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000325 }
326 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000327 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000328 hash_clear(&compat_hashtab);
329
Bram Moolenaard9fba312005-06-26 22:34:35 +0000330 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100331# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200332 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100333# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000334
335 /* global variables */
336 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000337
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000338 /* autoloaded script names */
339 ga_clear_strings(&ga_loaded);
340
Bram Moolenaarcca74132013-09-25 21:00:28 +0200341 /* Script-local variables. First clear all the variables and in a second
342 * loop free the scriptvar_T, because a variable in one script might hold
343 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200344 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200345 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200346 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200347 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200348 ga_clear(&ga_scripts);
349
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000350 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200351 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000352
353 /* functions */
354 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000355}
356#endif
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 * Set an internal variable to a string value. Creates the variable if it does
361 * not already exist.
362 */
363 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100364set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000366 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000367 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
369 val = vim_strsave(value);
370 if (val != NULL)
371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000372 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000373 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000375 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000376 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 }
378 }
379}
380
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000381static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200382#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000383static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000384static char_u *redir_endp = NULL;
385static char_u *redir_varname = NULL;
386
387/*
388 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100389 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000390 * Returns OK if successfully completed the setup. FAIL otherwise.
391 */
392 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100393var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000394{
395 int save_emsg;
396 int err;
397 typval_T tv;
398
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000399 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000400 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000401 {
402 EMSG(_(e_invarg));
403 return FAIL;
404 }
405
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000406 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000407 redir_varname = vim_strsave(name);
408 if (redir_varname == NULL)
409 return FAIL;
410
411 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
412 if (redir_lval == NULL)
413 {
414 var_redir_stop();
415 return FAIL;
416 }
417
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000418 /* The output is stored in growarray "redir_ga" until redirection ends. */
419 ga_init2(&redir_ga, (int)sizeof(char), 500);
420
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000421 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100422 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000423 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000424 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
425 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200426 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427 if (redir_endp != NULL && *redir_endp != NUL)
428 /* Trailing characters are present after the variable name */
429 EMSG(_(e_trailing));
430 else
431 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000432 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433 var_redir_stop();
434 return FAIL;
435 }
436
437 /* check if we can write to the variable: set it to or append an empty
438 * string */
439 save_emsg = did_emsg;
440 did_emsg = FALSE;
441 tv.v_type = VAR_STRING;
442 tv.vval.v_string = (char_u *)"";
443 if (append)
444 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
445 else
446 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200447 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000448 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000449 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 if (err)
451 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000452 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000453 var_redir_stop();
454 return FAIL;
455 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000456
457 return OK;
458}
459
460/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000461 * Append "value[value_len]" to the variable set by var_redir_start().
462 * The actual appending is postponed until redirection ends, because the value
463 * appended may in fact be the string we write to, changing it may cause freed
464 * memory to be used:
465 * :redir => foo
466 * :let foo
467 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000468 */
469 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100470var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000472 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473
474 if (redir_lval == NULL)
475 return;
476
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000477 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000478 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000480 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000481
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000482 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000483 {
484 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000485 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000486 }
487 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000488 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489}
490
491/*
492 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000493 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 */
495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100496var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000498 typval_T tv;
499
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200500 if (EVALCMD_BUSY)
501 {
502 redir_lval = NULL;
503 return;
504 }
505
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000506 if (redir_lval != NULL)
507 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000508 /* If there was no error: assign the text to the variable. */
509 if (redir_endp != NULL)
510 {
511 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
512 tv.v_type = VAR_STRING;
513 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200514 /* Call get_lval() again, if it's inside a Dict or List it may
515 * have changed. */
516 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100517 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200518 if (redir_endp != NULL && redir_lval->ll_name != NULL)
519 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
520 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000521 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000522
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000523 /* free the collected output */
524 vim_free(redir_ga.ga_data);
525 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527 vim_free(redir_lval);
528 redir_lval = NULL;
529 }
530 vim_free(redir_varname);
531 redir_varname = NULL;
532}
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534# if defined(FEAT_MBYTE) || defined(PROTO)
535 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100536eval_charconvert(
537 char_u *enc_from,
538 char_u *enc_to,
539 char_u *fname_from,
540 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 int err = FALSE;
543
544 set_vim_var_string(VV_CC_FROM, enc_from, -1);
545 set_vim_var_string(VV_CC_TO, enc_to, -1);
546 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
547 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
548 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
549 err = TRUE;
550 set_vim_var_string(VV_CC_FROM, NULL, -1);
551 set_vim_var_string(VV_CC_TO, NULL, -1);
552 set_vim_var_string(VV_FNAME_IN, NULL, -1);
553 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
554
555 if (err)
556 return FAIL;
557 return OK;
558}
559# endif
560
561# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100563eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 int err = FALSE;
566
567 set_vim_var_string(VV_FNAME_IN, fname, -1);
568 set_vim_var_string(VV_CMDARG, args, -1);
569 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
570 err = TRUE;
571 set_vim_var_string(VV_FNAME_IN, NULL, -1);
572 set_vim_var_string(VV_CMDARG, NULL, -1);
573
574 if (err)
575 {
576 mch_remove(fname);
577 return FAIL;
578 }
579 return OK;
580}
581# endif
582
583# if defined(FEAT_DIFF) || defined(PROTO)
584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100585eval_diff(
586 char_u *origfile,
587 char_u *newfile,
588 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 int err = FALSE;
591
592 set_vim_var_string(VV_FNAME_IN, origfile, -1);
593 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
594 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
595 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
596 set_vim_var_string(VV_FNAME_IN, NULL, -1);
597 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
598 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
599}
600
601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602eval_patch(
603 char_u *origfile,
604 char_u *difffile,
605 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 int err;
608
609 set_vim_var_string(VV_FNAME_IN, origfile, -1);
610 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
611 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
612 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
613 set_vim_var_string(VV_FNAME_IN, NULL, -1);
614 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
615 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
616}
617# endif
618
619/*
620 * Top level evaluation function, returning a boolean.
621 * Sets "error" to TRUE if there was an error.
622 * Return TRUE or FALSE.
623 */
624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100625eval_to_bool(
626 char_u *arg,
627 int *error,
628 char_u **nextcmd,
629 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
Bram Moolenaar33570922005-01-25 22:26:29 +0000631 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200632 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634 if (skip)
635 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000636 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 else
639 {
640 *error = FALSE;
641 if (!skip)
642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000644 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
646 }
647 if (skip)
648 --emsg_skip;
649
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200650 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651}
652
653/*
654 * Top level evaluation function, returning a string. If "skip" is TRUE,
655 * only parsing to "nextcmd" is done, without reporting errors. Return
656 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
657 */
658 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100659eval_to_string_skip(
660 char_u *arg,
661 char_u **nextcmd,
662 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
Bram Moolenaar33570922005-01-25 22:26:29 +0000664 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 char_u *retval;
666
667 if (skip)
668 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000669 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 retval = NULL;
671 else
672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000673 retval = vim_strsave(get_tv_string(&tv));
674 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
676 if (skip)
677 --emsg_skip;
678
679 return retval;
680}
681
682/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000683 * Skip over an expression at "*pp".
684 * Return FAIL for an error, OK otherwise.
685 */
686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000690
691 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000692 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000693}
694
695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000697 * When "convert" is TRUE convert a List into a sequence of lines and convert
698 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 * Return pointer to allocated memory, or NULL for failure.
700 */
701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100702eval_to_string(
703 char_u *arg,
704 char_u **nextcmd,
705 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
Bram Moolenaar33570922005-01-25 22:26:29 +0000707 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000709 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000710#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000711 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = NULL;
716 else
717 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000718 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000719 {
720 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000721 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200722 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200723 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200724 if (tv.vval.v_list->lv_len > 0)
725 ga_append(&ga, NL);
726 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000727 ga_append(&ga, NUL);
728 retval = (char_u *)ga.ga_data;
729 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000730#ifdef FEAT_FLOAT
731 else if (convert && tv.v_type == VAR_FLOAT)
732 {
733 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
734 retval = vim_strsave(numbuf);
735 }
736#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000737 else
738 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741
742 return retval;
743}
744
745/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000746 * Call eval_to_string() without using current local variables and using
747 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100750eval_to_string_safe(
751 char_u *arg,
752 char_u **nextcmd,
753 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 char_u *retval;
756 void *save_funccalp;
757
758 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000759 if (use_sandbox)
760 ++sandbox;
761 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000762 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000763 if (use_sandbox)
764 --sandbox;
765 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 restore_funccal(save_funccalp);
767 return retval;
768}
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770/*
771 * Top level evaluation function, returning a number.
772 * Evaluates "expr" silently.
773 * Returns -1 for an error.
774 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200775 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100776eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200779 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000780 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 ++emsg_off;
783
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 retval = -1;
786 else
787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000788 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 --emsg_off;
792
793 return retval;
794}
795
Bram Moolenaara40058a2005-07-11 22:42:07 +0000796/*
797 * Prepare v: variable "idx" to be used.
798 * Save the current typeval in "save_tv".
799 * When not used yet add the variable to the v: hashtable.
800 */
801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100802prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000803{
804 *save_tv = vimvars[idx].vv_tv;
805 if (vimvars[idx].vv_type == VAR_UNKNOWN)
806 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
807}
808
809/*
810 * Restore v: variable "idx" to typeval "save_tv".
811 * When no longer defined, remove the variable from the v: hashtable.
812 */
813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100814restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000815{
816 hashitem_T *hi;
817
Bram Moolenaara40058a2005-07-11 22:42:07 +0000818 vimvars[idx].vv_tv = *save_tv;
819 if (vimvars[idx].vv_type == VAR_UNKNOWN)
820 {
821 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
822 if (HASHITEM_EMPTY(hi))
823 EMSG2(_(e_intern2), "restore_vimvar()");
824 else
825 hash_remove(&vimvarht, hi);
826 }
827}
828
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000829#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000830/*
831 * Evaluate an expression to a list with suggestions.
832 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000833 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000834 */
835 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000837{
838 typval_T save_val;
839 typval_T rettv;
840 list_T *list = NULL;
841 char_u *p = skipwhite(expr);
842
843 /* Set "v:val" to the bad word. */
844 prepare_vimvar(VV_VAL, &save_val);
845 vimvars[VV_VAL].vv_type = VAR_STRING;
846 vimvars[VV_VAL].vv_str = badword;
847 if (p_verbose == 0)
848 ++emsg_off;
849
850 if (eval1(&p, &rettv, TRUE) == OK)
851 {
852 if (rettv.v_type != VAR_LIST)
853 clear_tv(&rettv);
854 else
855 list = rettv.vval.v_list;
856 }
857
858 if (p_verbose == 0)
859 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000860 restore_vimvar(VV_VAL, &save_val);
861
862 return list;
863}
864
865/*
866 * "list" is supposed to contain two items: a word and a number. Return the
867 * word in "pp" and the number as the return value.
868 * Return -1 if anything isn't right.
869 * Used to get the good word and score from the eval_spell_expr() result.
870 */
871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100872get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000873{
874 listitem_T *li;
875
876 li = list->lv_first;
877 if (li == NULL)
878 return -1;
879 *pp = get_tv_string(&li->li_tv);
880
881 li = li->li_next;
882 if (li == NULL)
883 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200884 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000885}
886#endif
887
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000888/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000889 * Top level evaluation function.
890 * Returns an allocated typval_T with the result.
891 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000892 */
893 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100894eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000895{
896 typval_T *tv;
897
898 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000899 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000900 {
901 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000902 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000903 }
904
905 return tv;
906}
907
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000910 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000911 * Uses argv[argc] for the function arguments. Only Number and String
912 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000913 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200915 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100916call_vim_function(
917 char_u *func,
918 int argc,
919 char_u **argv,
920 int safe, /* use the sandbox */
921 int str_arg_only, /* all arguments are strings */
922 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
Bram Moolenaar33570922005-01-25 22:26:29 +0000924 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200925 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 int len;
927 int i;
928 int doesrange;
929 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000930 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000932 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936 for (i = 0; i < argc; i++)
937 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000938 /* Pass a NULL or empty argument as an empty string */
939 if (argv[i] == NULL || *argv[i] == NUL)
940 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000941 argvars[i].v_type = VAR_STRING;
942 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000943 continue;
944 }
945
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200946 if (str_arg_only)
947 len = 0;
948 else
949 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100950 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (len != 0 && len == (int)STRLEN(argv[i]))
952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000953 argvars[i].v_type = VAR_NUMBER;
954 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 else
957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000958 argvars[i].v_type = VAR_STRING;
959 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961 }
962
963 if (safe)
964 {
965 save_funccalp = save_funccal();
966 ++sandbox;
967 }
968
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000969 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
970 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100972 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 if (safe)
974 {
975 --sandbox;
976 restore_funccal(save_funccalp);
977 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000978 vim_free(argvars);
979
980 if (ret == FAIL)
981 clear_tv(rettv);
982
983 return ret;
984}
985
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100986/*
987 * Call vimL function "func" and return the result as a number.
988 * Returns -1 when calling the function fails.
989 * Uses argv[argc] for the function arguments.
990 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200991 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100992call_func_retnr(
993 char_u *func,
994 int argc,
995 char_u **argv,
996 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100997{
998 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200999 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001000
1001 /* All arguments are passed as strings, no conversion to number. */
1002 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1003 return -1;
1004
1005 retval = get_tv_number_chk(&rettv, NULL);
1006 clear_tv(&rettv);
1007 return retval;
1008}
1009
1010#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1011 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1012
Bram Moolenaar4f688582007-07-24 12:34:30 +00001013# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001014/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001015 * Call vimL function "func" and return the result as a string.
1016 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001017 * Uses argv[argc] for the function arguments.
1018 */
1019 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001020call_func_retstr(
1021 char_u *func,
1022 int argc,
1023 char_u **argv,
1024 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001025{
1026 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001027 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001028
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001029 /* All arguments are passed as strings, no conversion to number. */
1030 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001031 return NULL;
1032
1033 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001034 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 return retval;
1036}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001037# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001038
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001039/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001040 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001041 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001042 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001043 */
1044 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001045call_func_retlist(
1046 char_u *func,
1047 int argc,
1048 char_u **argv,
1049 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001050{
1051 typval_T rettv;
1052
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001053 /* All arguments are passed as strings, no conversion to number. */
1054 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001055 return NULL;
1056
1057 if (rettv.v_type != VAR_LIST)
1058 {
1059 clear_tv(&rettv);
1060 return NULL;
1061 }
1062
1063 return rettv.vval.v_list;
1064}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#endif
1066
Bram Moolenaar05159a02005-02-26 23:04:13 +00001067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#ifdef FEAT_FOLDING
1069/*
1070 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1071 * it in "*cp". Doesn't give error messages.
1072 */
1073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001077 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001079 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1080 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
1082 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001083 if (use_sandbox)
1084 ++sandbox;
1085 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 retval = 0;
1089 else
1090 {
1091 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 if (tv.v_type == VAR_NUMBER)
1093 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001094 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 retval = 0;
1096 else
1097 {
1098 /* If the result is a string, check if there is a non-digit before
1099 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (!VIM_ISDIGIT(*s) && *s != '-')
1102 *cp = *s++;
1103 retval = atol((char *)s);
1104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001108 if (use_sandbox)
1109 --sandbox;
1110 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001112 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113}
1114#endif
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 * ":let" list all variable values
1118 * ":let var1 var2" list variable values
1119 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001120 * ":let var += expr" assignment command.
1121 * ":let var -= expr" assignment command.
1122 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 */
1125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001126ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 int var_count = 0;
1133 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001134 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001135 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001136 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
Bram Moolenaardb552d602006-03-23 22:59:57 +00001138 argend = skip_var_list(arg, &var_count, &semicolon);
1139 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001140 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001141 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1142 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001143 expr = skipwhite(argend);
1144 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1145 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001147 /*
1148 * ":let" without "=": list variables
1149 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001150 if (*arg == '[')
1151 EMSG(_(e_invarg));
1152 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001154 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001155 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001158 list_glob_vars(&first);
1159 list_buf_vars(&first);
1160 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001161#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001162 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001163#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001164 list_script_vars(&first);
1165 list_func_vars(&first);
1166 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 eap->nextcmd = check_nextcmd(arg);
1169 }
1170 else
1171 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001172 op[0] = '=';
1173 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001174 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001175 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001176 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1177 op[0] = *expr; /* +=, -= or .= */
1178 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001180 else
1181 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (eap->skip)
1184 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001185 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (eap->skip)
1187 {
1188 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001189 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 --emsg_skip;
1191 }
1192 else if (i != FAIL)
1193 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001194 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001195 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198 }
1199}
1200
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001201/*
1202 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1203 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001204 * When "nextchars" is not NULL it points to a string with characters that
1205 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1206 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001207 * Returns OK or FAIL;
1208 */
1209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001210ex_let_vars(
1211 char_u *arg_start,
1212 typval_T *tv,
1213 int copy, /* copy values from "tv", don't move */
1214 int semicolon, /* from skip_var_list() */
1215 int var_count, /* from skip_var_list() */
1216 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001217{
1218 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001219 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001220 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001221 listitem_T *item;
1222 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001223
1224 if (*arg != '[')
1225 {
1226 /*
1227 * ":let var = expr" or ":for var in list"
1228 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001229 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001230 return FAIL;
1231 return OK;
1232 }
1233
1234 /*
1235 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1236 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001237 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001238 {
1239 EMSG(_(e_listreq));
1240 return FAIL;
1241 }
1242
1243 i = list_len(l);
1244 if (semicolon == 0 && var_count < i)
1245 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001246 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001247 return FAIL;
1248 }
1249 if (var_count - semicolon > i)
1250 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001251 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001252 return FAIL;
1253 }
1254
1255 item = l->lv_first;
1256 while (*arg != ']')
1257 {
1258 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001259 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260 item = item->li_next;
1261 if (arg == NULL)
1262 return FAIL;
1263
1264 arg = skipwhite(arg);
1265 if (*arg == ';')
1266 {
1267 /* Put the rest of the list (may be empty) in the var after ';'.
1268 * Create a new list for this. */
1269 l = list_alloc();
1270 if (l == NULL)
1271 return FAIL;
1272 while (item != NULL)
1273 {
1274 list_append_tv(l, &item->li_tv);
1275 item = item->li_next;
1276 }
1277
1278 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001279 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001280 ltv.vval.v_list = l;
1281 l->lv_refcount = 1;
1282
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1284 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001285 clear_tv(&ltv);
1286 if (arg == NULL)
1287 return FAIL;
1288 break;
1289 }
1290 else if (*arg != ',' && *arg != ']')
1291 {
1292 EMSG2(_(e_intern2), "ex_let_vars()");
1293 return FAIL;
1294 }
1295 }
1296
1297 return OK;
1298}
1299
1300/*
1301 * Skip over assignable variable "var" or list of variables "[var, var]".
1302 * Used for ":let varvar = expr" and ":for varvar in expr".
1303 * For "[var, var]" increment "*var_count" for each variable.
1304 * for "[var, var; var]" set "semicolon".
1305 * Return NULL for an error.
1306 */
1307 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308skip_var_list(
1309 char_u *arg,
1310 int *var_count,
1311 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001312{
1313 char_u *p, *s;
1314
1315 if (*arg == '[')
1316 {
1317 /* "[var, var]": find the matching ']'. */
1318 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001319 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001320 {
1321 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1322 s = skip_var_one(p);
1323 if (s == p)
1324 {
1325 EMSG2(_(e_invarg2), p);
1326 return NULL;
1327 }
1328 ++*var_count;
1329
1330 p = skipwhite(s);
1331 if (*p == ']')
1332 break;
1333 else if (*p == ';')
1334 {
1335 if (*semicolon == 1)
1336 {
1337 EMSG(_("Double ; in list of variables"));
1338 return NULL;
1339 }
1340 *semicolon = 1;
1341 }
1342 else if (*p != ',')
1343 {
1344 EMSG2(_(e_invarg2), p);
1345 return NULL;
1346 }
1347 }
1348 return p + 1;
1349 }
1350 else
1351 return skip_var_one(arg);
1352}
1353
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001354/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001355 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001356 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001357 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001358 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001359skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001360{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001361 if (*arg == '@' && arg[1] != NUL)
1362 return arg + 2;
1363 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1364 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001365}
1366
Bram Moolenaara7043832005-01-21 11:56:39 +00001367/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 * List variables for hashtab "ht" with prefix "prefix".
1369 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001370 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372list_hashtable_vars(
1373 hashtab_T *ht,
1374 char_u *prefix,
1375 int empty,
1376 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001377{
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 hashitem_T *hi;
1379 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001380 int todo;
1381
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001382 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001383 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1384 {
1385 if (!HASHITEM_EMPTY(hi))
1386 {
1387 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001388 di = HI2DI(hi);
1389 if (empty || di->di_tv.v_type != VAR_STRING
1390 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001391 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001392 }
1393 }
1394}
1395
1396/*
1397 * List global variables.
1398 */
1399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001400list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001401{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001402 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001403}
1404
1405/*
1406 * List buffer variables.
1407 */
1408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001410{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001411 char_u numbuf[NUMBUFLEN];
1412
Bram Moolenaar429fa852013-04-15 12:27:36 +02001413 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001414 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001415
1416 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001417 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1418 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001419}
1420
1421/*
1422 * List window variables.
1423 */
1424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001425list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001426{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001427 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001428 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001429}
1430
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001431#ifdef FEAT_WINDOWS
1432/*
1433 * List tab page variables.
1434 */
1435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001436list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001437{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001438 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001439 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001440}
1441#endif
1442
Bram Moolenaara7043832005-01-21 11:56:39 +00001443/*
1444 * List Vim variables.
1445 */
1446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001448{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001449 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450}
1451
1452/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001453 * List script-local variables, if there is a script.
1454 */
1455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001457{
1458 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001459 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1460 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001461}
1462
1463/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 * List variables in "arg".
1465 */
1466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001467list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468{
1469 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001470 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001471 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001472 char_u *name_start;
1473 char_u *arg_subsc;
1474 char_u *tofree;
1475 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001476
1477 while (!ends_excmd(*arg) && !got_int)
1478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001479 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001481 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001482 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1483 {
1484 emsg_severe = TRUE;
1485 EMSG(_(e_trailing));
1486 break;
1487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001491 /* get_name_len() takes care of expanding curly braces */
1492 name_start = name = arg;
1493 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1494 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001496 /* This is mainly to keep test 49 working: when expanding
1497 * curly braces fails overrule the exception error message. */
1498 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001500 emsg_severe = TRUE;
1501 EMSG2(_(e_invarg2), arg);
1502 break;
1503 }
1504 error = TRUE;
1505 }
1506 else
1507 {
1508 if (tofree != NULL)
1509 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001510 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 else
1513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001514 /* handle d.key, l[idx], f(expr) */
1515 arg_subsc = arg;
1516 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001517 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001519 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001520 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001521 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001522 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001523 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001524 case 'g': list_glob_vars(first); break;
1525 case 'b': list_buf_vars(first); break;
1526 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001527#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001528 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001529#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001530 case 'v': list_vim_vars(first); break;
1531 case 's': list_script_vars(first); break;
1532 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001533 default:
1534 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001535 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001536 }
1537 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001538 {
1539 char_u numbuf[NUMBUFLEN];
1540 char_u *tf;
1541 int c;
1542 char_u *s;
1543
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001544 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001545 c = *arg;
1546 *arg = NUL;
1547 list_one_var_a((char_u *)"",
1548 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001549 tv.v_type,
1550 s == NULL ? (char_u *)"" : s,
1551 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 *arg = c;
1553 vim_free(tf);
1554 }
1555 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 }
1558 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559
1560 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001561 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562
1563 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 }
1565
1566 return arg;
1567}
1568
1569/*
1570 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1571 * Returns a pointer to the char just after the var name.
1572 * Returns NULL if there is an error.
1573 */
1574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001575ex_let_one(
1576 char_u *arg, /* points to variable name */
1577 typval_T *tv, /* value to assign to variable */
1578 int copy, /* copy value from "tv" */
1579 char_u *endchars, /* valid chars after variable name or NULL */
1580 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581{
1582 int c1;
1583 char_u *name;
1584 char_u *p;
1585 char_u *arg_end = NULL;
1586 int len;
1587 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589
1590 /*
1591 * ":let $VAR = expr": Set environment variable.
1592 */
1593 if (*arg == '$')
1594 {
1595 /* Find the end of the name. */
1596 ++arg;
1597 name = arg;
1598 len = get_env_len(&arg);
1599 if (len == 0)
1600 EMSG2(_(e_invarg2), name - 1);
1601 else
1602 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 if (op != NULL && (*op == '+' || *op == '-'))
1604 EMSG2(_(e_letwrong), op);
1605 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001608 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 {
1610 c1 = name[len];
1611 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001612 p = get_tv_string_chk(tv);
1613 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001614 {
1615 int mustfree = FALSE;
1616 char_u *s = vim_getenv(name, &mustfree);
1617
1618 if (s != NULL)
1619 {
1620 p = tofree = concat_str(s, p);
1621 if (mustfree)
1622 vim_free(s);
1623 }
1624 }
1625 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001626 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001628 if (STRICMP(name, "HOME") == 0)
1629 init_homedir();
1630 else if (didset_vim && STRICMP(name, "VIM") == 0)
1631 didset_vim = FALSE;
1632 else if (didset_vimruntime
1633 && STRICMP(name, "VIMRUNTIME") == 0)
1634 didset_vimruntime = FALSE;
1635 arg_end = arg;
1636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 }
1640 }
1641 }
1642
1643 /*
1644 * ":let &option = expr": Set option value.
1645 * ":let &l:option = expr": Set local option value.
1646 * ":let &g:option = expr": Set global option value.
1647 */
1648 else if (*arg == '&')
1649 {
1650 /* Find the end of the name. */
1651 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 if (p == NULL || (endchars != NULL
1653 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 EMSG(_(e_letunexp));
1655 else
1656 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 long n;
1658 int opt_type;
1659 long numval;
1660 char_u *stringval = NULL;
1661 char_u *s;
1662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 c1 = *p;
1664 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001666 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001667 s = get_tv_string_chk(tv); /* != NULL if number or string */
1668 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001669 {
1670 opt_type = get_option_value(arg, &numval,
1671 &stringval, opt_flags);
1672 if ((opt_type == 1 && *op == '.')
1673 || (opt_type == 0 && *op != '.'))
1674 EMSG2(_(e_letwrong), op);
1675 else
1676 {
1677 if (opt_type == 1) /* number */
1678 {
1679 if (*op == '+')
1680 n = numval + n;
1681 else
1682 n = numval - n;
1683 }
1684 else if (opt_type == 0 && stringval != NULL) /* string */
1685 {
1686 s = concat_str(stringval, s);
1687 vim_free(stringval);
1688 stringval = s;
1689 }
1690 }
1691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001692 if (s != NULL)
1693 {
1694 set_option_value(arg, n, s, opt_flags);
1695 arg_end = p;
1696 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 }
1700 }
1701
1702 /*
1703 * ":let @r = expr": Set register contents.
1704 */
1705 else if (*arg == '@')
1706 {
1707 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 if (op != NULL && (*op == '+' || *op == '-'))
1709 EMSG2(_(e_letwrong), op);
1710 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 EMSG(_(e_letunexp));
1713 else
1714 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 char_u *s;
1717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718 p = get_tv_string_chk(tv);
1719 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001721 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001722 if (s != NULL)
1723 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001724 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 vim_free(s);
1726 }
1727 }
1728 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731 arg_end = arg + 1;
1732 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001733 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 }
1735 }
1736
1737 /*
1738 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001739 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001741 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001743 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001745 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001746 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001748 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1749 EMSG(_(e_letunexp));
1750 else
1751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001753 arg_end = p;
1754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001756 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 }
1758
1759 else
1760 EMSG2(_(e_invarg2), arg);
1761
1762 return arg_end;
1763}
1764
1765/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001766 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1767 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001769check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001770{
1771 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1772 {
1773 EMSG2(_(e_readonlyvar), arg);
1774 return TRUE;
1775 }
1776 return FALSE;
1777}
1778
1779/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001780 * Get an lval: variable, Dict item or List item that can be assigned a value
1781 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1782 * "name.key", "name.key[expr]" etc.
1783 * Indexing only works if "name" is an existing List or Dictionary.
1784 * "name" points to the start of the name.
1785 * If "rettv" is not NULL it points to the value to be assigned.
1786 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1787 * wrong; must end in space or cmd separator.
1788 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001789 * flags:
1790 * GLV_QUIET: do not give error messages
1791 * GLV_NO_AUTOLOAD: do not use script autoloading
1792 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001794 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001795 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798get_lval(
1799 char_u *name,
1800 typval_T *rettv,
1801 lval_T *lp,
1802 int unlet,
1803 int skip,
1804 int flags, /* GLV_ values */
1805 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001808 char_u *expr_start, *expr_end;
1809 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001810 dictitem_T *v;
1811 typval_T var1;
1812 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001813 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001815 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001816 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001818 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001820 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001821 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001822
1823 if (skip)
1824 {
1825 /* When skipping just find the end of the name. */
1826 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001827 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001828 }
1829
1830 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001831 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001832 if (expr_start != NULL)
1833 {
1834 /* Don't expand the name when we already know there is an error. */
1835 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1836 && *p != '[' && *p != '.')
1837 {
1838 EMSG(_(e_trailing));
1839 return NULL;
1840 }
1841
1842 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1843 if (lp->ll_exp_name == NULL)
1844 {
1845 /* Report an invalid expression in braces, unless the
1846 * expression evaluation has been cancelled due to an
1847 * aborting error, an interrupt, or an exception. */
1848 if (!aborting() && !quiet)
1849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001850 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001851 EMSG2(_(e_invarg2), name);
1852 return NULL;
1853 }
1854 }
1855 lp->ll_name = lp->ll_exp_name;
1856 }
1857 else
1858 lp->ll_name = name;
1859
1860 /* Without [idx] or .key we are done. */
1861 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1862 return p;
1863
1864 cc = *p;
1865 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001866 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 if (v == NULL && !quiet)
1868 EMSG2(_(e_undefvar), lp->ll_name);
1869 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 if (v == NULL)
1871 return NULL;
1872
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001873 /*
1874 * Loop until no more [idx] or .key is following.
1875 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001876 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001877 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001879 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1880 && !(lp->ll_tv->v_type == VAR_DICT
1881 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001883 if (!quiet)
1884 EMSG(_("E689: Can only index a List or Dictionary"));
1885 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001887 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001889 if (!quiet)
1890 EMSG(_("E708: [:] must come last"));
1891 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001893
Bram Moolenaar8c711452005-01-14 21:53:12 +00001894 len = -1;
1895 if (*p == '.')
1896 {
1897 key = p + 1;
1898 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1899 ;
1900 if (len == 0)
1901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 if (!quiet)
1903 EMSG(_(e_emptykey));
1904 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001905 }
1906 p = key + len;
1907 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001908 else
1909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001910 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001911 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001912 if (*p == ':')
1913 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001914 else
1915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001916 empty1 = FALSE;
1917 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001919 if (get_tv_string_chk(&var1) == NULL)
1920 {
1921 /* not a number or string */
1922 clear_tv(&var1);
1923 return NULL;
1924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001925 }
1926
1927 /* Optionally get the second index [ :expr]. */
1928 if (*p == ':')
1929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001934 if (!empty1)
1935 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001937 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 if (rettv != NULL && (rettv->v_type != VAR_LIST
1939 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 if (!quiet)
1942 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001943 if (!empty1)
1944 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 }
1947 p = skipwhite(p + 1);
1948 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001950 else
1951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001953 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001955 if (!empty1)
1956 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001958 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959 if (get_tv_string_chk(&var2) == NULL)
1960 {
1961 /* not a number or string */
1962 if (!empty1)
1963 clear_tv(&var1);
1964 clear_tv(&var2);
1965 return NULL;
1966 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001972
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001974 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 if (!quiet)
1976 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 if (!empty1)
1978 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001981 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001982 }
1983
1984 /* Skip to past ']'. */
1985 ++p;
1986 }
1987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001988 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 {
1990 if (len == -1)
1991 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001993 key = get_tv_string_chk(&var1); /* is number or string */
1994 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 }
1999 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 lp->ll_list = NULL;
2001 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002002 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002003
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002004 /* When assigning to a scope dictionary check that a function and
2005 * variable name is valid (only variable name unless it is l: or
2006 * g: dictionary). Disallow overwriting a builtin function. */
2007 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002008 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002009 int prevval;
2010 int wrong;
2011
2012 if (len != -1)
2013 {
2014 prevval = key[len];
2015 key[len] = NUL;
2016 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002017 else
2018 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002019 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2020 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002021 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002022 || !valid_varname(key);
2023 if (len != -1)
2024 key[len] = prevval;
2025 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002026 return NULL;
2027 }
2028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002031 /* Can't add "v:" variable. */
2032 if (lp->ll_dict == &vimvardict)
2033 {
2034 EMSG2(_(e_illvar), name);
2035 return NULL;
2036 }
2037
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002038 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002039 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002043 if (len == -1)
2044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 }
2047 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 if (len == -1)
2052 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 p = NULL;
2055 break;
2056 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002057 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002058 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002059 return NULL;
2060
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 if (len == -1)
2062 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 }
2065 else
2066 {
2067 /*
2068 * Get the number and item for the only or first index of the List.
2069 */
2070 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 else
2073 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002074 lp->ll_n1 = (long)get_tv_number(&var1);
2075 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002076 clear_tv(&var1);
2077 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002078 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002079 lp->ll_list = lp->ll_tv->vval.v_list;
2080 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2081 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002083 if (lp->ll_n1 < 0)
2084 {
2085 lp->ll_n1 = 0;
2086 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2087 }
2088 }
2089 if (lp->ll_li == NULL)
2090 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002093 if (!quiet)
2094 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002096 }
2097
2098 /*
2099 * May need to find the item or absolute index for the second
2100 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002101 * When no index given: "lp->ll_empty2" is TRUE.
2102 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002104 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002105 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002106 lp->ll_n2 = (long)get_tv_number(&var2);
2107 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002112 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002113 {
2114 if (!quiet)
2115 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002117 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 }
2120
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2122 if (lp->ll_n1 < 0)
2123 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2124 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002125 {
2126 if (!quiet)
2127 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002129 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002130 }
2131
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 }
2135
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 return p;
2137}
2138
2139/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002143clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144{
2145 vim_free(lp->ll_exp_name);
2146 vim_free(lp->ll_newkey);
2147}
2148
2149/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002150 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 */
2154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155set_var_lval(
2156 lval_T *lp,
2157 char_u *endp,
2158 typval_T *rettv,
2159 int copy,
2160 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161{
2162 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002163 listitem_T *ri;
2164 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165
2166 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002169 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 cc = *endp;
2171 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 if (op != NULL && *op != '=')
2173 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002176 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002177 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002178 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002179 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002180 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002181 if ((di == NULL
2182 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2183 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2184 FALSE)))
2185 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 set_var(lp->ll_name, &tv, FALSE);
2187 clear_tv(&tv);
2188 }
2189 }
2190 else
2191 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002195 else if (tv_check_lock(lp->ll_newkey == NULL
2196 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002197 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002198 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 else if (lp->ll_range)
2200 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002201 listitem_T *ll_li = lp->ll_li;
2202 int ll_n1 = lp->ll_n1;
2203
2204 /*
2205 * Check whether any of the list items is locked
2206 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002207 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002208 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002209 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002210 return;
2211 ri = ri->li_next;
2212 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2213 break;
2214 ll_li = ll_li->li_next;
2215 ++ll_n1;
2216 }
2217
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 /*
2219 * Assign the List values to the list items.
2220 */
2221 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002222 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 if (op != NULL && *op != '=')
2224 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2225 else
2226 {
2227 clear_tv(&lp->ll_li->li_tv);
2228 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2229 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 ri = ri->li_next;
2231 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2232 break;
2233 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002236 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002237 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 ri = NULL;
2239 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002240 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002241 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 lp->ll_li = lp->ll_li->li_next;
2243 ++lp->ll_n1;
2244 }
2245 if (ri != NULL)
2246 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 else if (lp->ll_empty2
2248 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 : lp->ll_n1 != lp->ll_n2)
2250 EMSG(_("E711: List value has not enough items"));
2251 }
2252 else
2253 {
2254 /*
2255 * Assign to a List or Dictionary item.
2256 */
2257 if (lp->ll_newkey != NULL)
2258 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002259 if (op != NULL && *op != '=')
2260 {
2261 EMSG2(_(e_letwrong), op);
2262 return;
2263 }
2264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002266 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 if (di == NULL)
2268 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002269 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2270 {
2271 vim_free(di);
2272 return;
2273 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002275 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 else if (op != NULL && *op != '=')
2277 {
2278 tv_op(lp->ll_tv, rettv, op);
2279 return;
2280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 /*
2285 * Assign the value to the variable or list item.
2286 */
2287 if (copy)
2288 copy_tv(rettv, lp->ll_tv);
2289 else
2290 {
2291 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002292 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
2295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296}
2297
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002298/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2300 * Returns OK or FAIL.
2301 */
2302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002303tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002305 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u numbuf[NUMBUFLEN];
2307 char_u *s;
2308
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002309 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2310 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2311 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 {
2313 switch (tv1->v_type)
2314 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002315 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 case VAR_DICT:
2317 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002318 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002319 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002320 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002321 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 break;
2323
2324 case VAR_LIST:
2325 if (*op != '+' || tv2->v_type != VAR_LIST)
2326 break;
2327 /* List += List */
2328 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2329 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2330 return OK;
2331
2332 case VAR_NUMBER:
2333 case VAR_STRING:
2334 if (tv2->v_type == VAR_LIST)
2335 break;
2336 if (*op == '+' || *op == '-')
2337 {
2338 /* nr += nr or nr -= nr*/
2339 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002340#ifdef FEAT_FLOAT
2341 if (tv2->v_type == VAR_FLOAT)
2342 {
2343 float_T f = n;
2344
2345 if (*op == '+')
2346 f += tv2->vval.v_float;
2347 else
2348 f -= tv2->vval.v_float;
2349 clear_tv(tv1);
2350 tv1->v_type = VAR_FLOAT;
2351 tv1->vval.v_float = f;
2352 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002354#endif
2355 {
2356 if (*op == '+')
2357 n += get_tv_number(tv2);
2358 else
2359 n -= get_tv_number(tv2);
2360 clear_tv(tv1);
2361 tv1->v_type = VAR_NUMBER;
2362 tv1->vval.v_number = n;
2363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 }
2365 else
2366 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002367 if (tv2->v_type == VAR_FLOAT)
2368 break;
2369
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 /* str .= str */
2371 s = get_tv_string(tv1);
2372 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2373 clear_tv(tv1);
2374 tv1->v_type = VAR_STRING;
2375 tv1->vval.v_string = s;
2376 }
2377 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002379 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002380#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002381 {
2382 float_T f;
2383
2384 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2385 && tv2->v_type != VAR_NUMBER
2386 && tv2->v_type != VAR_STRING))
2387 break;
2388 if (tv2->v_type == VAR_FLOAT)
2389 f = tv2->vval.v_float;
2390 else
2391 f = get_tv_number(tv2);
2392 if (*op == '+')
2393 tv1->vval.v_float += f;
2394 else
2395 tv1->vval.v_float -= f;
2396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002397#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002398 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 }
2400 }
2401
2402 EMSG2(_(e_letwrong), op);
2403 return FAIL;
2404}
2405
2406/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002407 * Evaluate the expression used in a ":for var in expr" command.
2408 * "arg" points to "var".
2409 * Set "*errp" to TRUE for an error, FALSE otherwise;
2410 * Return a pointer that holds the info. Null when there is an error.
2411 */
2412 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002413eval_for_line(
2414 char_u *arg,
2415 int *errp,
2416 char_u **nextcmdp,
2417 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418{
Bram Moolenaar33570922005-01-25 22:26:29 +00002419 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002420 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002421 typval_T tv;
2422 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002423
2424 *errp = TRUE; /* default: there is an error */
2425
Bram Moolenaar33570922005-01-25 22:26:29 +00002426 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 if (fi == NULL)
2428 return NULL;
2429
2430 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2431 if (expr == NULL)
2432 return fi;
2433
2434 expr = skipwhite(expr);
2435 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2436 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002437 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 return fi;
2439 }
2440
2441 if (skip)
2442 ++emsg_skip;
2443 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2444 {
2445 *errp = FALSE;
2446 if (!skip)
2447 {
2448 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002449 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002450 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002452 clear_tv(&tv);
2453 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002454 else if (l == NULL)
2455 {
2456 /* a null list is like an empty list: do nothing */
2457 clear_tv(&tv);
2458 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459 else
2460 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002461 /* No need to increment the refcount, it's already set for the
2462 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002463 fi->fi_list = l;
2464 list_add_watch(l, &fi->fi_lw);
2465 fi->fi_lw.lw_item = l->lv_first;
2466 }
2467 }
2468 }
2469 if (skip)
2470 --emsg_skip;
2471
2472 return fi;
2473}
2474
2475/*
2476 * Use the first item in a ":for" list. Advance to the next.
2477 * Assign the values to the variable (list). "arg" points to the first one.
2478 * Return TRUE when a valid item was found, FALSE when at end of list or
2479 * something wrong.
2480 */
2481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002484 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487
2488 item = fi->fi_lw.lw_item;
2489 if (item == NULL)
2490 result = FALSE;
2491 else
2492 {
2493 fi->fi_lw.lw_item = item->li_next;
2494 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2495 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2496 }
2497 return result;
2498}
2499
2500/*
2501 * Free the structure used to store info used by ":for".
2502 */
2503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002504free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002505{
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002508 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002509 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002511 list_unref(fi->fi_list);
2512 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002513 vim_free(fi);
2514}
2515
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2517
2518 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002519set_context_for_expression(
2520 expand_T *xp,
2521 char_u *arg,
2522 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 int got_eq = FALSE;
2525 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528 if (cmdidx == CMD_let)
2529 {
2530 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002531 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002532 {
2533 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002534 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002535 {
2536 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002538 if (vim_iswhite(*p))
2539 break;
2540 }
2541 return;
2542 }
2543 }
2544 else
2545 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2546 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 while ((xp->xp_pattern = vim_strpbrk(arg,
2548 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2549 {
2550 c = *xp->xp_pattern;
2551 if (c == '&')
2552 {
2553 c = xp->xp_pattern[1];
2554 if (c == '&')
2555 {
2556 ++xp->xp_pattern;
2557 xp->xp_context = cmdidx != CMD_let || got_eq
2558 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2559 }
2560 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002563 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2564 xp->xp_pattern += 2;
2565
2566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 else if (c == '$')
2569 {
2570 /* environment variable */
2571 xp->xp_context = EXPAND_ENV_VARS;
2572 }
2573 else if (c == '=')
2574 {
2575 got_eq = TRUE;
2576 xp->xp_context = EXPAND_EXPRESSION;
2577 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002578 else if (c == '#'
2579 && xp->xp_context == EXPAND_EXPRESSION)
2580 {
2581 /* Autoload function/variable contains '#'. */
2582 break;
2583 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002584 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 && xp->xp_context == EXPAND_FUNCTIONS
2586 && vim_strchr(xp->xp_pattern, '(') == NULL)
2587 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002588 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 break;
2590 }
2591 else if (cmdidx != CMD_let || got_eq)
2592 {
2593 if (c == '"') /* string */
2594 {
2595 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2596 if (c == '\\' && xp->xp_pattern[1] != NUL)
2597 ++xp->xp_pattern;
2598 xp->xp_context = EXPAND_NOTHING;
2599 }
2600 else if (c == '\'') /* literal string */
2601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2604 /* skip */ ;
2605 xp->xp_context = EXPAND_NOTHING;
2606 }
2607 else if (c == '|')
2608 {
2609 if (xp->xp_pattern[1] == '|')
2610 {
2611 ++xp->xp_pattern;
2612 xp->xp_context = EXPAND_EXPRESSION;
2613 }
2614 else
2615 xp->xp_context = EXPAND_COMMANDS;
2616 }
2617 else
2618 xp->xp_context = EXPAND_EXPRESSION;
2619 }
2620 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002621 /* Doesn't look like something valid, expand as an expression
2622 * anyway. */
2623 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 arg = xp->xp_pattern;
2625 if (*arg != NUL)
2626 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2627 /* skip */ ;
2628 }
2629 xp->xp_pattern = arg;
2630}
2631
2632#endif /* FEAT_CMDL_COMPL */
2633
2634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * ":unlet[!] var1 ... " command.
2636 */
2637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002638ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002640 ex_unletlock(eap, eap->arg, 0);
2641}
2642
2643/*
2644 * ":lockvar" and ":unlockvar" commands
2645 */
2646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002647ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002648{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002650 int deep = 2;
2651
2652 if (eap->forceit)
2653 deep = -1;
2654 else if (vim_isdigit(*arg))
2655 {
2656 deep = getdigits(&arg);
2657 arg = skipwhite(arg);
2658 }
2659
2660 ex_unletlock(eap, arg, deep);
2661}
2662
2663/*
2664 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2665 */
2666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667ex_unletlock(
2668 exarg_T *eap,
2669 char_u *argstart,
2670 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002671{
2672 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002675 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677 do
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002680 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002681 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (lv.ll_name == NULL)
2683 error = TRUE; /* error but continue parsing */
2684 if (name_end == NULL || (!vim_iswhite(*name_end)
2685 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (name_end != NULL)
2688 {
2689 emsg_severe = TRUE;
2690 EMSG(_(e_trailing));
2691 }
2692 if (!(eap->skip || error))
2693 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 break;
2695 }
2696
2697 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002698 {
2699 if (eap->cmdidx == CMD_unlet)
2700 {
2701 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2702 error = TRUE;
2703 }
2704 else
2705 {
2706 if (do_lock_var(&lv, name_end, deep,
2707 eap->cmdidx == CMD_lockvar) == FAIL)
2708 error = TRUE;
2709 }
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!eap->skip)
2713 clear_lval(&lv);
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 arg = skipwhite(name_end);
2716 } while (!ends_excmd(*arg));
2717
2718 eap->nextcmd = check_nextcmd(arg);
2719}
2720
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002722do_unlet_var(
2723 lval_T *lp,
2724 char_u *name_end,
2725 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 int ret = OK;
2728 int cc;
2729
2730 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 cc = *name_end;
2733 *name_end = NUL;
2734
2735 /* Normal name or expanded name. */
2736 if (check_changedtick(lp->ll_name))
2737 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002738 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002742 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002743 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002744 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002745 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002746 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 else if (lp->ll_range)
2748 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002749 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002750 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002751 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002752
2753 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2754 {
2755 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002756 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002757 return FAIL;
2758 ll_li = li;
2759 ++ll_n1;
2760 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761
2762 /* Delete a range of List items. */
2763 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2764 {
2765 li = lp->ll_li->li_next;
2766 listitem_remove(lp->ll_list, lp->ll_li);
2767 lp->ll_li = li;
2768 ++lp->ll_n1;
2769 }
2770 }
2771 else
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 /* unlet a List item. */
2775 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002778 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 }
2780
2781 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782}
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784/*
2785 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002786 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 */
2788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002789do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
Bram Moolenaar33570922005-01-25 22:26:29 +00002791 hashtab_T *ht;
2792 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002793 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002794 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002795 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 ht = find_var_ht(name, &varname);
2798 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002801 if (d == NULL)
2802 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 if (ht == &globvarht)
2804 d = &globvardict;
2805 else if (ht == &compat_hashtab)
2806 d = &vimvardict;
2807 else
2808 {
2809 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2810 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2811 }
2812 if (d == NULL)
2813 {
2814 EMSG2(_(e_intern2), "do_unlet()");
2815 return FAIL;
2816 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002817 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 hi = hash_find(ht, varname);
2819 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002820 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002821 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002822 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002823 || var_check_ro(di->di_flags, name, FALSE)
2824 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002825 return FAIL;
2826
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002827 delete_var(ht, hi);
2828 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002831 if (forceit)
2832 return OK;
2833 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 return FAIL;
2835}
2836
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002837/*
2838 * Lock or unlock variable indicated by "lp".
2839 * "deep" is the levels to go (-1 for unlimited);
2840 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2841 */
2842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002843do_lock_var(
2844 lval_T *lp,
2845 char_u *name_end,
2846 int deep,
2847 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848{
2849 int ret = OK;
2850 int cc;
2851 dictitem_T *di;
2852
2853 if (deep == 0) /* nothing to do */
2854 return OK;
2855
2856 if (lp->ll_tv == NULL)
2857 {
2858 cc = *name_end;
2859 *name_end = NUL;
2860
2861 /* Normal name or expanded name. */
2862 if (check_changedtick(lp->ll_name))
2863 ret = FAIL;
2864 else
2865 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002866 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002867 if (di == NULL)
2868 ret = FAIL;
2869 else
2870 {
2871 if (lock)
2872 di->di_flags |= DI_FLAGS_LOCK;
2873 else
2874 di->di_flags &= ~DI_FLAGS_LOCK;
2875 item_lock(&di->di_tv, deep, lock);
2876 }
2877 }
2878 *name_end = cc;
2879 }
2880 else if (lp->ll_range)
2881 {
2882 listitem_T *li = lp->ll_li;
2883
2884 /* (un)lock a range of List items. */
2885 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2886 {
2887 item_lock(&li->li_tv, deep, lock);
2888 li = li->li_next;
2889 ++lp->ll_n1;
2890 }
2891 }
2892 else if (lp->ll_list != NULL)
2893 /* (un)lock a List item. */
2894 item_lock(&lp->ll_li->li_tv, deep, lock);
2895 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002896 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002897 item_lock(&lp->ll_di->di_tv, deep, lock);
2898
2899 return ret;
2900}
2901
2902/*
2903 * Lock or unlock an item. "deep" is nr of levels to go.
2904 */
2905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002907{
2908 static int recurse = 0;
2909 list_T *l;
2910 listitem_T *li;
2911 dict_T *d;
2912 hashitem_T *hi;
2913 int todo;
2914
2915 if (recurse >= DICT_MAXNEST)
2916 {
2917 EMSG(_("E743: variable nested too deep for (un)lock"));
2918 return;
2919 }
2920 if (deep == 0)
2921 return;
2922 ++recurse;
2923
2924 /* lock/unlock the item itself */
2925 if (lock)
2926 tv->v_lock |= VAR_LOCKED;
2927 else
2928 tv->v_lock &= ~VAR_LOCKED;
2929
2930 switch (tv->v_type)
2931 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002932 case VAR_UNKNOWN:
2933 case VAR_NUMBER:
2934 case VAR_STRING:
2935 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002936 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002937 case VAR_FLOAT:
2938 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002939 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002940 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002941 break;
2942
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002943 case VAR_LIST:
2944 if ((l = tv->vval.v_list) != NULL)
2945 {
2946 if (lock)
2947 l->lv_lock |= VAR_LOCKED;
2948 else
2949 l->lv_lock &= ~VAR_LOCKED;
2950 if (deep < 0 || deep > 1)
2951 /* recursive: lock/unlock the items the List contains */
2952 for (li = l->lv_first; li != NULL; li = li->li_next)
2953 item_lock(&li->li_tv, deep - 1, lock);
2954 }
2955 break;
2956 case VAR_DICT:
2957 if ((d = tv->vval.v_dict) != NULL)
2958 {
2959 if (lock)
2960 d->dv_lock |= VAR_LOCKED;
2961 else
2962 d->dv_lock &= ~VAR_LOCKED;
2963 if (deep < 0 || deep > 1)
2964 {
2965 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002966 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2968 {
2969 if (!HASHITEM_EMPTY(hi))
2970 {
2971 --todo;
2972 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2973 }
2974 }
2975 }
2976 }
2977 }
2978 --recurse;
2979}
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2982/*
2983 * Delete all "menutrans_" variables.
2984 */
2985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002986del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002989 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Bram Moolenaar33570922005-01-25 22:26:29 +00002991 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002992 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00002993 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00002994 {
2995 if (!HASHITEM_EMPTY(hi))
2996 {
2997 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002998 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2999 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003000 }
3001 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003}
3004#endif
3005
3006#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3007
3008/*
3009 * Local string buffer for the next two functions to store a variable name
3010 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3011 * get_user_var_name().
3012 */
3013
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003014static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016static char_u *varnamebuf = NULL;
3017static int varnamebuflen = 0;
3018
3019/*
3020 * Function to concatenate a prefix and a variable name.
3021 */
3022 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003023cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 int len;
3026
3027 len = (int)STRLEN(name) + 3;
3028 if (len > varnamebuflen)
3029 {
3030 vim_free(varnamebuf);
3031 len += 10; /* some additional space */
3032 varnamebuf = alloc(len);
3033 if (varnamebuf == NULL)
3034 {
3035 varnamebuflen = 0;
3036 return NULL;
3037 }
3038 varnamebuflen = len;
3039 }
3040 *varnamebuf = prefix;
3041 varnamebuf[1] = ':';
3042 STRCPY(varnamebuf + 2, name);
3043 return varnamebuf;
3044}
3045
3046/*
3047 * Function given to ExpandGeneric() to obtain the list of user defined
3048 * (global/buffer/window/built-in) variable names.
3049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003051get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003053 static long_u gdone;
3054 static long_u bdone;
3055 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003056#ifdef FEAT_WINDOWS
3057 static long_u tdone;
3058#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003059 static int vidx;
3060 static hashitem_T *hi;
3061 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003064 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003065 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003066#ifdef FEAT_WINDOWS
3067 tdone = 0;
3068#endif
3069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003070
3071 /* Global variables */
3072 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003074 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003075 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003076 else
3077 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003078 while (HASHITEM_EMPTY(hi))
3079 ++hi;
3080 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3081 return cat_prefix_varname('g', hi->hi_key);
3082 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003084
3085 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003086 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003089 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003091 else
3092 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003093 while (HASHITEM_EMPTY(hi))
3094 ++hi;
3095 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003097 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003099 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 return (char_u *)"b:changedtick";
3101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003102
3103 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003104 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003107 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003109 else
3110 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003111 while (HASHITEM_EMPTY(hi))
3112 ++hi;
3113 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003115
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003116#ifdef FEAT_WINDOWS
3117 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003118 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003119 if (tdone < ht->ht_used)
3120 {
3121 if (tdone++ == 0)
3122 hi = ht->ht_array;
3123 else
3124 ++hi;
3125 while (HASHITEM_EMPTY(hi))
3126 ++hi;
3127 return cat_prefix_varname('t', hi->hi_key);
3128 }
3129#endif
3130
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 /* v: variables */
3132 if (vidx < VV_LEN)
3133 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 vim_free(varnamebuf);
3136 varnamebuf = NULL;
3137 varnamebuflen = 0;
3138 return NULL;
3139}
3140
3141#endif /* FEAT_CMDL_COMPL */
3142
3143/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003144 * Return TRUE if "pat" matches "text".
3145 * Does not use 'cpo' and always uses 'magic'.
3146 */
3147 static int
3148pattern_match(char_u *pat, char_u *text, int ic)
3149{
3150 int matches = FALSE;
3151 char_u *save_cpo;
3152 regmatch_T regmatch;
3153
3154 /* avoid 'l' flag in 'cpoptions' */
3155 save_cpo = p_cpo;
3156 p_cpo = (char_u *)"";
3157 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3158 if (regmatch.regprog != NULL)
3159 {
3160 regmatch.rm_ic = ic;
3161 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3162 vim_regfree(regmatch.regprog);
3163 }
3164 p_cpo = save_cpo;
3165 return matches;
3166}
3167
3168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * types for expressions.
3170 */
3171typedef enum
3172{
3173 TYPE_UNKNOWN = 0
3174 , TYPE_EQUAL /* == */
3175 , TYPE_NEQUAL /* != */
3176 , TYPE_GREATER /* > */
3177 , TYPE_GEQUAL /* >= */
3178 , TYPE_SMALLER /* < */
3179 , TYPE_SEQUAL /* <= */
3180 , TYPE_MATCH /* =~ */
3181 , TYPE_NOMATCH /* !~ */
3182} exptype_T;
3183
3184/*
3185 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3188 */
3189
3190/*
3191 * Handle zero level expression.
3192 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003194 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 * Return OK or FAIL.
3196 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198eval0(
3199 char_u *arg,
3200 typval_T *rettv,
3201 char_u **nextcmd,
3202 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
3204 int ret;
3205 char_u *p;
3206
3207 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003208 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (ret == FAIL || !ends_excmd(*p))
3210 {
3211 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 /*
3214 * Report the invalid expression unless the expression evaluation has
3215 * been cancelled due to an aborting error, an interrupt, or an
3216 * exception.
3217 */
3218 if (!aborting())
3219 EMSG2(_(e_invexpr2), arg);
3220 ret = FAIL;
3221 }
3222 if (nextcmd != NULL)
3223 *nextcmd = check_nextcmd(p);
3224
3225 return ret;
3226}
3227
3228/*
3229 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003230 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 *
3232 * "arg" must point to the first non-white of the expression.
3233 * "arg" is advanced to the next non-white after the recognized expression.
3234 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003235 * Note: "rettv.v_lock" is not set.
3236 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * Return OK or FAIL.
3238 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /*
3246 * Get the first variable.
3247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003248 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 return FAIL;
3250
3251 if ((*arg)[0] == '?')
3252 {
3253 result = FALSE;
3254 if (evaluate)
3255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003256 int error = FALSE;
3257
3258 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003260 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003261 if (error)
3262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264
3265 /*
3266 * Get the second variable.
3267 */
3268 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 return FAIL;
3271
3272 /*
3273 * Check for the ":".
3274 */
3275 if ((*arg)[0] != ':')
3276 {
3277 EMSG(_("E109: Missing ':' after '?'"));
3278 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 return FAIL;
3281 }
3282
3283 /*
3284 * Get the third variable.
3285 */
3286 *arg = skipwhite(*arg + 1);
3287 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3288 {
3289 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003290 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292 }
3293 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296
3297 return OK;
3298}
3299
3300/*
3301 * Handle first level expression:
3302 * expr2 || expr2 || expr2 logical OR
3303 *
3304 * "arg" must point to the first non-white of the expression.
3305 * "arg" is advanced to the next non-white after the recognized expression.
3306 *
3307 * Return OK or FAIL.
3308 */
3309 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003310eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 long result;
3314 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003315 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
3317 /*
3318 * Get the first variable.
3319 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003320 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return FAIL;
3322
3323 /*
3324 * Repeat until there is no following "||".
3325 */
3326 first = TRUE;
3327 result = FALSE;
3328 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3329 {
3330 if (evaluate && first)
3331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 if (error)
3336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 first = FALSE;
3338 }
3339
3340 /*
3341 * Get the second variable.
3342 */
3343 *arg = skipwhite(*arg + 2);
3344 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3345 return FAIL;
3346
3347 /*
3348 * Compute the result.
3349 */
3350 if (evaluate && !result)
3351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 if (error)
3356 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358 if (evaluate)
3359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360 rettv->v_type = VAR_NUMBER;
3361 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363 }
3364
3365 return OK;
3366}
3367
3368/*
3369 * Handle second level expression:
3370 * expr3 && expr3 && expr3 logical AND
3371 *
3372 * "arg" must point to the first non-white of the expression.
3373 * "arg" is advanced to the next non-white after the recognized expression.
3374 *
3375 * Return OK or FAIL.
3376 */
3377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 long result;
3382 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003383 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 /*
3386 * Get the first variable.
3387 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003388 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return FAIL;
3390
3391 /*
3392 * Repeat until there is no following "&&".
3393 */
3394 first = TRUE;
3395 result = TRUE;
3396 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3397 {
3398 if (evaluate && first)
3399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003400 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403 if (error)
3404 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 first = FALSE;
3406 }
3407
3408 /*
3409 * Get the second variable.
3410 */
3411 *arg = skipwhite(*arg + 2);
3412 if (eval4(arg, &var2, evaluate && result) == FAIL)
3413 return FAIL;
3414
3415 /*
3416 * Compute the result.
3417 */
3418 if (evaluate && result)
3419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003422 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003423 if (error)
3424 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 if (evaluate)
3427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv->v_type = VAR_NUMBER;
3429 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431 }
3432
3433 return OK;
3434}
3435
3436/*
3437 * Handle third level expression:
3438 * var1 == var2
3439 * var1 =~ var2
3440 * var1 != var2
3441 * var1 !~ var2
3442 * var1 > var2
3443 * var1 >= var2
3444 * var1 < var2
3445 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003446 * var1 is var2
3447 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 *
3449 * "arg" must point to the first non-white of the expression.
3450 * "arg" is advanced to the next non-white after the recognized expression.
3451 *
3452 * Return OK or FAIL.
3453 */
3454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
Bram Moolenaar33570922005-01-25 22:26:29 +00003457 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 char_u *p;
3459 int i;
3460 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003461 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003463 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 char_u *s1, *s2;
3465 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 /*
3469 * Get the first variable.
3470 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473
3474 p = *arg;
3475 switch (p[0])
3476 {
3477 case '=': if (p[1] == '=')
3478 type = TYPE_EQUAL;
3479 else if (p[1] == '~')
3480 type = TYPE_MATCH;
3481 break;
3482 case '!': if (p[1] == '=')
3483 type = TYPE_NEQUAL;
3484 else if (p[1] == '~')
3485 type = TYPE_NOMATCH;
3486 break;
3487 case '>': if (p[1] != '=')
3488 {
3489 type = TYPE_GREATER;
3490 len = 1;
3491 }
3492 else
3493 type = TYPE_GEQUAL;
3494 break;
3495 case '<': if (p[1] != '=')
3496 {
3497 type = TYPE_SMALLER;
3498 len = 1;
3499 }
3500 else
3501 type = TYPE_SEQUAL;
3502 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003503 case 'i': if (p[1] == 's')
3504 {
3505 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3506 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003507 i = p[len];
3508 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003509 {
3510 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3511 type_is = TRUE;
3512 }
3513 }
3514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516
3517 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003518 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 */
3520 if (type != TYPE_UNKNOWN)
3521 {
3522 /* extra question mark appended: ignore case */
3523 if (p[len] == '?')
3524 {
3525 ic = TRUE;
3526 ++len;
3527 }
3528 /* extra '#' appended: match case */
3529 else if (p[len] == '#')
3530 {
3531 ic = FALSE;
3532 ++len;
3533 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 else
3536 ic = p_ic;
3537
3538 /*
3539 * Get the second variable.
3540 */
3541 *arg = skipwhite(p + len);
3542 if (eval5(arg, &var2, evaluate) == FAIL)
3543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return FAIL;
3546 }
3547
3548 if (evaluate)
3549 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003550 if (type_is && rettv->v_type != var2.v_type)
3551 {
3552 /* For "is" a different type always means FALSE, for "notis"
3553 * it means TRUE. */
3554 n1 = (type == TYPE_NEQUAL);
3555 }
3556 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3557 {
3558 if (type_is)
3559 {
3560 n1 = (rettv->v_type == var2.v_type
3561 && rettv->vval.v_list == var2.vval.v_list);
3562 if (type == TYPE_NEQUAL)
3563 n1 = !n1;
3564 }
3565 else if (rettv->v_type != var2.v_type
3566 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3567 {
3568 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003569 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003570 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003571 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003572 clear_tv(rettv);
3573 clear_tv(&var2);
3574 return FAIL;
3575 }
3576 else
3577 {
3578 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003579 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3580 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003581 if (type == TYPE_NEQUAL)
3582 n1 = !n1;
3583 }
3584 }
3585
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003586 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3587 {
3588 if (type_is)
3589 {
3590 n1 = (rettv->v_type == var2.v_type
3591 && rettv->vval.v_dict == var2.vval.v_dict);
3592 if (type == TYPE_NEQUAL)
3593 n1 = !n1;
3594 }
3595 else if (rettv->v_type != var2.v_type
3596 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3597 {
3598 if (rettv->v_type != var2.v_type)
3599 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3600 else
3601 EMSG(_("E736: Invalid operation for Dictionary"));
3602 clear_tv(rettv);
3603 clear_tv(&var2);
3604 return FAIL;
3605 }
3606 else
3607 {
3608 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003609 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3610 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003611 if (type == TYPE_NEQUAL)
3612 n1 = !n1;
3613 }
3614 }
3615
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003616 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3617 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003618 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003619 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003620 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003621 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003622 clear_tv(rettv);
3623 clear_tv(&var2);
3624 return FAIL;
3625 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003626 if ((rettv->v_type == VAR_PARTIAL
3627 && rettv->vval.v_partial == NULL)
3628 || (var2.v_type == VAR_PARTIAL
3629 && var2.vval.v_partial == NULL))
3630 /* when a partial is NULL assume not equal */
3631 n1 = FALSE;
3632 else if (type_is)
3633 {
3634 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3635 /* strings are considered the same if their value is
3636 * the same */
3637 n1 = tv_equal(rettv, &var2, ic, FALSE);
3638 else if (rettv->v_type == VAR_PARTIAL
3639 && var2.v_type == VAR_PARTIAL)
3640 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3641 else
3642 n1 = FALSE;
3643 }
3644 else
3645 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003646 if (type == TYPE_NEQUAL)
3647 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003648 }
3649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003650#ifdef FEAT_FLOAT
3651 /*
3652 * If one of the two variables is a float, compare as a float.
3653 * When using "=~" or "!~", always compare as string.
3654 */
3655 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3656 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3657 {
3658 float_T f1, f2;
3659
3660 if (rettv->v_type == VAR_FLOAT)
3661 f1 = rettv->vval.v_float;
3662 else
3663 f1 = get_tv_number(rettv);
3664 if (var2.v_type == VAR_FLOAT)
3665 f2 = var2.vval.v_float;
3666 else
3667 f2 = get_tv_number(&var2);
3668 n1 = FALSE;
3669 switch (type)
3670 {
3671 case TYPE_EQUAL: n1 = (f1 == f2); break;
3672 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3673 case TYPE_GREATER: n1 = (f1 > f2); break;
3674 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3675 case TYPE_SMALLER: n1 = (f1 < f2); break;
3676 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3677 case TYPE_UNKNOWN:
3678 case TYPE_MATCH:
3679 case TYPE_NOMATCH: break; /* avoid gcc warning */
3680 }
3681 }
3682#endif
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /*
3685 * If one of the two variables is a number, compare as a number.
3686 * When using "=~" or "!~", always compare as string.
3687 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003688 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003691 n1 = get_tv_number(rettv);
3692 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 switch (type)
3694 {
3695 case TYPE_EQUAL: n1 = (n1 == n2); break;
3696 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3697 case TYPE_GREATER: n1 = (n1 > n2); break;
3698 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3699 case TYPE_SMALLER: n1 = (n1 < n2); break;
3700 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3701 case TYPE_UNKNOWN:
3702 case TYPE_MATCH:
3703 case TYPE_NOMATCH: break; /* avoid gcc warning */
3704 }
3705 }
3706 else
3707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 s1 = get_tv_string_buf(rettv, buf1);
3709 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3711 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3712 else
3713 i = 0;
3714 n1 = FALSE;
3715 switch (type)
3716 {
3717 case TYPE_EQUAL: n1 = (i == 0); break;
3718 case TYPE_NEQUAL: n1 = (i != 0); break;
3719 case TYPE_GREATER: n1 = (i > 0); break;
3720 case TYPE_GEQUAL: n1 = (i >= 0); break;
3721 case TYPE_SMALLER: n1 = (i < 0); break;
3722 case TYPE_SEQUAL: n1 = (i <= 0); break;
3723
3724 case TYPE_MATCH:
3725 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003726 n1 = pattern_match(s2, s1, ic);
3727 if (type == TYPE_NOMATCH)
3728 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 break;
3730
3731 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3732 }
3733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 clear_tv(rettv);
3735 clear_tv(&var2);
3736 rettv->v_type = VAR_NUMBER;
3737 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739 }
3740
3741 return OK;
3742}
3743
3744/*
3745 * Handle fourth level expression:
3746 * + number addition
3747 * - number subtraction
3748 * . string concatenation
3749 *
3750 * "arg" must point to the first non-white of the expression.
3751 * "arg" is advanced to the next non-white after the recognized expression.
3752 *
3753 * Return OK or FAIL.
3754 */
3755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003756eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 typval_T var2;
3759 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003761 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003762#ifdef FEAT_FLOAT
3763 float_T f1 = 0, f2 = 0;
3764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 char_u *s1, *s2;
3766 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3767 char_u *p;
3768
3769 /*
3770 * Get the first variable.
3771 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003772 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return FAIL;
3774
3775 /*
3776 * Repeat computing, until no '+', '-' or '.' is following.
3777 */
3778 for (;;)
3779 {
3780 op = **arg;
3781 if (op != '+' && op != '-' && op != '.')
3782 break;
3783
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003784 if ((op != '+' || rettv->v_type != VAR_LIST)
3785#ifdef FEAT_FLOAT
3786 && (op == '.' || rettv->v_type != VAR_FLOAT)
3787#endif
3788 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 {
3790 /* For "list + ...", an illegal use of the first operand as
3791 * a number cannot be determined before evaluating the 2nd
3792 * operand: if this is also a list, all is ok.
3793 * For "something . ...", "something - ..." or "non-list + ...",
3794 * we know that the first operand needs to be a string or number
3795 * without evaluating the 2nd operand. So check before to avoid
3796 * side effects after an error. */
3797 if (evaluate && get_tv_string_chk(rettv) == NULL)
3798 {
3799 clear_tv(rettv);
3800 return FAIL;
3801 }
3802 }
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 /*
3805 * Get the second variable.
3806 */
3807 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003808 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 return FAIL;
3812 }
3813
3814 if (evaluate)
3815 {
3816 /*
3817 * Compute the result.
3818 */
3819 if (op == '.')
3820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003821 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3822 s2 = get_tv_string_buf_chk(&var2, buf2);
3823 if (s2 == NULL) /* type error ? */
3824 {
3825 clear_tv(rettv);
3826 clear_tv(&var2);
3827 return FAIL;
3828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003829 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 clear_tv(rettv);
3831 rettv->v_type = VAR_STRING;
3832 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003834 else if (op == '+' && rettv->v_type == VAR_LIST
3835 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003836 {
3837 /* concatenate Lists */
3838 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3839 &var3) == FAIL)
3840 {
3841 clear_tv(rettv);
3842 clear_tv(&var2);
3843 return FAIL;
3844 }
3845 clear_tv(rettv);
3846 *rettv = var3;
3847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 else
3849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 int error = FALSE;
3851
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003852#ifdef FEAT_FLOAT
3853 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003855 f1 = rettv->vval.v_float;
3856 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003858 else
3859#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861 n1 = get_tv_number_chk(rettv, &error);
3862 if (error)
3863 {
3864 /* This can only happen for "list + non-list". For
3865 * "non-list + ..." or "something - ...", we returned
3866 * before evaluating the 2nd operand. */
3867 clear_tv(rettv);
3868 return FAIL;
3869 }
3870#ifdef FEAT_FLOAT
3871 if (var2.v_type == VAR_FLOAT)
3872 f1 = n1;
3873#endif
3874 }
3875#ifdef FEAT_FLOAT
3876 if (var2.v_type == VAR_FLOAT)
3877 {
3878 f2 = var2.vval.v_float;
3879 n2 = 0;
3880 }
3881 else
3882#endif
3883 {
3884 n2 = get_tv_number_chk(&var2, &error);
3885 if (error)
3886 {
3887 clear_tv(rettv);
3888 clear_tv(&var2);
3889 return FAIL;
3890 }
3891#ifdef FEAT_FLOAT
3892 if (rettv->v_type == VAR_FLOAT)
3893 f2 = n2;
3894#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003895 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003896 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003897
3898#ifdef FEAT_FLOAT
3899 /* If there is a float on either side the result is a float. */
3900 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3901 {
3902 if (op == '+')
3903 f1 = f1 + f2;
3904 else
3905 f1 = f1 - f2;
3906 rettv->v_type = VAR_FLOAT;
3907 rettv->vval.v_float = f1;
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003910#endif
3911 {
3912 if (op == '+')
3913 n1 = n1 + n2;
3914 else
3915 n1 = n1 - n2;
3916 rettv->v_type = VAR_NUMBER;
3917 rettv->vval.v_number = n1;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003920 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 }
3923 return OK;
3924}
3925
3926/*
3927 * Handle fifth level expression:
3928 * * number multiplication
3929 * / number division
3930 * % number modulo
3931 *
3932 * "arg" must point to the first non-white of the expression.
3933 * "arg" is advanced to the next non-white after the recognized expression.
3934 *
3935 * Return OK or FAIL.
3936 */
3937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003938eval6(
3939 char_u **arg,
3940 typval_T *rettv,
3941 int evaluate,
3942 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003946 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003947#ifdef FEAT_FLOAT
3948 int use_float = FALSE;
3949 float_T f1 = 0, f2;
3950#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003951 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 /*
3954 * Get the first variable.
3955 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003956 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return FAIL;
3958
3959 /*
3960 * Repeat computing, until no '*', '/' or '%' is following.
3961 */
3962 for (;;)
3963 {
3964 op = **arg;
3965 if (op != '*' && op != '/' && op != '%')
3966 break;
3967
3968 if (evaluate)
3969 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003970#ifdef FEAT_FLOAT
3971 if (rettv->v_type == VAR_FLOAT)
3972 {
3973 f1 = rettv->vval.v_float;
3974 use_float = TRUE;
3975 n1 = 0;
3976 }
3977 else
3978#endif
3979 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003981 if (error)
3982 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984 else
3985 n1 = 0;
3986
3987 /*
3988 * Get the second variable.
3989 */
3990 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003991 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 return FAIL;
3993
3994 if (evaluate)
3995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003996#ifdef FEAT_FLOAT
3997 if (var2.v_type == VAR_FLOAT)
3998 {
3999 if (!use_float)
4000 {
4001 f1 = n1;
4002 use_float = TRUE;
4003 }
4004 f2 = var2.vval.v_float;
4005 n2 = 0;
4006 }
4007 else
4008#endif
4009 {
4010 n2 = get_tv_number_chk(&var2, &error);
4011 clear_tv(&var2);
4012 if (error)
4013 return FAIL;
4014#ifdef FEAT_FLOAT
4015 if (use_float)
4016 f2 = n2;
4017#endif
4018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020 /*
4021 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004022 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004024#ifdef FEAT_FLOAT
4025 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004027 if (op == '*')
4028 f1 = f1 * f2;
4029 else if (op == '/')
4030 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004031# ifdef VMS
4032 /* VMS crashes on divide by zero, work around it */
4033 if (f2 == 0.0)
4034 {
4035 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004036 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004037 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004038 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004039 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004040 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004041 }
4042 else
4043 f1 = f1 / f2;
4044# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045 /* We rely on the floating point library to handle divide
4046 * by zero to result in "inf" and not a crash. */
4047 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004051 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004052 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004053 return FAIL;
4054 }
4055 rettv->v_type = VAR_FLOAT;
4056 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 if (op == '*')
4062 n1 = n1 * n2;
4063 else if (op == '/')
4064 {
4065 if (n2 == 0) /* give an error message? */
4066 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004067#ifdef FEAT_NUM64
4068 if (n1 == 0)
4069 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
4070 else if (n1 < 0)
4071 n1 = -0x7fffffffffffffff;
4072 else
4073 n1 = 0x7fffffffffffffff;
4074#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075 if (n1 == 0)
4076 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4077 else if (n1 < 0)
4078 n1 = -0x7fffffffL;
4079 else
4080 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004081#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 }
4083 else
4084 n1 = n1 / n2;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 {
4088 if (n2 == 0) /* give an error message? */
4089 n1 = 0;
4090 else
4091 n1 = n1 % n2;
4092 }
4093 rettv->v_type = VAR_NUMBER;
4094 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 }
4098
4099 return OK;
4100}
4101
4102/*
4103 * Handle sixth level expression:
4104 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004105 * "string" string constant
4106 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 * &option-name option value
4108 * @r register contents
4109 * identifier variable value
4110 * function() function call
4111 * $VAR environment variable
4112 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004113 * [expr, expr] List
4114 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 *
4116 * Also handle:
4117 * ! in front logical NOT
4118 * - in front unary minus
4119 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004120 * trailing [] subscript in String or List
4121 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 *
4123 * "arg" must point to the first non-white of the expression.
4124 * "arg" is advanced to the next non-white after the recognized expression.
4125 *
4126 * Return OK or FAIL.
4127 */
4128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004129eval7(
4130 char_u **arg,
4131 typval_T *rettv,
4132 int evaluate,
4133 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004135 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 int len;
4137 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u *start_leader, *end_leader;
4139 int ret = OK;
4140 char_u *alias;
4141
4142 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004144 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /*
4149 * Skip '!' and '-' characters. They are handled later.
4150 */
4151 start_leader = *arg;
4152 while (**arg == '!' || **arg == '-' || **arg == '+')
4153 *arg = skipwhite(*arg + 1);
4154 end_leader = *arg;
4155
4156 switch (**arg)
4157 {
4158 /*
4159 * Number constant.
4160 */
4161 case '0':
4162 case '1':
4163 case '2':
4164 case '3':
4165 case '4':
4166 case '5':
4167 case '6':
4168 case '7':
4169 case '8':
4170 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004171 {
4172#ifdef FEAT_FLOAT
4173 char_u *p = skipdigits(*arg + 1);
4174 int get_float = FALSE;
4175
4176 /* We accept a float when the format matches
4177 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004178 * strict to avoid backwards compatibility problems.
4179 * Don't look for a float after the "." operator, so that
4180 * ":let vers = 1.2.3" doesn't fail. */
4181 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004183 get_float = TRUE;
4184 p = skipdigits(p + 2);
4185 if (*p == 'e' || *p == 'E')
4186 {
4187 ++p;
4188 if (*p == '-' || *p == '+')
4189 ++p;
4190 if (!vim_isdigit(*p))
4191 get_float = FALSE;
4192 else
4193 p = skipdigits(p + 1);
4194 }
4195 if (ASCII_ISALPHA(*p) || *p == '.')
4196 get_float = FALSE;
4197 }
4198 if (get_float)
4199 {
4200 float_T f;
4201
4202 *arg += string2float(*arg, &f);
4203 if (evaluate)
4204 {
4205 rettv->v_type = VAR_FLOAT;
4206 rettv->vval.v_float = f;
4207 }
4208 }
4209 else
4210#endif
4211 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004212 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004213 *arg += len;
4214 if (evaluate)
4215 {
4216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = n;
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * String constant: "string".
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 break;
4228
4229 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004230 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004233 break;
4234
4235 /*
4236 * List: [expr, expr]
4237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break;
4240
4241 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004242 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004243 * Dictionary: {key: val, key: val}
4244 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004245 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4246 if (ret == NOTDONE)
4247 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248 break;
4249
4250 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004251 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004253 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 break;
4255
4256 /*
4257 * Environment variable: $VAR.
4258 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 break;
4261
4262 /*
4263 * Register contents: @r.
4264 */
4265 case '@': ++*arg;
4266 if (evaluate)
4267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004269 rettv->vval.v_string = get_reg_contents(**arg,
4270 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 if (**arg != NUL)
4273 ++*arg;
4274 break;
4275
4276 /*
4277 * nested expression: (expression).
4278 */
4279 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (**arg == ')')
4282 ++*arg;
4283 else if (ret == OK)
4284 {
4285 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 ret = FAIL;
4288 }
4289 break;
4290
Bram Moolenaar8c711452005-01-14 21:53:12 +00004291 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004294
4295 if (ret == NOTDONE)
4296 {
4297 /*
4298 * Must be a variable or function name.
4299 * Can also be a curly-braces kind of name: {expr}.
4300 */
4301 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004302 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004303 if (alias != NULL)
4304 s = alias;
4305
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004306 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 ret = FAIL;
4308 else
4309 {
4310 if (**arg == '(') /* recursive! */
4311 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004312 partial_T *partial;
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
4318 /* Invoke the function. */
4319 ret = get_func_tv(s, len, rettv, arg,
4320 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004321 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004322
4323 /* If evaluate is FALSE rettv->v_type was not set in
4324 * get_func_tv, but it's needed in handle_subscript() to parse
4325 * what follows. So set it here. */
4326 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4327 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004328 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004329 rettv->v_type = VAR_FUNC;
4330 }
4331
Bram Moolenaar8c711452005-01-14 21:53:12 +00004332 /* Stop the expression evaluation when immediately
4333 * aborting on error, or when an interrupt occurred or
4334 * an exception was thrown but not caught. */
4335 if (aborting())
4336 {
4337 if (ret == OK)
4338 clear_tv(rettv);
4339 ret = FAIL;
4340 }
4341 }
4342 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004343 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004344 else
4345 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004346 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004347 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348 }
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 *arg = skipwhite(*arg);
4351
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004352 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4353 * expr(expr). */
4354 if (ret == OK)
4355 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
4358 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4359 */
4360 if (ret == OK && evaluate && end_leader > start_leader)
4361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004363 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004364#ifdef FEAT_FLOAT
4365 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004366
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004367 if (rettv->v_type == VAR_FLOAT)
4368 f = rettv->vval.v_float;
4369 else
4370#endif
4371 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 clear_tv(rettv);
4375 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 else
4378 {
4379 while (end_leader > start_leader)
4380 {
4381 --end_leader;
4382 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004383 {
4384#ifdef FEAT_FLOAT
4385 if (rettv->v_type == VAR_FLOAT)
4386 f = !f;
4387 else
4388#endif
4389 val = !val;
4390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004392 {
4393#ifdef FEAT_FLOAT
4394 if (rettv->v_type == VAR_FLOAT)
4395 f = -f;
4396 else
4397#endif
4398 val = -val;
4399 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004401#ifdef FEAT_FLOAT
4402 if (rettv->v_type == VAR_FLOAT)
4403 {
4404 clear_tv(rettv);
4405 rettv->vval.v_float = f;
4406 }
4407 else
4408#endif
4409 {
4410 clear_tv(rettv);
4411 rettv->v_type = VAR_NUMBER;
4412 rettv->vval.v_number = val;
4413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416
4417 return ret;
4418}
4419
4420/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004421 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4422 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4424 */
4425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004426eval_index(
4427 char_u **arg,
4428 typval_T *rettv,
4429 int evaluate,
4430 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004431{
4432 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004433 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 long len = -1;
4436 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439
Bram Moolenaara03f2332016-02-06 18:09:59 +01004440 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004442 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004443 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004444 if (verbose)
4445 EMSG(_("E695: Cannot index a Funcref"));
4446 return FAIL;
4447 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004448#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004449 if (verbose)
4450 EMSG(_(e_float_as_string));
4451 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004452#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004453 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004454 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004455 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004456 if (verbose)
4457 EMSG(_("E909: Cannot index a special variable"));
4458 return FAIL;
4459 case VAR_UNKNOWN:
4460 if (evaluate)
4461 return FAIL;
4462 /* FALLTHROUGH */
4463
4464 case VAR_STRING:
4465 case VAR_NUMBER:
4466 case VAR_LIST:
4467 case VAR_DICT:
4468 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004469 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004471 init_tv(&var1);
4472 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004475 /*
4476 * dict.name
4477 */
4478 key = *arg + 1;
4479 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4480 ;
4481 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004482 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484 }
4485 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004486 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004487 /*
4488 * something[idx]
4489 *
4490 * Get the (first) variable from inside the [].
4491 */
4492 *arg = skipwhite(*arg + 1);
4493 if (**arg == ':')
4494 empty1 = TRUE;
4495 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4496 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4498 {
4499 /* not a number or string */
4500 clear_tv(&var1);
4501 return FAIL;
4502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503
4504 /*
4505 * Get the second variable from inside the [:].
4506 */
4507 if (**arg == ':')
4508 {
4509 range = TRUE;
4510 *arg = skipwhite(*arg + 1);
4511 if (**arg == ']')
4512 empty2 = TRUE;
4513 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004515 if (!empty1)
4516 clear_tv(&var1);
4517 return FAIL;
4518 }
4519 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4520 {
4521 /* not a number or string */
4522 if (!empty1)
4523 clear_tv(&var1);
4524 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 return FAIL;
4526 }
4527 }
4528
4529 /* Check for the ']'. */
4530 if (**arg != ']')
4531 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004532 if (verbose)
4533 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 clear_tv(&var1);
4535 if (range)
4536 clear_tv(&var2);
4537 return FAIL;
4538 }
4539 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540 }
4541
4542 if (evaluate)
4543 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 n1 = 0;
4545 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 n1 = get_tv_number(&var1);
4548 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 }
4550 if (range)
4551 {
4552 if (empty2)
4553 n2 = -1;
4554 else
4555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004556 n2 = get_tv_number(&var2);
4557 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558 }
4559 }
4560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004563 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004564 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004565 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004566 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004567 case VAR_SPECIAL:
4568 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004569 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004570 break; /* not evaluating, skipping over subscript */
4571
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004572 case VAR_NUMBER:
4573 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 len = (long)STRLEN(s);
4576 if (range)
4577 {
4578 /* The resulting variable is a substring. If the indexes
4579 * are out of range the result is empty. */
4580 if (n1 < 0)
4581 {
4582 n1 = len + n1;
4583 if (n1 < 0)
4584 n1 = 0;
4585 }
4586 if (n2 < 0)
4587 n2 = len + n2;
4588 else if (n2 >= len)
4589 n2 = len;
4590 if (n1 >= len || n2 < 0 || n1 > n2)
4591 s = NULL;
4592 else
4593 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4594 }
4595 else
4596 {
4597 /* The resulting variable is a string of a single
4598 * character. If the index is too big or negative the
4599 * result is empty. */
4600 if (n1 >= len || n1 < 0)
4601 s = NULL;
4602 else
4603 s = vim_strnsave(s + n1, 1);
4604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 clear_tv(rettv);
4606 rettv->v_type = VAR_STRING;
4607 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 break;
4609
4610 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 if (n1 < 0)
4613 n1 = len + n1;
4614 if (!empty1 && (n1 < 0 || n1 >= len))
4615 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004616 /* For a range we allow invalid values and return an empty
4617 * list. A list index out of range is an error. */
4618 if (!range)
4619 {
4620 if (verbose)
4621 EMSGN(_(e_listidx), n1);
4622 return FAIL;
4623 }
4624 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 }
4626 if (range)
4627 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004628 list_T *l;
4629 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630
4631 if (n2 < 0)
4632 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004633 else if (n2 >= len)
4634 n2 = len - 1;
4635 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004636 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 l = list_alloc();
4638 if (l == NULL)
4639 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 n1 <= n2; ++n1)
4642 {
4643 if (list_append_tv(l, &item->li_tv) == FAIL)
4644 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004645 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 return FAIL;
4647 }
4648 item = item->li_next;
4649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 clear_tv(rettv);
4651 rettv->v_type = VAR_LIST;
4652 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004653 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 }
4655 else
4656 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004657 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 clear_tv(rettv);
4659 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 }
4661 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662
4663 case VAR_DICT:
4664 if (range)
4665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004666 if (verbose)
4667 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 if (len == -1)
4669 clear_tv(&var1);
4670 return FAIL;
4671 }
4672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004673 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674
4675 if (len == -1)
4676 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004677 key = get_tv_string_chk(&var1);
4678 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 clear_tv(&var1);
4681 return FAIL;
4682 }
4683 }
4684
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004685 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004686
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004687 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004688 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 if (len == -1)
4690 clear_tv(&var1);
4691 if (item == NULL)
4692 return FAIL;
4693
4694 copy_tv(&item->di_tv, &var1);
4695 clear_tv(rettv);
4696 *rettv = var1;
4697 }
4698 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 }
4700 }
4701
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 return OK;
4703}
4704
4705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 * Get an option value.
4707 * "arg" points to the '&' or '+' before the option name.
4708 * "arg" is advanced to character after the option name.
4709 * Return OK or FAIL.
4710 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004712get_option_tv(
4713 char_u **arg,
4714 typval_T *rettv, /* when NULL, only check if option exists */
4715 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 char_u *option_end;
4718 long numval;
4719 char_u *stringval;
4720 int opt_type;
4721 int c;
4722 int working = (**arg == '+'); /* has("+option") */
4723 int ret = OK;
4724 int opt_flags;
4725
4726 /*
4727 * Isolate the option name and find its value.
4728 */
4729 option_end = find_option_end(arg, &opt_flags);
4730 if (option_end == NULL)
4731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004732 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 EMSG2(_("E112: Option name missing: %s"), *arg);
4734 return FAIL;
4735 }
4736
4737 if (!evaluate)
4738 {
4739 *arg = option_end;
4740 return OK;
4741 }
4742
4743 c = *option_end;
4744 *option_end = NUL;
4745 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 if (opt_type == -3) /* invalid name */
4749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 EMSG2(_("E113: Unknown option: %s"), *arg);
4752 ret = FAIL;
4753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 if (opt_type == -2) /* hidden string option */
4757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 else if (opt_type == -1) /* hidden number option */
4762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 rettv->v_type = VAR_NUMBER;
4764 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 else if (opt_type == 1) /* number option */
4767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 rettv->v_type = VAR_NUMBER;
4769 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else /* string option */
4772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 rettv->v_type = VAR_STRING;
4774 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 }
4777 else if (working && (opt_type == -2 || opt_type == -1))
4778 ret = FAIL;
4779
4780 *option_end = c; /* put back for error messages */
4781 *arg = option_end;
4782
4783 return ret;
4784}
4785
4786/*
4787 * Allocate a variable for a string constant.
4788 * Return OK or FAIL.
4789 */
4790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004791get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 char_u *p;
4794 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int extra = 0;
4796
4797 /*
4798 * Find the end of the string, skipping backslashed characters.
4799 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004800 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 if (*p == '\\' && p[1] != NUL)
4803 {
4804 ++p;
4805 /* A "\<x>" form occupies at least 4 characters, and produces up
4806 * to 6 characters: reserve space for 2 extra */
4807 if (*p == '<')
4808 extra += 2;
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811
4812 if (*p != '"')
4813 {
4814 EMSG2(_("E114: Missing quote: %s"), *arg);
4815 return FAIL;
4816 }
4817
4818 /* If only parsing, set *arg and return here */
4819 if (!evaluate)
4820 {
4821 *arg = p + 1;
4822 return OK;
4823 }
4824
4825 /*
4826 * Copy the string into allocated memory, handling backslashed
4827 * characters.
4828 */
4829 name = alloc((unsigned)(p - *arg + extra));
4830 if (name == NULL)
4831 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832 rettv->v_type = VAR_STRING;
4833 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
4837 if (*p == '\\')
4838 {
4839 switch (*++p)
4840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 case 'b': *name++ = BS; ++p; break;
4842 case 'e': *name++ = ESC; ++p; break;
4843 case 'f': *name++ = FF; ++p; break;
4844 case 'n': *name++ = NL; ++p; break;
4845 case 'r': *name++ = CAR; ++p; break;
4846 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 case 'X': /* hex: "\x1", "\x12" */
4849 case 'x':
4850 case 'u': /* Unicode: "\u0023" */
4851 case 'U':
4852 if (vim_isxdigit(p[1]))
4853 {
4854 int n, nr;
4855 int c = toupper(*p);
4856
4857 if (c == 'X')
4858 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004859 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004861 else
4862 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 nr = 0;
4864 while (--n >= 0 && vim_isxdigit(p[1]))
4865 {
4866 ++p;
4867 nr = (nr << 4) + hex2nr(*p);
4868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#ifdef FEAT_MBYTE
4871 /* For "\u" store the number according to
4872 * 'encoding'. */
4873 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004874 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 else
4876#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004877 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 break;
4880
4881 /* octal: "\1", "\12", "\123" */
4882 case '0':
4883 case '1':
4884 case '2':
4885 case '3':
4886 case '4':
4887 case '5':
4888 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889 case '7': *name = *p++ - '0';
4890 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892 *name = (*name << 3) + *p++ - '0';
4893 if (*p >= '0' && *p <= '7')
4894 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004896 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 break;
4898
4899 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (extra != 0)
4902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 break;
4905 }
4906 /* FALLTHROUGH */
4907
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910 }
4911 }
4912 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 *arg = p + 1;
4918
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return OK;
4920}
4921
4922/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 * Return OK or FAIL.
4925 */
4926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004927get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
4929 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004930 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004931 int reduce = 0;
4932
4933 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004935 */
4936 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004941 break;
4942 ++reduce;
4943 ++p;
4944 }
4945 }
4946
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004950 return FAIL;
4951 }
4952
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004954 if (!evaluate)
4955 {
4956 *arg = p + 1;
4957 return OK;
4958 }
4959
4960 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004962 */
4963 str = alloc((unsigned)((p - *arg) - reduce));
4964 if (str == NULL)
4965 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 rettv->v_type = VAR_STRING;
4967 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004968
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004972 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004974 break;
4975 ++p;
4976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004980 *arg = p + 1;
4981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004982 return OK;
4983}
4984
Bram Moolenaarddecc252016-04-06 22:59:37 +02004985 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004986partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004987{
4988 int i;
4989
4990 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004991 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004992 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004993 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004994 func_unref(pt->pt_name);
4995 vim_free(pt->pt_name);
4996 vim_free(pt);
4997}
4998
4999/*
5000 * Unreference a closure: decrement the reference count and free it when it
5001 * becomes zero.
5002 */
5003 void
5004partial_unref(partial_T *pt)
5005{
5006 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005008}
5009
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005010static int tv_equal_recurse_limit;
5011
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005012 static int
5013func_equal(
5014 typval_T *tv1,
5015 typval_T *tv2,
5016 int ic) /* ignore case */
5017{
5018 char_u *s1, *s2;
5019 dict_T *d1, *d2;
5020 int a1, a2;
5021 int i;
5022
5023 /* empty and NULL function name considered the same */
5024 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
5025 : tv1->vval.v_partial->pt_name;
5026 if (s1 != NULL && *s1 == NUL)
5027 s1 = NULL;
5028 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
5029 : tv2->vval.v_partial->pt_name;
5030 if (s2 != NULL && *s2 == NUL)
5031 s2 = NULL;
5032 if (s1 == NULL || s2 == NULL)
5033 {
5034 if (s1 != s2)
5035 return FALSE;
5036 }
5037 else if (STRCMP(s1, s2) != 0)
5038 return FALSE;
5039
5040 /* empty dict and NULL dict is different */
5041 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5042 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5043 if (d1 == NULL || d2 == NULL)
5044 {
5045 if (d1 != d2)
5046 return FALSE;
5047 }
5048 else if (!dict_equal(d1, d2, ic, TRUE))
5049 return FALSE;
5050
5051 /* empty list and no list considered the same */
5052 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5053 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5054 if (a1 != a2)
5055 return FALSE;
5056 for (i = 0; i < a1; ++i)
5057 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5058 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5059 return FALSE;
5060
5061 return TRUE;
5062}
5063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005064/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005065 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005066 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005068 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005070tv_equal(
5071 typval_T *tv1,
5072 typval_T *tv2,
5073 int ic, /* ignore case */
5074 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005075{
5076 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005077 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005078 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005079 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005080
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005081 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005082 * recursiveness to a limit. We guess they are equal then.
5083 * A fixed limit has the problem of still taking an awful long time.
5084 * Reduce the limit every time running into it. That should work fine for
5085 * deeply linked structures that are not recursively linked and catch
5086 * recursiveness quickly. */
5087 if (!recursive)
5088 tv_equal_recurse_limit = 1000;
5089 if (recursive_cnt >= tv_equal_recurse_limit)
5090 {
5091 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005092 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005093 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005094
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005095 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5096 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005097 if ((tv1->v_type == VAR_FUNC
5098 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5099 && (tv2->v_type == VAR_FUNC
5100 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5101 {
5102 ++recursive_cnt;
5103 r = func_equal(tv1, tv2, ic);
5104 --recursive_cnt;
5105 return r;
5106 }
5107
5108 if (tv1->v_type != tv2->v_type)
5109 return FALSE;
5110
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005111 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005112 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005113 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005114 ++recursive_cnt;
5115 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5116 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005117 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005118
5119 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005120 ++recursive_cnt;
5121 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5122 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005123 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005124
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005125 case VAR_NUMBER:
5126 return tv1->vval.v_number == tv2->vval.v_number;
5127
5128 case VAR_STRING:
5129 s1 = get_tv_string_buf(tv1, buf1);
5130 s2 = get_tv_string_buf(tv2, buf2);
5131 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005132
5133 case VAR_SPECIAL:
5134 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005135
5136 case VAR_FLOAT:
5137#ifdef FEAT_FLOAT
5138 return tv1->vval.v_float == tv2->vval.v_float;
5139#endif
5140 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005142 return tv1->vval.v_job == tv2->vval.v_job;
5143#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005144 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005146 return tv1->vval.v_channel == tv2->vval.v_channel;
5147#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005148 case VAR_FUNC:
5149 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005150 case VAR_UNKNOWN:
5151 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005152 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005153
Bram Moolenaara03f2332016-02-06 18:09:59 +01005154 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5155 * does not equal anything, not even itself. */
5156 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005157}
5158
5159/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005160 * Return the next (unique) copy ID.
5161 * Used for serializing nested structures.
5162 */
5163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005164get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005165{
5166 current_copyID += COPYID_INC;
5167 return current_copyID;
5168}
5169
5170/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005171 * Garbage collection for lists and dictionaries.
5172 *
5173 * We use reference counts to be able to free most items right away when they
5174 * are no longer used. But for composite items it's possible that it becomes
5175 * unused while the reference count is > 0: When there is a recursive
5176 * reference. Example:
5177 * :let l = [1, 2, 3]
5178 * :let d = {9: l}
5179 * :let l[1] = d
5180 *
5181 * Since this is quite unusual we handle this with garbage collection: every
5182 * once in a while find out which lists and dicts are not referenced from any
5183 * variable.
5184 *
5185 * Here is a good reference text about garbage collection (refers to Python
5186 * but it applies to all reference-counting mechanisms):
5187 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005188 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005189
5190/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005191 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005192 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005193 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005194 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005195 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005196garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005197{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005198 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005199 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005200 buf_T *buf;
5201 win_T *wp;
5202 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005203 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005204#ifdef FEAT_WINDOWS
5205 tabpage_T *tp;
5206#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005207
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005208 if (!testing)
5209 {
5210 /* Only do this once. */
5211 want_garbage_collect = FALSE;
5212 may_garbage_collect = FALSE;
5213 garbage_collect_at_exit = FALSE;
5214 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005215
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005216 /* We advance by two because we add one for items referenced through
5217 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005218 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005219
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220 /*
5221 * 1. Go through all accessible variables and mark all lists and dicts
5222 * with copyID.
5223 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005224
5225 /* Don't free variables in the previous_funccal list unless they are only
5226 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005227 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005228 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005229
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230 /* script-local variables */
5231 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005232 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005233
5234 /* buffer-local variables */
5235 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005236 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5237 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238
5239 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005240 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005241 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5242 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005243#ifdef FEAT_AUTOCMD
5244 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005245 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5246 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005247#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005248
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005249#ifdef FEAT_WINDOWS
5250 /* tabpage-local variables */
5251 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5253 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005254#endif
5255
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005256 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005258
5259 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005261
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005262 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005264
Bram Moolenaard812df62008-11-09 12:46:09 +00005265 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005267
Bram Moolenaar1dced572012-04-05 16:54:08 +02005268#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005270#endif
5271
Bram Moolenaardb913952012-06-29 12:54:53 +02005272#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005274#endif
5275
5276#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005278#endif
5279
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005280#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005281 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005282 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005283#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005284#ifdef FEAT_NETBEANS_INTG
5285 abort = abort || set_ref_in_nb_channel(copyID);
5286#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005287
Bram Moolenaare3188e22016-05-31 21:13:04 +02005288#ifdef FEAT_TIMERS
5289 abort = abort || set_ref_in_timer(copyID);
5290#endif
5291
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005292 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005293 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 /*
5295 * 2. Free lists and dictionaries that are not referenced.
5296 */
5297 did_free = free_unref_items(copyID);
5298
5299 /*
5300 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005301 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005304 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 else if (p_verbose > 0)
5306 {
5307 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5308 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005309
5310 return did_free;
5311}
5312
5313/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005314 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005315 */
5316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005317free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005319 int did_free = FALSE;
5320
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005321 /* Let all "free" functions know that we are here. This means no
5322 * dictionaries, lists, channels or jobs are to be freed, because we will
5323 * do that here. */
5324 in_free_unref_items = TRUE;
5325
5326 /*
5327 * PASS 1: free the contents of the items. We don't free the items
5328 * themselves yet, so that it is possible to decrement refcount counters
5329 */
5330
Bram Moolenaarcd524592016-07-17 14:57:05 +02005331 /* Go through the list of dicts and free items without the copyID. */
5332 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005333
Bram Moolenaarda861d62016-07-17 15:46:27 +02005334 /* Go through the list of lists and free items without the copyID. */
5335 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005336
5337#ifdef FEAT_JOB_CHANNEL
5338 /* Go through the list of jobs and free items without the copyID. This
5339 * must happen before doing channels, because jobs refer to channels, but
5340 * the reference from the channel to the job isn't tracked. */
5341 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5342
5343 /* Go through the list of channels and free items without the copyID. */
5344 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5345#endif
5346
5347 /*
5348 * PASS 2: free the items themselves.
5349 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005350 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005351 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005352
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005353#ifdef FEAT_JOB_CHANNEL
5354 /* Go through the list of jobs and free items without the copyID. This
5355 * must happen before doing channels, because jobs refer to channels, but
5356 * the reference from the channel to the job isn't tracked. */
5357 free_unused_jobs(copyID, COPYID_MASK);
5358
5359 /* Go through the list of channels and free items without the copyID. */
5360 free_unused_channels(copyID, COPYID_MASK);
5361#endif
5362
5363 in_free_unref_items = FALSE;
5364
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005365 return did_free;
5366}
5367
5368/*
5369 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 * "list_stack" is used to add lists to be marked. Can be NULL.
5371 *
5372 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005373 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005375set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376{
5377 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005378 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005380 hashtab_T *cur_ht;
5381 ht_stack_T *ht_stack = NULL;
5382 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005384 cur_ht = ht;
5385 for (;;)
5386 {
5387 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 /* Mark each item in the hashtab. If the item contains a hashtab
5390 * it is added to ht_stack, if it contains a list it is added to
5391 * list_stack. */
5392 todo = (int)cur_ht->ht_used;
5393 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5394 if (!HASHITEM_EMPTY(hi))
5395 {
5396 --todo;
5397 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5398 &ht_stack, list_stack);
5399 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401
5402 if (ht_stack == NULL)
5403 break;
5404
5405 /* take an item from the stack */
5406 cur_ht = ht_stack->ht;
5407 tempitem = ht_stack;
5408 ht_stack = ht_stack->prev;
5409 free(tempitem);
5410 }
5411
5412 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005413}
5414
5415/*
5416 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005417 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5418 *
5419 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 listitem_T *li;
5425 int abort = FALSE;
5426 list_T *cur_l;
5427 list_stack_T *list_stack = NULL;
5428 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005430 cur_l = l;
5431 for (;;)
5432 {
5433 if (!abort)
5434 /* Mark each item in the list. If the item contains a hashtab
5435 * it is added to ht_stack, if it contains a list it is added to
5436 * list_stack. */
5437 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5438 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5439 ht_stack, &list_stack);
5440 if (list_stack == NULL)
5441 break;
5442
5443 /* take an item from the stack */
5444 cur_l = list_stack->list;
5445 tempitem = list_stack;
5446 list_stack = list_stack->prev;
5447 free(tempitem);
5448 }
5449
5450 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005451}
5452
5453/*
5454 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005455 * "list_stack" is used to add lists to be marked. Can be NULL.
5456 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5457 *
5458 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005459 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005461set_ref_in_item(
5462 typval_T *tv,
5463 int copyID,
5464 ht_stack_T **ht_stack,
5465 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005466{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005467 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005469 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005470 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005471 dict_T *dd = tv->vval.v_dict;
5472
Bram Moolenaara03f2332016-02-06 18:09:59 +01005473 if (dd != NULL && dd->dv_copyID != copyID)
5474 {
5475 /* Didn't see this dict yet. */
5476 dd->dv_copyID = copyID;
5477 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005478 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005479 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5480 }
5481 else
5482 {
5483 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5484 if (newitem == NULL)
5485 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 else
5487 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 newitem->ht = &dd->dv_hashtab;
5489 newitem->prev = *ht_stack;
5490 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005491 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005492 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005493 }
5494 }
5495 else if (tv->v_type == VAR_LIST)
5496 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005497 list_T *ll = tv->vval.v_list;
5498
Bram Moolenaara03f2332016-02-06 18:09:59 +01005499 if (ll != NULL && ll->lv_copyID != copyID)
5500 {
5501 /* Didn't see this list yet. */
5502 ll->lv_copyID = copyID;
5503 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005504 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005505 abort = set_ref_in_list(ll, copyID, ht_stack);
5506 }
5507 else
5508 {
5509 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005510 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 if (newitem == NULL)
5512 abort = TRUE;
5513 else
5514 {
5515 newitem->list = ll;
5516 newitem->prev = *list_stack;
5517 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005519 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005520 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005521 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005522 else if (tv->v_type == VAR_PARTIAL)
5523 {
5524 partial_T *pt = tv->vval.v_partial;
5525 int i;
5526
5527 /* A partial does not have a copyID, because it cannot contain itself.
5528 */
5529 if (pt != NULL)
5530 {
5531 if (pt->pt_dict != NULL)
5532 {
5533 typval_T dtv;
5534
5535 dtv.v_type = VAR_DICT;
5536 dtv.vval.v_dict = pt->pt_dict;
5537 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5538 }
5539
5540 for (i = 0; i < pt->pt_argc; ++i)
5541 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5542 ht_stack, list_stack);
5543 }
5544 }
5545#ifdef FEAT_JOB_CHANNEL
5546 else if (tv->v_type == VAR_JOB)
5547 {
5548 job_T *job = tv->vval.v_job;
5549 typval_T dtv;
5550
5551 if (job != NULL && job->jv_copyID != copyID)
5552 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005553 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005554 if (job->jv_channel != NULL)
5555 {
5556 dtv.v_type = VAR_CHANNEL;
5557 dtv.vval.v_channel = job->jv_channel;
5558 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5559 }
5560 if (job->jv_exit_partial != NULL)
5561 {
5562 dtv.v_type = VAR_PARTIAL;
5563 dtv.vval.v_partial = job->jv_exit_partial;
5564 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5565 }
5566 }
5567 }
5568 else if (tv->v_type == VAR_CHANNEL)
5569 {
5570 channel_T *ch =tv->vval.v_channel;
5571 int part;
5572 typval_T dtv;
5573 jsonq_T *jq;
5574 cbq_T *cq;
5575
5576 if (ch != NULL && ch->ch_copyID != copyID)
5577 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005578 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005579 for (part = PART_SOCK; part <= PART_IN; ++part)
5580 {
5581 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5582 jq = jq->jq_next)
5583 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5584 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5585 cq = cq->cq_next)
5586 if (cq->cq_partial != NULL)
5587 {
5588 dtv.v_type = VAR_PARTIAL;
5589 dtv.vval.v_partial = cq->cq_partial;
5590 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5591 }
5592 if (ch->ch_part[part].ch_partial != NULL)
5593 {
5594 dtv.v_type = VAR_PARTIAL;
5595 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5596 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5597 }
5598 }
5599 if (ch->ch_partial != NULL)
5600 {
5601 dtv.v_type = VAR_PARTIAL;
5602 dtv.vval.v_partial = ch->ch_partial;
5603 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5604 }
5605 if (ch->ch_close_partial != NULL)
5606 {
5607 dtv.v_type = VAR_PARTIAL;
5608 dtv.vval.v_partial = ch->ch_close_partial;
5609 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5610 }
5611 }
5612 }
5613#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005614 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005615}
5616
Bram Moolenaar17a13432016-01-24 14:22:10 +01005617 static char *
5618get_var_special_name(int nr)
5619{
5620 switch (nr)
5621 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005622 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005623 case VVAL_TRUE: return "v:true";
5624 case VVAL_NONE: return "v:none";
5625 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005626 }
5627 EMSG2(_(e_intern2), "get_var_special_name()");
5628 return "42";
5629}
5630
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 * Return a string with the string representation of a variable.
5633 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005634 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005635 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005636 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5637 * "string()", otherwise does not put quotes around strings, as ":echo"
5638 * displays values.
5639 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5640 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005641 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005643 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005644echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645 typval_T *tv,
5646 char_u **tofree,
5647 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005648 int copyID,
5649 int echo_style,
5650 int restore_copyID,
5651 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005653 static int recurse = 0;
5654 char_u *r = NULL;
5655
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005657 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005658 if (!did_echo_string_emsg)
5659 {
5660 /* Only give this message once for a recursive call to avoid
5661 * flooding the user with errors. And stop iterating over lists
5662 * and dicts. */
5663 did_echo_string_emsg = TRUE;
5664 EMSG(_("E724: variable nested too deep for displaying"));
5665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005666 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005667 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005668 }
5669 ++recurse;
5670
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 switch (tv->v_type)
5672 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005673 case VAR_STRING:
5674 if (echo_style && !dict_val)
5675 {
5676 *tofree = NULL;
5677 r = get_tv_string_buf(tv, numbuf);
5678 }
5679 else
5680 {
5681 *tofree = string_quote(tv->vval.v_string, FALSE);
5682 r = *tofree;
5683 }
5684 break;
5685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005687 if (echo_style)
5688 {
5689 *tofree = NULL;
5690 r = tv->vval.v_string;
5691 }
5692 else
5693 {
5694 *tofree = string_quote(tv->vval.v_string, TRUE);
5695 r = *tofree;
5696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005697 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005698
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005699 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005700 {
5701 partial_T *pt = tv->vval.v_partial;
5702 char_u *fname = string_quote(pt == NULL ? NULL
5703 : pt->pt_name, FALSE);
5704 garray_T ga;
5705 int i;
5706 char_u *tf;
5707
5708 ga_init2(&ga, 1, 100);
5709 ga_concat(&ga, (char_u *)"function(");
5710 if (fname != NULL)
5711 {
5712 ga_concat(&ga, fname);
5713 vim_free(fname);
5714 }
5715 if (pt != NULL && pt->pt_argc > 0)
5716 {
5717 ga_concat(&ga, (char_u *)", [");
5718 for (i = 0; i < pt->pt_argc; ++i)
5719 {
5720 if (i > 0)
5721 ga_concat(&ga, (char_u *)", ");
5722 ga_concat(&ga,
5723 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5724 vim_free(tf);
5725 }
5726 ga_concat(&ga, (char_u *)"]");
5727 }
5728 if (pt != NULL && pt->pt_dict != NULL)
5729 {
5730 typval_T dtv;
5731
5732 ga_concat(&ga, (char_u *)", ");
5733 dtv.v_type = VAR_DICT;
5734 dtv.vval.v_dict = pt->pt_dict;
5735 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5736 vim_free(tf);
5737 }
5738 ga_concat(&ga, (char_u *)")");
5739
5740 *tofree = ga.ga_data;
5741 r = *tofree;
5742 break;
5743 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005744
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005746 if (tv->vval.v_list == NULL)
5747 {
5748 *tofree = NULL;
5749 r = NULL;
5750 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005751 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5752 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005753 {
5754 *tofree = NULL;
5755 r = (char_u *)"[...]";
5756 }
5757 else
5758 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005759 int old_copyID = tv->vval.v_list->lv_copyID;
5760
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005761 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005762 *tofree = list2string(tv, copyID, restore_copyID);
5763 if (restore_copyID)
5764 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005765 r = *tofree;
5766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005767 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005768
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005770 if (tv->vval.v_dict == NULL)
5771 {
5772 *tofree = NULL;
5773 r = NULL;
5774 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005775 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5776 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005777 {
5778 *tofree = NULL;
5779 r = (char_u *)"{...}";
5780 }
5781 else
5782 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005783 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005784 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005785 *tofree = dict2string(tv, copyID, restore_copyID);
5786 if (restore_copyID)
5787 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005788 r = *tofree;
5789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005792 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005793 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005794 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005795 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005796 *tofree = NULL;
5797 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005799
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005800 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005801#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005802 *tofree = NULL;
5803 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5804 r = numbuf;
5805 break;
5806#endif
5807
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005808 case VAR_SPECIAL:
5809 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005810 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005811 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005813
Bram Moolenaar8502c702014-06-17 12:51:16 +02005814 if (--recurse == 0)
5815 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005816 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817}
5818
5819/*
5820 * Return a string with the string representation of a variable.
5821 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5822 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005823 * Does not put quotes around strings, as ":echo" displays values.
5824 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5825 * May return NULL.
5826 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005828echo_string(
5829 typval_T *tv,
5830 char_u **tofree,
5831 char_u *numbuf,
5832 int copyID)
5833{
5834 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5835}
5836
5837/*
5838 * Return a string with the string representation of a variable.
5839 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5840 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005842 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005844 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005845tv2string(
5846 typval_T *tv,
5847 char_u **tofree,
5848 char_u *numbuf,
5849 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005851 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852}
5853
5854/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 * Return string "str" in ' quotes, doubling ' characters.
5856 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005860string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861{
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 char_u *p, *r, *s;
5864
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 len = (function ? 13 : 3);
5866 if (str != NULL)
5867 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005868 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 for (p = str; *p != NUL; mb_ptr_adv(p))
5870 if (*p == '\'')
5871 ++len;
5872 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 s = r = alloc(len);
5874 if (r != NULL)
5875 {
5876 if (function)
5877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 r += 10;
5880 }
5881 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 if (str != NULL)
5884 for (p = str; *p != NUL; )
5885 {
5886 if (*p == '\'')
5887 *r++ = '\'';
5888 MB_COPY_CHAR(p, r);
5889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 if (function)
5892 *r++ = ')';
5893 *r++ = NUL;
5894 }
5895 return s;
5896}
5897
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005898#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005899/*
5900 * Convert the string "text" to a floating point number.
5901 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5902 * this always uses a decimal point.
5903 * Returns the length of the text that was consumed.
5904 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005906string2float(
5907 char_u *text,
5908 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005909{
5910 char *s = (char *)text;
5911 float_T f;
5912
5913 f = strtod(s, &s);
5914 *value = f;
5915 return (int)((char_u *)s - text);
5916}
5917#endif
5918
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 * Get the value of an environment variable.
5921 * "arg" is pointing to the '$'. It is advanced to after the name.
5922 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005923 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 */
5925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005926get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
5928 char_u *string = NULL;
5929 int len;
5930 int cc;
5931 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005932 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
5934 ++*arg;
5935 name = *arg;
5936 len = get_env_len(arg);
5937 if (evaluate)
5938 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005939 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005940 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005941
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005942 cc = name[len];
5943 name[len] = NUL;
5944 /* first try vim_getenv(), fast for normal environment vars */
5945 string = vim_getenv(name, &mustfree);
5946 if (string != NULL && *string != NUL)
5947 {
5948 if (!mustfree)
5949 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005951 else
5952 {
5953 if (mustfree)
5954 vim_free(string);
5955
5956 /* next try expanding things like $VIM and ${HOME} */
5957 string = expand_env_save(name - 1);
5958 if (string != NULL && *string == '$')
5959 {
5960 vim_free(string);
5961 string = NULL;
5962 }
5963 }
5964 name[len] = cc;
5965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005966 rettv->v_type = VAR_STRING;
5967 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
5969
5970 return OK;
5971}
5972
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005973
5974
5975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005977 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005980var2fpos(
5981 typval_T *varp,
5982 int dollar_lnum, /* TRUE when $ is last line */
5983 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005985 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005987 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988
Bram Moolenaara5525202006-03-02 22:52:09 +00005989 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005990 if (varp->v_type == VAR_LIST)
5991 {
5992 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005993 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005994 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005995 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005996
5997 l = varp->vval.v_list;
5998 if (l == NULL)
5999 return NULL;
6000
6001 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006002 pos.lnum = list_find_nr(l, 0L, &error);
6003 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006004 return NULL; /* invalid line number */
6005
6006 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006007 pos.col = list_find_nr(l, 1L, &error);
6008 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006009 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006010 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006011
6012 /* We accept "$" for the column number: last column. */
6013 li = list_find(l, 1L);
6014 if (li != NULL && li->li_tv.v_type == VAR_STRING
6015 && li->li_tv.vval.v_string != NULL
6016 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6017 pos.col = len + 1;
6018
Bram Moolenaara5525202006-03-02 22:52:09 +00006019 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006020 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006021 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006022 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006023
Bram Moolenaara5525202006-03-02 22:52:09 +00006024#ifdef FEAT_VIRTUALEDIT
6025 /* Get the virtual offset. Defaults to zero. */
6026 pos.coladd = list_find_nr(l, 2L, &error);
6027 if (error)
6028 pos.coladd = 0;
6029#endif
6030
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006031 return &pos;
6032 }
6033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006034 name = get_tv_string_chk(varp);
6035 if (name == NULL)
6036 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006037 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006039 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6040 {
6041 if (VIsual_active)
6042 return &VIsual;
6043 return &curwin->w_cursor;
6044 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006045 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006047 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6049 return NULL;
6050 return pp;
6051 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006052
6053#ifdef FEAT_VIRTUALEDIT
6054 pos.coladd = 0;
6055#endif
6056
Bram Moolenaar477933c2007-07-17 14:32:23 +00006057 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006058 {
6059 pos.col = 0;
6060 if (name[1] == '0') /* "w0": first visible line */
6061 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006063 pos.lnum = curwin->w_topline;
6064 return &pos;
6065 }
6066 else if (name[1] == '$') /* "w$": last visible line */
6067 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006068 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006069 pos.lnum = curwin->w_botline - 1;
6070 return &pos;
6071 }
6072 }
6073 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006075 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 pos.lnum = curbuf->b_ml.ml_line_count;
6078 pos.col = 0;
6079 }
6080 else
6081 {
6082 pos.lnum = curwin->w_cursor.lnum;
6083 pos.col = (colnr_T)STRLEN(ml_get_curline());
6084 }
6085 return &pos;
6086 }
6087 return NULL;
6088}
6089
6090/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006091 * Convert list in "arg" into a position and optional file number.
6092 * When "fnump" is NULL there is no file number, only 3 items.
6093 * Note that the column is passed on as-is, the caller may want to decrement
6094 * it to use 1 for the first column.
6095 * Return FAIL when conversion is not possible, doesn't check the position for
6096 * validity.
6097 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099list2fpos(
6100 typval_T *arg,
6101 pos_T *posp,
6102 int *fnump,
6103 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006104{
6105 list_T *l = arg->vval.v_list;
6106 long i = 0;
6107 long n;
6108
Bram Moolenaar493c1782014-05-28 14:34:46 +02006109 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6110 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006111 if (arg->v_type != VAR_LIST
6112 || l == NULL
6113 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006114 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006115 return FAIL;
6116
6117 if (fnump != NULL)
6118 {
6119 n = list_find_nr(l, i++, NULL); /* fnum */
6120 if (n < 0)
6121 return FAIL;
6122 if (n == 0)
6123 n = curbuf->b_fnum; /* current buffer */
6124 *fnump = n;
6125 }
6126
6127 n = list_find_nr(l, i++, NULL); /* lnum */
6128 if (n < 0)
6129 return FAIL;
6130 posp->lnum = n;
6131
6132 n = list_find_nr(l, i++, NULL); /* col */
6133 if (n < 0)
6134 return FAIL;
6135 posp->col = n;
6136
6137#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006138 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006139 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006140 posp->coladd = 0;
6141 else
6142 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006143#endif
6144
Bram Moolenaar493c1782014-05-28 14:34:46 +02006145 if (curswantp != NULL)
6146 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6147
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006148 return OK;
6149}
6150
6151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 * Get the length of an environment variable name.
6153 * Advance "arg" to the first character after the name.
6154 * Return 0 for error.
6155 */
6156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 char_u *p;
6160 int len;
6161
6162 for (p = *arg; vim_isIDc(*p); ++p)
6163 ;
6164 if (p == *arg) /* no name found */
6165 return 0;
6166
6167 len = (int)(p - *arg);
6168 *arg = p;
6169 return len;
6170}
6171
6172/*
6173 * Get the length of the name of a function or internal variable.
6174 * "arg" is advanced to the first non-white character after the name.
6175 * Return 0 if something is wrong.
6176 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 char_u *p;
6181 int len;
6182
6183 /* Find the end of the name. */
6184 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006185 {
6186 if (*p == ':')
6187 {
6188 /* "s:" is start of "s:var", but "n:" is not and can be used in
6189 * slice "[n:]". Also "xx:" is not a namespace. */
6190 len = (int)(p - *arg);
6191 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6192 || len > 1)
6193 break;
6194 }
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (p == *arg) /* no name found */
6197 return 0;
6198
6199 len = (int)(p - *arg);
6200 *arg = skipwhite(p);
6201
6202 return len;
6203}
6204
6205/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006206 * Get the length of the name of a variable or function.
6207 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006209 * Return -1 if curly braces expansion failed.
6210 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 * If the name contains 'magic' {}'s, expand them and return the
6212 * expanded name in an allocated string via 'alias' - caller must free.
6213 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215get_name_len(
6216 char_u **arg,
6217 char_u **alias,
6218 int evaluate,
6219 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 char_u *p;
6223 char_u *expr_start;
6224 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 *alias = NULL; /* default to no alias */
6227
6228 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6229 && (*arg)[2] == (int)KE_SNR)
6230 {
6231 /* hard coded <SNR>, already translated */
6232 *arg += 3;
6233 return get_id_len(arg) + 3;
6234 }
6235 len = eval_fname_script(*arg);
6236 if (len > 0)
6237 {
6238 /* literal "<SID>", "s:" or "<SNR>" */
6239 *arg += len;
6240 }
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006243 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006245 p = find_name_end(*arg, &expr_start, &expr_end,
6246 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (expr_start != NULL)
6248 {
6249 char_u *temp_string;
6250
6251 if (!evaluate)
6252 {
6253 len += (int)(p - *arg);
6254 *arg = skipwhite(p);
6255 return len;
6256 }
6257
6258 /*
6259 * Include any <SID> etc in the expanded string:
6260 * Thus the -len here.
6261 */
6262 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6263 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006264 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 *alias = temp_string;
6266 *arg = skipwhite(p);
6267 return (int)STRLEN(temp_string);
6268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006271 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 EMSG2(_(e_invexpr2), *arg);
6273
6274 return len;
6275}
6276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006277/*
6278 * Find the end of a variable or function name, taking care of magic braces.
6279 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6280 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006281 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006282 * Return a pointer to just after the name. Equal to "arg" if there is no
6283 * valid name.
6284 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006285 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006286find_name_end(
6287 char_u *arg,
6288 char_u **expr_start,
6289 char_u **expr_end,
6290 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006292 int mb_nest = 0;
6293 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006295 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006297 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006299 *expr_start = NULL;
6300 *expr_end = NULL;
6301 }
6302
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006303 /* Quick check for valid starting character. */
6304 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6305 return arg;
6306
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006307 for (p = arg; *p != NUL
6308 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006309 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006310 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006311 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006312 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006313 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006314 if (*p == '\'')
6315 {
6316 /* skip over 'string' to avoid counting [ and ] inside it. */
6317 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6318 ;
6319 if (*p == NUL)
6320 break;
6321 }
6322 else if (*p == '"')
6323 {
6324 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6325 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6326 if (*p == '\\' && p[1] != NUL)
6327 ++p;
6328 if (*p == NUL)
6329 break;
6330 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006331 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6332 {
6333 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006334 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006335 len = (int)(p - arg);
6336 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006337 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006338 break;
6339 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006341 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006343 if (*p == '[')
6344 ++br_nest;
6345 else if (*p == ']')
6346 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006349 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006351 if (*p == '{')
6352 {
6353 mb_nest++;
6354 if (expr_start != NULL && *expr_start == NULL)
6355 *expr_start = p;
6356 }
6357 else if (*p == '}')
6358 {
6359 mb_nest--;
6360 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6361 *expr_end = p;
6362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
6365
6366 return p;
6367}
6368
6369/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006370 * Expands out the 'magic' {}'s in a variable/function name.
6371 * Note that this can call itself recursively, to deal with
6372 * constructs like foo{bar}{baz}{bam}
6373 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6374 * "in_start" ^
6375 * "expr_start" ^
6376 * "expr_end" ^
6377 * "in_end" ^
6378 *
6379 * Returns a new allocated string, which the caller must free.
6380 * Returns NULL for failure.
6381 */
6382 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383make_expanded_name(
6384 char_u *in_start,
6385 char_u *expr_start,
6386 char_u *expr_end,
6387 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006388{
6389 char_u c1;
6390 char_u *retval = NULL;
6391 char_u *temp_result;
6392 char_u *nextcmd = NULL;
6393
6394 if (expr_end == NULL || in_end == NULL)
6395 return NULL;
6396 *expr_start = NUL;
6397 *expr_end = NUL;
6398 c1 = *in_end;
6399 *in_end = NUL;
6400
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006401 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006402 if (temp_result != NULL && nextcmd == NULL)
6403 {
6404 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6405 + (in_end - expr_end) + 1));
6406 if (retval != NULL)
6407 {
6408 STRCPY(retval, in_start);
6409 STRCAT(retval, temp_result);
6410 STRCAT(retval, expr_end + 1);
6411 }
6412 }
6413 vim_free(temp_result);
6414
6415 *in_end = c1; /* put char back for error messages */
6416 *expr_start = '{';
6417 *expr_end = '}';
6418
6419 if (retval != NULL)
6420 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006421 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006422 if (expr_start != NULL)
6423 {
6424 /* Further expansion! */
6425 temp_result = make_expanded_name(retval, expr_start,
6426 expr_end, temp_result);
6427 vim_free(retval);
6428 retval = temp_result;
6429 }
6430 }
6431
6432 return retval;
6433}
6434
6435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006442 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6443}
6444
6445/*
6446 * Return TRUE if character "c" can be used as the first character in a
6447 * variable or function name (excluding '{' and '}').
6448 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006451{
6452 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453}
6454
6455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 * Set number v: variable to "val".
6457 */
6458 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006459set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462}
6463
6464/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006465 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006467 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006468get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471}
6472
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006473/*
6474 * Get string v: variable value. Uses a static buffer, can only be used once.
6475 */
6476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006477get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006478{
6479 return get_tv_string(&vimvars[idx].vv_tv);
6480}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006481
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006483 * Get List v: variable value. Caller must take care of reference count when
6484 * needed.
6485 */
6486 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006487get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006488{
6489 return vimvars[idx].vv_list;
6490}
6491
6492/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006493 * Set v:char to character "c".
6494 */
6495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006497{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006498 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006499
6500#ifdef FEAT_MBYTE
6501 if (has_mbyte)
6502 buf[(*mb_char2bytes)(c, buf)] = NUL;
6503 else
6504#endif
6505 {
6506 buf[0] = c;
6507 buf[1] = NUL;
6508 }
6509 set_vim_var_string(VV_CHAR, buf, -1);
6510}
6511
6512/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006513 * Set v:count to "count" and v:count1 to "count1".
6514 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
6516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517set_vcount(
6518 long count,
6519 long count1,
6520 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006522 if (set_prevcount)
6523 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 vimvars[VV_COUNT].vv_nr = count;
6525 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
6528/*
6529 * Set string v: variable to a copy of "val".
6530 */
6531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532set_vim_var_string(
6533 int idx,
6534 char_u *val,
6535 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
Bram Moolenaara542c682016-01-31 16:28:04 +01006537 clear_tv(&vimvars[idx].vv_di.di_tv);
6538 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006540 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006542 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545}
6546
6547/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006548 * Set List v: variable to "val".
6549 */
6550 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006551set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006552{
Bram Moolenaara542c682016-01-31 16:28:04 +01006553 clear_tv(&vimvars[idx].vv_di.di_tv);
6554 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006555 vimvars[idx].vv_list = val;
6556 if (val != NULL)
6557 ++val->lv_refcount;
6558}
6559
6560/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006561 * Set Dictionary v: variable to "val".
6562 */
6563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006564set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006565{
6566 int todo;
6567 hashitem_T *hi;
6568
Bram Moolenaara542c682016-01-31 16:28:04 +01006569 clear_tv(&vimvars[idx].vv_di.di_tv);
6570 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006571 vimvars[idx].vv_dict = val;
6572 if (val != NULL)
6573 {
6574 ++val->dv_refcount;
6575
6576 /* Set readonly */
6577 todo = (int)val->dv_hashtab.ht_used;
6578 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6579 {
6580 if (HASHITEM_EMPTY(hi))
6581 continue;
6582 --todo;
6583 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6584 }
6585 }
6586}
6587
6588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 * Set v:register if needed.
6590 */
6591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 char_u regname;
6595
6596 if (c == 0 || c == ' ')
6597 regname = '"';
6598 else
6599 regname = c;
6600 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006601 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 set_vim_var_string(VV_REG, &regname, 1);
6603}
6604
6605/*
6606 * Get or set v:exception. If "oldval" == NULL, return the current value.
6607 * Otherwise, restore the value to "oldval" and return NULL.
6608 * Must always be called in pairs to save and restore v:exception! Does not
6609 * take care of memory allocations.
6610 */
6611 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
Bram Moolenaare9a41262005-01-15 22:18:47 +00006617 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 return NULL;
6619}
6620
6621/*
6622 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6623 * Otherwise, restore the value to "oldval" and return NULL.
6624 * Must always be called in pairs to save and restore v:throwpoint! Does not
6625 * take care of memory allocations.
6626 */
6627 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006628v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
Bram Moolenaare9a41262005-01-15 22:18:47 +00006633 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 return NULL;
6635}
6636
6637#if defined(FEAT_AUTOCMD) || defined(PROTO)
6638/*
6639 * Set v:cmdarg.
6640 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6641 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6642 * Must always be called in pairs!
6643 */
6644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 char_u *oldval;
6648 char_u *newval;
6649 unsigned len;
6650
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006652 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006654 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006656 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 }
6658
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006659 if (eap->force_bin == FORCE_BIN)
6660 len = 6;
6661 else if (eap->force_bin == FORCE_NOBIN)
6662 len = 8;
6663 else
6664 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006665
6666 if (eap->read_edit)
6667 len += 7;
6668
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006669 if (eap->force_ff != 0)
6670 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6671# ifdef FEAT_MBYTE
6672 if (eap->force_enc != 0)
6673 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006674 if (eap->bad_char != 0)
6675 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006676# endif
6677
6678 newval = alloc(len + 1);
6679 if (newval == NULL)
6680 return NULL;
6681
6682 if (eap->force_bin == FORCE_BIN)
6683 sprintf((char *)newval, " ++bin");
6684 else if (eap->force_bin == FORCE_NOBIN)
6685 sprintf((char *)newval, " ++nobin");
6686 else
6687 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006688
6689 if (eap->read_edit)
6690 STRCAT(newval, " ++edit");
6691
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006692 if (eap->force_ff != 0)
6693 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6694 eap->cmd + eap->force_ff);
6695# ifdef FEAT_MBYTE
6696 if (eap->force_enc != 0)
6697 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6698 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006699 if (eap->bad_char == BAD_KEEP)
6700 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6701 else if (eap->bad_char == BAD_DROP)
6702 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6703 else if (eap->bad_char != 0)
6704 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006705# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006707 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708}
6709#endif
6710
6711/*
6712 * Get the value of internal variable "name".
6713 * Return OK or FAIL.
6714 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006716get_var_tv(
6717 char_u *name,
6718 int len, /* length of "name" */
6719 typval_T *rettv, /* NULL when only checking existence */
6720 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6721 int verbose, /* may give error message */
6722 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 typval_T *tv = NULL;
6726 typval_T atv;
6727 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 /* truncate the name, so that we can use strcmp() */
6731 cc = name[len];
6732 name[len] = NUL;
6733
6734 /*
6735 * Check for "b:changedtick".
6736 */
6737 if (STRCMP(name, "b:changedtick") == 0)
6738 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 atv.v_type = VAR_NUMBER;
6740 atv.vval.v_number = curbuf->b_changedtick;
6741 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743
6744 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 * Check for user-defined variables.
6746 */
6747 else
6748 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01006749 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006751 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006752 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006753 if (dip != NULL)
6754 *dip = v;
6755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 }
6757
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006760 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006761 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 ret = FAIL;
6763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006764 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006765 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 name[len] = cc;
6768
6769 return ret;
6770}
6771
6772/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006773 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6774 * Also handle function call with Funcref variable: func(expr)
6775 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6776 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006778handle_subscript(
6779 char_u **arg,
6780 typval_T *rettv,
6781 int evaluate, /* do more than finding the end */
6782 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006783{
6784 int ret = OK;
6785 dict_T *selfdict = NULL;
6786 char_u *s;
6787 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006789
6790 while (ret == OK
6791 && (**arg == '['
6792 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006793 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6794 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006795 && !vim_iswhite(*(*arg - 1)))
6796 {
6797 if (**arg == '(')
6798 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006799 partial_T *pt = NULL;
6800
Bram Moolenaard9fba312005-06-26 22:34:35 +00006801 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006802 if (evaluate)
6803 {
6804 functv = *rettv;
6805 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006806
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006807 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006808 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006809 {
6810 pt = functv.vval.v_partial;
6811 s = pt->pt_name;
6812 }
6813 else
6814 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006815 }
6816 else
6817 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006818 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006819 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006820 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006821
6822 /* Clear the funcref afterwards, so that deleting it while
6823 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006824 if (evaluate)
6825 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006826
6827 /* Stop the expression evaluation when immediately aborting on
6828 * error, or when an interrupt occurred or an exception was thrown
6829 * but not caught. */
6830 if (aborting())
6831 {
6832 if (ret == OK)
6833 clear_tv(rettv);
6834 ret = FAIL;
6835 }
6836 dict_unref(selfdict);
6837 selfdict = NULL;
6838 }
6839 else /* **arg == '[' || **arg == '.' */
6840 {
6841 dict_unref(selfdict);
6842 if (rettv->v_type == VAR_DICT)
6843 {
6844 selfdict = rettv->vval.v_dict;
6845 if (selfdict != NULL)
6846 ++selfdict->dv_refcount;
6847 }
6848 else
6849 selfdict = NULL;
6850 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6851 {
6852 clear_tv(rettv);
6853 ret = FAIL;
6854 }
6855 }
6856 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006857
Bram Moolenaar1d429612016-05-24 15:44:17 +02006858 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6859 * Don't do this when "Func" is already a partial that was bound
6860 * explicitly (pt_auto is FALSE). */
6861 if (selfdict != NULL
6862 && (rettv->v_type == VAR_FUNC
6863 || (rettv->v_type == VAR_PARTIAL
6864 && (rettv->vval.v_partial->pt_auto
6865 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006866 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006867
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868 dict_unref(selfdict);
6869 return ret;
6870}
6871
6872/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006873 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006874 * value).
6875 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006876 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006877alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006878{
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006880}
6881
6882/*
6883 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 * The string "s" must have been allocated, it is consumed.
6885 * Return NULL for out of memory, the variable otherwise.
6886 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006888alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006892 rettv = alloc_tv();
6893 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 rettv->v_type = VAR_STRING;
6896 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
6898 else
6899 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006900 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901}
6902
6903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006904 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006907free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908{
6909 if (varp != NULL)
6910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006911 switch (varp->v_type)
6912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006913 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006914 func_unref(varp->vval.v_string);
6915 /*FALLTHROUGH*/
6916 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006917 vim_free(varp->vval.v_string);
6918 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006919 case VAR_PARTIAL:
6920 partial_unref(varp->vval.v_partial);
6921 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006922 case VAR_LIST:
6923 list_unref(varp->vval.v_list);
6924 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006925 case VAR_DICT:
6926 dict_unref(varp->vval.v_dict);
6927 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006928 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006929#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006930 job_unref(varp->vval.v_job);
6931 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006932#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006933 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006934#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006935 channel_unref(varp->vval.v_channel);
6936 break;
6937#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006938 case VAR_NUMBER:
6939 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006940 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006941 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006942 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 vim_free(varp);
6945 }
6946}
6947
6948/*
6949 * Free the memory for a variable value and set the value to NULL or 0.
6950 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006952clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 if (varp != NULL)
6955 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006956 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006958 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006959 func_unref(varp->vval.v_string);
6960 /*FALLTHROUGH*/
6961 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006962 vim_free(varp->vval.v_string);
6963 varp->vval.v_string = NULL;
6964 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006965 case VAR_PARTIAL:
6966 partial_unref(varp->vval.v_partial);
6967 varp->vval.v_partial = NULL;
6968 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006969 case VAR_LIST:
6970 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006972 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006973 case VAR_DICT:
6974 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006977 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006978 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006979 varp->vval.v_number = 0;
6980 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006981 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006982#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006983 varp->vval.v_float = 0.0;
6984 break;
6985#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006986 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006987#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006988 job_unref(varp->vval.v_job);
6989 varp->vval.v_job = NULL;
6990#endif
6991 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006992 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006993#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006994 channel_unref(varp->vval.v_channel);
6995 varp->vval.v_channel = NULL;
6996#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 case VAR_UNKNOWN:
6998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002}
7003
7004/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007005 * Set the value of a variable to NULL without freeing items.
7006 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007008init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009{
7010 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007012}
7013
7014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 * Get the number value of a variable.
7016 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007017 * For incompatible types, return 0.
7018 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7019 * caller of incompatible types: it sets *denote to TRUE if "denote"
7020 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007022 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007025 int error = FALSE;
7026
7027 return get_tv_number_chk(varp, &error); /* return 0L on error */
7028}
7029
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007030 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007031get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007032{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007033 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007038 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007039 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007040#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007041 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007042 break;
7043#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007044 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007045 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007046 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007047 break;
7048 case VAR_STRING:
7049 if (varp->vval.v_string != NULL)
7050 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007051 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007052 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007053 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007054 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007055 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007056 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007057 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007058 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007059 case VAR_SPECIAL:
7060 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7061 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007062 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007063#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007064 EMSG(_("E910: Using a Job as a Number"));
7065 break;
7066#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007067 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007068#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007069 EMSG(_("E913: Using a Channel as a Number"));
7070 break;
7071#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007072 case VAR_UNKNOWN:
7073 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007076 if (denote == NULL) /* useful for values that must be unsigned */
7077 n = -1;
7078 else
7079 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007080 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081}
7082
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007083#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007085get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007086{
7087 switch (varp->v_type)
7088 {
7089 case VAR_NUMBER:
7090 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007091 case VAR_FLOAT:
7092 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007093 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007094 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007095 EMSG(_("E891: Using a Funcref as a Float"));
7096 break;
7097 case VAR_STRING:
7098 EMSG(_("E892: Using a String as a Float"));
7099 break;
7100 case VAR_LIST:
7101 EMSG(_("E893: Using a List as a Float"));
7102 break;
7103 case VAR_DICT:
7104 EMSG(_("E894: Using a Dictionary as a Float"));
7105 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007106 case VAR_SPECIAL:
7107 EMSG(_("E907: Using a special value as a Float"));
7108 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007109 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007110# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007111 EMSG(_("E911: Using a Job as a Float"));
7112 break;
7113# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007114 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007115# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007116 EMSG(_("E914: Using a Channel as a Float"));
7117 break;
7118# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007119 case VAR_UNKNOWN:
7120 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007121 break;
7122 }
7123 return 0;
7124}
7125#endif
7126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 * Get the string value of a variable.
7129 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007130 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7131 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 * If the String variable has never been set, return an empty string.
7133 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007134 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7135 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007137 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 static char_u mybuf[NUMBUFLEN];
7141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007142 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143}
7144
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007148 char_u *res = get_tv_string_buf_chk(varp, buf);
7149
7150 return res != NULL ? res : (char_u *)"";
7151}
7152
Bram Moolenaar7d647822014-04-05 21:28:56 +02007153/*
7154 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7155 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158{
7159 static char_u mybuf[NUMBUFLEN];
7160
7161 return get_tv_string_buf_chk(varp, mybuf);
7162}
7163
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007164 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007166{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007167 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007170 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7171 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 return buf;
7173 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007174 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007175 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007176 break;
7177 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007178 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 break;
7180 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007181 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007183 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007184#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007185 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007186 break;
7187#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007188 case VAR_STRING:
7189 if (varp->vval.v_string != NULL)
7190 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007191 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007192 case VAR_SPECIAL:
7193 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7194 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007195 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007196#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007197 {
7198 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007199 char *status;
7200
7201 if (job == NULL)
7202 return (char_u *)"no process";
7203 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007204 : job->jv_status == JOB_ENDED ? "dead"
7205 : "run";
7206# ifdef UNIX
7207 vim_snprintf((char *)buf, NUMBUFLEN,
7208 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007209# elif defined(WIN32)
7210 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007211 "process %ld %s",
7212 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007213 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007214# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007215 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007216 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7217# endif
7218 return buf;
7219 }
7220#endif
7221 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007222 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007223#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007224 {
7225 channel_T *channel = varp->vval.v_channel;
7226 char *status = channel_status(channel);
7227
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007228 if (channel == NULL)
7229 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7230 else
7231 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007232 "channel %d %s", channel->ch_id, status);
7233 return buf;
7234 }
7235#endif
7236 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007237 case VAR_UNKNOWN:
7238 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007241 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242}
7243
7244/*
7245 * Find variable "name" in the list of variables.
7246 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007247 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007248 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007251 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaara7043832005-01-21 11:56:39 +00007257 ht = find_var_ht(name, &varname);
7258 if (htp != NULL)
7259 *htp = ht;
7260 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007262 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
7264
7265/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007266 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007267 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270find_var_in_ht(
7271 hashtab_T *ht,
7272 int htname,
7273 char_u *varname,
7274 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007275{
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 hashitem_T *hi;
7277
7278 if (*varname == NUL)
7279 {
7280 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007281 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007283 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 case 'g': return &globvars_var;
7285 case 'v': return &vimvars_var;
7286 case 'b': return &curbuf->b_bufvar;
7287 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007288#ifdef FEAT_WINDOWS
7289 case 't': return &curtab->tp_winvar;
7290#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007291 case 'l': return get_funccal_local_var();
7292 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 }
7294 return NULL;
7295 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007296
7297 hi = hash_find(ht, varname);
7298 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007299 {
7300 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007301 * worked find the variable again. Don't auto-load a script if it was
7302 * loaded already, otherwise it would be loaded every time when
7303 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007304 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007305 {
7306 /* Note: script_autoload() may make "hi" invalid. It must either
7307 * be obtained again or not used. */
7308 if (!script_autoload(varname, FALSE) || aborting())
7309 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007310 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007311 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007312 if (HASHITEM_EMPTY(hi))
7313 return NULL;
7314 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007316}
7317
7318/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007320 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007321 * Set "varname" to the start of name without ':'.
7322 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007324find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007326 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007327 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007328
Bram Moolenaar73627d02015-08-11 15:46:09 +02007329 if (name[0] == NUL)
7330 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (name[1] != ':')
7332 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007333 /* The name must not start with a colon or #. */
7334 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 return NULL;
7336 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007337
7338 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007339 hi = hash_find(&compat_hashtab, name);
7340 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007341 return &compat_hashtab;
7342
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007343 ht = get_funccal_local_ht();
7344 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007346 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 }
7348 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007349 if (*name == 'g') /* global variable */
7350 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007351 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7352 */
7353 if (vim_strchr(name + 2, ':') != NULL
7354 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007355 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007357 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007359 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007360#ifdef FEAT_WINDOWS
7361 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007362 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007363#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 if (*name == 'v') /* v: variable */
7365 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007366 if (*name == 'a') /* a: function argument */
7367 return get_funccal_args_ht();
7368 if (*name == 'l') /* l: local function variable */
7369 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (*name == 's' /* script variable */
7371 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7372 return &SCRIPT_VARS(current_SID);
7373 return NULL;
7374}
7375
7376/*
7377 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007378 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 * Returns NULL when it doesn't exist.
7380 */
7381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007382get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007386 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 if (v == NULL)
7388 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390}
7391
7392/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 * sourcing this script and when executing functions defined in the script.
7395 */
7396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007397new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaara7043832005-01-21 11:56:39 +00007399 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 hashtab_T *ht;
7401 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007402
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7404 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007405 /* Re-allocating ga_data means that an ht_array pointing to
7406 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007408 for (i = 1; i <= ga_scripts.ga_len; ++i)
7409 {
7410 ht = &SCRIPT_VARS(i);
7411 if (ht->ht_mask == HT_INIT_SIZE - 1)
7412 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007413 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007415 }
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 while (ga_scripts.ga_len < id)
7418 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007419 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007420 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007421 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424 }
7425}
7426
7427/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7429 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 */
7431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007432init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007435 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007436 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007437 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007438 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 dict_var->di_tv.vval.v_dict = dict;
7440 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007441 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7443 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
7446/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007447 * Unreference a dictionary initialized by init_var_dict().
7448 */
7449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007451{
7452 /* Now the dict needs to be freed if no one else is using it, go back to
7453 * normal reference counting. */
7454 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7455 dict_unref(dict);
7456}
7457
7458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 * Frees all allocated variables and the value they contain.
7461 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 */
7463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007464vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007465{
7466 vars_clear_ext(ht, TRUE);
7467}
7468
7469/*
7470 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7471 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
Bram Moolenaara7043832005-01-21 11:56:39 +00007475 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 hashitem_T *hi;
7477 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007480 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007481 for (hi = ht->ht_array; todo > 0; ++hi)
7482 {
7483 if (!HASHITEM_EMPTY(hi))
7484 {
7485 --todo;
7486
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007488 * ht_array might change then. hash_clear() takes care of it
7489 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 v = HI2DI(hi);
7491 if (free_val)
7492 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007493 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007495 }
7496 }
7497 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007498 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499}
7500
Bram Moolenaara7043832005-01-21 11:56:39 +00007501/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 * Delete a variable from hashtab "ht" at item "hi".
7503 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007506delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007509
7510 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 clear_tv(&di->di_tv);
7512 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
7515/*
7516 * List the value of one internal variable.
7517 */
7518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007519list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007521 char_u *tofree;
7522 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007523 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007525 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007527 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007528 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529}
7530
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532list_one_var_a(
7533 char_u *prefix,
7534 char_u *name,
7535 int type,
7536 char_u *string,
7537 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
Bram Moolenaar31859182007-08-14 20:41:13 +00007539 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7540 msg_start();
7541 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (name != NULL) /* "a:" vars don't have a name stored */
7543 msg_puts(name);
7544 msg_putchar(' ');
7545 msg_advance(22);
7546 if (type == VAR_NUMBER)
7547 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007548 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 msg_putchar('*');
7550 else if (type == VAR_LIST)
7551 {
7552 msg_putchar('[');
7553 if (*string == '[')
7554 ++string;
7555 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 else if (type == VAR_DICT)
7557 {
7558 msg_putchar('{');
7559 if (*string == '{')
7560 ++string;
7561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 else
7563 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007567 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007569 if (*first)
7570 {
7571 msg_clr_eos();
7572 *first = FALSE;
7573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574}
7575
7576/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 * If the variable already exists, the value is updated.
7579 * Otherwise the variable is created.
7580 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007582set_var(
7583 char_u *name,
7584 typval_T *tv,
7585 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007591 ht = find_var_ht(name, &varname);
7592 if (ht == NULL || *varname == NUL)
7593 {
7594 EMSG2(_(e_illvar), name);
7595 return;
7596 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007597 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007598
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007599 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7600 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007601 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007606 if (var_check_ro(v->di_flags, name, FALSE)
7607 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007609
7610 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007611 * Handle setting internal v: variables separately where needed to
7612 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007613 */
7614 if (ht == &vimvarht)
7615 {
7616 if (v->di_tv.v_type == VAR_STRING)
7617 {
7618 vim_free(v->di_tv.vval.v_string);
7619 if (copy || tv->v_type != VAR_STRING)
7620 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7621 else
7622 {
7623 /* Take over the string to avoid an extra alloc/free. */
7624 v->di_tv.vval.v_string = tv->vval.v_string;
7625 tv->vval.v_string = NULL;
7626 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007627 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007629 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007632 if (STRCMP(varname, "searchforward") == 0)
7633 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007634#ifdef FEAT_SEARCH_EXTRA
7635 else if (STRCMP(varname, "hlsearch") == 0)
7636 {
7637 no_hlsearch = !v->di_tv.vval.v_number;
7638 redraw_all_later(SOME_VALID);
7639 }
7640#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007641 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007642 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007643 else if (v->di_tv.v_type != tv->v_type)
7644 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 }
7646
7647 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649 else /* add a new variable */
7650 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007651 /* Can't add "v:" variable. */
7652 if (ht == &vimvarht)
7653 {
7654 EMSG2(_(e_illvar), name);
7655 return;
7656 }
7657
Bram Moolenaar92124a32005-06-17 22:03:40 +00007658 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007659 if (!valid_varname(varname))
7660 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007661
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007662 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7663 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007664 if (v == NULL)
7665 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007669 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 return;
7671 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007672 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007677 else
7678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007680 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683}
7684
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007685/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007686 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 * Also give an error message.
7688 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007689 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007690var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007691{
7692 if (flags & DI_FLAGS_RO)
7693 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007694 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 return TRUE;
7696 }
7697 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7698 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007699 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 return TRUE;
7701 }
7702 return FALSE;
7703}
7704
7705/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007706 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7707 * Also give an error message.
7708 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007710var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007711{
7712 if (flags & DI_FLAGS_FIX)
7713 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007714 EMSG2(_("E795: Cannot delete variable %s"),
7715 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007716 return TRUE;
7717 }
7718 return FALSE;
7719}
7720
7721/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007722 * Check if a funcref is assigned to a valid variable name.
7723 * Return TRUE and give an error if not.
7724 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007726var_check_func_name(
7727 char_u *name, /* points to start of variable name */
7728 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007729{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007730 /* Allow for w: b: s: and t:. */
7731 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007732 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7733 ? name[2] : name[0]))
7734 {
7735 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7736 name);
7737 return TRUE;
7738 }
7739 /* Don't allow hiding a function. When "v" is not NULL we might be
7740 * assigning another function to the same var, the type is checked
7741 * below. */
7742 if (new_var && function_exists(name))
7743 {
7744 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7745 name);
7746 return TRUE;
7747 }
7748 return FALSE;
7749}
7750
7751/*
7752 * Check if a variable name is valid.
7753 * Return FALSE and give an error if not.
7754 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007756valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007757{
7758 char_u *p;
7759
7760 for (p = varname; *p != NUL; ++p)
7761 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7762 && *p != AUTOLOAD_CHAR)
7763 {
7764 EMSG2(_(e_illvar), varname);
7765 return FALSE;
7766 }
7767 return TRUE;
7768}
7769
7770/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007771 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007772 * Also give an error message, using "name" or _("name") when use_gettext is
7773 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007774 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007777{
7778 if (lock & VAR_LOCKED)
7779 {
7780 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007781 name == NULL ? (char_u *)_("Unknown")
7782 : use_gettext ? (char_u *)_(name)
7783 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007784 return TRUE;
7785 }
7786 if (lock & VAR_FIXED)
7787 {
7788 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007789 name == NULL ? (char_u *)_("Unknown")
7790 : use_gettext ? (char_u *)_(name)
7791 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007792 return TRUE;
7793 }
7794 return FALSE;
7795}
7796
7797/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007801 * It is OK for "from" and "to" to point to the same item. This is used to
7802 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007808 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 switch (from->v_type)
7810 {
7811 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007812 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 to->vval.v_number = from->vval.v_number;
7814 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007817 to->vval.v_float = from->vval.v_float;
7818 break;
7819#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007820 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007821#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007822 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007823 if (to->vval.v_job != NULL)
7824 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 break;
7826#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007827 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007828#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007829 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007830 if (to->vval.v_channel != NULL)
7831 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007832 break;
7833#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834 case VAR_STRING:
7835 case VAR_FUNC:
7836 if (from->vval.v_string == NULL)
7837 to->vval.v_string = NULL;
7838 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007839 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007841 if (from->v_type == VAR_FUNC)
7842 func_ref(to->vval.v_string);
7843 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007844 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007845 case VAR_PARTIAL:
7846 if (from->vval.v_partial == NULL)
7847 to->vval.v_partial = NULL;
7848 else
7849 {
7850 to->vval.v_partial = from->vval.v_partial;
7851 ++to->vval.v_partial->pt_refcount;
7852 }
7853 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854 case VAR_LIST:
7855 if (from->vval.v_list == NULL)
7856 to->vval.v_list = NULL;
7857 else
7858 {
7859 to->vval.v_list = from->vval.v_list;
7860 ++to->vval.v_list->lv_refcount;
7861 }
7862 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007863 case VAR_DICT:
7864 if (from->vval.v_dict == NULL)
7865 to->vval.v_dict = NULL;
7866 else
7867 {
7868 to->vval.v_dict = from->vval.v_dict;
7869 ++to->vval.v_dict->dv_refcount;
7870 }
7871 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007872 case VAR_UNKNOWN:
7873 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 break;
7875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876}
7877
7878/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 * Make a copy of an item.
7880 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007881 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7882 * reference to an already copied list/dict can be used.
7883 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007886item_copy(
7887 typval_T *from,
7888 typval_T *to,
7889 int deep,
7890 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891{
7892 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007893 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 {
7897 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007898 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 }
7900 ++recurse;
7901
7902 switch (from->v_type)
7903 {
7904 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 case VAR_STRING:
7907 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007908 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007909 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007910 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007911 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912 copy_tv(from, to);
7913 break;
7914 case VAR_LIST:
7915 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007916 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007917 if (from->vval.v_list == NULL)
7918 to->vval.v_list = NULL;
7919 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7920 {
7921 /* use the copy made earlier */
7922 to->vval.v_list = from->vval.v_list->lv_copylist;
7923 ++to->vval.v_list->lv_refcount;
7924 }
7925 else
7926 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7927 if (to->vval.v_list == NULL)
7928 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007929 break;
7930 case VAR_DICT:
7931 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007932 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007933 if (from->vval.v_dict == NULL)
7934 to->vval.v_dict = NULL;
7935 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7936 {
7937 /* use the copy made earlier */
7938 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7939 ++to->vval.v_dict->dv_refcount;
7940 }
7941 else
7942 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7943 if (to->vval.v_dict == NULL)
7944 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007945 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007946 case VAR_UNKNOWN:
7947 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007948 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007949 }
7950 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007951 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007952}
7953
7954/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 * This function is used by f_input() and f_inputdialog() functions. The third
7956 * argument to f_input() specifies the type of completion to use at the
7957 * prompt. The third argument to f_inputdialog() specifies the value to return
7958 * when the user cancels the prompt.
7959 */
7960 void
7961get_user_input(
7962 typval_T *argvars,
7963 typval_T *rettv,
7964 int inputdialog,
7965 int secret)
7966{
7967 char_u *prompt = get_tv_string_chk(&argvars[0]);
7968 char_u *p = NULL;
7969 int c;
7970 char_u buf[NUMBUFLEN];
7971 int cmd_silent_save = cmd_silent;
7972 char_u *defstr = (char_u *)"";
7973 int xp_type = EXPAND_NOTHING;
7974 char_u *xp_arg = NULL;
7975
7976 rettv->v_type = VAR_STRING;
7977 rettv->vval.v_string = NULL;
7978
7979#ifdef NO_CONSOLE_INPUT
7980 /* While starting up, there is no place to enter text. */
7981 if (no_console_input())
7982 return;
7983#endif
7984
7985 cmd_silent = FALSE; /* Want to see the prompt. */
7986 if (prompt != NULL)
7987 {
7988 /* Only the part of the message after the last NL is considered as
7989 * prompt for the command line */
7990 p = vim_strrchr(prompt, '\n');
7991 if (p == NULL)
7992 p = prompt;
7993 else
7994 {
7995 ++p;
7996 c = *p;
7997 *p = NUL;
7998 msg_start();
7999 msg_clr_eos();
8000 msg_puts_attr(prompt, echo_attr);
8001 msg_didout = FALSE;
8002 msg_starthere();
8003 *p = c;
8004 }
8005 cmdline_row = msg_row;
8006
8007 if (argvars[1].v_type != VAR_UNKNOWN)
8008 {
8009 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8010 if (defstr != NULL)
8011 stuffReadbuffSpec(defstr);
8012
8013 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8014 {
8015 char_u *xp_name;
8016 int xp_namelen;
8017 long argt;
8018
8019 /* input() with a third argument: completion */
8020 rettv->vval.v_string = NULL;
8021
8022 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8023 if (xp_name == NULL)
8024 return;
8025
8026 xp_namelen = (int)STRLEN(xp_name);
8027
8028 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8029 &xp_arg) == FAIL)
8030 return;
8031 }
8032 }
8033
8034 if (defstr != NULL)
8035 {
8036 int save_ex_normal_busy = ex_normal_busy;
8037 ex_normal_busy = 0;
8038 rettv->vval.v_string =
8039 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8040 xp_type, xp_arg);
8041 ex_normal_busy = save_ex_normal_busy;
8042 }
8043 if (inputdialog && rettv->vval.v_string == NULL
8044 && argvars[1].v_type != VAR_UNKNOWN
8045 && argvars[2].v_type != VAR_UNKNOWN)
8046 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8047 &argvars[2], buf));
8048
8049 vim_free(xp_arg);
8050
8051 /* since the user typed this, no need to wait for return */
8052 need_wait_return = FALSE;
8053 msg_didout = FALSE;
8054 }
8055 cmd_silent = cmd_silent_save;
8056}
8057
8058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 * ":echo expr1 ..." print each argument separated with a space, add a
8060 * newline at the end.
8061 * ":echon expr1 ..." print each argument plain.
8062 */
8063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008064ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 char_u *p;
8070 int needclr = TRUE;
8071 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008072 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073
8074 if (eap->skip)
8075 ++emsg_skip;
8076 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078 /* If eval1() causes an error message the text from the command may
8079 * still need to be cleared. E.g., "echo 22,44". */
8080 need_clr_eos = needclr;
8081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {
8085 /*
8086 * Report the invalid expression unless the expression evaluation
8087 * has been cancelled due to an aborting error, an interrupt, or an
8088 * exception.
8089 */
8090 if (!aborting())
8091 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 break;
8094 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095 need_clr_eos = FALSE;
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 if (!eap->skip)
8098 {
8099 if (atstart)
8100 {
8101 atstart = FALSE;
8102 /* Call msg_start() after eval1(), evaluating the expression
8103 * may cause a message to appear. */
8104 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008105 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008106 /* Mark the saved text as finishing the line, so that what
8107 * follows is displayed on a new line when scrolling back
8108 * at the more prompt. */
8109 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
8113 else if (eap->cmdidx == CMD_echo)
8114 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008115 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008116 if (p != NULL)
8117 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008119 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008121 if (*p != TAB && needclr)
8122 {
8123 /* remove any text still there from the command */
8124 msg_clr_eos();
8125 needclr = FALSE;
8126 }
8127 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
8129 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008130 {
8131#ifdef FEAT_MBYTE
8132 if (has_mbyte)
8133 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008134 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008135
8136 (void)msg_outtrans_len_attr(p, i, echo_attr);
8137 p += i - 1;
8138 }
8139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008141 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 arg = skipwhite(arg);
8148 }
8149 eap->nextcmd = check_nextcmd(arg);
8150
8151 if (eap->skip)
8152 --emsg_skip;
8153 else
8154 {
8155 /* remove text that may still be there from the command */
8156 if (needclr)
8157 msg_clr_eos();
8158 if (eap->cmdidx == CMD_echo)
8159 msg_end();
8160 }
8161}
8162
8163/*
8164 * ":echohl {name}".
8165 */
8166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008167ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 int id;
8170
8171 id = syn_name2id(eap->arg);
8172 if (id == 0)
8173 echo_attr = 0;
8174 else
8175 echo_attr = syn_id2attr(id);
8176}
8177
8178/*
8179 * ":execute expr1 ..." execute the result of an expression.
8180 * ":echomsg expr1 ..." Print a message
8181 * ":echoerr expr1 ..." Print an error
8182 * Each gets spaces around each argument and a newline at the end for
8183 * echo commands
8184 */
8185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008186ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187{
8188 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008189 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 int ret = OK;
8191 char_u *p;
8192 garray_T ga;
8193 int len;
8194 int save_did_emsg;
8195
8196 ga_init2(&ga, 1, 80);
8197
8198 if (eap->skip)
8199 ++emsg_skip;
8200 while (*arg != NUL && *arg != '|' && *arg != '\n')
8201 {
8202 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
8205 /*
8206 * Report the invalid expression unless the expression evaluation
8207 * has been cancelled due to an aborting error, an interrupt, or an
8208 * exception.
8209 */
8210 if (!aborting())
8211 EMSG2(_(e_invexpr2), p);
8212 ret = FAIL;
8213 break;
8214 }
8215
8216 if (!eap->skip)
8217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 len = (int)STRLEN(p);
8220 if (ga_grow(&ga, len + 2) == FAIL)
8221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 ret = FAIL;
8224 break;
8225 }
8226 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 ga.ga_len += len;
8230 }
8231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 arg = skipwhite(arg);
8234 }
8235
8236 if (ret != FAIL && ga.ga_data != NULL)
8237 {
8238 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008241 out_flush();
8242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 else if (eap->cmdidx == CMD_echoerr)
8244 {
8245 /* We don't want to abort following commands, restore did_emsg. */
8246 save_did_emsg = did_emsg;
8247 EMSG((char_u *)ga.ga_data);
8248 if (!force_abort)
8249 did_emsg = save_did_emsg;
8250 }
8251 else if (eap->cmdidx == CMD_execute)
8252 do_cmdline((char_u *)ga.ga_data,
8253 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8254 }
8255
8256 ga_clear(&ga);
8257
8258 if (eap->skip)
8259 --emsg_skip;
8260
8261 eap->nextcmd = check_nextcmd(arg);
8262}
8263
8264/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265 * Find window specified by "vp" in tabpage "tp".
8266 */
8267 win_T *
8268find_win_by_nr(
8269 typval_T *vp,
8270 tabpage_T *tp UNUSED) /* NULL for current tab page */
8271{
8272#ifdef FEAT_WINDOWS
8273 win_T *wp;
8274#endif
8275 int nr;
8276
8277 nr = (int)get_tv_number_chk(vp, NULL);
8278
8279#ifdef FEAT_WINDOWS
8280 if (nr < 0)
8281 return NULL;
8282 if (nr == 0)
8283 return curwin;
8284
8285 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
8286 wp != NULL; wp = wp->w_next)
8287 if (nr >= LOWEST_WIN_ID)
8288 {
8289 if (wp->w_id == nr)
8290 return wp;
8291 }
8292 else if (--nr <= 0)
8293 break;
8294 if (nr >= LOWEST_WIN_ID)
8295 return NULL;
8296 return wp;
8297#else
8298 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8299 return curwin;
8300 return NULL;
8301#endif
8302}
8303
8304/*
8305 * Find window specified by "wvp" in tabpage "tvp".
8306 */
8307 win_T *
8308find_tabwin(
8309 typval_T *wvp, /* VAR_UNKNOWN for current window */
8310 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8311{
8312 win_T *wp = NULL;
8313 tabpage_T *tp = NULL;
8314 long n;
8315
8316 if (wvp->v_type != VAR_UNKNOWN)
8317 {
8318 if (tvp->v_type != VAR_UNKNOWN)
8319 {
8320 n = (long)get_tv_number(tvp);
8321 if (n >= 0)
8322 tp = find_tabpage(n);
8323 }
8324 else
8325 tp = curtab;
8326
8327 if (tp != NULL)
8328 wp = find_win_by_nr(wvp, tp);
8329 }
8330 else
8331 wp = curwin;
8332
8333 return wp;
8334}
8335
8336/*
8337 * getwinvar() and gettabwinvar()
8338 */
8339 void
8340getwinvar(
8341 typval_T *argvars,
8342 typval_T *rettv,
8343 int off) /* 1 for gettabwinvar() */
8344{
8345 win_T *win;
8346 char_u *varname;
8347 dictitem_T *v;
8348 tabpage_T *tp = NULL;
8349 int done = FALSE;
8350#ifdef FEAT_WINDOWS
8351 win_T *oldcurwin;
8352 tabpage_T *oldtabpage;
8353 int need_switch_win;
8354#endif
8355
8356#ifdef FEAT_WINDOWS
8357 if (off == 1)
8358 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8359 else
8360 tp = curtab;
8361#endif
8362 win = find_win_by_nr(&argvars[off], tp);
8363 varname = get_tv_string_chk(&argvars[off + 1]);
8364 ++emsg_off;
8365
8366 rettv->v_type = VAR_STRING;
8367 rettv->vval.v_string = NULL;
8368
8369 if (win != NULL && varname != NULL)
8370 {
8371#ifdef FEAT_WINDOWS
8372 /* Set curwin to be our win, temporarily. Also set the tabpage,
8373 * otherwise the window is not valid. Only do this when needed,
8374 * autocommands get blocked. */
8375 need_switch_win = !(tp == curtab && win == curwin);
8376 if (!need_switch_win
8377 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8378#endif
8379 {
8380 if (*varname == '&') /* window-local-option */
8381 {
8382 if (get_option_tv(&varname, rettv, 1) == OK)
8383 done = TRUE;
8384 }
8385 else
8386 {
8387 /* Look up the variable. */
8388 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8389 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8390 varname, FALSE);
8391 if (v != NULL)
8392 {
8393 copy_tv(&v->di_tv, rettv);
8394 done = TRUE;
8395 }
8396 }
8397 }
8398
8399#ifdef FEAT_WINDOWS
8400 if (need_switch_win)
8401 /* restore previous notion of curwin */
8402 restore_win(oldcurwin, oldtabpage, TRUE);
8403#endif
8404 }
8405
8406 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8407 /* use the default return value */
8408 copy_tv(&argvars[off + 2], rettv);
8409
8410 --emsg_off;
8411}
8412
8413/*
8414 * "setwinvar()" and "settabwinvar()" functions
8415 */
8416 void
8417setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8418{
8419 win_T *win;
8420#ifdef FEAT_WINDOWS
8421 win_T *save_curwin;
8422 tabpage_T *save_curtab;
8423 int need_switch_win;
8424#endif
8425 char_u *varname, *winvarname;
8426 typval_T *varp;
8427 char_u nbuf[NUMBUFLEN];
8428 tabpage_T *tp = NULL;
8429
8430 if (check_restricted() || check_secure())
8431 return;
8432
8433#ifdef FEAT_WINDOWS
8434 if (off == 1)
8435 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8436 else
8437 tp = curtab;
8438#endif
8439 win = find_win_by_nr(&argvars[off], tp);
8440 varname = get_tv_string_chk(&argvars[off + 1]);
8441 varp = &argvars[off + 2];
8442
8443 if (win != NULL && varname != NULL && varp != NULL)
8444 {
8445#ifdef FEAT_WINDOWS
8446 need_switch_win = !(tp == curtab && win == curwin);
8447 if (!need_switch_win
8448 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8449#endif
8450 {
8451 if (*varname == '&')
8452 {
8453 long numval;
8454 char_u *strval;
8455 int error = FALSE;
8456
8457 ++varname;
8458 numval = (long)get_tv_number_chk(varp, &error);
8459 strval = get_tv_string_buf_chk(varp, nbuf);
8460 if (!error && strval != NULL)
8461 set_option_value(varname, numval, strval, OPT_LOCAL);
8462 }
8463 else
8464 {
8465 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8466 if (winvarname != NULL)
8467 {
8468 STRCPY(winvarname, "w:");
8469 STRCPY(winvarname + 2, varname);
8470 set_var(winvarname, varp, TRUE);
8471 vim_free(winvarname);
8472 }
8473 }
8474 }
8475#ifdef FEAT_WINDOWS
8476 if (need_switch_win)
8477 restore_win(save_curwin, save_curtab, TRUE);
8478#endif
8479 }
8480}
8481
8482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8484 * "arg" points to the "&" or '+' when called, to "option" when returning.
8485 * Returns NULL when no option name found. Otherwise pointer to the char
8486 * after the option name.
8487 */
8488 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008489find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 char_u *p = *arg;
8492
8493 ++p;
8494 if (*p == 'g' && p[1] == ':')
8495 {
8496 *opt_flags = OPT_GLOBAL;
8497 p += 2;
8498 }
8499 else if (*p == 'l' && p[1] == ':')
8500 {
8501 *opt_flags = OPT_LOCAL;
8502 p += 2;
8503 }
8504 else
8505 *opt_flags = 0;
8506
8507 if (!ASCII_ISALPHA(*p))
8508 return NULL;
8509 *arg = p;
8510
8511 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8512 p += 4; /* termcap option */
8513 else
8514 while (ASCII_ISALPHA(*p))
8515 ++p;
8516 return p;
8517}
8518
8519/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008520 * Return the autoload script name for a function or variable name.
8521 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008523 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008524autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008525{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008526 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008527 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008528
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008529 /* Get the script file name: replace '#' with '/', append ".vim". */
8530 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8531 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008532 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008533 STRCPY(scriptname, "autoload/");
8534 STRCAT(scriptname, name);
8535 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8536 STRCAT(scriptname, ".vim");
8537 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8538 *p = '/';
8539 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008540}
8541
8542/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008543 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008544 * Return TRUE if a package was loaded.
8545 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547script_autoload(
8548 char_u *name,
8549 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008550{
8551 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008552 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008553 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008554 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008555
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008556 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008557 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008558 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008559 return FALSE;
8560
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008561 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008562
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008563 /* Find the name in the list of previously loaded package names. Skip
8564 * "autoload/", it's always the same. */
8565 for (i = 0; i < ga_loaded.ga_len; ++i)
8566 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8567 break;
8568 if (!reload && i < ga_loaded.ga_len)
8569 ret = FALSE; /* was loaded already */
8570 else
8571 {
8572 /* Remember the name if it wasn't loaded already. */
8573 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8574 {
8575 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8576 tofree = NULL;
8577 }
8578
8579 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008580 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008581 ret = TRUE;
8582 }
8583
8584 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008585 return ret;
8586}
8587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8589typedef enum
8590{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8592 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8593 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594} var_flavour_T;
8595
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008596static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597
8598 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008599var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 char_u *p = varname;
8602
8603 if (ASCII_ISUPPER(*p))
8604 {
8605 while (*(++p))
8606 if (ASCII_ISLOWER(*p))
8607 return VAR_FLAVOUR_SESSION;
8608 return VAR_FLAVOUR_VIMINFO;
8609 }
8610 else
8611 return VAR_FLAVOUR_DEFAULT;
8612}
8613#endif
8614
8615#if defined(FEAT_VIMINFO) || defined(PROTO)
8616/*
8617 * Restore global vars that start with a capital from the viminfo file
8618 */
8619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008620read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008623 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008624 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008625 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 if (!writing && (find_viminfo_parameter('!') != NULL))
8628 {
8629 tab = vim_strchr(virp->vir_line + 1, '\t');
8630 if (tab != NULL)
8631 {
8632 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008633 switch (*tab)
8634 {
8635 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008636#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008637 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008638#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008639 case 'D': type = VAR_DICT; break;
8640 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008641 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 tab = vim_strchr(tab, '\t');
8645 if (tab != NULL)
8646 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008647 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008648 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008649 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651#ifdef FEAT_FLOAT
8652 else if (type == VAR_FLOAT)
8653 (void)string2float(tab + 1, &tv.vval.v_float);
8654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008656 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008657 if (type == VAR_DICT || type == VAR_LIST)
8658 {
8659 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8660
8661 if (etv == NULL)
8662 /* Failed to parse back the dict or list, use it as a
8663 * string. */
8664 tv.v_type = VAR_STRING;
8665 else
8666 {
8667 vim_free(tv.vval.v_string);
8668 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008669 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008670 }
8671 }
8672
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008673 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008674 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008675 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008676 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008677
8678 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008679 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008680 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8681 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 }
8683 }
8684 }
8685
8686 return viminfo_readline(virp);
8687}
8688
8689/*
8690 * Write global vars that start with a capital to the viminfo file
8691 */
8692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008693write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
Bram Moolenaar33570922005-01-25 22:26:29 +00008695 hashitem_T *hi;
8696 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008697 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008698 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008699 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008701 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703 if (find_viminfo_parameter('!') == NULL)
8704 return;
8705
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008706 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008708 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008711 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008713 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 this_var = HI2DI(hi);
8715 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008718 {
8719 case VAR_STRING: s = "STR"; break;
8720 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008721 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008722 case VAR_DICT: s = "DIC"; break;
8723 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008724 case VAR_SPECIAL: s = "XPL"; break;
8725
8726 case VAR_UNKNOWN:
8727 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008728 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008729 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008730 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008731 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008732 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008734 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008735 if (p != NULL)
8736 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008737 vim_free(tofree);
8738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 }
8740 }
8741}
8742#endif
8743
8744#if defined(FEAT_SESSION) || defined(PROTO)
8745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008746store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 hashitem_T *hi;
8749 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008750 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 char_u *p, *t;
8752
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008753 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008756 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008758 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008759 this_var = HI2DI(hi);
8760 if ((this_var->di_tv.v_type == VAR_NUMBER
8761 || this_var->di_tv.v_type == VAR_STRING)
8762 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008763 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008764 /* Escape special characters with a backslash. Turn a LF and
8765 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008767 (char_u *)"\\\"\n\r");
8768 if (p == NULL) /* out of memory */
8769 break;
8770 for (t = p; *t != NUL; ++t)
8771 if (*t == '\n')
8772 *t = 'n';
8773 else if (*t == '\r')
8774 *t = 'r';
8775 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 this_var->di_key,
8777 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8778 : ' ',
8779 p,
8780 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8781 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008782 || put_eol(fd) == FAIL)
8783 {
8784 vim_free(p);
8785 return FAIL;
8786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 vim_free(p);
8788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008789#ifdef FEAT_FLOAT
8790 else if (this_var->di_tv.v_type == VAR_FLOAT
8791 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8792 {
8793 float_T f = this_var->di_tv.vval.v_float;
8794 int sign = ' ';
8795
8796 if (f < 0)
8797 {
8798 f = -f;
8799 sign = '-';
8800 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008801 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008802 this_var->di_key, sign, f) < 0)
8803 || put_eol(fd) == FAIL)
8804 return FAIL;
8805 }
8806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 }
8808 }
8809 return OK;
8810}
8811#endif
8812
Bram Moolenaar661b1822005-07-28 22:36:45 +00008813/*
8814 * Display script name where an item was last set.
8815 * Should only be invoked when 'verbose' is non-zero.
8816 */
8817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008818last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008819{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008820 char_u *p;
8821
Bram Moolenaar661b1822005-07-28 22:36:45 +00008822 if (scriptID != 0)
8823 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008824 p = home_replace_save(NULL, get_scriptname(scriptID));
8825 if (p != NULL)
8826 {
8827 verbose_enter();
8828 MSG_PUTS(_("\n\tLast set from "));
8829 MSG_PUTS(p);
8830 vim_free(p);
8831 verbose_leave();
8832 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008833 }
8834}
8835
Bram Moolenaard812df62008-11-09 12:46:09 +00008836/*
8837 * List v:oldfiles in a nice way.
8838 */
Bram Moolenaard812df62008-11-09 12:46:09 +00008839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008840ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +00008841{
8842 list_T *l = vimvars[VV_OLDFILES].vv_list;
8843 listitem_T *li;
8844 int nr = 0;
8845
8846 if (l == NULL)
8847 msg((char_u *)_("No old files"));
8848 else
8849 {
8850 msg_start();
8851 msg_scroll = TRUE;
8852 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8853 {
8854 msg_outnum((long)++nr);
8855 MSG_PUTS(": ");
8856 msg_outtrans(get_tv_string(&li->li_tv));
8857 msg_putchar('\n');
8858 out_flush(); /* output one line at a time */
8859 ui_breakcheck();
8860 }
8861 /* Assume "got_int" was set to truncate the listing. */
8862 got_int = FALSE;
8863
8864#ifdef FEAT_BROWSE_CMD
8865 if (cmdmod.browse)
8866 {
8867 quit_more = FALSE;
8868 nr = prompt_for_number(FALSE);
8869 msg_starthere();
8870 if (nr > 0)
8871 {
8872 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8873 (long)nr);
8874
8875 if (p != NULL)
8876 {
8877 p = expand_env_save(p);
8878 eap->arg = p;
8879 eap->cmdidx = CMD_edit;
8880 cmdmod.browse = FALSE;
8881 do_exedit(eap, NULL);
8882 vim_free(p);
8883 }
8884 }
8885 }
8886#endif
8887 }
8888}
8889
Bram Moolenaar53744302015-07-17 17:38:22 +02008890/* reset v:option_new, v:option_old and v:option_type */
8891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008893{
8894 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8895 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8896 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8897}
8898
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899/*
8900 * Prepare "gap" for an assert error and add the sourcing position.
8901 */
8902 void
8903prepare_assert_error(garray_T *gap)
8904{
8905 char buf[NUMBUFLEN];
8906
8907 ga_init2(gap, 1, 100);
8908 if (sourcing_name != NULL)
8909 {
8910 ga_concat(gap, sourcing_name);
8911 if (sourcing_lnum > 0)
8912 ga_concat(gap, (char_u *)" ");
8913 }
8914 if (sourcing_lnum > 0)
8915 {
8916 sprintf(buf, "line %ld", (long)sourcing_lnum);
8917 ga_concat(gap, (char_u *)buf);
8918 }
8919 if (sourcing_name != NULL || sourcing_lnum > 0)
8920 ga_concat(gap, (char_u *)": ");
8921}
8922
8923/*
8924 * Add an assert error to v:errors.
8925 */
8926 void
8927assert_error(garray_T *gap)
8928{
8929 struct vimvar *vp = &vimvars[VV_ERRORS];
8930
8931 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8932 /* Make sure v:errors is a list. */
8933 set_vim_var_list(VV_ERRORS, list_alloc());
8934 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8935}
8936
8937 void
8938assert_equal_common(typval_T *argvars, assert_type_T atype)
8939{
8940 garray_T ga;
8941
8942 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8943 != (atype == ASSERT_EQUAL))
8944 {
8945 prepare_assert_error(&ga);
8946 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8947 atype);
8948 assert_error(&ga);
8949 ga_clear(&ga);
8950 }
8951}
8952
8953 void
8954assert_match_common(typval_T *argvars, assert_type_T atype)
8955{
8956 garray_T ga;
8957 char_u buf1[NUMBUFLEN];
8958 char_u buf2[NUMBUFLEN];
8959 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8960 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8961
8962 if (pat == NULL || text == NULL)
8963 EMSG(_(e_invarg));
8964 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8965 {
8966 prepare_assert_error(&ga);
8967 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8968 atype);
8969 assert_error(&ga);
8970 ga_clear(&ga);
8971 }
8972}
8973
8974/*
8975 * Common for assert_true() and assert_false().
8976 */
8977 void
8978assert_bool(typval_T *argvars, int isTrue)
8979{
8980 int error = FALSE;
8981 garray_T ga;
8982
8983 if (argvars[0].v_type == VAR_SPECIAL
8984 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
8985 return;
8986 if (argvars[0].v_type != VAR_NUMBER
8987 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8988 || error)
8989 {
8990 prepare_assert_error(&ga);
8991 fill_assert_error(&ga, &argvars[1],
8992 (char_u *)(isTrue ? "True" : "False"),
8993 NULL, &argvars[0], ASSERT_OTHER);
8994 assert_error(&ga);
8995 ga_clear(&ga);
8996 }
8997}
8998
8999 void
9000assert_exception(typval_T *argvars)
9001{
9002 garray_T ga;
9003 char_u *error = get_tv_string_chk(&argvars[0]);
9004
9005 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9006 {
9007 prepare_assert_error(&ga);
9008 ga_concat(&ga, (char_u *)"v:exception is not set");
9009 assert_error(&ga);
9010 ga_clear(&ga);
9011 }
9012 else if (error != NULL
9013 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9014 {
9015 prepare_assert_error(&ga);
9016 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9017 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9018 assert_error(&ga);
9019 ga_clear(&ga);
9020 }
9021}
9022
9023 void
9024assert_fails(typval_T *argvars)
9025{
9026 char_u *cmd = get_tv_string_chk(&argvars[0]);
9027 garray_T ga;
9028
9029 called_emsg = FALSE;
9030 suppress_errthrow = TRUE;
9031 emsg_silent = TRUE;
9032 do_cmdline_cmd(cmd);
9033 if (!called_emsg)
9034 {
9035 prepare_assert_error(&ga);
9036 ga_concat(&ga, (char_u *)"command did not fail: ");
9037 ga_concat(&ga, cmd);
9038 assert_error(&ga);
9039 ga_clear(&ga);
9040 }
9041 else if (argvars[1].v_type != VAR_UNKNOWN)
9042 {
9043 char_u buf[NUMBUFLEN];
9044 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9045
9046 if (error == NULL
9047 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9048 {
9049 prepare_assert_error(&ga);
9050 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9051 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9052 assert_error(&ga);
9053 ga_clear(&ga);
9054 }
9055 }
9056
9057 called_emsg = FALSE;
9058 suppress_errthrow = FALSE;
9059 emsg_silent = FALSE;
9060 emsg_on_display = FALSE;
9061 set_vim_var_string(VV_ERRMSG, NULL, 0);
9062}
9063
9064/*
9065 * Append "str" to "gap", escaping unprintable characters.
9066 * Changes NL to \n, CR to \r, etc.
9067 */
9068 static void
9069ga_concat_esc(garray_T *gap, char_u *str)
9070{
9071 char_u *p;
9072 char_u buf[NUMBUFLEN];
9073
9074 if (str == NULL)
9075 {
9076 ga_concat(gap, (char_u *)"NULL");
9077 return;
9078 }
9079
9080 for (p = str; *p != NUL; ++p)
9081 switch (*p)
9082 {
9083 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9084 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9085 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9086 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9087 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9088 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9089 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9090 default:
9091 if (*p < ' ')
9092 {
9093 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9094 ga_concat(gap, buf);
9095 }
9096 else
9097 ga_append(gap, *p);
9098 break;
9099 }
9100}
9101
9102/*
9103 * Fill "gap" with information about an assert error.
9104 */
9105 void
9106fill_assert_error(
9107 garray_T *gap,
9108 typval_T *opt_msg_tv,
9109 char_u *exp_str,
9110 typval_T *exp_tv,
9111 typval_T *got_tv,
9112 assert_type_T atype)
9113{
9114 char_u numbuf[NUMBUFLEN];
9115 char_u *tofree;
9116
9117 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9118 {
9119 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9120 vim_free(tofree);
9121 }
9122 else
9123 {
9124 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9125 ga_concat(gap, (char_u *)"Pattern ");
9126 else
9127 ga_concat(gap, (char_u *)"Expected ");
9128 if (exp_str == NULL)
9129 {
9130 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9131 vim_free(tofree);
9132 }
9133 else
9134 ga_concat_esc(gap, exp_str);
9135 if (atype == ASSERT_MATCH)
9136 ga_concat(gap, (char_u *)" does not match ");
9137 else if (atype == ASSERT_NOTMATCH)
9138 ga_concat(gap, (char_u *)" does match ");
9139 else if (atype == ASSERT_NOTEQUAL)
9140 ga_concat(gap, (char_u *)" differs from ");
9141 else
9142 ga_concat(gap, (char_u *)" but got ");
9143 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9144 vim_free(tofree);
9145 }
9146}
9147
Bram Moolenaar53744302015-07-17 17:38:22 +02009148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#endif /* FEAT_EVAL */
9150
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009152#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153
9154#ifdef WIN3264
9155/*
9156 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9157 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009158static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9159static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9160static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009163 * Get the short path (8.3) for the filename in "fnamep".
9164 * Only works for a valid file name.
9165 * When the path gets longer "fnamep" is changed and the allocated buffer
9166 * is put in "bufp".
9167 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9168 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 */
9170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009171get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009173 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 char_u *newbuf;
9175
9176 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009177 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 if (l > len - 1)
9179 {
9180 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009181 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 newbuf = vim_strnsave(*fnamep, l);
9183 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
9186 vim_free(*bufp);
9187 *fnamep = *bufp = newbuf;
9188
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009189 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009190 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 }
9192
9193 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009194 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195}
9196
9197/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009198 * Get the short path (8.3) for the filename in "fname". The converted
9199 * path is returned in "bufp".
9200 *
9201 * Some of the directories specified in "fname" may not exist. This function
9202 * will shorten the existing directories at the beginning of the path and then
9203 * append the remaining non-existing path.
9204 *
9205 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009206 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009207 * bufp - Pointer to an allocated buffer for the filename.
9208 * fnamelen - Length of the filename pointed to by fname
9209 *
9210 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 */
9212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009213shortpath_for_invalid_fname(
9214 char_u **fname,
9215 char_u **bufp,
9216 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009218 char_u *short_fname, *save_fname, *pbuf_unused;
9219 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009221 int old_len, len;
9222 int new_len, sfx_len;
9223 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009226 old_len = *fnamelen;
9227 save_fname = vim_strnsave(*fname, old_len);
9228 pbuf_unused = NULL;
9229 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009231 endp = save_fname + old_len - 1; /* Find the end of the copy */
9232 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009234 /*
9235 * Try shortening the supplied path till it succeeds by removing one
9236 * directory at a time from the tail of the path.
9237 */
9238 len = 0;
9239 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009241 /* go back one path-separator */
9242 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9243 --endp;
9244 if (endp <= save_fname)
9245 break; /* processed the complete path */
9246
9247 /*
9248 * Replace the path separator with a NUL and try to shorten the
9249 * resulting path.
9250 */
9251 ch = *endp;
9252 *endp = 0;
9253 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009254 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009255 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9256 {
9257 retval = FAIL;
9258 goto theend;
9259 }
9260 *endp = ch; /* preserve the string */
9261
9262 if (len > 0)
9263 break; /* successfully shortened the path */
9264
9265 /* failed to shorten the path. Skip the path separator */
9266 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 }
9268
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009269 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009271 /*
9272 * Succeeded in shortening the path. Now concatenate the shortened
9273 * path with the remaining path at the tail.
9274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009276 /* Compute the length of the new path. */
9277 sfx_len = (int)(save_endp - endp) + 1;
9278 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009280 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009282 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009284 /* There is not enough space in the currently allocated string,
9285 * copy it to a buffer big enough. */
9286 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009288 {
9289 retval = FAIL;
9290 goto theend;
9291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 }
9293 else
9294 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009295 /* Transfer short_fname to the main buffer (it's big enough),
9296 * unless get_short_pathname() did its work in-place. */
9297 *fname = *bufp = save_fname;
9298 if (short_fname != save_fname)
9299 vim_strncpy(save_fname, short_fname, len);
9300 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009302
9303 /* concat the not-shortened part of the path */
9304 vim_strncpy(*fname + len, endp, sfx_len);
9305 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009307
9308theend:
9309 vim_free(pbuf_unused);
9310 vim_free(save_fname);
9311
9312 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
9314
9315/*
9316 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009317 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 */
9319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320shortpath_for_partial(
9321 char_u **fnamep,
9322 char_u **bufp,
9323 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 int sepcount, len, tflen;
9326 char_u *p;
9327 char_u *pbuf, *tfname;
9328 int hasTilde;
9329
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009330 /* Count up the path separators from the RHS.. so we know which part
9331 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009333 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 if (vim_ispathsep(*p))
9335 ++sepcount;
9336
9337 /* Need full path first (use expand_env() to remove a "~/") */
9338 hasTilde = (**fnamep == '~');
9339 if (hasTilde)
9340 pbuf = tfname = expand_env_save(*fnamep);
9341 else
9342 pbuf = tfname = FullName_save(*fnamep, FALSE);
9343
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009344 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009346 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
9349 if (len == 0)
9350 {
9351 /* Don't have a valid filename, so shorten the rest of the
9352 * path if we can. This CAN give us invalid 8.3 filenames, but
9353 * there's not a lot of point in guessing what it might be.
9354 */
9355 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009356 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 }
9359
9360 /* Count the paths backward to find the beginning of the desired string. */
9361 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009362 {
9363#ifdef FEAT_MBYTE
9364 if (has_mbyte)
9365 p -= mb_head_off(tfname, p);
9366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 if (vim_ispathsep(*p))
9368 {
9369 if (sepcount == 0 || (hasTilde && sepcount == 1))
9370 break;
9371 else
9372 sepcount --;
9373 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (hasTilde)
9376 {
9377 --p;
9378 if (p >= tfname)
9379 *p = '~';
9380 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 }
9383 else
9384 ++p;
9385
9386 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9387 vim_free(*bufp);
9388 *fnamelen = (int)STRLEN(p);
9389 *bufp = pbuf;
9390 *fnamep = p;
9391
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009392 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393}
9394#endif /* WIN3264 */
9395
9396/*
9397 * Adjust a filename, according to a string of modifiers.
9398 * *fnamep must be NUL terminated when called. When returning, the length is
9399 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009400 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 * When there is an error, *fnamep is set to NULL.
9402 */
9403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009404modify_fname(
9405 char_u *src, /* string with modifiers */
9406 int *usedlen, /* characters after src that are used */
9407 char_u **fnamep, /* file name so far */
9408 char_u **bufp, /* buffer for allocated file name or NULL */
9409 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411 int valid = 0;
9412 char_u *tail;
9413 char_u *s, *p, *pbuf;
9414 char_u dirname[MAXPATHL];
9415 int c;
9416 int has_fullname = 0;
9417#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009418 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 int has_shortname = 0;
9420#endif
9421
9422repeat:
9423 /* ":p" - full path/file_name */
9424 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9425 {
9426 has_fullname = 1;
9427
9428 valid |= VALID_PATH;
9429 *usedlen += 2;
9430
9431 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9432 if ((*fnamep)[0] == '~'
9433#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9434 && ((*fnamep)[1] == '/'
9435# ifdef BACKSLASH_IN_FILENAME
9436 || (*fnamep)[1] == '\\'
9437# endif
9438 || (*fnamep)[1] == NUL)
9439
9440#endif
9441 )
9442 {
9443 *fnamep = expand_env_save(*fnamep);
9444 vim_free(*bufp); /* free any allocated file name */
9445 *bufp = *fnamep;
9446 if (*fnamep == NULL)
9447 return -1;
9448 }
9449
9450 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009451 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 {
9453 if (vim_ispathsep(*p)
9454 && p[1] == '.'
9455 && (p[2] == NUL
9456 || vim_ispathsep(p[2])
9457 || (p[2] == '.'
9458 && (p[3] == NUL || vim_ispathsep(p[3])))))
9459 break;
9460 }
9461
9462 /* FullName_save() is slow, don't use it when not needed. */
9463 if (*p != NUL || !vim_isAbsName(*fnamep))
9464 {
9465 *fnamep = FullName_save(*fnamep, *p != NUL);
9466 vim_free(*bufp); /* free any allocated file name */
9467 *bufp = *fnamep;
9468 if (*fnamep == NULL)
9469 return -1;
9470 }
9471
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009472#ifdef WIN3264
9473# if _WIN32_WINNT >= 0x0500
9474 if (vim_strchr(*fnamep, '~') != NULL)
9475 {
9476 /* Expand 8.3 filename to full path. Needed to make sure the same
9477 * file does not have two different names.
9478 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9479 p = alloc(_MAX_PATH + 1);
9480 if (p != NULL)
9481 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009482 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009483 {
9484 vim_free(*bufp);
9485 *bufp = *fnamep = p;
9486 }
9487 else
9488 vim_free(p);
9489 }
9490 }
9491# endif
9492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 /* Append a path separator to a directory. */
9494 if (mch_isdir(*fnamep))
9495 {
9496 /* Make room for one or two extra characters. */
9497 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9498 vim_free(*bufp); /* free any allocated file name */
9499 *bufp = *fnamep;
9500 if (*fnamep == NULL)
9501 return -1;
9502 add_pathsep(*fnamep);
9503 }
9504 }
9505
9506 /* ":." - path relative to the current directory */
9507 /* ":~" - path relative to the home directory */
9508 /* ":8" - shortname path - postponed till after */
9509 while (src[*usedlen] == ':'
9510 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9511 {
9512 *usedlen += 2;
9513 if (c == '8')
9514 {
9515#ifdef WIN3264
9516 has_shortname = 1; /* Postpone this. */
9517#endif
9518 continue;
9519 }
9520 pbuf = NULL;
9521 /* Need full path first (use expand_env() to remove a "~/") */
9522 if (!has_fullname)
9523 {
9524 if (c == '.' && **fnamep == '~')
9525 p = pbuf = expand_env_save(*fnamep);
9526 else
9527 p = pbuf = FullName_save(*fnamep, FALSE);
9528 }
9529 else
9530 p = *fnamep;
9531
9532 has_fullname = 0;
9533
9534 if (p != NULL)
9535 {
9536 if (c == '.')
9537 {
9538 mch_dirname(dirname, MAXPATHL);
9539 s = shorten_fname(p, dirname);
9540 if (s != NULL)
9541 {
9542 *fnamep = s;
9543 if (pbuf != NULL)
9544 {
9545 vim_free(*bufp); /* free any allocated file name */
9546 *bufp = pbuf;
9547 pbuf = NULL;
9548 }
9549 }
9550 }
9551 else
9552 {
9553 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9554 /* Only replace it when it starts with '~' */
9555 if (*dirname == '~')
9556 {
9557 s = vim_strsave(dirname);
9558 if (s != NULL)
9559 {
9560 *fnamep = s;
9561 vim_free(*bufp);
9562 *bufp = s;
9563 }
9564 }
9565 }
9566 vim_free(pbuf);
9567 }
9568 }
9569
9570 tail = gettail(*fnamep);
9571 *fnamelen = (int)STRLEN(*fnamep);
9572
9573 /* ":h" - head, remove "/file_name", can be repeated */
9574 /* Don't remove the first "/" or "c:\" */
9575 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9576 {
9577 valid |= VALID_HEAD;
9578 *usedlen += 2;
9579 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009580 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009581 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 *fnamelen = (int)(tail - *fnamep);
9583#ifdef VMS
9584 if (*fnamelen > 0)
9585 *fnamelen += 1; /* the path separator is part of the path */
9586#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009587 if (*fnamelen == 0)
9588 {
9589 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9590 p = vim_strsave((char_u *)".");
9591 if (p == NULL)
9592 return -1;
9593 vim_free(*bufp);
9594 *bufp = *fnamep = tail = p;
9595 *fnamelen = 1;
9596 }
9597 else
9598 {
9599 while (tail > s && !after_pathsep(s, tail))
9600 mb_ptr_back(*fnamep, tail);
9601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603
9604 /* ":8" - shortname */
9605 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9606 {
9607 *usedlen += 2;
9608#ifdef WIN3264
9609 has_shortname = 1;
9610#endif
9611 }
9612
9613#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009614 /*
9615 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 */
9617 if (has_shortname)
9618 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009619 /* Copy the string if it is shortened by :h and when it wasn't copied
9620 * yet, because we are going to change it in place. Avoids changing
9621 * the buffer name for "%:8". */
9622 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 {
9624 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009625 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 return -1;
9627 vim_free(*bufp);
9628 *bufp = *fnamep = p;
9629 }
9630
9631 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009632 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 if (!has_fullname && !vim_isAbsName(*fnamep))
9634 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009635 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 return -1;
9637 }
9638 else
9639 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009640 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641
Bram Moolenaardc935552011-08-17 15:23:23 +02009642 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009644 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 return -1;
9646
9647 if (l == 0)
9648 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009649 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009651 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 return -1;
9653 }
9654 *fnamelen = l;
9655 }
9656 }
9657#endif /* WIN3264 */
9658
9659 /* ":t" - tail, just the basename */
9660 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9661 {
9662 *usedlen += 2;
9663 *fnamelen -= (int)(tail - *fnamep);
9664 *fnamep = tail;
9665 }
9666
9667 /* ":e" - extension, can be repeated */
9668 /* ":r" - root, without extension, can be repeated */
9669 while (src[*usedlen] == ':'
9670 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9671 {
9672 /* find a '.' in the tail:
9673 * - for second :e: before the current fname
9674 * - otherwise: The last '.'
9675 */
9676 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9677 s = *fnamep - 2;
9678 else
9679 s = *fnamep + *fnamelen - 1;
9680 for ( ; s > tail; --s)
9681 if (s[0] == '.')
9682 break;
9683 if (src[*usedlen + 1] == 'e') /* :e */
9684 {
9685 if (s > tail)
9686 {
9687 *fnamelen += (int)(*fnamep - (s + 1));
9688 *fnamep = s + 1;
9689#ifdef VMS
9690 /* cut version from the extension */
9691 s = *fnamep + *fnamelen - 1;
9692 for ( ; s > *fnamep; --s)
9693 if (s[0] == ';')
9694 break;
9695 if (s > *fnamep)
9696 *fnamelen = s - *fnamep;
9697#endif
9698 }
9699 else if (*fnamep <= tail)
9700 *fnamelen = 0;
9701 }
9702 else /* :r */
9703 {
9704 if (s > tail) /* remove one extension */
9705 *fnamelen = (int)(s - *fnamep);
9706 }
9707 *usedlen += 2;
9708 }
9709
9710 /* ":s?pat?foo?" - substitute */
9711 /* ":gs?pat?foo?" - global substitute */
9712 if (src[*usedlen] == ':'
9713 && (src[*usedlen + 1] == 's'
9714 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9715 {
9716 char_u *str;
9717 char_u *pat;
9718 char_u *sub;
9719 int sep;
9720 char_u *flags;
9721 int didit = FALSE;
9722
9723 flags = (char_u *)"";
9724 s = src + *usedlen + 2;
9725 if (src[*usedlen + 1] == 'g')
9726 {
9727 flags = (char_u *)"g";
9728 ++s;
9729 }
9730
9731 sep = *s++;
9732 if (sep)
9733 {
9734 /* find end of pattern */
9735 p = vim_strchr(s, sep);
9736 if (p != NULL)
9737 {
9738 pat = vim_strnsave(s, (int)(p - s));
9739 if (pat != NULL)
9740 {
9741 s = p + 1;
9742 /* find end of substitution */
9743 p = vim_strchr(s, sep);
9744 if (p != NULL)
9745 {
9746 sub = vim_strnsave(s, (int)(p - s));
9747 str = vim_strnsave(*fnamep, *fnamelen);
9748 if (sub != NULL && str != NULL)
9749 {
9750 *usedlen = (int)(p + 1 - src);
9751 s = do_string_sub(str, pat, sub, flags);
9752 if (s != NULL)
9753 {
9754 *fnamep = s;
9755 *fnamelen = (int)STRLEN(s);
9756 vim_free(*bufp);
9757 *bufp = s;
9758 didit = TRUE;
9759 }
9760 }
9761 vim_free(sub);
9762 vim_free(str);
9763 }
9764 vim_free(pat);
9765 }
9766 }
9767 /* after using ":s", repeat all the modifiers */
9768 if (didit)
9769 goto repeat;
9770 }
9771 }
9772
Bram Moolenaar26df0922014-02-23 23:39:13 +01009773 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9774 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009775 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009776 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009777 if (c != NUL)
9778 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009779 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009780 if (c != NUL)
9781 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009782 if (p == NULL)
9783 return -1;
9784 vim_free(*bufp);
9785 *bufp = *fnamep = p;
9786 *fnamelen = (int)STRLEN(p);
9787 *usedlen += 2;
9788 }
9789
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 return valid;
9791}
9792
9793/*
9794 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
9795 * "flags" can be "g" to do a global substitute.
9796 * Returns an allocated string, NULL for error.
9797 */
9798 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009799do_string_sub(
9800 char_u *str,
9801 char_u *pat,
9802 char_u *sub,
9803 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
9805 int sublen;
9806 regmatch_T regmatch;
9807 int i;
9808 int do_all;
9809 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009810 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 garray_T ga;
9812 char_u *ret;
9813 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009814 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9817 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009818 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819
9820 ga_init2(&ga, 1, 200);
9821
9822 do_all = (flags[0] == 'g');
9823
9824 regmatch.rm_ic = p_ic;
9825 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9826 if (regmatch.regprog != NULL)
9827 {
9828 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009829 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9831 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009832 /* Skip empty match except for first match. */
9833 if (regmatch.startp[0] == regmatch.endp[0])
9834 {
9835 if (zero_width == regmatch.startp[0])
9836 {
9837 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009838 i = MB_PTR2LEN(tail);
9839 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9840 (size_t)i);
9841 ga.ga_len += i;
9842 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009843 continue;
9844 }
9845 zero_width = regmatch.startp[0];
9846 }
9847
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 /*
9849 * Get some space for a temporary buffer to do the substitution
9850 * into. It will contain:
9851 * - The text up to where the match is.
9852 * - The substituted text.
9853 * - The text after the match.
9854 */
9855 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009856 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9858 {
9859 ga_clear(&ga);
9860 break;
9861 }
9862
9863 /* copy the text up to where the match is */
9864 i = (int)(regmatch.startp[0] - tail);
9865 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9866 /* add the substituted text */
9867 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
9868 + ga.ga_len + i, TRUE, TRUE, FALSE);
9869 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009870 tail = regmatch.endp[0];
9871 if (*tail == NUL)
9872 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 if (!do_all)
9874 break;
9875 }
9876
9877 if (ga.ga_data != NULL)
9878 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9879
Bram Moolenaar473de612013-06-08 18:19:48 +02009880 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 }
9882
9883 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9884 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009885 if (p_cpo == empty_option)
9886 p_cpo = save_cpo;
9887 else
9888 /* Darn, evaluating {sub} expression changed the value. */
9889 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 return ret;
9892}
9893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009894 static int
9895filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9896{
9897 typval_T rettv;
9898 typval_T argv[3];
9899 char_u buf[NUMBUFLEN];
9900 char_u *s;
9901 int retval = FAIL;
9902 int dummy;
9903
9904 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9905 argv[0] = vimvars[VV_KEY].vv_tv;
9906 argv[1] = vimvars[VV_VAL].vv_tv;
9907 if (expr->v_type == VAR_FUNC)
9908 {
9909 s = expr->vval.v_string;
9910 if (call_func(s, (int)STRLEN(s),
9911 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
9912 goto theend;
9913 }
9914 else if (expr->v_type == VAR_PARTIAL)
9915 {
9916 partial_T *partial = expr->vval.v_partial;
9917
9918 s = partial->pt_name;
9919 if (call_func(s, (int)STRLEN(s),
9920 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
9921 == FAIL)
9922 goto theend;
9923 }
9924 else
9925 {
9926 s = get_tv_string_buf_chk(expr, buf);
9927 if (s == NULL)
9928 goto theend;
9929 s = skipwhite(s);
9930 if (eval1(&s, &rettv, TRUE) == FAIL)
9931 goto theend;
9932 if (*s != NUL) /* check for trailing chars after expr */
9933 {
9934 EMSG2(_(e_invexpr2), s);
9935 goto theend;
9936 }
9937 }
9938 if (map)
9939 {
9940 /* map(): replace the list item value */
9941 clear_tv(tv);
9942 rettv.v_lock = 0;
9943 *tv = rettv;
9944 }
9945 else
9946 {
9947 int error = FALSE;
9948
9949 /* filter(): when expr is zero remove the item */
9950 *remp = (get_tv_number_chk(&rettv, &error) == 0);
9951 clear_tv(&rettv);
9952 /* On type error, nothing has been removed; return FAIL to stop the
9953 * loop. The error message was given by get_tv_number_chk(). */
9954 if (error)
9955 goto theend;
9956 }
9957 retval = OK;
9958theend:
9959 clear_tv(&vimvars[VV_VAL].vv_tv);
9960 return retval;
9961}
9962
9963
9964/*
9965 * Implementation of map() and filter().
9966 */
9967 void
9968filter_map(typval_T *argvars, typval_T *rettv, int map)
9969{
9970 typval_T *expr;
9971 listitem_T *li, *nli;
9972 list_T *l = NULL;
9973 dictitem_T *di;
9974 hashtab_T *ht;
9975 hashitem_T *hi;
9976 dict_T *d = NULL;
9977 typval_T save_val;
9978 typval_T save_key;
9979 int rem;
9980 int todo;
9981 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
9982 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
9983 : N_("filter() argument"));
9984 int save_did_emsg;
9985 int idx = 0;
9986
9987 if (argvars[0].v_type == VAR_LIST)
9988 {
9989 if ((l = argvars[0].vval.v_list) == NULL
9990 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
9991 return;
9992 }
9993 else if (argvars[0].v_type == VAR_DICT)
9994 {
9995 if ((d = argvars[0].vval.v_dict) == NULL
9996 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
9997 return;
9998 }
9999 else
10000 {
10001 EMSG2(_(e_listdictarg), ermsg);
10002 return;
10003 }
10004
10005 expr = &argvars[1];
10006 /* On type errors, the preceding call has already displayed an error
10007 * message. Avoid a misleading error message for an empty string that
10008 * was not passed as argument. */
10009 if (expr->v_type != VAR_UNKNOWN)
10010 {
10011 prepare_vimvar(VV_VAL, &save_val);
10012
10013 /* We reset "did_emsg" to be able to detect whether an error
10014 * occurred during evaluation of the expression. */
10015 save_did_emsg = did_emsg;
10016 did_emsg = FALSE;
10017
10018 prepare_vimvar(VV_KEY, &save_key);
10019 if (argvars[0].v_type == VAR_DICT)
10020 {
10021 vimvars[VV_KEY].vv_type = VAR_STRING;
10022
10023 ht = &d->dv_hashtab;
10024 hash_lock(ht);
10025 todo = (int)ht->ht_used;
10026 for (hi = ht->ht_array; todo > 0; ++hi)
10027 {
10028 if (!HASHITEM_EMPTY(hi))
10029 {
10030 int r;
10031
10032 --todo;
10033 di = HI2DI(hi);
10034 if (map &&
10035 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10036 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10037 break;
10038 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10039 r = filter_map_one(&di->di_tv, expr, map, &rem);
10040 clear_tv(&vimvars[VV_KEY].vv_tv);
10041 if (r == FAIL || did_emsg)
10042 break;
10043 if (!map && rem)
10044 {
10045 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10046 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10047 break;
10048 dictitem_remove(d, di);
10049 }
10050 }
10051 }
10052 hash_unlock(ht);
10053 }
10054 else
10055 {
10056 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10057
10058 for (li = l->lv_first; li != NULL; li = nli)
10059 {
10060 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10061 break;
10062 nli = li->li_next;
10063 vimvars[VV_KEY].vv_nr = idx;
10064 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10065 || did_emsg)
10066 break;
10067 if (!map && rem)
10068 listitem_remove(l, li);
10069 ++idx;
10070 }
10071 }
10072
10073 restore_vimvar(VV_KEY, &save_key);
10074 restore_vimvar(VV_VAL, &save_val);
10075
10076 did_emsg |= save_did_emsg;
10077 }
10078
10079 copy_tv(&argvars[0], rettv);
10080}
10081
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */