blob: 495bbb0697698753332d1206b81ae2623d0388ba [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 Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000190};
191
192/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000193#define vv_type vv_di.di_tv.v_type
194#define vv_nr vv_di.di_tv.vval.v_number
195#define vv_float vv_di.di_tv.vval.v_float
196#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000197#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200198#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000200
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200201static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define vimvarht vimvardict.dv_hashtab
203
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100204static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
205static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
206static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100207static void list_glob_vars(int *first);
208static void list_buf_vars(int *first);
209static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000210#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000212#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_vim_vars(int *first);
214static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
216static 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 +0100217static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
218static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
220static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
221static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
222static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000223
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static int eval2(char_u **arg, typval_T *rettv, int evaluate);
225static int eval3(char_u **arg, typval_T *rettv, int evaluate);
226static int eval4(char_u **arg, typval_T *rettv, int evaluate);
227static int eval5(char_u **arg, typval_T *rettv, int evaluate);
228static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
229static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000230
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
233static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar33570922005-01-25 22:26:29 +0000236
Bram Moolenaar33570922005-01-25 22:26:29 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200240static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static void delete_var(hashtab_T *ht, hashitem_T *hi);
243static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
244static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200246
247#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int compare_func_name(const void *s1, const void *s2);
249static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200250#endif
251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaar33570922005-01-25 22:26:29 +0000255/*
256 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000257 */
258 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100259eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000260{
Bram Moolenaar33570922005-01-25 22:26:29 +0000261 int i;
262 struct vimvar *p;
263
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200264 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
265 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200266 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000267 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000269
270 for (i = 0; i < VV_LEN; ++i)
271 {
272 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200273 if (STRLEN(p->vv_name) > 16)
274 {
275 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
276 getout(1);
277 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 STRCPY(p->vv_di.di_key, p->vv_name);
279 if (p->vv_flags & VV_RO)
280 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
281 else if (p->vv_flags & VV_RO_SBX)
282 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
283 else
284 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000285
286 /* add to v: scope dict, unless the value is not always available */
287 if (p->vv_type != VAR_UNKNOWN)
288 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000289 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000290 /* add to compat scope dict */
291 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000292 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100293 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
294
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000295 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100296 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200297 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100298 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100299
300 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
301 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
302 set_vim_var_nr(VV_NONE, VVAL_NONE);
303 set_vim_var_nr(VV_NULL, VVAL_NULL);
304
Bram Moolenaarf562e722016-07-19 17:25:25 +0200305 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
306 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
307 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
308 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
309 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
310 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
311 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
312 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
313 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
314 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
315
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200316 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200317
318#ifdef EBCDIC
319 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100320 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200321 */
322 sortFunctions();
323#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000324}
325
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000326#if defined(EXITFREE) || defined(PROTO)
327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000329{
330 int i;
331 struct vimvar *p;
332
333 for (i = 0; i < VV_LEN; ++i)
334 {
335 p = &vimvars[i];
336 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000337 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000338 vim_free(p->vv_str);
339 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000340 }
341 else if (p->vv_di.di_tv.v_type == VAR_LIST)
342 {
343 list_unref(p->vv_list);
344 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000345 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000346 }
347 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000348 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000349 hash_clear(&compat_hashtab);
350
Bram Moolenaard9fba312005-06-26 22:34:35 +0000351 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100352# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200353 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100354# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000355
356 /* global variables */
357 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000358
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000359 /* autoloaded script names */
360 ga_clear_strings(&ga_loaded);
361
Bram Moolenaarcca74132013-09-25 21:00:28 +0200362 /* Script-local variables. First clear all the variables and in a second
363 * loop free the scriptvar_T, because a variable in one script might hold
364 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200365 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200366 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200367 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200368 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200369 ga_clear(&ga_scripts);
370
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000371 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200372 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373
374 /* functions */
375 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000376}
377#endif
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379
380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * Set an internal variable to a string value. Creates the variable if it does
382 * not already exist.
383 */
384 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100385set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000387 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000388 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
390 val = vim_strsave(value);
391 if (val != NULL)
392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000393 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000394 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000396 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000397 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
399 }
400}
401
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000402static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200403#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000404static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000405static char_u *redir_endp = NULL;
406static char_u *redir_varname = NULL;
407
408/*
409 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100410 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000411 * Returns OK if successfully completed the setup. FAIL otherwise.
412 */
413 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100414var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000415{
416 int save_emsg;
417 int err;
418 typval_T tv;
419
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000420 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000421 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000422 {
423 EMSG(_(e_invarg));
424 return FAIL;
425 }
426
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000427 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000428 redir_varname = vim_strsave(name);
429 if (redir_varname == NULL)
430 return FAIL;
431
432 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
433 if (redir_lval == NULL)
434 {
435 var_redir_stop();
436 return FAIL;
437 }
438
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000439 /* The output is stored in growarray "redir_ga" until redirection ends. */
440 ga_init2(&redir_ga, (int)sizeof(char), 500);
441
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000442 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100443 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000444 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000445 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
446 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200447 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000448 if (redir_endp != NULL && *redir_endp != NUL)
449 /* Trailing characters are present after the variable name */
450 EMSG(_(e_trailing));
451 else
452 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000453 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000454 var_redir_stop();
455 return FAIL;
456 }
457
458 /* check if we can write to the variable: set it to or append an empty
459 * string */
460 save_emsg = did_emsg;
461 did_emsg = FALSE;
462 tv.v_type = VAR_STRING;
463 tv.vval.v_string = (char_u *)"";
464 if (append)
465 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
466 else
467 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200468 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000470 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471 if (err)
472 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000473 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474 var_redir_stop();
475 return FAIL;
476 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477
478 return OK;
479}
480
481/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000482 * Append "value[value_len]" to the variable set by var_redir_start().
483 * The actual appending is postponed until redirection ends, because the value
484 * appended may in fact be the string we write to, changing it may cause freed
485 * memory to be used:
486 * :redir => foo
487 * :let foo
488 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489 */
490 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100491var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000493 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494
495 if (redir_lval == NULL)
496 return;
497
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000498 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000499 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000501 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000503 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000504 {
505 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000506 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000507 }
508 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000509 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000510}
511
512/*
513 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000514 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 */
516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100517var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000519 typval_T tv;
520
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200521 if (EVALCMD_BUSY)
522 {
523 redir_lval = NULL;
524 return;
525 }
526
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527 if (redir_lval != NULL)
528 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000529 /* If there was no error: assign the text to the variable. */
530 if (redir_endp != NULL)
531 {
532 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
533 tv.v_type = VAR_STRING;
534 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200535 /* Call get_lval() again, if it's inside a Dict or List it may
536 * have changed. */
537 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100538 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200539 if (redir_endp != NULL && redir_lval->ll_name != NULL)
540 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
541 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000544 /* free the collected output */
545 vim_free(redir_ga.ga_data);
546 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000548 vim_free(redir_lval);
549 redir_lval = NULL;
550 }
551 vim_free(redir_varname);
552 redir_varname = NULL;
553}
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555# if defined(FEAT_MBYTE) || defined(PROTO)
556 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100557eval_charconvert(
558 char_u *enc_from,
559 char_u *enc_to,
560 char_u *fname_from,
561 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
563 int err = FALSE;
564
565 set_vim_var_string(VV_CC_FROM, enc_from, -1);
566 set_vim_var_string(VV_CC_TO, enc_to, -1);
567 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
568 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
569 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
570 err = TRUE;
571 set_vim_var_string(VV_CC_FROM, NULL, -1);
572 set_vim_var_string(VV_CC_TO, NULL, -1);
573 set_vim_var_string(VV_FNAME_IN, NULL, -1);
574 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
575
576 if (err)
577 return FAIL;
578 return OK;
579}
580# endif
581
582# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100584eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 int err = FALSE;
587
588 set_vim_var_string(VV_FNAME_IN, fname, -1);
589 set_vim_var_string(VV_CMDARG, args, -1);
590 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
591 err = TRUE;
592 set_vim_var_string(VV_FNAME_IN, NULL, -1);
593 set_vim_var_string(VV_CMDARG, NULL, -1);
594
595 if (err)
596 {
597 mch_remove(fname);
598 return FAIL;
599 }
600 return OK;
601}
602# endif
603
604# if defined(FEAT_DIFF) || defined(PROTO)
605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606eval_diff(
607 char_u *origfile,
608 char_u *newfile,
609 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, origfile, -1);
614 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
615 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
616 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
619 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
620}
621
622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100623eval_patch(
624 char_u *origfile,
625 char_u *difffile,
626 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 int err;
629
630 set_vim_var_string(VV_FNAME_IN, origfile, -1);
631 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
632 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
633 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
634 set_vim_var_string(VV_FNAME_IN, NULL, -1);
635 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
636 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
637}
638# endif
639
640/*
641 * Top level evaluation function, returning a boolean.
642 * Sets "error" to TRUE if there was an error.
643 * Return TRUE or FALSE.
644 */
645 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100646eval_to_bool(
647 char_u *arg,
648 int *error,
649 char_u **nextcmd,
650 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar33570922005-01-25 22:26:29 +0000652 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200653 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655 if (skip)
656 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000657 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 else
660 {
661 *error = FALSE;
662 if (!skip)
663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000665 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667 }
668 if (skip)
669 --emsg_skip;
670
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200671 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672}
673
674/*
675 * Top level evaluation function, returning a string. If "skip" is TRUE,
676 * only parsing to "nextcmd" is done, without reporting errors. Return
677 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
678 */
679 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100680eval_to_string_skip(
681 char_u *arg,
682 char_u **nextcmd,
683 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar33570922005-01-25 22:26:29 +0000685 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 char_u *retval;
687
688 if (skip)
689 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 retval = NULL;
692 else
693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000694 retval = vim_strsave(get_tv_string(&tv));
695 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 if (skip)
698 --emsg_skip;
699
700 return retval;
701}
702
703/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000704 * Skip over an expression at "*pp".
705 * Return FAIL for an error, OK otherwise.
706 */
707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100708skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000709{
Bram Moolenaar33570922005-01-25 22:26:29 +0000710 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000711
712 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000713 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000714}
715
716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000718 * When "convert" is TRUE convert a List into a sequence of lines and convert
719 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * Return pointer to allocated memory, or NULL for failure.
721 */
722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723eval_to_string(
724 char_u *arg,
725 char_u **nextcmd,
726 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000730 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000731#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000732 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 retval = NULL;
737 else
738 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000739 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000740 {
741 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000742 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200743 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200744 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200745 if (tv.vval.v_list->lv_len > 0)
746 ga_append(&ga, NL);
747 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000748 ga_append(&ga, NUL);
749 retval = (char_u *)ga.ga_data;
750 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000751#ifdef FEAT_FLOAT
752 else if (convert && tv.v_type == VAR_FLOAT)
753 {
754 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
755 retval = vim_strsave(numbuf);
756 }
757#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000758 else
759 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000760 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762
763 return retval;
764}
765
766/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 * Call eval_to_string() without using current local variables and using
768 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 */
770 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100771eval_to_string_safe(
772 char_u *arg,
773 char_u **nextcmd,
774 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 char_u *retval;
777 void *save_funccalp;
778
779 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000780 if (use_sandbox)
781 ++sandbox;
782 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000783 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000784 if (use_sandbox)
785 --sandbox;
786 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 restore_funccal(save_funccalp);
788 return retval;
789}
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
792 * Top level evaluation function, returning a number.
793 * Evaluates "expr" silently.
794 * Returns -1 for an error.
795 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200796 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200800 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000801 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 ++emsg_off;
804
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000805 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 retval = -1;
807 else
808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000809 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 --emsg_off;
813
814 return retval;
815}
816
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817/*
818 * Prepare v: variable "idx" to be used.
819 * Save the current typeval in "save_tv".
820 * When not used yet add the variable to the v: hashtable.
821 */
822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000824{
825 *save_tv = vimvars[idx].vv_tv;
826 if (vimvars[idx].vv_type == VAR_UNKNOWN)
827 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
828}
829
830/*
831 * Restore v: variable "idx" to typeval "save_tv".
832 * When no longer defined, remove the variable from the v: hashtable.
833 */
834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836{
837 hashitem_T *hi;
838
Bram Moolenaara40058a2005-07-11 22:42:07 +0000839 vimvars[idx].vv_tv = *save_tv;
840 if (vimvars[idx].vv_type == VAR_UNKNOWN)
841 {
842 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
843 if (HASHITEM_EMPTY(hi))
844 EMSG2(_(e_intern2), "restore_vimvar()");
845 else
846 hash_remove(&vimvarht, hi);
847 }
848}
849
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000850#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000851/*
852 * Evaluate an expression to a list with suggestions.
853 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000854 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000855 */
856 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000858{
859 typval_T save_val;
860 typval_T rettv;
861 list_T *list = NULL;
862 char_u *p = skipwhite(expr);
863
864 /* Set "v:val" to the bad word. */
865 prepare_vimvar(VV_VAL, &save_val);
866 vimvars[VV_VAL].vv_type = VAR_STRING;
867 vimvars[VV_VAL].vv_str = badword;
868 if (p_verbose == 0)
869 ++emsg_off;
870
871 if (eval1(&p, &rettv, TRUE) == OK)
872 {
873 if (rettv.v_type != VAR_LIST)
874 clear_tv(&rettv);
875 else
876 list = rettv.vval.v_list;
877 }
878
879 if (p_verbose == 0)
880 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000881 restore_vimvar(VV_VAL, &save_val);
882
883 return list;
884}
885
886/*
887 * "list" is supposed to contain two items: a word and a number. Return the
888 * word in "pp" and the number as the return value.
889 * Return -1 if anything isn't right.
890 * Used to get the good word and score from the eval_spell_expr() result.
891 */
892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000894{
895 listitem_T *li;
896
897 li = list->lv_first;
898 if (li == NULL)
899 return -1;
900 *pp = get_tv_string(&li->li_tv);
901
902 li = li->li_next;
903 if (li == NULL)
904 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200905 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000906}
907#endif
908
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000909/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000910 * Top level evaluation function.
911 * Returns an allocated typval_T with the result.
912 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000913 */
914 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000916{
917 typval_T *tv;
918
919 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000920 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000921 {
922 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000923 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000924 }
925
926 return tv;
927}
928
929
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000931 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932 * Uses argv[argc] for the function arguments. Only Number and String
933 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000934 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937call_vim_function(
938 char_u *func,
939 int argc,
940 char_u **argv,
941 int safe, /* use the sandbox */
942 int str_arg_only, /* all arguments are strings */
943 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
Bram Moolenaar33570922005-01-25 22:26:29 +0000945 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200946 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 int len;
948 int i;
949 int doesrange;
950 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000951 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000953 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000955 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 for (i = 0; i < argc; i++)
958 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000959 /* Pass a NULL or empty argument as an empty string */
960 if (argv[i] == NULL || *argv[i] == NUL)
961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000962 argvars[i].v_type = VAR_STRING;
963 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000964 continue;
965 }
966
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200967 if (str_arg_only)
968 len = 0;
969 else
970 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100971 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (len != 0 && len == (int)STRLEN(argv[i]))
973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000974 argvars[i].v_type = VAR_NUMBER;
975 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 else
978 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000979 argvars[i].v_type = VAR_STRING;
980 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982 }
983
984 if (safe)
985 {
986 save_funccalp = save_funccal();
987 ++sandbox;
988 }
989
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000990 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200991 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100993 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (safe)
995 {
996 --sandbox;
997 restore_funccal(save_funccalp);
998 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000999 vim_free(argvars);
1000
1001 if (ret == FAIL)
1002 clear_tv(rettv);
1003
1004 return ret;
1005}
1006
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001007/*
1008 * Call vimL function "func" and return the result as a number.
1009 * Returns -1 when calling the function fails.
1010 * Uses argv[argc] for the function arguments.
1011 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001012 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013call_func_retnr(
1014 char_u *func,
1015 int argc,
1016 char_u **argv,
1017 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001018{
1019 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001020 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001021
1022 /* All arguments are passed as strings, no conversion to number. */
1023 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1024 return -1;
1025
1026 retval = get_tv_number_chk(&rettv, NULL);
1027 clear_tv(&rettv);
1028 return retval;
1029}
1030
1031#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1032 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1033
Bram Moolenaar4f688582007-07-24 12:34:30 +00001034# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001035/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001036 * Call vimL function "func" and return the result as a string.
1037 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001038 * Uses argv[argc] for the function arguments.
1039 */
1040 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001041call_func_retstr(
1042 char_u *func,
1043 int argc,
1044 char_u **argv,
1045 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001046{
1047 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001048 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001049
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001050 /* All arguments are passed as strings, no conversion to number. */
1051 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001052 return NULL;
1053
1054 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001055 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 return retval;
1057}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001058# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001059
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001060/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001061 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001063 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001064 */
1065 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001066call_func_retlist(
1067 char_u *func,
1068 int argc,
1069 char_u **argv,
1070 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071{
1072 typval_T rettv;
1073
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001074 /* All arguments are passed as strings, no conversion to number. */
1075 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001076 return NULL;
1077
1078 if (rettv.v_type != VAR_LIST)
1079 {
1080 clear_tv(&rettv);
1081 return NULL;
1082 }
1083
1084 return rettv.vval.v_list;
1085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086#endif
1087
Bram Moolenaar05159a02005-02-26 23:04:13 +00001088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef FEAT_FOLDING
1090/*
1091 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1092 * it in "*cp". Doesn't give error messages.
1093 */
1094 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001095eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096{
Bram Moolenaar33570922005-01-25 22:26:29 +00001097 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001098 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001100 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1101 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001104 if (use_sandbox)
1105 ++sandbox;
1106 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001108 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 retval = 0;
1110 else
1111 {
1112 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (tv.v_type == VAR_NUMBER)
1114 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001115 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 retval = 0;
1117 else
1118 {
1119 /* If the result is a string, check if there is a non-digit before
1120 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (!VIM_ISDIGIT(*s) && *s != '-')
1123 *cp = *s++;
1124 retval = atol((char *)s);
1125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001129 if (use_sandbox)
1130 --sandbox;
1131 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001133 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134}
1135#endif
1136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 * ":let" list all variable values
1139 * ":let var1 var2" list variable values
1140 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001141 * ":let var += expr" assignment command.
1142 * ":let var -= expr" assignment command.
1143 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 */
1146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001147ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001151 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 int var_count = 0;
1154 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001155 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001156 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001157 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaardb552d602006-03-23 22:59:57 +00001159 argend = skip_var_list(arg, &var_count, &semicolon);
1160 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001161 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001162 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1163 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001164 expr = skipwhite(argend);
1165 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1166 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001168 /*
1169 * ":let" without "=": list variables
1170 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001171 if (*arg == '[')
1172 EMSG(_(e_invarg));
1173 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001175 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001176 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001179 list_glob_vars(&first);
1180 list_buf_vars(&first);
1181 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001182#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001183 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001184#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001185 list_script_vars(&first);
1186 list_func_vars(&first);
1187 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 eap->nextcmd = check_nextcmd(arg);
1190 }
1191 else
1192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001193 op[0] = '=';
1194 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001195 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001196 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001197 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1198 op[0] = *expr; /* +=, -= or .= */
1199 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001200 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001201 else
1202 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (eap->skip)
1205 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001206 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (eap->skip)
1208 {
1209 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 --emsg_skip;
1212 }
1213 else if (i != FAIL)
1214 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001215 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001216 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220}
1221
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001222/*
1223 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1224 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001225 * When "nextchars" is not NULL it points to a string with characters that
1226 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1227 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 * Returns OK or FAIL;
1229 */
1230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001231ex_let_vars(
1232 char_u *arg_start,
1233 typval_T *tv,
1234 int copy, /* copy values from "tv", don't move */
1235 int semicolon, /* from skip_var_list() */
1236 int var_count, /* from skip_var_list() */
1237 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001238{
1239 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001240 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001241 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001242 listitem_T *item;
1243 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244
1245 if (*arg != '[')
1246 {
1247 /*
1248 * ":let var = expr" or ":for var in list"
1249 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001250 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001251 return FAIL;
1252 return OK;
1253 }
1254
1255 /*
1256 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1257 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001258 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259 {
1260 EMSG(_(e_listreq));
1261 return FAIL;
1262 }
1263
1264 i = list_len(l);
1265 if (semicolon == 0 && var_count < i)
1266 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001267 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001268 return FAIL;
1269 }
1270 if (var_count - semicolon > i)
1271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001272 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 }
1275
1276 item = l->lv_first;
1277 while (*arg != ']')
1278 {
1279 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281 item = item->li_next;
1282 if (arg == NULL)
1283 return FAIL;
1284
1285 arg = skipwhite(arg);
1286 if (*arg == ';')
1287 {
1288 /* Put the rest of the list (may be empty) in the var after ';'.
1289 * Create a new list for this. */
1290 l = list_alloc();
1291 if (l == NULL)
1292 return FAIL;
1293 while (item != NULL)
1294 {
1295 list_append_tv(l, &item->li_tv);
1296 item = item->li_next;
1297 }
1298
1299 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001300 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001301 ltv.vval.v_list = l;
1302 l->lv_refcount = 1;
1303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1305 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001306 clear_tv(&ltv);
1307 if (arg == NULL)
1308 return FAIL;
1309 break;
1310 }
1311 else if (*arg != ',' && *arg != ']')
1312 {
1313 EMSG2(_(e_intern2), "ex_let_vars()");
1314 return FAIL;
1315 }
1316 }
1317
1318 return OK;
1319}
1320
1321/*
1322 * Skip over assignable variable "var" or list of variables "[var, var]".
1323 * Used for ":let varvar = expr" and ":for varvar in expr".
1324 * For "[var, var]" increment "*var_count" for each variable.
1325 * for "[var, var; var]" set "semicolon".
1326 * Return NULL for an error.
1327 */
1328 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329skip_var_list(
1330 char_u *arg,
1331 int *var_count,
1332 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001333{
1334 char_u *p, *s;
1335
1336 if (*arg == '[')
1337 {
1338 /* "[var, var]": find the matching ']'. */
1339 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001340 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001341 {
1342 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1343 s = skip_var_one(p);
1344 if (s == p)
1345 {
1346 EMSG2(_(e_invarg2), p);
1347 return NULL;
1348 }
1349 ++*var_count;
1350
1351 p = skipwhite(s);
1352 if (*p == ']')
1353 break;
1354 else if (*p == ';')
1355 {
1356 if (*semicolon == 1)
1357 {
1358 EMSG(_("Double ; in list of variables"));
1359 return NULL;
1360 }
1361 *semicolon = 1;
1362 }
1363 else if (*p != ',')
1364 {
1365 EMSG2(_(e_invarg2), p);
1366 return NULL;
1367 }
1368 }
1369 return p + 1;
1370 }
1371 else
1372 return skip_var_one(arg);
1373}
1374
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001375/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001376 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001377 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001378 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001379 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001381{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001382 if (*arg == '@' && arg[1] != NUL)
1383 return arg + 2;
1384 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1385 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386}
1387
Bram Moolenaara7043832005-01-21 11:56:39 +00001388/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 * List variables for hashtab "ht" with prefix "prefix".
1390 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001391 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001393list_hashtable_vars(
1394 hashtab_T *ht,
1395 char_u *prefix,
1396 int empty,
1397 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 hashitem_T *hi;
1400 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001401 int todo;
1402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001403 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001404 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1405 {
1406 if (!HASHITEM_EMPTY(hi))
1407 {
1408 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 di = HI2DI(hi);
1410 if (empty || di->di_tv.v_type != VAR_STRING
1411 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001412 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 }
1414 }
1415}
1416
1417/*
1418 * List global variables.
1419 */
1420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001421list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001422{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001423 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001424}
1425
1426/*
1427 * List buffer variables.
1428 */
1429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001431{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001432 char_u numbuf[NUMBUFLEN];
1433
Bram Moolenaar429fa852013-04-15 12:27:36 +02001434 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001435 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001436
1437 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001438 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1439 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001440}
1441
1442/*
1443 * List window variables.
1444 */
1445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001447{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001448 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001449 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001450}
1451
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001452#ifdef FEAT_WINDOWS
1453/*
1454 * List tab page variables.
1455 */
1456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001458{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001459 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001460 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001461}
1462#endif
1463
Bram Moolenaara7043832005-01-21 11:56:39 +00001464/*
1465 * List Vim variables.
1466 */
1467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001468list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001470 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001471}
1472
1473/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001474 * List script-local variables, if there is a script.
1475 */
1476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001478{
1479 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001480 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1481 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001482}
1483
1484/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 * List variables in "arg".
1486 */
1487 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001488list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489{
1490 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001491 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001493 char_u *name_start;
1494 char_u *arg_subsc;
1495 char_u *tofree;
1496 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497
1498 while (!ends_excmd(*arg) && !got_int)
1499 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001500 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001502 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001503 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1504 {
1505 emsg_severe = TRUE;
1506 EMSG(_(e_trailing));
1507 break;
1508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001512 /* get_name_len() takes care of expanding curly braces */
1513 name_start = name = arg;
1514 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1515 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001517 /* This is mainly to keep test 49 working: when expanding
1518 * curly braces fails overrule the exception error message. */
1519 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001521 emsg_severe = TRUE;
1522 EMSG2(_(e_invarg2), arg);
1523 break;
1524 }
1525 error = TRUE;
1526 }
1527 else
1528 {
1529 if (tofree != NULL)
1530 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001531 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 else
1534 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 /* handle d.key, l[idx], f(expr) */
1536 arg_subsc = arg;
1537 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001538 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001544 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001545 case 'g': list_glob_vars(first); break;
1546 case 'b': list_buf_vars(first); break;
1547 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001548#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001549 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001550#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001551 case 'v': list_vim_vars(first); break;
1552 case 's': list_script_vars(first); break;
1553 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001554 default:
1555 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001557 }
1558 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 {
1560 char_u numbuf[NUMBUFLEN];
1561 char_u *tf;
1562 int c;
1563 char_u *s;
1564
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001565 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 c = *arg;
1567 *arg = NUL;
1568 list_one_var_a((char_u *)"",
1569 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001570 tv.v_type,
1571 s == NULL ? (char_u *)"" : s,
1572 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001573 *arg = c;
1574 vim_free(tf);
1575 }
1576 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 }
1579 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001580
1581 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583
1584 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 }
1586
1587 return arg;
1588}
1589
1590/*
1591 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1592 * Returns a pointer to the char just after the var name.
1593 * Returns NULL if there is an error.
1594 */
1595 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596ex_let_one(
1597 char_u *arg, /* points to variable name */
1598 typval_T *tv, /* value to assign to variable */
1599 int copy, /* copy value from "tv" */
1600 char_u *endchars, /* valid chars after variable name or NULL */
1601 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602{
1603 int c1;
1604 char_u *name;
1605 char_u *p;
1606 char_u *arg_end = NULL;
1607 int len;
1608 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610
1611 /*
1612 * ":let $VAR = expr": Set environment variable.
1613 */
1614 if (*arg == '$')
1615 {
1616 /* Find the end of the name. */
1617 ++arg;
1618 name = arg;
1619 len = get_env_len(&arg);
1620 if (len == 0)
1621 EMSG2(_(e_invarg2), name - 1);
1622 else
1623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 if (op != NULL && (*op == '+' || *op == '-'))
1625 EMSG2(_(e_letwrong), op);
1626 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001629 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 {
1631 c1 = name[len];
1632 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001633 p = get_tv_string_chk(tv);
1634 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 {
1636 int mustfree = FALSE;
1637 char_u *s = vim_getenv(name, &mustfree);
1638
1639 if (s != NULL)
1640 {
1641 p = tofree = concat_str(s, p);
1642 if (mustfree)
1643 vim_free(s);
1644 }
1645 }
1646 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649 if (STRICMP(name, "HOME") == 0)
1650 init_homedir();
1651 else if (didset_vim && STRICMP(name, "VIM") == 0)
1652 didset_vim = FALSE;
1653 else if (didset_vimruntime
1654 && STRICMP(name, "VIMRUNTIME") == 0)
1655 didset_vimruntime = FALSE;
1656 arg_end = arg;
1657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 }
1661 }
1662 }
1663
1664 /*
1665 * ":let &option = expr": Set option value.
1666 * ":let &l:option = expr": Set local option value.
1667 * ":let &g:option = expr": Set global option value.
1668 */
1669 else if (*arg == '&')
1670 {
1671 /* Find the end of the name. */
1672 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 if (p == NULL || (endchars != NULL
1674 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 EMSG(_(e_letunexp));
1676 else
1677 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 long n;
1679 int opt_type;
1680 long numval;
1681 char_u *stringval = NULL;
1682 char_u *s;
1683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 c1 = *p;
1685 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001687 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001688 s = get_tv_string_chk(tv); /* != NULL if number or string */
1689 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 {
1691 opt_type = get_option_value(arg, &numval,
1692 &stringval, opt_flags);
1693 if ((opt_type == 1 && *op == '.')
1694 || (opt_type == 0 && *op != '.'))
1695 EMSG2(_(e_letwrong), op);
1696 else
1697 {
1698 if (opt_type == 1) /* number */
1699 {
1700 if (*op == '+')
1701 n = numval + n;
1702 else
1703 n = numval - n;
1704 }
1705 else if (opt_type == 0 && stringval != NULL) /* string */
1706 {
1707 s = concat_str(stringval, s);
1708 vim_free(stringval);
1709 stringval = s;
1710 }
1711 }
1712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001713 if (s != NULL)
1714 {
1715 set_option_value(arg, n, s, opt_flags);
1716 arg_end = p;
1717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 }
1721 }
1722
1723 /*
1724 * ":let @r = expr": Set register contents.
1725 */
1726 else if (*arg == '@')
1727 {
1728 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001729 if (op != NULL && (*op == '+' || *op == '-'))
1730 EMSG2(_(e_letwrong), op);
1731 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 EMSG(_(e_letunexp));
1734 else
1735 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 char_u *s;
1738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001739 p = get_tv_string_chk(tv);
1740 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001742 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 if (s != NULL)
1744 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001745 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 vim_free(s);
1747 }
1748 }
1749 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001750 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001752 arg_end = arg + 1;
1753 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001754 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 }
1756 }
1757
1758 /*
1759 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001760 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001762 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001766 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001767 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001769 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1770 EMSG(_(e_letunexp));
1771 else
1772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001774 arg_end = p;
1775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001777 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 }
1779
1780 else
1781 EMSG2(_(e_invarg2), arg);
1782
1783 return arg_end;
1784}
1785
1786/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001787 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1788 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001791{
1792 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1793 {
1794 EMSG2(_(e_readonlyvar), arg);
1795 return TRUE;
1796 }
1797 return FALSE;
1798}
1799
1800/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 * Get an lval: variable, Dict item or List item that can be assigned a value
1802 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1803 * "name.key", "name.key[expr]" etc.
1804 * Indexing only works if "name" is an existing List or Dictionary.
1805 * "name" points to the start of the name.
1806 * If "rettv" is not NULL it points to the value to be assigned.
1807 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1808 * wrong; must end in space or cmd separator.
1809 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001810 * flags:
1811 * GLV_QUIET: do not give error messages
1812 * GLV_NO_AUTOLOAD: do not use script autoloading
1813 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001815 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001816 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819get_lval(
1820 char_u *name,
1821 typval_T *rettv,
1822 lval_T *lp,
1823 int unlet,
1824 int skip,
1825 int flags, /* GLV_ values */
1826 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001829 char_u *expr_start, *expr_end;
1830 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 dictitem_T *v;
1832 typval_T var1;
1833 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001834 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001836 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001837 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001839 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843
1844 if (skip)
1845 {
1846 /* When skipping just find the end of the name. */
1847 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001848 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001849 }
1850
1851 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001852 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 if (expr_start != NULL)
1854 {
1855 /* Don't expand the name when we already know there is an error. */
1856 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1857 && *p != '[' && *p != '.')
1858 {
1859 EMSG(_(e_trailing));
1860 return NULL;
1861 }
1862
1863 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1864 if (lp->ll_exp_name == NULL)
1865 {
1866 /* Report an invalid expression in braces, unless the
1867 * expression evaluation has been cancelled due to an
1868 * aborting error, an interrupt, or an exception. */
1869 if (!aborting() && !quiet)
1870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001871 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 EMSG2(_(e_invarg2), name);
1873 return NULL;
1874 }
1875 }
1876 lp->ll_name = lp->ll_exp_name;
1877 }
1878 else
1879 lp->ll_name = name;
1880
1881 /* Without [idx] or .key we are done. */
1882 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1883 return p;
1884
1885 cc = *p;
1886 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001887 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 if (v == NULL && !quiet)
1889 EMSG2(_(e_undefvar), lp->ll_name);
1890 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 if (v == NULL)
1892 return NULL;
1893
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 /*
1895 * Loop until no more [idx] or .key is following.
1896 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1901 && !(lp->ll_tv->v_type == VAR_DICT
1902 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (!quiet)
1905 EMSG(_("E689: Can only index a List or Dictionary"));
1906 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (!quiet)
1911 EMSG(_("E708: [:] must come last"));
1912 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001914
Bram Moolenaar8c711452005-01-14 21:53:12 +00001915 len = -1;
1916 if (*p == '.')
1917 {
1918 key = p + 1;
1919 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1920 ;
1921 if (len == 0)
1922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923 if (!quiet)
1924 EMSG(_(e_emptykey));
1925 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001926 }
1927 p = key + len;
1928 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001929 else
1930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001932 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001933 if (*p == ':')
1934 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001935 else
1936 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001937 empty1 = FALSE;
1938 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940 if (get_tv_string_chk(&var1) == NULL)
1941 {
1942 /* not a number or string */
1943 clear_tv(&var1);
1944 return NULL;
1945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001946 }
1947
1948 /* Optionally get the second index [ :expr]. */
1949 if (*p == ':')
1950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955 if (!empty1)
1956 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (rettv != NULL && (rettv->v_type != VAR_LIST
1960 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (!quiet)
1963 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 if (!empty1)
1965 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001967 }
1968 p = skipwhite(p + 1);
1969 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 else
1972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 if (!empty1)
1977 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001979 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001980 if (get_tv_string_chk(&var2) == NULL)
1981 {
1982 /* not a number or string */
1983 if (!empty1)
1984 clear_tv(&var1);
1985 clear_tv(&var2);
1986 return NULL;
1987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001990 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001993
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 if (!quiet)
1997 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 if (!empty1)
1999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 }
2004
2005 /* Skip to past ']'. */
2006 ++p;
2007 }
2008
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002010 {
2011 if (len == -1)
2012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002014 key = get_tv_string_chk(&var1); /* is number or string */
2015 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 }
2020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 lp->ll_list = NULL;
2022 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002023 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002024
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002025 /* When assigning to a scope dictionary check that a function and
2026 * variable name is valid (only variable name unless it is l: or
2027 * g: dictionary). Disallow overwriting a builtin function. */
2028 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002029 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002030 int prevval;
2031 int wrong;
2032
2033 if (len != -1)
2034 {
2035 prevval = key[len];
2036 key[len] = NUL;
2037 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002038 else
2039 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002040 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2041 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002042 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002043 || !valid_varname(key);
2044 if (len != -1)
2045 key[len] = prevval;
2046 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047 return NULL;
2048 }
2049
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 /* Can't add "v:" variable. */
2053 if (lp->ll_dict == &vimvardict)
2054 {
2055 EMSG2(_(e_illvar), name);
2056 return NULL;
2057 }
2058
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002059 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002063 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 if (len == -1)
2065 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 }
2068 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 if (len == -1)
2073 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002075 p = NULL;
2076 break;
2077 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002078 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002079 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 return NULL;
2081
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 if (len == -1)
2083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 }
2086 else
2087 {
2088 /*
2089 * Get the number and item for the only or first index of the List.
2090 */
2091 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002093 else
2094 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002095 lp->ll_n1 = (long)get_tv_number(&var1);
2096 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002097 clear_tv(&var1);
2098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 lp->ll_list = lp->ll_tv->vval.v_list;
2101 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2102 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002104 if (lp->ll_n1 < 0)
2105 {
2106 lp->ll_n1 = 0;
2107 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2108 }
2109 }
2110 if (lp->ll_li == NULL)
2111 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002114 if (!quiet)
2115 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 }
2118
2119 /*
2120 * May need to find the item or absolute index for the second
2121 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 * When no index given: "lp->ll_empty2" is TRUE.
2123 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002127 lp->ll_n2 = (long)get_tv_number(&var2);
2128 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002134 {
2135 if (!quiet)
2136 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002138 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 }
2141
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002142 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2143 if (lp->ll_n1 < 0)
2144 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2145 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002146 {
2147 if (!quiet)
2148 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002150 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002151 }
2152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 }
2156
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 return p;
2158}
2159
2160/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165{
2166 vim_free(lp->ll_exp_name);
2167 vim_free(lp->ll_newkey);
2168}
2169
2170/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002171 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 */
2175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176set_var_lval(
2177 lval_T *lp,
2178 char_u *endp,
2179 typval_T *rettv,
2180 int copy,
2181 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182{
2183 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 listitem_T *ri;
2185 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186
2187 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 cc = *endp;
2192 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 if (op != NULL && *op != '=')
2194 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002195 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002197 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002198 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002199 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002200 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002201 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002202 if ((di == NULL
2203 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2204 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2205 FALSE)))
2206 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 set_var(lp->ll_name, &tv, FALSE);
2208 clear_tv(&tv);
2209 }
2210 }
2211 else
2212 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002216 else if (tv_check_lock(lp->ll_newkey == NULL
2217 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002218 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002219 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 else if (lp->ll_range)
2221 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002222 listitem_T *ll_li = lp->ll_li;
2223 int ll_n1 = lp->ll_n1;
2224
2225 /*
2226 * Check whether any of the list items is locked
2227 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002228 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002229 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002230 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002231 return;
2232 ri = ri->li_next;
2233 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2234 break;
2235 ll_li = ll_li->li_next;
2236 ++ll_n1;
2237 }
2238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 /*
2240 * Assign the List values to the list items.
2241 */
2242 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 if (op != NULL && *op != '=')
2245 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2246 else
2247 {
2248 clear_tv(&lp->ll_li->li_tv);
2249 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 ri = ri->li_next;
2252 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2253 break;
2254 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002257 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 ri = NULL;
2260 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002262 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 lp->ll_li = lp->ll_li->li_next;
2264 ++lp->ll_n1;
2265 }
2266 if (ri != NULL)
2267 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 else if (lp->ll_empty2
2269 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 : lp->ll_n1 != lp->ll_n2)
2271 EMSG(_("E711: List value has not enough items"));
2272 }
2273 else
2274 {
2275 /*
2276 * Assign to a List or Dictionary item.
2277 */
2278 if (lp->ll_newkey != NULL)
2279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 if (op != NULL && *op != '=')
2281 {
2282 EMSG2(_(e_letwrong), op);
2283 return;
2284 }
2285
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002287 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (di == NULL)
2289 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002290 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2291 {
2292 vim_free(di);
2293 return;
2294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297 else if (op != NULL && *op != '=')
2298 {
2299 tv_op(lp->ll_tv, rettv, op);
2300 return;
2301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 /*
2306 * Assign the value to the variable or list item.
2307 */
2308 if (copy)
2309 copy_tv(rettv, lp->ll_tv);
2310 else
2311 {
2312 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002313 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
2316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317}
2318
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2321 * Returns OK or FAIL.
2322 */
2323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002324tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002326 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 char_u numbuf[NUMBUFLEN];
2328 char_u *s;
2329
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002330 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2331 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2332 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 {
2334 switch (tv1->v_type)
2335 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002336 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 case VAR_DICT:
2338 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002339 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002340 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002341 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002342 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 break;
2344
2345 case VAR_LIST:
2346 if (*op != '+' || tv2->v_type != VAR_LIST)
2347 break;
2348 /* List += List */
2349 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2350 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2351 return OK;
2352
2353 case VAR_NUMBER:
2354 case VAR_STRING:
2355 if (tv2->v_type == VAR_LIST)
2356 break;
2357 if (*op == '+' || *op == '-')
2358 {
2359 /* nr += nr or nr -= nr*/
2360 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002361#ifdef FEAT_FLOAT
2362 if (tv2->v_type == VAR_FLOAT)
2363 {
2364 float_T f = n;
2365
2366 if (*op == '+')
2367 f += tv2->vval.v_float;
2368 else
2369 f -= tv2->vval.v_float;
2370 clear_tv(tv1);
2371 tv1->v_type = VAR_FLOAT;
2372 tv1->vval.v_float = f;
2373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002375#endif
2376 {
2377 if (*op == '+')
2378 n += get_tv_number(tv2);
2379 else
2380 n -= get_tv_number(tv2);
2381 clear_tv(tv1);
2382 tv1->v_type = VAR_NUMBER;
2383 tv1->vval.v_number = n;
2384 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 }
2386 else
2387 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002388 if (tv2->v_type == VAR_FLOAT)
2389 break;
2390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 /* str .= str */
2392 s = get_tv_string(tv1);
2393 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2394 clear_tv(tv1);
2395 tv1->v_type = VAR_STRING;
2396 tv1->vval.v_string = s;
2397 }
2398 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002400 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002401#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402 {
2403 float_T f;
2404
2405 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2406 && tv2->v_type != VAR_NUMBER
2407 && tv2->v_type != VAR_STRING))
2408 break;
2409 if (tv2->v_type == VAR_FLOAT)
2410 f = tv2->vval.v_float;
2411 else
2412 f = get_tv_number(tv2);
2413 if (*op == '+')
2414 tv1->vval.v_float += f;
2415 else
2416 tv1->vval.v_float -= f;
2417 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002418#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002419 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 }
2421 }
2422
2423 EMSG2(_(e_letwrong), op);
2424 return FAIL;
2425}
2426
2427/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428 * Evaluate the expression used in a ":for var in expr" command.
2429 * "arg" points to "var".
2430 * Set "*errp" to TRUE for an error, FALSE otherwise;
2431 * Return a pointer that holds the info. Null when there is an error.
2432 */
2433 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434eval_for_line(
2435 char_u *arg,
2436 int *errp,
2437 char_u **nextcmdp,
2438 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439{
Bram Moolenaar33570922005-01-25 22:26:29 +00002440 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002442 typval_T tv;
2443 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444
2445 *errp = TRUE; /* default: there is an error */
2446
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002448 if (fi == NULL)
2449 return NULL;
2450
2451 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2452 if (expr == NULL)
2453 return fi;
2454
2455 expr = skipwhite(expr);
2456 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2457 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002458 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002459 return fi;
2460 }
2461
2462 if (skip)
2463 ++emsg_skip;
2464 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2465 {
2466 *errp = FALSE;
2467 if (!skip)
2468 {
2469 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002470 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002472 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002473 clear_tv(&tv);
2474 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002475 else if (l == NULL)
2476 {
2477 /* a null list is like an empty list: do nothing */
2478 clear_tv(&tv);
2479 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 else
2481 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002482 /* No need to increment the refcount, it's already set for the
2483 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002484 fi->fi_list = l;
2485 list_add_watch(l, &fi->fi_lw);
2486 fi->fi_lw.lw_item = l->lv_first;
2487 }
2488 }
2489 }
2490 if (skip)
2491 --emsg_skip;
2492
2493 return fi;
2494}
2495
2496/*
2497 * Use the first item in a ":for" list. Advance to the next.
2498 * Assign the values to the variable (list). "arg" points to the first one.
2499 * Return TRUE when a valid item was found, FALSE when at end of list or
2500 * something wrong.
2501 */
2502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002503next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002505 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002508
2509 item = fi->fi_lw.lw_item;
2510 if (item == NULL)
2511 result = FALSE;
2512 else
2513 {
2514 fi->fi_lw.lw_item = item->li_next;
2515 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2516 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2517 }
2518 return result;
2519}
2520
2521/*
2522 * Free the structure used to store info used by ":for".
2523 */
2524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002525free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526{
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002529 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002530 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002531 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002532 list_unref(fi->fi_list);
2533 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 vim_free(fi);
2535}
2536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2538
2539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002540set_context_for_expression(
2541 expand_T *xp,
2542 char_u *arg,
2543 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 int got_eq = FALSE;
2546 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 if (cmdidx == CMD_let)
2550 {
2551 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002552 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002553 {
2554 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002555 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 {
2557 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002559 if (vim_iswhite(*p))
2560 break;
2561 }
2562 return;
2563 }
2564 }
2565 else
2566 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2567 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 while ((xp->xp_pattern = vim_strpbrk(arg,
2569 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2570 {
2571 c = *xp->xp_pattern;
2572 if (c == '&')
2573 {
2574 c = xp->xp_pattern[1];
2575 if (c == '&')
2576 {
2577 ++xp->xp_pattern;
2578 xp->xp_context = cmdidx != CMD_let || got_eq
2579 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2580 }
2581 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002584 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2585 xp->xp_pattern += 2;
2586
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 else if (c == '$')
2590 {
2591 /* environment variable */
2592 xp->xp_context = EXPAND_ENV_VARS;
2593 }
2594 else if (c == '=')
2595 {
2596 got_eq = TRUE;
2597 xp->xp_context = EXPAND_EXPRESSION;
2598 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002599 else if (c == '#'
2600 && xp->xp_context == EXPAND_EXPRESSION)
2601 {
2602 /* Autoload function/variable contains '#'. */
2603 break;
2604 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002605 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 && xp->xp_context == EXPAND_FUNCTIONS
2607 && vim_strchr(xp->xp_pattern, '(') == NULL)
2608 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002609 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
2611 }
2612 else if (cmdidx != CMD_let || got_eq)
2613 {
2614 if (c == '"') /* string */
2615 {
2616 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2617 if (c == '\\' && xp->xp_pattern[1] != NUL)
2618 ++xp->xp_pattern;
2619 xp->xp_context = EXPAND_NOTHING;
2620 }
2621 else if (c == '\'') /* literal string */
2622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2625 /* skip */ ;
2626 xp->xp_context = EXPAND_NOTHING;
2627 }
2628 else if (c == '|')
2629 {
2630 if (xp->xp_pattern[1] == '|')
2631 {
2632 ++xp->xp_pattern;
2633 xp->xp_context = EXPAND_EXPRESSION;
2634 }
2635 else
2636 xp->xp_context = EXPAND_COMMANDS;
2637 }
2638 else
2639 xp->xp_context = EXPAND_EXPRESSION;
2640 }
2641 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002642 /* Doesn't look like something valid, expand as an expression
2643 * anyway. */
2644 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 arg = xp->xp_pattern;
2646 if (*arg != NUL)
2647 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2648 /* skip */ ;
2649 }
2650 xp->xp_pattern = arg;
2651}
2652
2653#endif /* FEAT_CMDL_COMPL */
2654
2655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 * ":unlet[!] var1 ... " command.
2657 */
2658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002659ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002661 ex_unletlock(eap, eap->arg, 0);
2662}
2663
2664/*
2665 * ":lockvar" and ":unlockvar" commands
2666 */
2667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002668ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002671 int deep = 2;
2672
2673 if (eap->forceit)
2674 deep = -1;
2675 else if (vim_isdigit(*arg))
2676 {
2677 deep = getdigits(&arg);
2678 arg = skipwhite(arg);
2679 }
2680
2681 ex_unletlock(eap, arg, deep);
2682}
2683
2684/*
2685 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2686 */
2687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002688ex_unletlock(
2689 exarg_T *eap,
2690 char_u *argstart,
2691 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002692{
2693 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002696 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 do
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002701 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002702 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lv.ll_name == NULL)
2704 error = TRUE; /* error but continue parsing */
2705 if (name_end == NULL || (!vim_iswhite(*name_end)
2706 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (name_end != NULL)
2709 {
2710 emsg_severe = TRUE;
2711 EMSG(_(e_trailing));
2712 }
2713 if (!(eap->skip || error))
2714 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 break;
2716 }
2717
2718 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002719 {
2720 if (eap->cmdidx == CMD_unlet)
2721 {
2722 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2723 error = TRUE;
2724 }
2725 else
2726 {
2727 if (do_lock_var(&lv, name_end, deep,
2728 eap->cmdidx == CMD_lockvar) == FAIL)
2729 error = TRUE;
2730 }
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!eap->skip)
2734 clear_lval(&lv);
2735
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 arg = skipwhite(name_end);
2737 } while (!ends_excmd(*arg));
2738
2739 eap->nextcmd = check_nextcmd(arg);
2740}
2741
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002743do_unlet_var(
2744 lval_T *lp,
2745 char_u *name_end,
2746 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 int ret = OK;
2749 int cc;
2750
2751 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 cc = *name_end;
2754 *name_end = NUL;
2755
2756 /* Normal name or expanded name. */
2757 if (check_changedtick(lp->ll_name))
2758 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002759 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002763 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002764 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002765 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002766 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002767 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 else if (lp->ll_range)
2769 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002771 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002772 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002773
2774 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2775 {
2776 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002777 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002778 return FAIL;
2779 ll_li = li;
2780 ++ll_n1;
2781 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782
2783 /* Delete a range of List items. */
2784 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2785 {
2786 li = lp->ll_li->li_next;
2787 listitem_remove(lp->ll_list, lp->ll_li);
2788 lp->ll_li = li;
2789 ++lp->ll_n1;
2790 }
2791 }
2792 else
2793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* unlet a List item. */
2796 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 }
2801
2802 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803}
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805/*
2806 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002807 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 hashtab_T *ht;
2813 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002814 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002815 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002816 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 ht = find_var_ht(name, &varname);
2819 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002822 if (d == NULL)
2823 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824 if (ht == &globvarht)
2825 d = &globvardict;
2826 else if (ht == &compat_hashtab)
2827 d = &vimvardict;
2828 else
2829 {
2830 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2831 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2832 }
2833 if (d == NULL)
2834 {
2835 EMSG2(_(e_intern2), "do_unlet()");
2836 return FAIL;
2837 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002838 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002839 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002840 if (HASHITEM_EMPTY(hi))
2841 hi = find_hi_in_scoped_ht(name, &varname, &ht);
2842 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002843 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002844 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002845 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002846 || var_check_ro(di->di_flags, name, FALSE)
2847 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002848 return FAIL;
2849
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002850 delete_var(ht, hi);
2851 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002854 if (forceit)
2855 return OK;
2856 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 return FAIL;
2858}
2859
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002860/*
2861 * Lock or unlock variable indicated by "lp".
2862 * "deep" is the levels to go (-1 for unlimited);
2863 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2864 */
2865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002866do_lock_var(
2867 lval_T *lp,
2868 char_u *name_end,
2869 int deep,
2870 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002871{
2872 int ret = OK;
2873 int cc;
2874 dictitem_T *di;
2875
2876 if (deep == 0) /* nothing to do */
2877 return OK;
2878
2879 if (lp->ll_tv == NULL)
2880 {
2881 cc = *name_end;
2882 *name_end = NUL;
2883
2884 /* Normal name or expanded name. */
2885 if (check_changedtick(lp->ll_name))
2886 ret = FAIL;
2887 else
2888 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002889 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002890 if (di == NULL)
2891 ret = FAIL;
2892 else
2893 {
2894 if (lock)
2895 di->di_flags |= DI_FLAGS_LOCK;
2896 else
2897 di->di_flags &= ~DI_FLAGS_LOCK;
2898 item_lock(&di->di_tv, deep, lock);
2899 }
2900 }
2901 *name_end = cc;
2902 }
2903 else if (lp->ll_range)
2904 {
2905 listitem_T *li = lp->ll_li;
2906
2907 /* (un)lock a range of List items. */
2908 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2909 {
2910 item_lock(&li->li_tv, deep, lock);
2911 li = li->li_next;
2912 ++lp->ll_n1;
2913 }
2914 }
2915 else if (lp->ll_list != NULL)
2916 /* (un)lock a List item. */
2917 item_lock(&lp->ll_li->li_tv, deep, lock);
2918 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002919 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002920 item_lock(&lp->ll_di->di_tv, deep, lock);
2921
2922 return ret;
2923}
2924
2925/*
2926 * Lock or unlock an item. "deep" is nr of levels to go.
2927 */
2928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002929item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930{
2931 static int recurse = 0;
2932 list_T *l;
2933 listitem_T *li;
2934 dict_T *d;
2935 hashitem_T *hi;
2936 int todo;
2937
2938 if (recurse >= DICT_MAXNEST)
2939 {
2940 EMSG(_("E743: variable nested too deep for (un)lock"));
2941 return;
2942 }
2943 if (deep == 0)
2944 return;
2945 ++recurse;
2946
2947 /* lock/unlock the item itself */
2948 if (lock)
2949 tv->v_lock |= VAR_LOCKED;
2950 else
2951 tv->v_lock &= ~VAR_LOCKED;
2952
2953 switch (tv->v_type)
2954 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 case VAR_UNKNOWN:
2956 case VAR_NUMBER:
2957 case VAR_STRING:
2958 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002959 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002960 case VAR_FLOAT:
2961 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002962 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002963 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002964 break;
2965
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 case VAR_LIST:
2967 if ((l = tv->vval.v_list) != NULL)
2968 {
2969 if (lock)
2970 l->lv_lock |= VAR_LOCKED;
2971 else
2972 l->lv_lock &= ~VAR_LOCKED;
2973 if (deep < 0 || deep > 1)
2974 /* recursive: lock/unlock the items the List contains */
2975 for (li = l->lv_first; li != NULL; li = li->li_next)
2976 item_lock(&li->li_tv, deep - 1, lock);
2977 }
2978 break;
2979 case VAR_DICT:
2980 if ((d = tv->vval.v_dict) != NULL)
2981 {
2982 if (lock)
2983 d->dv_lock |= VAR_LOCKED;
2984 else
2985 d->dv_lock &= ~VAR_LOCKED;
2986 if (deep < 0 || deep > 1)
2987 {
2988 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002989 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002990 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2991 {
2992 if (!HASHITEM_EMPTY(hi))
2993 {
2994 --todo;
2995 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2996 }
2997 }
2998 }
2999 }
3000 }
3001 --recurse;
3002}
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3005/*
3006 * Delete all "menutrans_" variables.
3007 */
3008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003009del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003012 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaar33570922005-01-25 22:26:29 +00003014 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003015 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003017 {
3018 if (!HASHITEM_EMPTY(hi))
3019 {
3020 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003021 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3022 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003023 }
3024 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026}
3027#endif
3028
3029#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3030
3031/*
3032 * Local string buffer for the next two functions to store a variable name
3033 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3034 * get_user_var_name().
3035 */
3036
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003037static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039static char_u *varnamebuf = NULL;
3040static int varnamebuflen = 0;
3041
3042/*
3043 * Function to concatenate a prefix and a variable name.
3044 */
3045 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 int len;
3049
3050 len = (int)STRLEN(name) + 3;
3051 if (len > varnamebuflen)
3052 {
3053 vim_free(varnamebuf);
3054 len += 10; /* some additional space */
3055 varnamebuf = alloc(len);
3056 if (varnamebuf == NULL)
3057 {
3058 varnamebuflen = 0;
3059 return NULL;
3060 }
3061 varnamebuflen = len;
3062 }
3063 *varnamebuf = prefix;
3064 varnamebuf[1] = ':';
3065 STRCPY(varnamebuf + 2, name);
3066 return varnamebuf;
3067}
3068
3069/*
3070 * Function given to ExpandGeneric() to obtain the list of user defined
3071 * (global/buffer/window/built-in) variable names.
3072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003076 static long_u gdone;
3077 static long_u bdone;
3078 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003079#ifdef FEAT_WINDOWS
3080 static long_u tdone;
3081#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003082 static int vidx;
3083 static hashitem_T *hi;
3084 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
3086 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003087 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003088 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003089#ifdef FEAT_WINDOWS
3090 tdone = 0;
3091#endif
3092 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003093
3094 /* Global variables */
3095 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003097 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003098 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003099 else
3100 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003101 while (HASHITEM_EMPTY(hi))
3102 ++hi;
3103 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3104 return cat_prefix_varname('g', hi->hi_key);
3105 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003107
3108 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003109 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003112 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003114 else
3115 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003116 while (HASHITEM_EMPTY(hi))
3117 ++hi;
3118 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003122 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return (char_u *)"b:changedtick";
3124 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003125
3126 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003127 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003130 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003132 else
3133 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003134 while (HASHITEM_EMPTY(hi))
3135 ++hi;
3136 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139#ifdef FEAT_WINDOWS
3140 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003141 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003142 if (tdone < ht->ht_used)
3143 {
3144 if (tdone++ == 0)
3145 hi = ht->ht_array;
3146 else
3147 ++hi;
3148 while (HASHITEM_EMPTY(hi))
3149 ++hi;
3150 return cat_prefix_varname('t', hi->hi_key);
3151 }
3152#endif
3153
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 /* v: variables */
3155 if (vidx < VV_LEN)
3156 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 vim_free(varnamebuf);
3159 varnamebuf = NULL;
3160 varnamebuflen = 0;
3161 return NULL;
3162}
3163
3164#endif /* FEAT_CMDL_COMPL */
3165
3166/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003167 * Return TRUE if "pat" matches "text".
3168 * Does not use 'cpo' and always uses 'magic'.
3169 */
3170 static int
3171pattern_match(char_u *pat, char_u *text, int ic)
3172{
3173 int matches = FALSE;
3174 char_u *save_cpo;
3175 regmatch_T regmatch;
3176
3177 /* avoid 'l' flag in 'cpoptions' */
3178 save_cpo = p_cpo;
3179 p_cpo = (char_u *)"";
3180 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3181 if (regmatch.regprog != NULL)
3182 {
3183 regmatch.rm_ic = ic;
3184 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3185 vim_regfree(regmatch.regprog);
3186 }
3187 p_cpo = save_cpo;
3188 return matches;
3189}
3190
3191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * types for expressions.
3193 */
3194typedef enum
3195{
3196 TYPE_UNKNOWN = 0
3197 , TYPE_EQUAL /* == */
3198 , TYPE_NEQUAL /* != */
3199 , TYPE_GREATER /* > */
3200 , TYPE_GEQUAL /* >= */
3201 , TYPE_SMALLER /* < */
3202 , TYPE_SEQUAL /* <= */
3203 , TYPE_MATCH /* =~ */
3204 , TYPE_NOMATCH /* !~ */
3205} exptype_T;
3206
3207/*
3208 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003209 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3211 */
3212
3213/*
3214 * Handle zero level expression.
3215 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003216 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003217 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * Return OK or FAIL.
3219 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221eval0(
3222 char_u *arg,
3223 typval_T *rettv,
3224 char_u **nextcmd,
3225 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 int ret;
3228 char_u *p;
3229
3230 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003231 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (ret == FAIL || !ends_excmd(*p))
3233 {
3234 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 /*
3237 * Report the invalid expression unless the expression evaluation has
3238 * been cancelled due to an aborting error, an interrupt, or an
3239 * exception.
3240 */
3241 if (!aborting())
3242 EMSG2(_(e_invexpr2), arg);
3243 ret = FAIL;
3244 }
3245 if (nextcmd != NULL)
3246 *nextcmd = check_nextcmd(p);
3247
3248 return ret;
3249}
3250
3251/*
3252 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003253 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 *
3255 * "arg" must point to the first non-white of the expression.
3256 * "arg" is advanced to the next non-white after the recognized expression.
3257 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003258 * Note: "rettv.v_lock" is not set.
3259 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 * Return OK or FAIL.
3261 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003263eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268 /*
3269 * Get the first variable.
3270 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003271 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return FAIL;
3273
3274 if ((*arg)[0] == '?')
3275 {
3276 result = FALSE;
3277 if (evaluate)
3278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003279 int error = FALSE;
3280
3281 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003284 if (error)
3285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287
3288 /*
3289 * Get the second variable.
3290 */
3291 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003292 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return FAIL;
3294
3295 /*
3296 * Check for the ":".
3297 */
3298 if ((*arg)[0] != ':')
3299 {
3300 EMSG(_("E109: Missing ':' after '?'"));
3301 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003302 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 return FAIL;
3304 }
3305
3306 /*
3307 * Get the third variable.
3308 */
3309 *arg = skipwhite(*arg + 1);
3310 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3311 {
3312 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003313 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return FAIL;
3315 }
3316 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003317 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319
3320 return OK;
3321}
3322
3323/*
3324 * Handle first level expression:
3325 * expr2 || expr2 || expr2 logical OR
3326 *
3327 * "arg" must point to the first non-white of the expression.
3328 * "arg" is advanced to the next non-white after the recognized expression.
3329 *
3330 * Return OK or FAIL.
3331 */
3332 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 long result;
3337 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
3340 /*
3341 * Get the first variable.
3342 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003343 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 return FAIL;
3345
3346 /*
3347 * Repeat until there is no following "||".
3348 */
3349 first = TRUE;
3350 result = FALSE;
3351 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3352 {
3353 if (evaluate && first)
3354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 if (error)
3359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 first = FALSE;
3361 }
3362
3363 /*
3364 * Get the second variable.
3365 */
3366 *arg = skipwhite(*arg + 2);
3367 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3368 return FAIL;
3369
3370 /*
3371 * Compute the result.
3372 */
3373 if (evaluate && !result)
3374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003375 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 if (error)
3379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 if (evaluate)
3382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 rettv->v_type = VAR_NUMBER;
3384 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
3386 }
3387
3388 return OK;
3389}
3390
3391/*
3392 * Handle second level expression:
3393 * expr3 && expr3 && expr3 logical AND
3394 *
3395 * "arg" must point to the first non-white of the expression.
3396 * "arg" is advanced to the next non-white after the recognized expression.
3397 *
3398 * Return OK or FAIL.
3399 */
3400 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003401eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 long result;
3405 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 /*
3409 * Get the first variable.
3410 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003411 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return FAIL;
3413
3414 /*
3415 * Repeat until there is no following "&&".
3416 */
3417 first = TRUE;
3418 result = TRUE;
3419 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3420 {
3421 if (evaluate && first)
3422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003423 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 if (error)
3427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 first = FALSE;
3429 }
3430
3431 /*
3432 * Get the second variable.
3433 */
3434 *arg = skipwhite(*arg + 2);
3435 if (eval4(arg, &var2, evaluate && result) == FAIL)
3436 return FAIL;
3437
3438 /*
3439 * Compute the result.
3440 */
3441 if (evaluate && result)
3442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003443 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 if (error)
3447 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449 if (evaluate)
3450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003451 rettv->v_type = VAR_NUMBER;
3452 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 }
3455
3456 return OK;
3457}
3458
3459/*
3460 * Handle third level expression:
3461 * var1 == var2
3462 * var1 =~ var2
3463 * var1 != var2
3464 * var1 !~ var2
3465 * var1 > var2
3466 * var1 >= var2
3467 * var1 < var2
3468 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003469 * var1 is var2
3470 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 *
3472 * "arg" must point to the first non-white of the expression.
3473 * "arg" is advanced to the next non-white after the recognized expression.
3474 *
3475 * Return OK or FAIL.
3476 */
3477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003478eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaar33570922005-01-25 22:26:29 +00003480 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 char_u *p;
3482 int i;
3483 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003484 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003486 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 char_u *s1, *s2;
3488 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491 /*
3492 * Get the first variable.
3493 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 return FAIL;
3496
3497 p = *arg;
3498 switch (p[0])
3499 {
3500 case '=': if (p[1] == '=')
3501 type = TYPE_EQUAL;
3502 else if (p[1] == '~')
3503 type = TYPE_MATCH;
3504 break;
3505 case '!': if (p[1] == '=')
3506 type = TYPE_NEQUAL;
3507 else if (p[1] == '~')
3508 type = TYPE_NOMATCH;
3509 break;
3510 case '>': if (p[1] != '=')
3511 {
3512 type = TYPE_GREATER;
3513 len = 1;
3514 }
3515 else
3516 type = TYPE_GEQUAL;
3517 break;
3518 case '<': if (p[1] != '=')
3519 {
3520 type = TYPE_SMALLER;
3521 len = 1;
3522 }
3523 else
3524 type = TYPE_SEQUAL;
3525 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003526 case 'i': if (p[1] == 's')
3527 {
3528 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3529 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003530 i = p[len];
3531 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003532 {
3533 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3534 type_is = TRUE;
3535 }
3536 }
3537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539
3540 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 */
3543 if (type != TYPE_UNKNOWN)
3544 {
3545 /* extra question mark appended: ignore case */
3546 if (p[len] == '?')
3547 {
3548 ic = TRUE;
3549 ++len;
3550 }
3551 /* extra '#' appended: match case */
3552 else if (p[len] == '#')
3553 {
3554 ic = FALSE;
3555 ++len;
3556 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else
3559 ic = p_ic;
3560
3561 /*
3562 * Get the second variable.
3563 */
3564 *arg = skipwhite(p + len);
3565 if (eval5(arg, &var2, evaluate) == FAIL)
3566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003567 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 return FAIL;
3569 }
3570
3571 if (evaluate)
3572 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003573 if (type_is && rettv->v_type != var2.v_type)
3574 {
3575 /* For "is" a different type always means FALSE, for "notis"
3576 * it means TRUE. */
3577 n1 = (type == TYPE_NEQUAL);
3578 }
3579 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3580 {
3581 if (type_is)
3582 {
3583 n1 = (rettv->v_type == var2.v_type
3584 && rettv->vval.v_list == var2.vval.v_list);
3585 if (type == TYPE_NEQUAL)
3586 n1 = !n1;
3587 }
3588 else if (rettv->v_type != var2.v_type
3589 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3590 {
3591 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003592 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003593 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003594 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003595 clear_tv(rettv);
3596 clear_tv(&var2);
3597 return FAIL;
3598 }
3599 else
3600 {
3601 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003602 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3603 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003604 if (type == TYPE_NEQUAL)
3605 n1 = !n1;
3606 }
3607 }
3608
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003609 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3610 {
3611 if (type_is)
3612 {
3613 n1 = (rettv->v_type == var2.v_type
3614 && rettv->vval.v_dict == var2.vval.v_dict);
3615 if (type == TYPE_NEQUAL)
3616 n1 = !n1;
3617 }
3618 else if (rettv->v_type != var2.v_type
3619 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3620 {
3621 if (rettv->v_type != var2.v_type)
3622 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3623 else
3624 EMSG(_("E736: Invalid operation for Dictionary"));
3625 clear_tv(rettv);
3626 clear_tv(&var2);
3627 return FAIL;
3628 }
3629 else
3630 {
3631 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003632 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3633 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003634 if (type == TYPE_NEQUAL)
3635 n1 = !n1;
3636 }
3637 }
3638
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003639 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3640 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003641 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003642 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003643 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003644 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003645 clear_tv(rettv);
3646 clear_tv(&var2);
3647 return FAIL;
3648 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003649 if ((rettv->v_type == VAR_PARTIAL
3650 && rettv->vval.v_partial == NULL)
3651 || (var2.v_type == VAR_PARTIAL
3652 && var2.vval.v_partial == NULL))
3653 /* when a partial is NULL assume not equal */
3654 n1 = FALSE;
3655 else if (type_is)
3656 {
3657 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3658 /* strings are considered the same if their value is
3659 * the same */
3660 n1 = tv_equal(rettv, &var2, ic, FALSE);
3661 else if (rettv->v_type == VAR_PARTIAL
3662 && var2.v_type == VAR_PARTIAL)
3663 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3664 else
3665 n1 = FALSE;
3666 }
3667 else
3668 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003669 if (type == TYPE_NEQUAL)
3670 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003671 }
3672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003673#ifdef FEAT_FLOAT
3674 /*
3675 * If one of the two variables is a float, compare as a float.
3676 * When using "=~" or "!~", always compare as string.
3677 */
3678 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3679 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3680 {
3681 float_T f1, f2;
3682
3683 if (rettv->v_type == VAR_FLOAT)
3684 f1 = rettv->vval.v_float;
3685 else
3686 f1 = get_tv_number(rettv);
3687 if (var2.v_type == VAR_FLOAT)
3688 f2 = var2.vval.v_float;
3689 else
3690 f2 = get_tv_number(&var2);
3691 n1 = FALSE;
3692 switch (type)
3693 {
3694 case TYPE_EQUAL: n1 = (f1 == f2); break;
3695 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3696 case TYPE_GREATER: n1 = (f1 > f2); break;
3697 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3698 case TYPE_SMALLER: n1 = (f1 < f2); break;
3699 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3700 case TYPE_UNKNOWN:
3701 case TYPE_MATCH:
3702 case TYPE_NOMATCH: break; /* avoid gcc warning */
3703 }
3704 }
3705#endif
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 /*
3708 * If one of the two variables is a number, compare as a number.
3709 * When using "=~" or "!~", always compare as string.
3710 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003711 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 n1 = get_tv_number(rettv);
3715 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 switch (type)
3717 {
3718 case TYPE_EQUAL: n1 = (n1 == n2); break;
3719 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3720 case TYPE_GREATER: n1 = (n1 > n2); break;
3721 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3722 case TYPE_SMALLER: n1 = (n1 < n2); break;
3723 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3724 case TYPE_UNKNOWN:
3725 case TYPE_MATCH:
3726 case TYPE_NOMATCH: break; /* avoid gcc warning */
3727 }
3728 }
3729 else
3730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003731 s1 = get_tv_string_buf(rettv, buf1);
3732 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3734 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3735 else
3736 i = 0;
3737 n1 = FALSE;
3738 switch (type)
3739 {
3740 case TYPE_EQUAL: n1 = (i == 0); break;
3741 case TYPE_NEQUAL: n1 = (i != 0); break;
3742 case TYPE_GREATER: n1 = (i > 0); break;
3743 case TYPE_GEQUAL: n1 = (i >= 0); break;
3744 case TYPE_SMALLER: n1 = (i < 0); break;
3745 case TYPE_SEQUAL: n1 = (i <= 0); break;
3746
3747 case TYPE_MATCH:
3748 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003749 n1 = pattern_match(s2, s1, ic);
3750 if (type == TYPE_NOMATCH)
3751 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 break;
3753
3754 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3755 }
3756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 clear_tv(rettv);
3758 clear_tv(&var2);
3759 rettv->v_type = VAR_NUMBER;
3760 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763
3764 return OK;
3765}
3766
3767/*
3768 * Handle fourth level expression:
3769 * + number addition
3770 * - number subtraction
3771 * . string concatenation
3772 *
3773 * "arg" must point to the first non-white of the expression.
3774 * "arg" is advanced to the next non-white after the recognized expression.
3775 *
3776 * Return OK or FAIL.
3777 */
3778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 typval_T var2;
3782 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003784 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003785#ifdef FEAT_FLOAT
3786 float_T f1 = 0, f2 = 0;
3787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 char_u *s1, *s2;
3789 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3790 char_u *p;
3791
3792 /*
3793 * Get the first variable.
3794 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003795 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797
3798 /*
3799 * Repeat computing, until no '+', '-' or '.' is following.
3800 */
3801 for (;;)
3802 {
3803 op = **arg;
3804 if (op != '+' && op != '-' && op != '.')
3805 break;
3806
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003807 if ((op != '+' || rettv->v_type != VAR_LIST)
3808#ifdef FEAT_FLOAT
3809 && (op == '.' || rettv->v_type != VAR_FLOAT)
3810#endif
3811 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 {
3813 /* For "list + ...", an illegal use of the first operand as
3814 * a number cannot be determined before evaluating the 2nd
3815 * operand: if this is also a list, all is ok.
3816 * For "something . ...", "something - ..." or "non-list + ...",
3817 * we know that the first operand needs to be a string or number
3818 * without evaluating the 2nd operand. So check before to avoid
3819 * side effects after an error. */
3820 if (evaluate && get_tv_string_chk(rettv) == NULL)
3821 {
3822 clear_tv(rettv);
3823 return FAIL;
3824 }
3825 }
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 /*
3828 * Get the second variable.
3829 */
3830 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003831 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return FAIL;
3835 }
3836
3837 if (evaluate)
3838 {
3839 /*
3840 * Compute the result.
3841 */
3842 if (op == '.')
3843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003844 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3845 s2 = get_tv_string_buf_chk(&var2, buf2);
3846 if (s2 == NULL) /* type error ? */
3847 {
3848 clear_tv(rettv);
3849 clear_tv(&var2);
3850 return FAIL;
3851 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003852 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 clear_tv(rettv);
3854 rettv->v_type = VAR_STRING;
3855 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003857 else if (op == '+' && rettv->v_type == VAR_LIST
3858 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003859 {
3860 /* concatenate Lists */
3861 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3862 &var3) == FAIL)
3863 {
3864 clear_tv(rettv);
3865 clear_tv(&var2);
3866 return FAIL;
3867 }
3868 clear_tv(rettv);
3869 *rettv = var3;
3870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
3872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 int error = FALSE;
3874
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003875#ifdef FEAT_FLOAT
3876 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003877 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003878 f1 = rettv->vval.v_float;
3879 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003881 else
3882#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003884 n1 = get_tv_number_chk(rettv, &error);
3885 if (error)
3886 {
3887 /* This can only happen for "list + non-list". For
3888 * "non-list + ..." or "something - ...", we returned
3889 * before evaluating the 2nd operand. */
3890 clear_tv(rettv);
3891 return FAIL;
3892 }
3893#ifdef FEAT_FLOAT
3894 if (var2.v_type == VAR_FLOAT)
3895 f1 = n1;
3896#endif
3897 }
3898#ifdef FEAT_FLOAT
3899 if (var2.v_type == VAR_FLOAT)
3900 {
3901 f2 = var2.vval.v_float;
3902 n2 = 0;
3903 }
3904 else
3905#endif
3906 {
3907 n2 = get_tv_number_chk(&var2, &error);
3908 if (error)
3909 {
3910 clear_tv(rettv);
3911 clear_tv(&var2);
3912 return FAIL;
3913 }
3914#ifdef FEAT_FLOAT
3915 if (rettv->v_type == VAR_FLOAT)
3916 f2 = n2;
3917#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003920
3921#ifdef FEAT_FLOAT
3922 /* If there is a float on either side the result is a float. */
3923 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3924 {
3925 if (op == '+')
3926 f1 = f1 + f2;
3927 else
3928 f1 = f1 - f2;
3929 rettv->v_type = VAR_FLOAT;
3930 rettv->vval.v_float = f1;
3931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003933#endif
3934 {
3935 if (op == '+')
3936 n1 = n1 + n2;
3937 else
3938 n1 = n1 - n2;
3939 rettv->v_type = VAR_NUMBER;
3940 rettv->vval.v_number = n1;
3941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 }
3946 return OK;
3947}
3948
3949/*
3950 * Handle fifth level expression:
3951 * * number multiplication
3952 * / number division
3953 * % number modulo
3954 *
3955 * "arg" must point to the first non-white of the expression.
3956 * "arg" is advanced to the next non-white after the recognized expression.
3957 *
3958 * Return OK or FAIL.
3959 */
3960 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003961eval6(
3962 char_u **arg,
3963 typval_T *rettv,
3964 int evaluate,
3965 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003969 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003970#ifdef FEAT_FLOAT
3971 int use_float = FALSE;
3972 float_T f1 = 0, f2;
3973#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003974 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 /*
3977 * Get the first variable.
3978 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003979 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return FAIL;
3981
3982 /*
3983 * Repeat computing, until no '*', '/' or '%' is following.
3984 */
3985 for (;;)
3986 {
3987 op = **arg;
3988 if (op != '*' && op != '/' && op != '%')
3989 break;
3990
3991 if (evaluate)
3992 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003993#ifdef FEAT_FLOAT
3994 if (rettv->v_type == VAR_FLOAT)
3995 {
3996 f1 = rettv->vval.v_float;
3997 use_float = TRUE;
3998 n1 = 0;
3999 }
4000 else
4001#endif
4002 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004003 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 if (error)
4005 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 else
4008 n1 = 0;
4009
4010 /*
4011 * Get the second variable.
4012 */
4013 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004014 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return FAIL;
4016
4017 if (evaluate)
4018 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004019#ifdef FEAT_FLOAT
4020 if (var2.v_type == VAR_FLOAT)
4021 {
4022 if (!use_float)
4023 {
4024 f1 = n1;
4025 use_float = TRUE;
4026 }
4027 f2 = var2.vval.v_float;
4028 n2 = 0;
4029 }
4030 else
4031#endif
4032 {
4033 n2 = get_tv_number_chk(&var2, &error);
4034 clear_tv(&var2);
4035 if (error)
4036 return FAIL;
4037#ifdef FEAT_FLOAT
4038 if (use_float)
4039 f2 = n2;
4040#endif
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
4043 /*
4044 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004047#ifdef FEAT_FLOAT
4048 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004050 if (op == '*')
4051 f1 = f1 * f2;
4052 else if (op == '/')
4053 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004054# ifdef VMS
4055 /* VMS crashes on divide by zero, work around it */
4056 if (f2 == 0.0)
4057 {
4058 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004059 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004060 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004061 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004062 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004063 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004064 }
4065 else
4066 f1 = f1 / f2;
4067# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068 /* We rely on the floating point library to handle divide
4069 * by zero to result in "inf" and not a crash. */
4070 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004071# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004075 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076 return FAIL;
4077 }
4078 rettv->v_type = VAR_FLOAT;
4079 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004084 if (op == '*')
4085 n1 = n1 * n2;
4086 else if (op == '/')
4087 {
4088 if (n2 == 0) /* give an error message? */
4089 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004090#ifdef FEAT_NUM64
4091 if (n1 == 0)
4092 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
4093 else if (n1 < 0)
4094 n1 = -0x7fffffffffffffff;
4095 else
4096 n1 = 0x7fffffffffffffff;
4097#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004098 if (n1 == 0)
4099 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4100 else if (n1 < 0)
4101 n1 = -0x7fffffffL;
4102 else
4103 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004104#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004105 }
4106 else
4107 n1 = n1 / n2;
4108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004110 {
4111 if (n2 == 0) /* give an error message? */
4112 n1 = 0;
4113 else
4114 n1 = n1 % n2;
4115 }
4116 rettv->v_type = VAR_NUMBER;
4117 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 }
4121
4122 return OK;
4123}
4124
4125/*
4126 * Handle sixth level expression:
4127 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004128 * "string" string constant
4129 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * &option-name option value
4131 * @r register contents
4132 * identifier variable value
4133 * function() function call
4134 * $VAR environment variable
4135 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004136 * [expr, expr] List
4137 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 *
4139 * Also handle:
4140 * ! in front logical NOT
4141 * - in front unary minus
4142 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 * trailing [] subscript in String or List
4144 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 *
4146 * "arg" must point to the first non-white of the expression.
4147 * "arg" is advanced to the next non-white after the recognized expression.
4148 *
4149 * Return OK or FAIL.
4150 */
4151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152eval7(
4153 char_u **arg,
4154 typval_T *rettv,
4155 int evaluate,
4156 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004158 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 int len;
4160 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u *start_leader, *end_leader;
4162 int ret = OK;
4163 char_u *alias;
4164
4165 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Skip '!' and '-' characters. They are handled later.
4173 */
4174 start_leader = *arg;
4175 while (**arg == '!' || **arg == '-' || **arg == '+')
4176 *arg = skipwhite(*arg + 1);
4177 end_leader = *arg;
4178
4179 switch (**arg)
4180 {
4181 /*
4182 * Number constant.
4183 */
4184 case '0':
4185 case '1':
4186 case '2':
4187 case '3':
4188 case '4':
4189 case '5':
4190 case '6':
4191 case '7':
4192 case '8':
4193 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004194 {
4195#ifdef FEAT_FLOAT
4196 char_u *p = skipdigits(*arg + 1);
4197 int get_float = FALSE;
4198
4199 /* We accept a float when the format matches
4200 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004201 * strict to avoid backwards compatibility problems.
4202 * Don't look for a float after the "." operator, so that
4203 * ":let vers = 1.2.3" doesn't fail. */
4204 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004206 get_float = TRUE;
4207 p = skipdigits(p + 2);
4208 if (*p == 'e' || *p == 'E')
4209 {
4210 ++p;
4211 if (*p == '-' || *p == '+')
4212 ++p;
4213 if (!vim_isdigit(*p))
4214 get_float = FALSE;
4215 else
4216 p = skipdigits(p + 1);
4217 }
4218 if (ASCII_ISALPHA(*p) || *p == '.')
4219 get_float = FALSE;
4220 }
4221 if (get_float)
4222 {
4223 float_T f;
4224
4225 *arg += string2float(*arg, &f);
4226 if (evaluate)
4227 {
4228 rettv->v_type = VAR_FLOAT;
4229 rettv->vval.v_float = f;
4230 }
4231 }
4232 else
4233#endif
4234 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004235 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004236 *arg += len;
4237 if (evaluate)
4238 {
4239 rettv->v_type = VAR_NUMBER;
4240 rettv->vval.v_number = n;
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
4247 * String constant: "string".
4248 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 break;
4251
4252 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004253 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004256 break;
4257
4258 /*
4259 * List: [expr, expr]
4260 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 break;
4263
4264 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004265 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004266 * Dictionary: {key: val, key: val}
4267 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004268 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4269 if (ret == NOTDONE)
4270 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004271 break;
4272
4273 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004274 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004276 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 break;
4278
4279 /*
4280 * Environment variable: $VAR.
4281 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 break;
4284
4285 /*
4286 * Register contents: @r.
4287 */
4288 case '@': ++*arg;
4289 if (evaluate)
4290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004292 rettv->vval.v_string = get_reg_contents(**arg,
4293 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 if (**arg != NUL)
4296 ++*arg;
4297 break;
4298
4299 /*
4300 * nested expression: (expression).
4301 */
4302 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (**arg == ')')
4305 ++*arg;
4306 else if (ret == OK)
4307 {
4308 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 ret = FAIL;
4311 }
4312 break;
4313
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 break;
4316 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004317
4318 if (ret == NOTDONE)
4319 {
4320 /*
4321 * Must be a variable or function name.
4322 * Can also be a curly-braces kind of name: {expr}.
4323 */
4324 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004325 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326 if (alias != NULL)
4327 s = alias;
4328
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004329 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004330 ret = FAIL;
4331 else
4332 {
4333 if (**arg == '(') /* recursive! */
4334 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004335 partial_T *partial;
4336
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004337 if (!evaluate)
4338 check_vars(s, len);
4339
Bram Moolenaar8c711452005-01-14 21:53:12 +00004340 /* If "s" is the name of a variable of type VAR_FUNC
4341 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004342 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004343
4344 /* Invoke the function. */
4345 ret = get_func_tv(s, len, rettv, arg,
4346 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004347 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004348
4349 /* If evaluate is FALSE rettv->v_type was not set in
4350 * get_func_tv, but it's needed in handle_subscript() to parse
4351 * what follows. So set it here. */
4352 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4353 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004354 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004355 rettv->v_type = VAR_FUNC;
4356 }
4357
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 /* Stop the expression evaluation when immediately
4359 * aborting on error, or when an interrupt occurred or
4360 * an exception was thrown but not caught. */
4361 if (aborting())
4362 {
4363 if (ret == OK)
4364 clear_tv(rettv);
4365 ret = FAIL;
4366 }
4367 }
4368 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004369 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004370 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004371 {
4372 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004373 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004374 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004375 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004376 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 }
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 *arg = skipwhite(*arg);
4380
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004381 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4382 * expr(expr). */
4383 if (ret == OK)
4384 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 /*
4387 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4388 */
4389 if (ret == OK && evaluate && end_leader > start_leader)
4390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004392 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004393#ifdef FEAT_FLOAT
4394 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004396 if (rettv->v_type == VAR_FLOAT)
4397 f = rettv->vval.v_float;
4398 else
4399#endif
4400 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 clear_tv(rettv);
4404 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 else
4407 {
4408 while (end_leader > start_leader)
4409 {
4410 --end_leader;
4411 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412 {
4413#ifdef FEAT_FLOAT
4414 if (rettv->v_type == VAR_FLOAT)
4415 f = !f;
4416 else
4417#endif
4418 val = !val;
4419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004420 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004421 {
4422#ifdef FEAT_FLOAT
4423 if (rettv->v_type == VAR_FLOAT)
4424 f = -f;
4425 else
4426#endif
4427 val = -val;
4428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004430#ifdef FEAT_FLOAT
4431 if (rettv->v_type == VAR_FLOAT)
4432 {
4433 clear_tv(rettv);
4434 rettv->vval.v_float = f;
4435 }
4436 else
4437#endif
4438 {
4439 clear_tv(rettv);
4440 rettv->v_type = VAR_NUMBER;
4441 rettv->vval.v_number = val;
4442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445
4446 return ret;
4447}
4448
4449/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004450 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4451 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4453 */
4454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004455eval_index(
4456 char_u **arg,
4457 typval_T *rettv,
4458 int evaluate,
4459 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460{
4461 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004462 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004464 long len = -1;
4465 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468
Bram Moolenaara03f2332016-02-06 18:09:59 +01004469 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004471 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004472 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004473 if (verbose)
4474 EMSG(_("E695: Cannot index a Funcref"));
4475 return FAIL;
4476 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004477#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004478 if (verbose)
4479 EMSG(_(e_float_as_string));
4480 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004481#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004482 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004483 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004484 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004485 if (verbose)
4486 EMSG(_("E909: Cannot index a special variable"));
4487 return FAIL;
4488 case VAR_UNKNOWN:
4489 if (evaluate)
4490 return FAIL;
4491 /* FALLTHROUGH */
4492
4493 case VAR_STRING:
4494 case VAR_NUMBER:
4495 case VAR_LIST:
4496 case VAR_DICT:
4497 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004498 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004500 init_tv(&var1);
4501 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 /*
4505 * dict.name
4506 */
4507 key = *arg + 1;
4508 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4509 ;
4510 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004512 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 }
4514 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 /*
4517 * something[idx]
4518 *
4519 * Get the (first) variable from inside the [].
4520 */
4521 *arg = skipwhite(*arg + 1);
4522 if (**arg == ':')
4523 empty1 = TRUE;
4524 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4525 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4527 {
4528 /* not a number or string */
4529 clear_tv(&var1);
4530 return FAIL;
4531 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532
4533 /*
4534 * Get the second variable from inside the [:].
4535 */
4536 if (**arg == ':')
4537 {
4538 range = TRUE;
4539 *arg = skipwhite(*arg + 1);
4540 if (**arg == ']')
4541 empty2 = TRUE;
4542 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004544 if (!empty1)
4545 clear_tv(&var1);
4546 return FAIL;
4547 }
4548 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4549 {
4550 /* not a number or string */
4551 if (!empty1)
4552 clear_tv(&var1);
4553 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 return FAIL;
4555 }
4556 }
4557
4558 /* Check for the ']'. */
4559 if (**arg != ']')
4560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004561 if (verbose)
4562 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 clear_tv(&var1);
4564 if (range)
4565 clear_tv(&var2);
4566 return FAIL;
4567 }
4568 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 }
4570
4571 if (evaluate)
4572 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 n1 = 0;
4574 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 n1 = get_tv_number(&var1);
4577 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 }
4579 if (range)
4580 {
4581 if (empty2)
4582 n2 = -1;
4583 else
4584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 n2 = get_tv_number(&var2);
4586 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 }
4588 }
4589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004590 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004592 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004593 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004594 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004595 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004596 case VAR_SPECIAL:
4597 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004598 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004599 break; /* not evaluating, skipping over subscript */
4600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 case VAR_NUMBER:
4602 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 len = (long)STRLEN(s);
4605 if (range)
4606 {
4607 /* The resulting variable is a substring. If the indexes
4608 * are out of range the result is empty. */
4609 if (n1 < 0)
4610 {
4611 n1 = len + n1;
4612 if (n1 < 0)
4613 n1 = 0;
4614 }
4615 if (n2 < 0)
4616 n2 = len + n2;
4617 else if (n2 >= len)
4618 n2 = len;
4619 if (n1 >= len || n2 < 0 || n1 > n2)
4620 s = NULL;
4621 else
4622 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4623 }
4624 else
4625 {
4626 /* The resulting variable is a string of a single
4627 * character. If the index is too big or negative the
4628 * result is empty. */
4629 if (n1 >= len || n1 < 0)
4630 s = NULL;
4631 else
4632 s = vim_strnsave(s + n1, 1);
4633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004634 clear_tv(rettv);
4635 rettv->v_type = VAR_STRING;
4636 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 break;
4638
4639 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 if (n1 < 0)
4642 n1 = len + n1;
4643 if (!empty1 && (n1 < 0 || n1 >= len))
4644 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004645 /* For a range we allow invalid values and return an empty
4646 * list. A list index out of range is an error. */
4647 if (!range)
4648 {
4649 if (verbose)
4650 EMSGN(_(e_listidx), n1);
4651 return FAIL;
4652 }
4653 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 }
4655 if (range)
4656 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004657 list_T *l;
4658 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659
4660 if (n2 < 0)
4661 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004662 else if (n2 >= len)
4663 n2 = len - 1;
4664 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004665 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666 l = list_alloc();
4667 if (l == NULL)
4668 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 n1 <= n2; ++n1)
4671 {
4672 if (list_append_tv(l, &item->li_tv) == FAIL)
4673 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004674 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 return FAIL;
4676 }
4677 item = item->li_next;
4678 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 rettv->v_type = VAR_LIST;
4681 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004682 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 }
4684 else
4685 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004686 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687 clear_tv(rettv);
4688 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 }
4690 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691
4692 case VAR_DICT:
4693 if (range)
4694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004695 if (verbose)
4696 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 if (len == -1)
4698 clear_tv(&var1);
4699 return FAIL;
4700 }
4701 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004702 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703
4704 if (len == -1)
4705 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004706 key = get_tv_string_chk(&var1);
4707 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 clear_tv(&var1);
4710 return FAIL;
4711 }
4712 }
4713
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004714 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004715
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004716 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004717 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 if (len == -1)
4719 clear_tv(&var1);
4720 if (item == NULL)
4721 return FAIL;
4722
4723 copy_tv(&item->di_tv, &var1);
4724 clear_tv(rettv);
4725 *rettv = var1;
4726 }
4727 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 }
4729 }
4730
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 return OK;
4732}
4733
4734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 * Get an option value.
4736 * "arg" points to the '&' or '+' before the option name.
4737 * "arg" is advanced to character after the option name.
4738 * Return OK or FAIL.
4739 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741get_option_tv(
4742 char_u **arg,
4743 typval_T *rettv, /* when NULL, only check if option exists */
4744 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 char_u *option_end;
4747 long numval;
4748 char_u *stringval;
4749 int opt_type;
4750 int c;
4751 int working = (**arg == '+'); /* has("+option") */
4752 int ret = OK;
4753 int opt_flags;
4754
4755 /*
4756 * Isolate the option name and find its value.
4757 */
4758 option_end = find_option_end(arg, &opt_flags);
4759 if (option_end == NULL)
4760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 EMSG2(_("E112: Option name missing: %s"), *arg);
4763 return FAIL;
4764 }
4765
4766 if (!evaluate)
4767 {
4768 *arg = option_end;
4769 return OK;
4770 }
4771
4772 c = *option_end;
4773 *option_end = NUL;
4774 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 if (opt_type == -3) /* invalid name */
4778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 EMSG2(_("E113: Unknown option: %s"), *arg);
4781 ret = FAIL;
4782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 if (opt_type == -2) /* hidden string option */
4786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 rettv->v_type = VAR_STRING;
4788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 else if (opt_type == -1) /* hidden number option */
4791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 rettv->v_type = VAR_NUMBER;
4793 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else if (opt_type == 1) /* number option */
4796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 rettv->v_type = VAR_NUMBER;
4798 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
4800 else /* string option */
4801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 rettv->v_type = VAR_STRING;
4803 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 }
4806 else if (working && (opt_type == -2 || opt_type == -1))
4807 ret = FAIL;
4808
4809 *option_end = c; /* put back for error messages */
4810 *arg = option_end;
4811
4812 return ret;
4813}
4814
4815/*
4816 * Allocate a variable for a string constant.
4817 * Return OK or FAIL.
4818 */
4819 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004820get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
4822 char_u *p;
4823 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 int extra = 0;
4825
4826 /*
4827 * Find the end of the string, skipping backslashed characters.
4828 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004829 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 if (*p == '\\' && p[1] != NUL)
4832 {
4833 ++p;
4834 /* A "\<x>" form occupies at least 4 characters, and produces up
4835 * to 6 characters: reserve space for 2 extra */
4836 if (*p == '<')
4837 extra += 2;
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840
4841 if (*p != '"')
4842 {
4843 EMSG2(_("E114: Missing quote: %s"), *arg);
4844 return FAIL;
4845 }
4846
4847 /* If only parsing, set *arg and return here */
4848 if (!evaluate)
4849 {
4850 *arg = p + 1;
4851 return OK;
4852 }
4853
4854 /*
4855 * Copy the string into allocated memory, handling backslashed
4856 * characters.
4857 */
4858 name = alloc((unsigned)(p - *arg + extra));
4859 if (name == NULL)
4860 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 rettv->v_type = VAR_STRING;
4862 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar8c711452005-01-14 21:53:12 +00004864 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
4866 if (*p == '\\')
4867 {
4868 switch (*++p)
4869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004870 case 'b': *name++ = BS; ++p; break;
4871 case 'e': *name++ = ESC; ++p; break;
4872 case 'f': *name++ = FF; ++p; break;
4873 case 'n': *name++ = NL; ++p; break;
4874 case 'r': *name++ = CAR; ++p; break;
4875 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 case 'X': /* hex: "\x1", "\x12" */
4878 case 'x':
4879 case 'u': /* Unicode: "\u0023" */
4880 case 'U':
4881 if (vim_isxdigit(p[1]))
4882 {
4883 int n, nr;
4884 int c = toupper(*p);
4885
4886 if (c == 'X')
4887 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004888 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004890 else
4891 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 nr = 0;
4893 while (--n >= 0 && vim_isxdigit(p[1]))
4894 {
4895 ++p;
4896 nr = (nr << 4) + hex2nr(*p);
4897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#ifdef FEAT_MBYTE
4900 /* For "\u" store the number according to
4901 * 'encoding'. */
4902 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 else
4905#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 break;
4909
4910 /* octal: "\1", "\12", "\123" */
4911 case '0':
4912 case '1':
4913 case '2':
4914 case '3':
4915 case '4':
4916 case '5':
4917 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918 case '7': *name = *p++ - '0';
4919 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 *name = (*name << 3) + *p++ - '0';
4922 if (*p >= '0' && *p <= '7')
4923 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 break;
4927
4928 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (extra != 0)
4931 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004932 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 break;
4934 }
4935 /* FALLTHROUGH */
4936
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 break;
4939 }
4940 }
4941 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 *arg = p + 1;
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return OK;
4949}
4950
4951/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 * Return OK or FAIL.
4954 */
4955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004956get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957{
4958 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004959 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004960 int reduce = 0;
4961
4962 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004964 */
4965 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004970 break;
4971 ++reduce;
4972 ++p;
4973 }
4974 }
4975
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004979 return FAIL;
4980 }
4981
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004983 if (!evaluate)
4984 {
4985 *arg = p + 1;
4986 return OK;
4987 }
4988
4989 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004991 */
4992 str = alloc((unsigned)((p - *arg) - reduce));
4993 if (str == NULL)
4994 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 rettv->v_type = VAR_STRING;
4996 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004997
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005003 break;
5004 ++p;
5005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005007 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005009 *arg = p + 1;
5010
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005011 return OK;
5012}
5013
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005014/*
5015 * Return the function name of the partial.
5016 */
5017 char_u *
5018partial_name(partial_T *pt)
5019{
5020 if (pt->pt_name != NULL)
5021 return pt->pt_name;
5022 return pt->pt_func->uf_name;
5023}
5024
Bram Moolenaarddecc252016-04-06 22:59:37 +02005025 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005026partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005027{
5028 int i;
5029
5030 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005031 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005032 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005033 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005034 if (pt->pt_name != NULL)
5035 {
5036 func_unref(pt->pt_name);
5037 vim_free(pt->pt_name);
5038 }
5039 else
5040 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005041 vim_free(pt);
5042}
5043
5044/*
5045 * Unreference a closure: decrement the reference count and free it when it
5046 * becomes zero.
5047 */
5048 void
5049partial_unref(partial_T *pt)
5050{
5051 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005052 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005053}
5054
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005055static int tv_equal_recurse_limit;
5056
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005057 static int
5058func_equal(
5059 typval_T *tv1,
5060 typval_T *tv2,
5061 int ic) /* ignore case */
5062{
5063 char_u *s1, *s2;
5064 dict_T *d1, *d2;
5065 int a1, a2;
5066 int i;
5067
5068 /* empty and NULL function name considered the same */
5069 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005070 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005071 if (s1 != NULL && *s1 == NUL)
5072 s1 = NULL;
5073 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005074 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005075 if (s2 != NULL && *s2 == NUL)
5076 s2 = NULL;
5077 if (s1 == NULL || s2 == NULL)
5078 {
5079 if (s1 != s2)
5080 return FALSE;
5081 }
5082 else if (STRCMP(s1, s2) != 0)
5083 return FALSE;
5084
5085 /* empty dict and NULL dict is different */
5086 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5087 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5088 if (d1 == NULL || d2 == NULL)
5089 {
5090 if (d1 != d2)
5091 return FALSE;
5092 }
5093 else if (!dict_equal(d1, d2, ic, TRUE))
5094 return FALSE;
5095
5096 /* empty list and no list considered the same */
5097 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5098 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5099 if (a1 != a2)
5100 return FALSE;
5101 for (i = 0; i < a1; ++i)
5102 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5103 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5104 return FALSE;
5105
5106 return TRUE;
5107}
5108
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005109/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005110 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005111 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005113 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005114 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005115tv_equal(
5116 typval_T *tv1,
5117 typval_T *tv2,
5118 int ic, /* ignore case */
5119 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005120{
5121 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005123 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005124 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005125
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005126 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005127 * recursiveness to a limit. We guess they are equal then.
5128 * A fixed limit has the problem of still taking an awful long time.
5129 * Reduce the limit every time running into it. That should work fine for
5130 * deeply linked structures that are not recursively linked and catch
5131 * recursiveness quickly. */
5132 if (!recursive)
5133 tv_equal_recurse_limit = 1000;
5134 if (recursive_cnt >= tv_equal_recurse_limit)
5135 {
5136 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005137 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005138 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005139
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005140 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5141 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005142 if ((tv1->v_type == VAR_FUNC
5143 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5144 && (tv2->v_type == VAR_FUNC
5145 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5146 {
5147 ++recursive_cnt;
5148 r = func_equal(tv1, tv2, ic);
5149 --recursive_cnt;
5150 return r;
5151 }
5152
5153 if (tv1->v_type != tv2->v_type)
5154 return FALSE;
5155
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005156 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005157 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005158 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005159 ++recursive_cnt;
5160 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5161 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005162 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005163
5164 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005165 ++recursive_cnt;
5166 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5167 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005168 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005169
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005170 case VAR_NUMBER:
5171 return tv1->vval.v_number == tv2->vval.v_number;
5172
5173 case VAR_STRING:
5174 s1 = get_tv_string_buf(tv1, buf1);
5175 s2 = get_tv_string_buf(tv2, buf2);
5176 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005177
5178 case VAR_SPECIAL:
5179 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005180
5181 case VAR_FLOAT:
5182#ifdef FEAT_FLOAT
5183 return tv1->vval.v_float == tv2->vval.v_float;
5184#endif
5185 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005186#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005187 return tv1->vval.v_job == tv2->vval.v_job;
5188#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005189 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005190#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005191 return tv1->vval.v_channel == tv2->vval.v_channel;
5192#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005193 case VAR_FUNC:
5194 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005195 case VAR_UNKNOWN:
5196 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005197 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005198
Bram Moolenaara03f2332016-02-06 18:09:59 +01005199 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5200 * does not equal anything, not even itself. */
5201 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005202}
5203
5204/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005205 * Return the next (unique) copy ID.
5206 * Used for serializing nested structures.
5207 */
5208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005210{
5211 current_copyID += COPYID_INC;
5212 return current_copyID;
5213}
5214
5215/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005216 * Garbage collection for lists and dictionaries.
5217 *
5218 * We use reference counts to be able to free most items right away when they
5219 * are no longer used. But for composite items it's possible that it becomes
5220 * unused while the reference count is > 0: When there is a recursive
5221 * reference. Example:
5222 * :let l = [1, 2, 3]
5223 * :let d = {9: l}
5224 * :let l[1] = d
5225 *
5226 * Since this is quite unusual we handle this with garbage collection: every
5227 * once in a while find out which lists and dicts are not referenced from any
5228 * variable.
5229 *
5230 * Here is a good reference text about garbage collection (refers to Python
5231 * but it applies to all reference-counting mechanisms):
5232 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005233 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005234
5235/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005236 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005237 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005239 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005240 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005241garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005242{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005243 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005244 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245 buf_T *buf;
5246 win_T *wp;
5247 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005248 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005249#ifdef FEAT_WINDOWS
5250 tabpage_T *tp;
5251#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005253 if (!testing)
5254 {
5255 /* Only do this once. */
5256 want_garbage_collect = FALSE;
5257 may_garbage_collect = FALSE;
5258 garbage_collect_at_exit = FALSE;
5259 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005260
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005261 /* We advance by two because we add one for items referenced through
5262 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005263 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005264
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005265 /*
5266 * 1. Go through all accessible variables and mark all lists and dicts
5267 * with copyID.
5268 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005269
5270 /* Don't free variables in the previous_funccal list unless they are only
5271 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005272 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005273 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005274
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275 /* script-local variables */
5276 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005278
5279 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005280 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005281 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5282 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005283
5284 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005285 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005286 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5287 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005288#ifdef FEAT_AUTOCMD
5289 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5291 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005292#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005294#ifdef FEAT_WINDOWS
5295 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005296 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005297 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5298 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005299#endif
5300
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005301 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005303
5304 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005306
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005307 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005308 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005309
Bram Moolenaard812df62008-11-09 12:46:09 +00005310 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005311 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005312
Bram Moolenaar1dced572012-04-05 16:54:08 +02005313#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005314 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005315#endif
5316
Bram Moolenaardb913952012-06-29 12:54:53 +02005317#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005318 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005319#endif
5320
5321#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005322 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005323#endif
5324
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005325#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005326 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005327 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005328#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005329#ifdef FEAT_NETBEANS_INTG
5330 abort = abort || set_ref_in_nb_channel(copyID);
5331#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005332
Bram Moolenaare3188e22016-05-31 21:13:04 +02005333#ifdef FEAT_TIMERS
5334 abort = abort || set_ref_in_timer(copyID);
5335#endif
5336
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005337 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005338 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005339 /*
5340 * 2. Free lists and dictionaries that are not referenced.
5341 */
5342 did_free = free_unref_items(copyID);
5343
5344 /*
5345 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005346 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005347 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005348 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005349 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005350 else if (p_verbose > 0)
5351 {
5352 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5353 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005354
5355 return did_free;
5356}
5357
5358/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005359 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005360 */
5361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005362free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005363{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005364 int did_free = FALSE;
5365
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005366 /* Let all "free" functions know that we are here. This means no
5367 * dictionaries, lists, channels or jobs are to be freed, because we will
5368 * do that here. */
5369 in_free_unref_items = TRUE;
5370
5371 /*
5372 * PASS 1: free the contents of the items. We don't free the items
5373 * themselves yet, so that it is possible to decrement refcount counters
5374 */
5375
Bram Moolenaarcd524592016-07-17 14:57:05 +02005376 /* Go through the list of dicts and free items without the copyID. */
5377 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005378
Bram Moolenaarda861d62016-07-17 15:46:27 +02005379 /* Go through the list of lists and free items without the copyID. */
5380 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381
5382#ifdef FEAT_JOB_CHANNEL
5383 /* Go through the list of jobs and free items without the copyID. This
5384 * must happen before doing channels, because jobs refer to channels, but
5385 * the reference from the channel to the job isn't tracked. */
5386 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5387
5388 /* Go through the list of channels and free items without the copyID. */
5389 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5390#endif
5391
5392 /*
5393 * PASS 2: free the items themselves.
5394 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005395 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005396 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005397
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005398#ifdef FEAT_JOB_CHANNEL
5399 /* Go through the list of jobs and free items without the copyID. This
5400 * must happen before doing channels, because jobs refer to channels, but
5401 * the reference from the channel to the job isn't tracked. */
5402 free_unused_jobs(copyID, COPYID_MASK);
5403
5404 /* Go through the list of channels and free items without the copyID. */
5405 free_unused_channels(copyID, COPYID_MASK);
5406#endif
5407
5408 in_free_unref_items = FALSE;
5409
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005410 return did_free;
5411}
5412
5413/*
5414 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005415 * "list_stack" is used to add lists to be marked. Can be NULL.
5416 *
5417 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005418 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005420set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421{
5422 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005424 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 hashtab_T *cur_ht;
5426 ht_stack_T *ht_stack = NULL;
5427 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005429 cur_ht = ht;
5430 for (;;)
5431 {
5432 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005434 /* Mark each item in the hashtab. 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 todo = (int)cur_ht->ht_used;
5438 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5439 if (!HASHITEM_EMPTY(hi))
5440 {
5441 --todo;
5442 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5443 &ht_stack, list_stack);
5444 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005446
5447 if (ht_stack == NULL)
5448 break;
5449
5450 /* take an item from the stack */
5451 cur_ht = ht_stack->ht;
5452 tempitem = ht_stack;
5453 ht_stack = ht_stack->prev;
5454 free(tempitem);
5455 }
5456
5457 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005458}
5459
5460/*
5461 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005462 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5463 *
5464 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005465 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005467set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005468{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 listitem_T *li;
5470 int abort = FALSE;
5471 list_T *cur_l;
5472 list_stack_T *list_stack = NULL;
5473 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005474
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005475 cur_l = l;
5476 for (;;)
5477 {
5478 if (!abort)
5479 /* Mark each item in the list. If the item contains a hashtab
5480 * it is added to ht_stack, if it contains a list it is added to
5481 * list_stack. */
5482 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5483 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5484 ht_stack, &list_stack);
5485 if (list_stack == NULL)
5486 break;
5487
5488 /* take an item from the stack */
5489 cur_l = list_stack->list;
5490 tempitem = list_stack;
5491 list_stack = list_stack->prev;
5492 free(tempitem);
5493 }
5494
5495 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005496}
5497
5498/*
5499 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005500 * "list_stack" is used to add lists to be marked. Can be NULL.
5501 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5502 *
5503 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005504 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005506set_ref_in_item(
5507 typval_T *tv,
5508 int copyID,
5509 ht_stack_T **ht_stack,
5510 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005511{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005512 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005513
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005514 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005515 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005516 dict_T *dd = tv->vval.v_dict;
5517
Bram Moolenaara03f2332016-02-06 18:09:59 +01005518 if (dd != NULL && dd->dv_copyID != copyID)
5519 {
5520 /* Didn't see this dict yet. */
5521 dd->dv_copyID = copyID;
5522 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005523 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005524 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5525 }
5526 else
5527 {
5528 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5529 if (newitem == NULL)
5530 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 else
5532 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005533 newitem->ht = &dd->dv_hashtab;
5534 newitem->prev = *ht_stack;
5535 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005536 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005537 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 }
5539 }
5540 else if (tv->v_type == VAR_LIST)
5541 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005542 list_T *ll = tv->vval.v_list;
5543
Bram Moolenaara03f2332016-02-06 18:09:59 +01005544 if (ll != NULL && ll->lv_copyID != copyID)
5545 {
5546 /* Didn't see this list yet. */
5547 ll->lv_copyID = copyID;
5548 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005549 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005550 abort = set_ref_in_list(ll, copyID, ht_stack);
5551 }
5552 else
5553 {
5554 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005555 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005556 if (newitem == NULL)
5557 abort = TRUE;
5558 else
5559 {
5560 newitem->list = ll;
5561 newitem->prev = *list_stack;
5562 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005563 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005564 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005565 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005566 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005567 else if (tv->v_type == VAR_FUNC)
5568 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005569 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005570 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005571 else if (tv->v_type == VAR_PARTIAL)
5572 {
5573 partial_T *pt = tv->vval.v_partial;
5574 int i;
5575
5576 /* A partial does not have a copyID, because it cannot contain itself.
5577 */
5578 if (pt != NULL)
5579 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005580 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005581
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005582 if (pt->pt_dict != NULL)
5583 {
5584 typval_T dtv;
5585
5586 dtv.v_type = VAR_DICT;
5587 dtv.vval.v_dict = pt->pt_dict;
5588 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5589 }
5590
5591 for (i = 0; i < pt->pt_argc; ++i)
5592 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5593 ht_stack, list_stack);
5594 }
5595 }
5596#ifdef FEAT_JOB_CHANNEL
5597 else if (tv->v_type == VAR_JOB)
5598 {
5599 job_T *job = tv->vval.v_job;
5600 typval_T dtv;
5601
5602 if (job != NULL && job->jv_copyID != copyID)
5603 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005604 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005605 if (job->jv_channel != NULL)
5606 {
5607 dtv.v_type = VAR_CHANNEL;
5608 dtv.vval.v_channel = job->jv_channel;
5609 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5610 }
5611 if (job->jv_exit_partial != NULL)
5612 {
5613 dtv.v_type = VAR_PARTIAL;
5614 dtv.vval.v_partial = job->jv_exit_partial;
5615 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5616 }
5617 }
5618 }
5619 else if (tv->v_type == VAR_CHANNEL)
5620 {
5621 channel_T *ch =tv->vval.v_channel;
5622 int part;
5623 typval_T dtv;
5624 jsonq_T *jq;
5625 cbq_T *cq;
5626
5627 if (ch != NULL && ch->ch_copyID != copyID)
5628 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005629 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005630 for (part = PART_SOCK; part <= PART_IN; ++part)
5631 {
5632 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5633 jq = jq->jq_next)
5634 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5635 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5636 cq = cq->cq_next)
5637 if (cq->cq_partial != NULL)
5638 {
5639 dtv.v_type = VAR_PARTIAL;
5640 dtv.vval.v_partial = cq->cq_partial;
5641 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5642 }
5643 if (ch->ch_part[part].ch_partial != NULL)
5644 {
5645 dtv.v_type = VAR_PARTIAL;
5646 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5647 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5648 }
5649 }
5650 if (ch->ch_partial != NULL)
5651 {
5652 dtv.v_type = VAR_PARTIAL;
5653 dtv.vval.v_partial = ch->ch_partial;
5654 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5655 }
5656 if (ch->ch_close_partial != NULL)
5657 {
5658 dtv.v_type = VAR_PARTIAL;
5659 dtv.vval.v_partial = ch->ch_close_partial;
5660 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5661 }
5662 }
5663 }
5664#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005665 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005666}
5667
Bram Moolenaar17a13432016-01-24 14:22:10 +01005668 static char *
5669get_var_special_name(int nr)
5670{
5671 switch (nr)
5672 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005673 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005674 case VVAL_TRUE: return "v:true";
5675 case VVAL_NONE: return "v:none";
5676 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005677 }
5678 EMSG2(_(e_intern2), "get_var_special_name()");
5679 return "42";
5680}
5681
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683 * Return a string with the string representation of a variable.
5684 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005685 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005686 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005687 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5688 * "string()", otherwise does not put quotes around strings, as ":echo"
5689 * displays values.
5690 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5691 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005692 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005694 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005695echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005696 typval_T *tv,
5697 char_u **tofree,
5698 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005699 int copyID,
5700 int echo_style,
5701 int restore_copyID,
5702 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005704 static int recurse = 0;
5705 char_u *r = NULL;
5706
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005708 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005709 if (!did_echo_string_emsg)
5710 {
5711 /* Only give this message once for a recursive call to avoid
5712 * flooding the user with errors. And stop iterating over lists
5713 * and dicts. */
5714 did_echo_string_emsg = TRUE;
5715 EMSG(_("E724: variable nested too deep for displaying"));
5716 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005717 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005718 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005719 }
5720 ++recurse;
5721
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 switch (tv->v_type)
5723 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005724 case VAR_STRING:
5725 if (echo_style && !dict_val)
5726 {
5727 *tofree = NULL;
5728 r = get_tv_string_buf(tv, numbuf);
5729 }
5730 else
5731 {
5732 *tofree = string_quote(tv->vval.v_string, FALSE);
5733 r = *tofree;
5734 }
5735 break;
5736
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005738 if (echo_style)
5739 {
5740 *tofree = NULL;
5741 r = tv->vval.v_string;
5742 }
5743 else
5744 {
5745 *tofree = string_quote(tv->vval.v_string, TRUE);
5746 r = *tofree;
5747 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005748 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005749
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005750 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005751 {
5752 partial_T *pt = tv->vval.v_partial;
5753 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005754 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005755 garray_T ga;
5756 int i;
5757 char_u *tf;
5758
5759 ga_init2(&ga, 1, 100);
5760 ga_concat(&ga, (char_u *)"function(");
5761 if (fname != NULL)
5762 {
5763 ga_concat(&ga, fname);
5764 vim_free(fname);
5765 }
5766 if (pt != NULL && pt->pt_argc > 0)
5767 {
5768 ga_concat(&ga, (char_u *)", [");
5769 for (i = 0; i < pt->pt_argc; ++i)
5770 {
5771 if (i > 0)
5772 ga_concat(&ga, (char_u *)", ");
5773 ga_concat(&ga,
5774 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5775 vim_free(tf);
5776 }
5777 ga_concat(&ga, (char_u *)"]");
5778 }
5779 if (pt != NULL && pt->pt_dict != NULL)
5780 {
5781 typval_T dtv;
5782
5783 ga_concat(&ga, (char_u *)", ");
5784 dtv.v_type = VAR_DICT;
5785 dtv.vval.v_dict = pt->pt_dict;
5786 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5787 vim_free(tf);
5788 }
5789 ga_concat(&ga, (char_u *)")");
5790
5791 *tofree = ga.ga_data;
5792 r = *tofree;
5793 break;
5794 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005797 if (tv->vval.v_list == NULL)
5798 {
5799 *tofree = NULL;
5800 r = NULL;
5801 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005802 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5803 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005804 {
5805 *tofree = NULL;
5806 r = (char_u *)"[...]";
5807 }
5808 else
5809 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005810 int old_copyID = tv->vval.v_list->lv_copyID;
5811
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005812 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005813 *tofree = list2string(tv, copyID, restore_copyID);
5814 if (restore_copyID)
5815 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005816 r = *tofree;
5817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005818 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005819
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005821 if (tv->vval.v_dict == NULL)
5822 {
5823 *tofree = NULL;
5824 r = NULL;
5825 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005826 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5827 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005828 {
5829 *tofree = NULL;
5830 r = (char_u *)"{...}";
5831 }
5832 else
5833 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005834 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005835 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005836 *tofree = dict2string(tv, copyID, restore_copyID);
5837 if (restore_copyID)
5838 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005839 r = *tofree;
5840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005841 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005844 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005845 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005846 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005847 *tofree = NULL;
5848 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005850
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005851 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005852#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005853 *tofree = NULL;
5854 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5855 r = numbuf;
5856 break;
5857#endif
5858
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005859 case VAR_SPECIAL:
5860 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005861 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005862 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005864
Bram Moolenaar8502c702014-06-17 12:51:16 +02005865 if (--recurse == 0)
5866 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005867 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868}
5869
5870/*
5871 * Return a string with the string representation of a variable.
5872 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5873 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005874 * Does not put quotes around strings, as ":echo" displays values.
5875 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5876 * May return NULL.
5877 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005878 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005879echo_string(
5880 typval_T *tv,
5881 char_u **tofree,
5882 char_u *numbuf,
5883 int copyID)
5884{
5885 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5886}
5887
5888/*
5889 * Return a string with the string representation of a variable.
5890 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5891 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005893 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005895 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005896tv2string(
5897 typval_T *tv,
5898 char_u **tofree,
5899 char_u *numbuf,
5900 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005902 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903}
5904
5905/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 * Return string "str" in ' quotes, doubling ' characters.
5907 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005910 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005911string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912{
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 char_u *p, *r, *s;
5915
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 len = (function ? 13 : 3);
5917 if (str != NULL)
5918 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005919 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 for (p = str; *p != NUL; mb_ptr_adv(p))
5921 if (*p == '\'')
5922 ++len;
5923 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 s = r = alloc(len);
5925 if (r != NULL)
5926 {
5927 if (function)
5928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930 r += 10;
5931 }
5932 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 if (str != NULL)
5935 for (p = str; *p != NUL; )
5936 {
5937 if (*p == '\'')
5938 *r++ = '\'';
5939 MB_COPY_CHAR(p, r);
5940 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 if (function)
5943 *r++ = ')';
5944 *r++ = NUL;
5945 }
5946 return s;
5947}
5948
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005949#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005950/*
5951 * Convert the string "text" to a floating point number.
5952 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5953 * this always uses a decimal point.
5954 * Returns the length of the text that was consumed.
5955 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957string2float(
5958 char_u *text,
5959 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005960{
5961 char *s = (char *)text;
5962 float_T f;
5963
5964 f = strtod(s, &s);
5965 *value = f;
5966 return (int)((char_u *)s - text);
5967}
5968#endif
5969
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 * Get the value of an environment variable.
5972 * "arg" is pointing to the '$'. It is advanced to after the name.
5973 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005974 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 */
5976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978{
5979 char_u *string = NULL;
5980 int len;
5981 int cc;
5982 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005983 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
5985 ++*arg;
5986 name = *arg;
5987 len = get_env_len(arg);
5988 if (evaluate)
5989 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005990 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005991 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005992
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005993 cc = name[len];
5994 name[len] = NUL;
5995 /* first try vim_getenv(), fast for normal environment vars */
5996 string = vim_getenv(name, &mustfree);
5997 if (string != NULL && *string != NUL)
5998 {
5999 if (!mustfree)
6000 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006002 else
6003 {
6004 if (mustfree)
6005 vim_free(string);
6006
6007 /* next try expanding things like $VIM and ${HOME} */
6008 string = expand_env_save(name - 1);
6009 if (string != NULL && *string == '$')
6010 {
6011 vim_free(string);
6012 string = NULL;
6013 }
6014 }
6015 name[len] = cc;
6016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006017 rettv->v_type = VAR_STRING;
6018 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
6020
6021 return OK;
6022}
6023
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006024
6025
6026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006028 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031var2fpos(
6032 typval_T *varp,
6033 int dollar_lnum, /* TRUE when $ is last line */
6034 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006036 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006038 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
Bram Moolenaara5525202006-03-02 22:52:09 +00006040 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006041 if (varp->v_type == VAR_LIST)
6042 {
6043 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006044 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006045 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006046 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006047
6048 l = varp->vval.v_list;
6049 if (l == NULL)
6050 return NULL;
6051
6052 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006053 pos.lnum = list_find_nr(l, 0L, &error);
6054 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006055 return NULL; /* invalid line number */
6056
6057 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006058 pos.col = list_find_nr(l, 1L, &error);
6059 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006060 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006061 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006062
6063 /* We accept "$" for the column number: last column. */
6064 li = list_find(l, 1L);
6065 if (li != NULL && li->li_tv.v_type == VAR_STRING
6066 && li->li_tv.vval.v_string != NULL
6067 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6068 pos.col = len + 1;
6069
Bram Moolenaara5525202006-03-02 22:52:09 +00006070 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006071 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006072 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006073 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006074
Bram Moolenaara5525202006-03-02 22:52:09 +00006075#ifdef FEAT_VIRTUALEDIT
6076 /* Get the virtual offset. Defaults to zero. */
6077 pos.coladd = list_find_nr(l, 2L, &error);
6078 if (error)
6079 pos.coladd = 0;
6080#endif
6081
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006082 return &pos;
6083 }
6084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006085 name = get_tv_string_chk(varp);
6086 if (name == NULL)
6087 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006088 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006090 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6091 {
6092 if (VIsual_active)
6093 return &VIsual;
6094 return &curwin->w_cursor;
6095 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006096 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006098 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6100 return NULL;
6101 return pp;
6102 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006103
6104#ifdef FEAT_VIRTUALEDIT
6105 pos.coladd = 0;
6106#endif
6107
Bram Moolenaar477933c2007-07-17 14:32:23 +00006108 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006109 {
6110 pos.col = 0;
6111 if (name[1] == '0') /* "w0": first visible line */
6112 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006113 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006114 pos.lnum = curwin->w_topline;
6115 return &pos;
6116 }
6117 else if (name[1] == '$') /* "w$": last visible line */
6118 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006119 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006120 pos.lnum = curwin->w_botline - 1;
6121 return &pos;
6122 }
6123 }
6124 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006126 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
6128 pos.lnum = curbuf->b_ml.ml_line_count;
6129 pos.col = 0;
6130 }
6131 else
6132 {
6133 pos.lnum = curwin->w_cursor.lnum;
6134 pos.col = (colnr_T)STRLEN(ml_get_curline());
6135 }
6136 return &pos;
6137 }
6138 return NULL;
6139}
6140
6141/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006142 * Convert list in "arg" into a position and optional file number.
6143 * When "fnump" is NULL there is no file number, only 3 items.
6144 * Note that the column is passed on as-is, the caller may want to decrement
6145 * it to use 1 for the first column.
6146 * Return FAIL when conversion is not possible, doesn't check the position for
6147 * validity.
6148 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006150list2fpos(
6151 typval_T *arg,
6152 pos_T *posp,
6153 int *fnump,
6154 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006155{
6156 list_T *l = arg->vval.v_list;
6157 long i = 0;
6158 long n;
6159
Bram Moolenaar493c1782014-05-28 14:34:46 +02006160 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6161 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006162 if (arg->v_type != VAR_LIST
6163 || l == NULL
6164 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006165 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006166 return FAIL;
6167
6168 if (fnump != NULL)
6169 {
6170 n = list_find_nr(l, i++, NULL); /* fnum */
6171 if (n < 0)
6172 return FAIL;
6173 if (n == 0)
6174 n = curbuf->b_fnum; /* current buffer */
6175 *fnump = n;
6176 }
6177
6178 n = list_find_nr(l, i++, NULL); /* lnum */
6179 if (n < 0)
6180 return FAIL;
6181 posp->lnum = n;
6182
6183 n = list_find_nr(l, i++, NULL); /* col */
6184 if (n < 0)
6185 return FAIL;
6186 posp->col = n;
6187
6188#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006189 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006190 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006191 posp->coladd = 0;
6192 else
6193 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006194#endif
6195
Bram Moolenaar493c1782014-05-28 14:34:46 +02006196 if (curswantp != NULL)
6197 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6198
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006199 return OK;
6200}
6201
6202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 * Get the length of an environment variable name.
6204 * Advance "arg" to the first character after the name.
6205 * Return 0 for error.
6206 */
6207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006208get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 char_u *p;
6211 int len;
6212
6213 for (p = *arg; vim_isIDc(*p); ++p)
6214 ;
6215 if (p == *arg) /* no name found */
6216 return 0;
6217
6218 len = (int)(p - *arg);
6219 *arg = p;
6220 return len;
6221}
6222
6223/*
6224 * Get the length of the name of a function or internal variable.
6225 * "arg" is advanced to the first non-white character after the name.
6226 * Return 0 if something is wrong.
6227 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006228 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006229get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 char_u *p;
6232 int len;
6233
6234 /* Find the end of the name. */
6235 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006236 {
6237 if (*p == ':')
6238 {
6239 /* "s:" is start of "s:var", but "n:" is not and can be used in
6240 * slice "[n:]". Also "xx:" is not a namespace. */
6241 len = (int)(p - *arg);
6242 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6243 || len > 1)
6244 break;
6245 }
6246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (p == *arg) /* no name found */
6248 return 0;
6249
6250 len = (int)(p - *arg);
6251 *arg = skipwhite(p);
6252
6253 return len;
6254}
6255
6256/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006257 * Get the length of the name of a variable or function.
6258 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006260 * Return -1 if curly braces expansion failed.
6261 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 * If the name contains 'magic' {}'s, expand them and return the
6263 * expanded name in an allocated string via 'alias' - caller must free.
6264 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266get_name_len(
6267 char_u **arg,
6268 char_u **alias,
6269 int evaluate,
6270 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 char_u *p;
6274 char_u *expr_start;
6275 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 *alias = NULL; /* default to no alias */
6278
6279 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6280 && (*arg)[2] == (int)KE_SNR)
6281 {
6282 /* hard coded <SNR>, already translated */
6283 *arg += 3;
6284 return get_id_len(arg) + 3;
6285 }
6286 len = eval_fname_script(*arg);
6287 if (len > 0)
6288 {
6289 /* literal "<SID>", "s:" or "<SNR>" */
6290 *arg += len;
6291 }
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006296 p = find_name_end(*arg, &expr_start, &expr_end,
6297 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (expr_start != NULL)
6299 {
6300 char_u *temp_string;
6301
6302 if (!evaluate)
6303 {
6304 len += (int)(p - *arg);
6305 *arg = skipwhite(p);
6306 return len;
6307 }
6308
6309 /*
6310 * Include any <SID> etc in the expanded string:
6311 * Thus the -len here.
6312 */
6313 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6314 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006315 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 *alias = temp_string;
6317 *arg = skipwhite(p);
6318 return (int)STRLEN(temp_string);
6319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
6321 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006322 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 EMSG2(_(e_invexpr2), *arg);
6324
6325 return len;
6326}
6327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006328/*
6329 * Find the end of a variable or function name, taking care of magic braces.
6330 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6331 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006332 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006333 * Return a pointer to just after the name. Equal to "arg" if there is no
6334 * valid name.
6335 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006337find_name_end(
6338 char_u *arg,
6339 char_u **expr_start,
6340 char_u **expr_end,
6341 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006343 int mb_nest = 0;
6344 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006346 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006348 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350 *expr_start = NULL;
6351 *expr_end = NULL;
6352 }
6353
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006354 /* Quick check for valid starting character. */
6355 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6356 return arg;
6357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006358 for (p = arg; *p != NUL
6359 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006361 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006363 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006365 if (*p == '\'')
6366 {
6367 /* skip over 'string' to avoid counting [ and ] inside it. */
6368 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6369 ;
6370 if (*p == NUL)
6371 break;
6372 }
6373 else if (*p == '"')
6374 {
6375 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6376 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6377 if (*p == '\\' && p[1] != NUL)
6378 ++p;
6379 if (*p == NUL)
6380 break;
6381 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006382 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6383 {
6384 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006385 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006386 len = (int)(p - arg);
6387 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006388 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006389 break;
6390 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006392 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006394 if (*p == '[')
6395 ++br_nest;
6396 else if (*p == ']')
6397 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006400 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006402 if (*p == '{')
6403 {
6404 mb_nest++;
6405 if (expr_start != NULL && *expr_start == NULL)
6406 *expr_start = p;
6407 }
6408 else if (*p == '}')
6409 {
6410 mb_nest--;
6411 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6412 *expr_end = p;
6413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416
6417 return p;
6418}
6419
6420/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006421 * Expands out the 'magic' {}'s in a variable/function name.
6422 * Note that this can call itself recursively, to deal with
6423 * constructs like foo{bar}{baz}{bam}
6424 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6425 * "in_start" ^
6426 * "expr_start" ^
6427 * "expr_end" ^
6428 * "in_end" ^
6429 *
6430 * Returns a new allocated string, which the caller must free.
6431 * Returns NULL for failure.
6432 */
6433 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434make_expanded_name(
6435 char_u *in_start,
6436 char_u *expr_start,
6437 char_u *expr_end,
6438 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006439{
6440 char_u c1;
6441 char_u *retval = NULL;
6442 char_u *temp_result;
6443 char_u *nextcmd = NULL;
6444
6445 if (expr_end == NULL || in_end == NULL)
6446 return NULL;
6447 *expr_start = NUL;
6448 *expr_end = NUL;
6449 c1 = *in_end;
6450 *in_end = NUL;
6451
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006452 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006453 if (temp_result != NULL && nextcmd == NULL)
6454 {
6455 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6456 + (in_end - expr_end) + 1));
6457 if (retval != NULL)
6458 {
6459 STRCPY(retval, in_start);
6460 STRCAT(retval, temp_result);
6461 STRCAT(retval, expr_end + 1);
6462 }
6463 }
6464 vim_free(temp_result);
6465
6466 *in_end = c1; /* put char back for error messages */
6467 *expr_start = '{';
6468 *expr_end = '}';
6469
6470 if (retval != NULL)
6471 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006472 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006473 if (expr_start != NULL)
6474 {
6475 /* Further expansion! */
6476 temp_result = make_expanded_name(retval, expr_start,
6477 expr_end, temp_result);
6478 vim_free(retval);
6479 retval = temp_result;
6480 }
6481 }
6482
6483 return retval;
6484}
6485
6486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006488 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006491eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006493 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6494}
6495
6496/*
6497 * Return TRUE if character "c" can be used as the first character in a
6498 * variable or function name (excluding '{' and '}').
6499 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006500 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006501eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006502{
6503 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504}
6505
6506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 * Set number v: variable to "val".
6508 */
6509 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006510set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006512 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513}
6514
6515/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006516 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006518 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522}
6523
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006524/*
6525 * Get string v: variable value. Uses a static buffer, can only be used once.
6526 */
6527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006528get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006529{
6530 return get_tv_string(&vimvars[idx].vv_tv);
6531}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006532
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006534 * Get List v: variable value. Caller must take care of reference count when
6535 * needed.
6536 */
6537 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006539{
6540 return vimvars[idx].vv_list;
6541}
6542
6543/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006544 * Set v:char to character "c".
6545 */
6546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006548{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006549 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006550
6551#ifdef FEAT_MBYTE
6552 if (has_mbyte)
6553 buf[(*mb_char2bytes)(c, buf)] = NUL;
6554 else
6555#endif
6556 {
6557 buf[0] = c;
6558 buf[1] = NUL;
6559 }
6560 set_vim_var_string(VV_CHAR, buf, -1);
6561}
6562
6563/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006564 * Set v:count to "count" and v:count1 to "count1".
6565 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 */
6567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568set_vcount(
6569 long count,
6570 long count1,
6571 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006573 if (set_prevcount)
6574 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 vimvars[VV_COUNT].vv_nr = count;
6576 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577}
6578
6579/*
6580 * Set string v: variable to a copy of "val".
6581 */
6582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583set_vim_var_string(
6584 int idx,
6585 char_u *val,
6586 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
Bram Moolenaara542c682016-01-31 16:28:04 +01006588 clear_tv(&vimvars[idx].vv_di.di_tv);
6589 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006591 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596}
6597
6598/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006599 * Set List v: variable to "val".
6600 */
6601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006602set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006603{
Bram Moolenaara542c682016-01-31 16:28:04 +01006604 clear_tv(&vimvars[idx].vv_di.di_tv);
6605 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006606 vimvars[idx].vv_list = val;
6607 if (val != NULL)
6608 ++val->lv_refcount;
6609}
6610
6611/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006612 * Set Dictionary v: variable to "val".
6613 */
6614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006615set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006616{
6617 int todo;
6618 hashitem_T *hi;
6619
Bram Moolenaara542c682016-01-31 16:28:04 +01006620 clear_tv(&vimvars[idx].vv_di.di_tv);
6621 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006622 vimvars[idx].vv_dict = val;
6623 if (val != NULL)
6624 {
6625 ++val->dv_refcount;
6626
6627 /* Set readonly */
6628 todo = (int)val->dv_hashtab.ht_used;
6629 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6630 {
6631 if (HASHITEM_EMPTY(hi))
6632 continue;
6633 --todo;
6634 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6635 }
6636 }
6637}
6638
6639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 * Set v:register if needed.
6641 */
6642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006643set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 char_u regname;
6646
6647 if (c == 0 || c == ' ')
6648 regname = '"';
6649 else
6650 regname = c;
6651 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 set_vim_var_string(VV_REG, &regname, 1);
6654}
6655
6656/*
6657 * Get or set v:exception. If "oldval" == NULL, return the current value.
6658 * Otherwise, restore the value to "oldval" and return NULL.
6659 * Must always be called in pairs to save and restore v:exception! Does not
6660 * take care of memory allocations.
6661 */
6662 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 return NULL;
6670}
6671
6672/*
6673 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6674 * Otherwise, restore the value to "oldval" and return NULL.
6675 * Must always be called in pairs to save and restore v:throwpoint! Does not
6676 * take care of memory allocations.
6677 */
6678 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006679v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
Bram Moolenaare9a41262005-01-15 22:18:47 +00006684 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 return NULL;
6686}
6687
6688#if defined(FEAT_AUTOCMD) || defined(PROTO)
6689/*
6690 * Set v:cmdarg.
6691 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6692 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6693 * Must always be called in pairs!
6694 */
6695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006696set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 char_u *oldval;
6699 char_u *newval;
6700 unsigned len;
6701
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006703 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006705 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006707 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006710 if (eap->force_bin == FORCE_BIN)
6711 len = 6;
6712 else if (eap->force_bin == FORCE_NOBIN)
6713 len = 8;
6714 else
6715 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006716
6717 if (eap->read_edit)
6718 len += 7;
6719
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006720 if (eap->force_ff != 0)
6721 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6722# ifdef FEAT_MBYTE
6723 if (eap->force_enc != 0)
6724 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006725 if (eap->bad_char != 0)
6726 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006727# endif
6728
6729 newval = alloc(len + 1);
6730 if (newval == NULL)
6731 return NULL;
6732
6733 if (eap->force_bin == FORCE_BIN)
6734 sprintf((char *)newval, " ++bin");
6735 else if (eap->force_bin == FORCE_NOBIN)
6736 sprintf((char *)newval, " ++nobin");
6737 else
6738 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006739
6740 if (eap->read_edit)
6741 STRCAT(newval, " ++edit");
6742
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006743 if (eap->force_ff != 0)
6744 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6745 eap->cmd + eap->force_ff);
6746# ifdef FEAT_MBYTE
6747 if (eap->force_enc != 0)
6748 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6749 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006750 if (eap->bad_char == BAD_KEEP)
6751 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6752 else if (eap->bad_char == BAD_DROP)
6753 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6754 else if (eap->bad_char != 0)
6755 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006756# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006757 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006758 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759}
6760#endif
6761
6762/*
6763 * Get the value of internal variable "name".
6764 * Return OK or FAIL.
6765 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767get_var_tv(
6768 char_u *name,
6769 int len, /* length of "name" */
6770 typval_T *rettv, /* NULL when only checking existence */
6771 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6772 int verbose, /* may give error message */
6773 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006776 typval_T *tv = NULL;
6777 typval_T atv;
6778 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
6781 /* truncate the name, so that we can use strcmp() */
6782 cc = name[len];
6783 name[len] = NUL;
6784
6785 /*
6786 * Check for "b:changedtick".
6787 */
6788 if (STRCMP(name, "b:changedtick") == 0)
6789 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006790 atv.v_type = VAR_NUMBER;
6791 atv.vval.v_number = curbuf->b_changedtick;
6792 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794
6795 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 * Check for user-defined variables.
6797 */
6798 else
6799 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01006800 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006802 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006803 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006804 if (dip != NULL)
6805 *dip = v;
6806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006811 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 ret = FAIL;
6814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 name[len] = cc;
6819
6820 return ret;
6821}
6822
6823/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006824 * Check if variable "name[len]" is a local variable or an argument.
6825 * If so, "*eval_lavars_used" is set to TRUE.
6826 */
6827 static void
6828check_vars(char_u *name, int len)
6829{
6830 int cc;
6831 char_u *varname;
6832 hashtab_T *ht;
6833
6834 if (eval_lavars_used == NULL)
6835 return;
6836
6837 /* truncate the name, so that we can use strcmp() */
6838 cc = name[len];
6839 name[len] = NUL;
6840
6841 ht = find_var_ht(name, &varname);
6842 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6843 {
6844 if (find_var(name, NULL, TRUE) != NULL)
6845 *eval_lavars_used = TRUE;
6846 }
6847
6848 name[len] = cc;
6849}
6850
6851/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006852 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6853 * Also handle function call with Funcref variable: func(expr)
6854 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6855 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006857handle_subscript(
6858 char_u **arg,
6859 typval_T *rettv,
6860 int evaluate, /* do more than finding the end */
6861 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006862{
6863 int ret = OK;
6864 dict_T *selfdict = NULL;
6865 char_u *s;
6866 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006867 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868
6869 while (ret == OK
6870 && (**arg == '['
6871 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006872 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6873 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006874 && !vim_iswhite(*(*arg - 1)))
6875 {
6876 if (**arg == '(')
6877 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006878 partial_T *pt = NULL;
6879
Bram Moolenaard9fba312005-06-26 22:34:35 +00006880 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006881 if (evaluate)
6882 {
6883 functv = *rettv;
6884 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006885
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006886 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006887 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006888 {
6889 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006890 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006891 }
6892 else
6893 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006894 }
6895 else
6896 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006897 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006898 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006899 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006900
6901 /* Clear the funcref afterwards, so that deleting it while
6902 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006903 if (evaluate)
6904 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006905
6906 /* Stop the expression evaluation when immediately aborting on
6907 * error, or when an interrupt occurred or an exception was thrown
6908 * but not caught. */
6909 if (aborting())
6910 {
6911 if (ret == OK)
6912 clear_tv(rettv);
6913 ret = FAIL;
6914 }
6915 dict_unref(selfdict);
6916 selfdict = NULL;
6917 }
6918 else /* **arg == '[' || **arg == '.' */
6919 {
6920 dict_unref(selfdict);
6921 if (rettv->v_type == VAR_DICT)
6922 {
6923 selfdict = rettv->vval.v_dict;
6924 if (selfdict != NULL)
6925 ++selfdict->dv_refcount;
6926 }
6927 else
6928 selfdict = NULL;
6929 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6930 {
6931 clear_tv(rettv);
6932 ret = FAIL;
6933 }
6934 }
6935 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006936
Bram Moolenaar1d429612016-05-24 15:44:17 +02006937 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6938 * Don't do this when "Func" is already a partial that was bound
6939 * explicitly (pt_auto is FALSE). */
6940 if (selfdict != NULL
6941 && (rettv->v_type == VAR_FUNC
6942 || (rettv->v_type == VAR_PARTIAL
6943 && (rettv->vval.v_partial->pt_auto
6944 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006945 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006946
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006947 dict_unref(selfdict);
6948 return ret;
6949}
6950
6951/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006952 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006953 * value).
6954 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006955 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957{
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006959}
6960
6961/*
6962 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 * The string "s" must have been allocated, it is consumed.
6964 * Return NULL for out of memory, the variable otherwise.
6965 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 rettv = alloc_tv();
6972 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006974 rettv->v_type = VAR_STRING;
6975 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977 else
6978 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980}
6981
6982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006983 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 if (varp != NULL)
6989 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006990 switch (varp->v_type)
6991 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006992 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006993 func_unref(varp->vval.v_string);
6994 /*FALLTHROUGH*/
6995 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006996 vim_free(varp->vval.v_string);
6997 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006998 case VAR_PARTIAL:
6999 partial_unref(varp->vval.v_partial);
7000 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001 case VAR_LIST:
7002 list_unref(varp->vval.v_list);
7003 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007004 case VAR_DICT:
7005 dict_unref(varp->vval.v_dict);
7006 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007008#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009 job_unref(varp->vval.v_job);
7010 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007011#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007012 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007013#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007014 channel_unref(varp->vval.v_channel);
7015 break;
7016#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007017 case VAR_NUMBER:
7018 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007019 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007020 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007021 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 vim_free(varp);
7024 }
7025}
7026
7027/*
7028 * Free the memory for a variable value and set the value to NULL or 0.
7029 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007031clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 if (varp != NULL)
7034 {
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_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007038 func_unref(varp->vval.v_string);
7039 /*FALLTHROUGH*/
7040 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 vim_free(varp->vval.v_string);
7042 varp->vval.v_string = NULL;
7043 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007044 case VAR_PARTIAL:
7045 partial_unref(varp->vval.v_partial);
7046 varp->vval.v_partial = NULL;
7047 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007048 case VAR_LIST:
7049 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007051 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 case VAR_DICT:
7053 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007056 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007057 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007058 varp->vval.v_number = 0;
7059 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007060 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007061#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007062 varp->vval.v_float = 0.0;
7063 break;
7064#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007065 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007067 job_unref(varp->vval.v_job);
7068 varp->vval.v_job = NULL;
7069#endif
7070 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007071 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007073 channel_unref(varp->vval.v_channel);
7074 varp->vval.v_channel = NULL;
7075#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007076 case VAR_UNKNOWN:
7077 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007079 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081}
7082
7083/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084 * Set the value of a variable to NULL without freeing items.
7085 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007087init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007088{
7089 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007091}
7092
7093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 * Get the number value of a variable.
7095 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 * For incompatible types, return 0.
7097 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7098 * caller of incompatible types: it sets *denote to TRUE if "denote"
7099 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007101 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007104 int error = FALSE;
7105
7106 return get_tv_number_chk(varp, &error); /* return 0L on error */
7107}
7108
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007109 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007111{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007112 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007117 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007118 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007119#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007120 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007121 break;
7122#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007123 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007124 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007125 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 break;
7127 case VAR_STRING:
7128 if (varp->vval.v_string != NULL)
7129 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007130 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007131 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007132 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007133 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007134 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007135 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007136 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007137 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007138 case VAR_SPECIAL:
7139 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7140 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007141 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007143 EMSG(_("E910: Using a Job as a Number"));
7144 break;
7145#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007146 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007148 EMSG(_("E913: Using a Channel as a Number"));
7149 break;
7150#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007151 case VAR_UNKNOWN:
7152 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007155 if (denote == NULL) /* useful for values that must be unsigned */
7156 n = -1;
7157 else
7158 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007159 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160}
7161
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007162#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007165{
7166 switch (varp->v_type)
7167 {
7168 case VAR_NUMBER:
7169 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007170 case VAR_FLOAT:
7171 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007172 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007173 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007174 EMSG(_("E891: Using a Funcref as a Float"));
7175 break;
7176 case VAR_STRING:
7177 EMSG(_("E892: Using a String as a Float"));
7178 break;
7179 case VAR_LIST:
7180 EMSG(_("E893: Using a List as a Float"));
7181 break;
7182 case VAR_DICT:
7183 EMSG(_("E894: Using a Dictionary as a Float"));
7184 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007185 case VAR_SPECIAL:
7186 EMSG(_("E907: Using a special value as a Float"));
7187 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007188 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007189# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007190 EMSG(_("E911: Using a Job as a Float"));
7191 break;
7192# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007193 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007194# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007195 EMSG(_("E914: Using a Channel as a Float"));
7196 break;
7197# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007198 case VAR_UNKNOWN:
7199 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007200 break;
7201 }
7202 return 0;
7203}
7204#endif
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 * Get the string value of a variable.
7208 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007209 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7210 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 * If the String variable has never been set, return an empty string.
7212 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007213 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7214 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007216 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007217get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 static char_u mybuf[NUMBUFLEN];
7220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222}
7223
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007224 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 char_u *res = get_tv_string_buf_chk(varp, buf);
7228
7229 return res != NULL ? res : (char_u *)"";
7230}
7231
Bram Moolenaar7d647822014-04-05 21:28:56 +02007232/*
7233 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7234 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007235 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237{
7238 static char_u mybuf[NUMBUFLEN];
7239
7240 return get_tv_string_buf_chk(varp, mybuf);
7241}
7242
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007243 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007245{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007246 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007249 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7250 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007251 return buf;
7252 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007253 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007254 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007255 break;
7256 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007257 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 break;
7259 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007260 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007261 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007262 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007263#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007264 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007265 break;
7266#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007267 case VAR_STRING:
7268 if (varp->vval.v_string != NULL)
7269 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007270 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007271 case VAR_SPECIAL:
7272 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7273 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007274 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007275#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007276 {
7277 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007278 char *status;
7279
7280 if (job == NULL)
7281 return (char_u *)"no process";
7282 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007283 : job->jv_status == JOB_ENDED ? "dead"
7284 : "run";
7285# ifdef UNIX
7286 vim_snprintf((char *)buf, NUMBUFLEN,
7287 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007288# elif defined(WIN32)
7289 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007290 "process %ld %s",
7291 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007292 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007293# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007294 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007295 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7296# endif
7297 return buf;
7298 }
7299#endif
7300 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007301 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007302#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007303 {
7304 channel_T *channel = varp->vval.v_channel;
7305 char *status = channel_status(channel);
7306
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007307 if (channel == NULL)
7308 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7309 else
7310 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007311 "channel %d %s", channel->ch_id, status);
7312 return buf;
7313 }
7314#endif
7315 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007316 case VAR_UNKNOWN:
7317 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007318 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
7323/*
7324 * Find variable "name" in the list of variables.
7325 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007326 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007327 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007330 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007335 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
Bram Moolenaara7043832005-01-21 11:56:39 +00007337 ht = find_var_ht(name, &varname);
7338 if (htp != NULL)
7339 *htp = ht;
7340 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007342 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7343 if (ret != NULL)
7344 return ret;
7345
7346 /* Search in parent scope for lambda */
7347 return find_var_in_scoped_ht(name, varname ? &varname : NULL,
7348 no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349}
7350
7351/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007352 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007353 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007355 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356find_var_in_ht(
7357 hashtab_T *ht,
7358 int htname,
7359 char_u *varname,
7360 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007361{
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hashitem_T *hi;
7363
7364 if (*varname == NUL)
7365 {
7366 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007367 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007369 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 case 'g': return &globvars_var;
7371 case 'v': return &vimvars_var;
7372 case 'b': return &curbuf->b_bufvar;
7373 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007374#ifdef FEAT_WINDOWS
7375 case 't': return &curtab->tp_winvar;
7376#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007377 case 'l': return get_funccal_local_var();
7378 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 }
7380 return NULL;
7381 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007382
7383 hi = hash_find(ht, varname);
7384 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007385 {
7386 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007387 * worked find the variable again. Don't auto-load a script if it was
7388 * loaded already, otherwise it would be loaded every time when
7389 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007390 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007391 {
7392 /* Note: script_autoload() may make "hi" invalid. It must either
7393 * be obtained again or not used. */
7394 if (!script_autoload(varname, FALSE) || aborting())
7395 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007396 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007397 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007398 if (HASHITEM_EMPTY(hi))
7399 return NULL;
7400 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007402}
7403
7404/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007406 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007407 * Set "varname" to the start of name without ':'.
7408 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007409 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007410find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007412 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007413 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007414
Bram Moolenaar73627d02015-08-11 15:46:09 +02007415 if (name[0] == NUL)
7416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (name[1] != ':')
7418 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007419 /* The name must not start with a colon or #. */
7420 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return NULL;
7422 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007423
7424 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007425 hi = hash_find(&compat_hashtab, name);
7426 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007427 return &compat_hashtab;
7428
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007429 ht = get_funccal_local_ht();
7430 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007432 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007435 if (*name == 'g') /* global variable */
7436 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007437 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7438 */
7439 if (vim_strchr(name + 2, ':') != NULL
7440 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007443 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007445 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446#ifdef FEAT_WINDOWS
7447 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007448 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007449#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 if (*name == 'v') /* v: variable */
7451 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007452 if (*name == 'a') /* a: function argument */
7453 return get_funccal_args_ht();
7454 if (*name == 'l') /* l: local function variable */
7455 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (*name == 's' /* script variable */
7457 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7458 return &SCRIPT_VARS(current_SID);
7459 return NULL;
7460}
7461
7462/*
7463 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007464 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 * Returns NULL when it doesn't exist.
7466 */
7467 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007468get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469{
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007472 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (v == NULL)
7474 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
7478/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 * sourcing this script and when executing functions defined in the script.
7481 */
7482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007483new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
Bram Moolenaara7043832005-01-21 11:56:39 +00007485 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 hashtab_T *ht;
7487 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7490 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007491 /* Re-allocating ga_data means that an ht_array pointing to
7492 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007494 for (i = 1; i <= ga_scripts.ga_len; ++i)
7495 {
7496 ht = &SCRIPT_VARS(i);
7497 if (ht->ht_mask == HT_INIT_SIZE - 1)
7498 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007499 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007501 }
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 while (ga_scripts.ga_len < id)
7504 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007505 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007506 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007507 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 }
7511}
7512
7513/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7515 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 */
7517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007518init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007521 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007522 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007523 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007524 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 dict_var->di_tv.vval.v_dict = dict;
7526 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007527 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7529 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531
7532/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007533 * Unreference a dictionary initialized by init_var_dict().
7534 */
7535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007537{
7538 /* Now the dict needs to be freed if no one else is using it, go back to
7539 * normal reference counting. */
7540 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7541 dict_unref(dict);
7542}
7543
7544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 * Frees all allocated variables and the value they contain.
7547 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 */
7549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007551{
7552 vars_clear_ext(ht, TRUE);
7553}
7554
7555/*
7556 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7557 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007559vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
Bram Moolenaara7043832005-01-21 11:56:39 +00007561 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 hashitem_T *hi;
7563 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007566 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007567 for (hi = ht->ht_array; todo > 0; ++hi)
7568 {
7569 if (!HASHITEM_EMPTY(hi))
7570 {
7571 --todo;
7572
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007574 * ht_array might change then. hash_clear() takes care of it
7575 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 v = HI2DI(hi);
7577 if (free_val)
7578 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007579 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007581 }
7582 }
7583 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007584 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585}
7586
Bram Moolenaara7043832005-01-21 11:56:39 +00007587/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 * Delete a variable from hashtab "ht" at item "hi".
7589 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007592delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007595
7596 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 clear_tv(&di->di_tv);
7598 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599}
7600
7601/*
7602 * List the value of one internal variable.
7603 */
7604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007605list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 char_u *tofree;
7608 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007609 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007610
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007611 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007613 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007618list_one_var_a(
7619 char_u *prefix,
7620 char_u *name,
7621 int type,
7622 char_u *string,
7623 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
Bram Moolenaar31859182007-08-14 20:41:13 +00007625 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7626 msg_start();
7627 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (name != NULL) /* "a:" vars don't have a name stored */
7629 msg_puts(name);
7630 msg_putchar(' ');
7631 msg_advance(22);
7632 if (type == VAR_NUMBER)
7633 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007634 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 msg_putchar('*');
7636 else if (type == VAR_LIST)
7637 {
7638 msg_putchar('[');
7639 if (*string == '[')
7640 ++string;
7641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 else if (type == VAR_DICT)
7643 {
7644 msg_putchar('{');
7645 if (*string == '{')
7646 ++string;
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 else
7649 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007653 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007654 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007655 if (*first)
7656 {
7657 msg_clr_eos();
7658 *first = FALSE;
7659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 * If the variable already exists, the value is updated.
7665 * Otherwise the variable is created.
7666 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007668set_var(
7669 char_u *name,
7670 typval_T *tv,
7671 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007677 ht = find_var_ht(name, &varname);
7678 if (ht == NULL || *varname == NUL)
7679 {
7680 EMSG2(_(e_illvar), name);
7681 return;
7682 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007683 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007684
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007685 /* Search in parent scope which is possible to reference from lambda */
7686 if (v == NULL)
7687 v = find_var_in_scoped_ht(name, varname ? &varname : NULL, TRUE);
7688
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007689 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7690 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007691 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007696 if (var_check_ro(v->di_flags, name, FALSE)
7697 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007699
7700 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007701 * Handle setting internal v: variables separately where needed to
7702 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 */
7704 if (ht == &vimvarht)
7705 {
7706 if (v->di_tv.v_type == VAR_STRING)
7707 {
7708 vim_free(v->di_tv.vval.v_string);
7709 if (copy || tv->v_type != VAR_STRING)
7710 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7711 else
7712 {
7713 /* Take over the string to avoid an extra alloc/free. */
7714 v->di_tv.vval.v_string = tv->vval.v_string;
7715 tv->vval.v_string = NULL;
7716 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007717 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007719 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007720 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007722 if (STRCMP(varname, "searchforward") == 0)
7723 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007724#ifdef FEAT_SEARCH_EXTRA
7725 else if (STRCMP(varname, "hlsearch") == 0)
7726 {
7727 no_hlsearch = !v->di_tv.vval.v_number;
7728 redraw_all_later(SOME_VALID);
7729 }
7730#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007731 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007732 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007733 else if (v->di_tv.v_type != tv->v_type)
7734 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 }
7736
7737 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739 else /* add a new variable */
7740 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007741 /* Can't add "v:" variable. */
7742 if (ht == &vimvarht)
7743 {
7744 EMSG2(_(e_illvar), name);
7745 return;
7746 }
7747
Bram Moolenaar92124a32005-06-17 22:03:40 +00007748 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007749 if (!valid_varname(varname))
7750 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007751
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007752 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7753 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007754 if (v == NULL)
7755 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007759 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 return;
7761 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007762 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007764
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007765 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007767 else
7768 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007769 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007770 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007776 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 * Also give an error message.
7778 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007780var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007781{
7782 if (flags & DI_FLAGS_RO)
7783 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007784 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 return TRUE;
7786 }
7787 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7788 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007789 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007790 return TRUE;
7791 }
7792 return FALSE;
7793}
7794
7795/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007796 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7797 * Also give an error message.
7798 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007800var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007801{
7802 if (flags & DI_FLAGS_FIX)
7803 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007804 EMSG2(_("E795: Cannot delete variable %s"),
7805 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007806 return TRUE;
7807 }
7808 return FALSE;
7809}
7810
7811/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007812 * Check if a funcref is assigned to a valid variable name.
7813 * Return TRUE and give an error if not.
7814 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007816var_check_func_name(
7817 char_u *name, /* points to start of variable name */
7818 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007819{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007820 /* Allow for w: b: s: and t:. */
7821 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007822 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7823 ? name[2] : name[0]))
7824 {
7825 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7826 name);
7827 return TRUE;
7828 }
7829 /* Don't allow hiding a function. When "v" is not NULL we might be
7830 * assigning another function to the same var, the type is checked
7831 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007832 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007833 {
7834 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7835 name);
7836 return TRUE;
7837 }
7838 return FALSE;
7839}
7840
7841/*
7842 * Check if a variable name is valid.
7843 * Return FALSE and give an error if not.
7844 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007846valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007847{
7848 char_u *p;
7849
7850 for (p = varname; *p != NUL; ++p)
7851 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7852 && *p != AUTOLOAD_CHAR)
7853 {
7854 EMSG2(_(e_illvar), varname);
7855 return FALSE;
7856 }
7857 return TRUE;
7858}
7859
7860/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007861 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007862 * Also give an error message, using "name" or _("name") when use_gettext is
7863 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007864 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007866tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007867{
7868 if (lock & VAR_LOCKED)
7869 {
7870 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007871 name == NULL ? (char_u *)_("Unknown")
7872 : use_gettext ? (char_u *)_(name)
7873 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007874 return TRUE;
7875 }
7876 if (lock & VAR_FIXED)
7877 {
7878 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007879 name == NULL ? (char_u *)_("Unknown")
7880 : use_gettext ? (char_u *)_(name)
7881 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007882 return TRUE;
7883 }
7884 return FALSE;
7885}
7886
7887/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007891 * It is OK for "from" and "to" to point to the same item. This is used to
7892 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007897 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007898 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007899 switch (from->v_type)
7900 {
7901 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007902 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903 to->vval.v_number = from->vval.v_number;
7904 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007906#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907 to->vval.v_float = from->vval.v_float;
7908 break;
7909#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007910 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007911#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007912 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007913 if (to->vval.v_job != NULL)
7914 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007915 break;
7916#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007917 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007918#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007919 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007920 if (to->vval.v_channel != NULL)
7921 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007922 break;
7923#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 case VAR_STRING:
7925 case VAR_FUNC:
7926 if (from->vval.v_string == NULL)
7927 to->vval.v_string = NULL;
7928 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007929 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007930 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007931 if (from->v_type == VAR_FUNC)
7932 func_ref(to->vval.v_string);
7933 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007934 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007935 case VAR_PARTIAL:
7936 if (from->vval.v_partial == NULL)
7937 to->vval.v_partial = NULL;
7938 else
7939 {
7940 to->vval.v_partial = from->vval.v_partial;
7941 ++to->vval.v_partial->pt_refcount;
7942 }
7943 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 case VAR_LIST:
7945 if (from->vval.v_list == NULL)
7946 to->vval.v_list = NULL;
7947 else
7948 {
7949 to->vval.v_list = from->vval.v_list;
7950 ++to->vval.v_list->lv_refcount;
7951 }
7952 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 case VAR_DICT:
7954 if (from->vval.v_dict == NULL)
7955 to->vval.v_dict = NULL;
7956 else
7957 {
7958 to->vval.v_dict = from->vval.v_dict;
7959 ++to->vval.v_dict->dv_refcount;
7960 }
7961 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007962 case VAR_UNKNOWN:
7963 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 break;
7965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
7968/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969 * Make a copy of an item.
7970 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007971 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7972 * reference to an already copied list/dict can be used.
7973 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007974 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007976item_copy(
7977 typval_T *from,
7978 typval_T *to,
7979 int deep,
7980 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981{
7982 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007983 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986 {
7987 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007988 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007989 }
7990 ++recurse;
7991
7992 switch (from->v_type)
7993 {
7994 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007995 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007996 case VAR_STRING:
7997 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007998 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007999 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008000 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008001 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 copy_tv(from, to);
8003 break;
8004 case VAR_LIST:
8005 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008006 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008007 if (from->vval.v_list == NULL)
8008 to->vval.v_list = NULL;
8009 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8010 {
8011 /* use the copy made earlier */
8012 to->vval.v_list = from->vval.v_list->lv_copylist;
8013 ++to->vval.v_list->lv_refcount;
8014 }
8015 else
8016 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8017 if (to->vval.v_list == NULL)
8018 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008019 break;
8020 case VAR_DICT:
8021 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008022 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008023 if (from->vval.v_dict == NULL)
8024 to->vval.v_dict = NULL;
8025 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8026 {
8027 /* use the copy made earlier */
8028 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8029 ++to->vval.v_dict->dv_refcount;
8030 }
8031 else
8032 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8033 if (to->vval.v_dict == NULL)
8034 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008035 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008036 case VAR_UNKNOWN:
8037 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008038 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039 }
8040 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008041 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042}
8043
8044/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 * This function is used by f_input() and f_inputdialog() functions. The third
8046 * argument to f_input() specifies the type of completion to use at the
8047 * prompt. The third argument to f_inputdialog() specifies the value to return
8048 * when the user cancels the prompt.
8049 */
8050 void
8051get_user_input(
8052 typval_T *argvars,
8053 typval_T *rettv,
8054 int inputdialog,
8055 int secret)
8056{
8057 char_u *prompt = get_tv_string_chk(&argvars[0]);
8058 char_u *p = NULL;
8059 int c;
8060 char_u buf[NUMBUFLEN];
8061 int cmd_silent_save = cmd_silent;
8062 char_u *defstr = (char_u *)"";
8063 int xp_type = EXPAND_NOTHING;
8064 char_u *xp_arg = NULL;
8065
8066 rettv->v_type = VAR_STRING;
8067 rettv->vval.v_string = NULL;
8068
8069#ifdef NO_CONSOLE_INPUT
8070 /* While starting up, there is no place to enter text. */
8071 if (no_console_input())
8072 return;
8073#endif
8074
8075 cmd_silent = FALSE; /* Want to see the prompt. */
8076 if (prompt != NULL)
8077 {
8078 /* Only the part of the message after the last NL is considered as
8079 * prompt for the command line */
8080 p = vim_strrchr(prompt, '\n');
8081 if (p == NULL)
8082 p = prompt;
8083 else
8084 {
8085 ++p;
8086 c = *p;
8087 *p = NUL;
8088 msg_start();
8089 msg_clr_eos();
8090 msg_puts_attr(prompt, echo_attr);
8091 msg_didout = FALSE;
8092 msg_starthere();
8093 *p = c;
8094 }
8095 cmdline_row = msg_row;
8096
8097 if (argvars[1].v_type != VAR_UNKNOWN)
8098 {
8099 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8100 if (defstr != NULL)
8101 stuffReadbuffSpec(defstr);
8102
8103 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8104 {
8105 char_u *xp_name;
8106 int xp_namelen;
8107 long argt;
8108
8109 /* input() with a third argument: completion */
8110 rettv->vval.v_string = NULL;
8111
8112 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8113 if (xp_name == NULL)
8114 return;
8115
8116 xp_namelen = (int)STRLEN(xp_name);
8117
8118 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8119 &xp_arg) == FAIL)
8120 return;
8121 }
8122 }
8123
8124 if (defstr != NULL)
8125 {
8126 int save_ex_normal_busy = ex_normal_busy;
8127 ex_normal_busy = 0;
8128 rettv->vval.v_string =
8129 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8130 xp_type, xp_arg);
8131 ex_normal_busy = save_ex_normal_busy;
8132 }
8133 if (inputdialog && rettv->vval.v_string == NULL
8134 && argvars[1].v_type != VAR_UNKNOWN
8135 && argvars[2].v_type != VAR_UNKNOWN)
8136 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8137 &argvars[2], buf));
8138
8139 vim_free(xp_arg);
8140
8141 /* since the user typed this, no need to wait for return */
8142 need_wait_return = FALSE;
8143 msg_didout = FALSE;
8144 }
8145 cmd_silent = cmd_silent_save;
8146}
8147
8148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 * ":echo expr1 ..." print each argument separated with a space, add a
8150 * newline at the end.
8151 * ":echon expr1 ..." print each argument plain.
8152 */
8153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008154ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
8156 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 char_u *p;
8160 int needclr = TRUE;
8161 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008162 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
8164 if (eap->skip)
8165 ++emsg_skip;
8166 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8167 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168 /* If eval1() causes an error message the text from the command may
8169 * still need to be cleared. E.g., "echo 22,44". */
8170 need_clr_eos = needclr;
8171
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 /*
8176 * Report the invalid expression unless the expression evaluation
8177 * has been cancelled due to an aborting error, an interrupt, or an
8178 * exception.
8179 */
8180 if (!aborting())
8181 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 break;
8184 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185 need_clr_eos = FALSE;
8186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (!eap->skip)
8188 {
8189 if (atstart)
8190 {
8191 atstart = FALSE;
8192 /* Call msg_start() after eval1(), evaluating the expression
8193 * may cause a message to appear. */
8194 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008195 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008196 /* Mark the saved text as finishing the line, so that what
8197 * follows is displayed on a new line when scrolling back
8198 * at the more prompt. */
8199 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 }
8203 else if (eap->cmdidx == CMD_echo)
8204 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008205 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 if (p != NULL)
8207 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008209 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008211 if (*p != TAB && needclr)
8212 {
8213 /* remove any text still there from the command */
8214 msg_clr_eos();
8215 needclr = FALSE;
8216 }
8217 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 }
8219 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008220 {
8221#ifdef FEAT_MBYTE
8222 if (has_mbyte)
8223 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008224 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008225
8226 (void)msg_outtrans_len_attr(p, i, echo_attr);
8227 p += i - 1;
8228 }
8229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008231 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008236 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 arg = skipwhite(arg);
8238 }
8239 eap->nextcmd = check_nextcmd(arg);
8240
8241 if (eap->skip)
8242 --emsg_skip;
8243 else
8244 {
8245 /* remove text that may still be there from the command */
8246 if (needclr)
8247 msg_clr_eos();
8248 if (eap->cmdidx == CMD_echo)
8249 msg_end();
8250 }
8251}
8252
8253/*
8254 * ":echohl {name}".
8255 */
8256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008257ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 int id;
8260
8261 id = syn_name2id(eap->arg);
8262 if (id == 0)
8263 echo_attr = 0;
8264 else
8265 echo_attr = syn_id2attr(id);
8266}
8267
8268/*
8269 * ":execute expr1 ..." execute the result of an expression.
8270 * ":echomsg expr1 ..." Print a message
8271 * ":echoerr expr1 ..." Print an error
8272 * Each gets spaces around each argument and a newline at the end for
8273 * echo commands
8274 */
8275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008276ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008279 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 int ret = OK;
8281 char_u *p;
8282 garray_T ga;
8283 int len;
8284 int save_did_emsg;
8285
8286 ga_init2(&ga, 1, 80);
8287
8288 if (eap->skip)
8289 ++emsg_skip;
8290 while (*arg != NUL && *arg != '|' && *arg != '\n')
8291 {
8292 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {
8295 /*
8296 * Report the invalid expression unless the expression evaluation
8297 * has been cancelled due to an aborting error, an interrupt, or an
8298 * exception.
8299 */
8300 if (!aborting())
8301 EMSG2(_(e_invexpr2), p);
8302 ret = FAIL;
8303 break;
8304 }
8305
8306 if (!eap->skip)
8307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 len = (int)STRLEN(p);
8310 if (ga_grow(&ga, len + 2) == FAIL)
8311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 ret = FAIL;
8314 break;
8315 }
8316 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 ga.ga_len += len;
8320 }
8321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 arg = skipwhite(arg);
8324 }
8325
8326 if (ret != FAIL && ga.ga_data != NULL)
8327 {
8328 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008331 out_flush();
8332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 else if (eap->cmdidx == CMD_echoerr)
8334 {
8335 /* We don't want to abort following commands, restore did_emsg. */
8336 save_did_emsg = did_emsg;
8337 EMSG((char_u *)ga.ga_data);
8338 if (!force_abort)
8339 did_emsg = save_did_emsg;
8340 }
8341 else if (eap->cmdidx == CMD_execute)
8342 do_cmdline((char_u *)ga.ga_data,
8343 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8344 }
8345
8346 ga_clear(&ga);
8347
8348 if (eap->skip)
8349 --emsg_skip;
8350
8351 eap->nextcmd = check_nextcmd(arg);
8352}
8353
8354/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 * Find window specified by "vp" in tabpage "tp".
8356 */
8357 win_T *
8358find_win_by_nr(
8359 typval_T *vp,
8360 tabpage_T *tp UNUSED) /* NULL for current tab page */
8361{
8362#ifdef FEAT_WINDOWS
8363 win_T *wp;
8364#endif
8365 int nr;
8366
8367 nr = (int)get_tv_number_chk(vp, NULL);
8368
8369#ifdef FEAT_WINDOWS
8370 if (nr < 0)
8371 return NULL;
8372 if (nr == 0)
8373 return curwin;
8374
Bram Moolenaar29323592016-07-24 22:04:11 +02008375 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8376 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 if (nr >= LOWEST_WIN_ID)
8378 {
8379 if (wp->w_id == nr)
8380 return wp;
8381 }
8382 else if (--nr <= 0)
8383 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008384 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 if (nr >= LOWEST_WIN_ID)
8386 return NULL;
8387 return wp;
8388#else
8389 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8390 return curwin;
8391 return NULL;
8392#endif
8393}
8394
8395/*
8396 * Find window specified by "wvp" in tabpage "tvp".
8397 */
8398 win_T *
8399find_tabwin(
8400 typval_T *wvp, /* VAR_UNKNOWN for current window */
8401 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8402{
8403 win_T *wp = NULL;
8404 tabpage_T *tp = NULL;
8405 long n;
8406
8407 if (wvp->v_type != VAR_UNKNOWN)
8408 {
8409 if (tvp->v_type != VAR_UNKNOWN)
8410 {
8411 n = (long)get_tv_number(tvp);
8412 if (n >= 0)
8413 tp = find_tabpage(n);
8414 }
8415 else
8416 tp = curtab;
8417
8418 if (tp != NULL)
8419 wp = find_win_by_nr(wvp, tp);
8420 }
8421 else
8422 wp = curwin;
8423
8424 return wp;
8425}
8426
8427/*
8428 * getwinvar() and gettabwinvar()
8429 */
8430 void
8431getwinvar(
8432 typval_T *argvars,
8433 typval_T *rettv,
8434 int off) /* 1 for gettabwinvar() */
8435{
8436 win_T *win;
8437 char_u *varname;
8438 dictitem_T *v;
8439 tabpage_T *tp = NULL;
8440 int done = FALSE;
8441#ifdef FEAT_WINDOWS
8442 win_T *oldcurwin;
8443 tabpage_T *oldtabpage;
8444 int need_switch_win;
8445#endif
8446
8447#ifdef FEAT_WINDOWS
8448 if (off == 1)
8449 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8450 else
8451 tp = curtab;
8452#endif
8453 win = find_win_by_nr(&argvars[off], tp);
8454 varname = get_tv_string_chk(&argvars[off + 1]);
8455 ++emsg_off;
8456
8457 rettv->v_type = VAR_STRING;
8458 rettv->vval.v_string = NULL;
8459
8460 if (win != NULL && varname != NULL)
8461 {
8462#ifdef FEAT_WINDOWS
8463 /* Set curwin to be our win, temporarily. Also set the tabpage,
8464 * otherwise the window is not valid. Only do this when needed,
8465 * autocommands get blocked. */
8466 need_switch_win = !(tp == curtab && win == curwin);
8467 if (!need_switch_win
8468 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8469#endif
8470 {
8471 if (*varname == '&') /* window-local-option */
8472 {
8473 if (get_option_tv(&varname, rettv, 1) == OK)
8474 done = TRUE;
8475 }
8476 else
8477 {
8478 /* Look up the variable. */
8479 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8480 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8481 varname, FALSE);
8482 if (v != NULL)
8483 {
8484 copy_tv(&v->di_tv, rettv);
8485 done = TRUE;
8486 }
8487 }
8488 }
8489
8490#ifdef FEAT_WINDOWS
8491 if (need_switch_win)
8492 /* restore previous notion of curwin */
8493 restore_win(oldcurwin, oldtabpage, TRUE);
8494#endif
8495 }
8496
8497 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8498 /* use the default return value */
8499 copy_tv(&argvars[off + 2], rettv);
8500
8501 --emsg_off;
8502}
8503
8504/*
8505 * "setwinvar()" and "settabwinvar()" functions
8506 */
8507 void
8508setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8509{
8510 win_T *win;
8511#ifdef FEAT_WINDOWS
8512 win_T *save_curwin;
8513 tabpage_T *save_curtab;
8514 int need_switch_win;
8515#endif
8516 char_u *varname, *winvarname;
8517 typval_T *varp;
8518 char_u nbuf[NUMBUFLEN];
8519 tabpage_T *tp = NULL;
8520
8521 if (check_restricted() || check_secure())
8522 return;
8523
8524#ifdef FEAT_WINDOWS
8525 if (off == 1)
8526 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8527 else
8528 tp = curtab;
8529#endif
8530 win = find_win_by_nr(&argvars[off], tp);
8531 varname = get_tv_string_chk(&argvars[off + 1]);
8532 varp = &argvars[off + 2];
8533
8534 if (win != NULL && varname != NULL && varp != NULL)
8535 {
8536#ifdef FEAT_WINDOWS
8537 need_switch_win = !(tp == curtab && win == curwin);
8538 if (!need_switch_win
8539 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8540#endif
8541 {
8542 if (*varname == '&')
8543 {
8544 long numval;
8545 char_u *strval;
8546 int error = FALSE;
8547
8548 ++varname;
8549 numval = (long)get_tv_number_chk(varp, &error);
8550 strval = get_tv_string_buf_chk(varp, nbuf);
8551 if (!error && strval != NULL)
8552 set_option_value(varname, numval, strval, OPT_LOCAL);
8553 }
8554 else
8555 {
8556 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8557 if (winvarname != NULL)
8558 {
8559 STRCPY(winvarname, "w:");
8560 STRCPY(winvarname + 2, varname);
8561 set_var(winvarname, varp, TRUE);
8562 vim_free(winvarname);
8563 }
8564 }
8565 }
8566#ifdef FEAT_WINDOWS
8567 if (need_switch_win)
8568 restore_win(save_curwin, save_curtab, TRUE);
8569#endif
8570 }
8571}
8572
8573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8575 * "arg" points to the "&" or '+' when called, to "option" when returning.
8576 * Returns NULL when no option name found. Otherwise pointer to the char
8577 * after the option name.
8578 */
8579 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008580find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 char_u *p = *arg;
8583
8584 ++p;
8585 if (*p == 'g' && p[1] == ':')
8586 {
8587 *opt_flags = OPT_GLOBAL;
8588 p += 2;
8589 }
8590 else if (*p == 'l' && p[1] == ':')
8591 {
8592 *opt_flags = OPT_LOCAL;
8593 p += 2;
8594 }
8595 else
8596 *opt_flags = 0;
8597
8598 if (!ASCII_ISALPHA(*p))
8599 return NULL;
8600 *arg = p;
8601
8602 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8603 p += 4; /* termcap option */
8604 else
8605 while (ASCII_ISALPHA(*p))
8606 ++p;
8607 return p;
8608}
8609
8610/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008611 * Return the autoload script name for a function or variable name.
8612 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008614 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008615autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008616{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008617 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008618 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008619
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008620 /* Get the script file name: replace '#' with '/', append ".vim". */
8621 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8622 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008623 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008624 STRCPY(scriptname, "autoload/");
8625 STRCAT(scriptname, name);
8626 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8627 STRCAT(scriptname, ".vim");
8628 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8629 *p = '/';
8630 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008631}
8632
8633/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008634 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635 * Return TRUE if a package was loaded.
8636 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008638script_autoload(
8639 char_u *name,
8640 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008641{
8642 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008643 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008644 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008645 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008646
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008647 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008648 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008649 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008650 return FALSE;
8651
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008652 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008653
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008654 /* Find the name in the list of previously loaded package names. Skip
8655 * "autoload/", it's always the same. */
8656 for (i = 0; i < ga_loaded.ga_len; ++i)
8657 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8658 break;
8659 if (!reload && i < ga_loaded.ga_len)
8660 ret = FALSE; /* was loaded already */
8661 else
8662 {
8663 /* Remember the name if it wasn't loaded already. */
8664 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8665 {
8666 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8667 tofree = NULL;
8668 }
8669
8670 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008671 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008672 ret = TRUE;
8673 }
8674
8675 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008676 return ret;
8677}
8678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8680typedef enum
8681{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008682 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8683 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8684 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685} var_flavour_T;
8686
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008687static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
8689 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008690var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
8692 char_u *p = varname;
8693
8694 if (ASCII_ISUPPER(*p))
8695 {
8696 while (*(++p))
8697 if (ASCII_ISLOWER(*p))
8698 return VAR_FLAVOUR_SESSION;
8699 return VAR_FLAVOUR_VIMINFO;
8700 }
8701 else
8702 return VAR_FLAVOUR_DEFAULT;
8703}
8704#endif
8705
8706#if defined(FEAT_VIMINFO) || defined(PROTO)
8707/*
8708 * Restore global vars that start with a capital from the viminfo file
8709 */
8710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008711read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008714 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008716 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 if (!writing && (find_viminfo_parameter('!') != NULL))
8719 {
8720 tab = vim_strchr(virp->vir_line + 1, '\t');
8721 if (tab != NULL)
8722 {
8723 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008724 switch (*tab)
8725 {
8726 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008727#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008728 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008729#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008730 case 'D': type = VAR_DICT; break;
8731 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008732 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
8735 tab = vim_strchr(tab, '\t');
8736 if (tab != NULL)
8737 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008738 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008739 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008740 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008742#ifdef FEAT_FLOAT
8743 else if (type == VAR_FLOAT)
8744 (void)string2float(tab + 1, &tv.vval.v_float);
8745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008747 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008748 if (type == VAR_DICT || type == VAR_LIST)
8749 {
8750 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8751
8752 if (etv == NULL)
8753 /* Failed to parse back the dict or list, use it as a
8754 * string. */
8755 tv.v_type = VAR_STRING;
8756 else
8757 {
8758 vim_free(tv.vval.v_string);
8759 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008760 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008761 }
8762 }
8763
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008764 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008765 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008766 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008767 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008768
8769 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008770 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008771 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8772 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 }
8774 }
8775 }
8776
8777 return viminfo_readline(virp);
8778}
8779
8780/*
8781 * Write global vars that start with a capital to the viminfo file
8782 */
8783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008784write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 hashitem_T *hi;
8787 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008788 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008789 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008790 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008792 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
8794 if (find_viminfo_parameter('!') == NULL)
8795 return;
8796
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008797 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008798
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008799 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008802 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008804 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008805 this_var = HI2DI(hi);
8806 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008809 {
8810 case VAR_STRING: s = "STR"; break;
8811 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008812 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008813 case VAR_DICT: s = "DIC"; break;
8814 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008815 case VAR_SPECIAL: s = "XPL"; break;
8816
8817 case VAR_UNKNOWN:
8818 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008819 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008820 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008821 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008822 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008825 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008826 if (p != NULL)
8827 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008828 vim_free(tofree);
8829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 }
8831 }
8832}
8833#endif
8834
8835#if defined(FEAT_SESSION) || defined(PROTO)
8836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008837store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 hashitem_T *hi;
8840 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008841 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 char_u *p, *t;
8843
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008844 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008847 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008849 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 this_var = HI2DI(hi);
8851 if ((this_var->di_tv.v_type == VAR_NUMBER
8852 || this_var->di_tv.v_type == VAR_STRING)
8853 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008854 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008855 /* Escape special characters with a backslash. Turn a LF and
8856 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008858 (char_u *)"\\\"\n\r");
8859 if (p == NULL) /* out of memory */
8860 break;
8861 for (t = p; *t != NUL; ++t)
8862 if (*t == '\n')
8863 *t = 'n';
8864 else if (*t == '\r')
8865 *t = 'r';
8866 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 this_var->di_key,
8868 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8869 : ' ',
8870 p,
8871 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8872 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008873 || put_eol(fd) == FAIL)
8874 {
8875 vim_free(p);
8876 return FAIL;
8877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 vim_free(p);
8879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880#ifdef FEAT_FLOAT
8881 else if (this_var->di_tv.v_type == VAR_FLOAT
8882 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8883 {
8884 float_T f = this_var->di_tv.vval.v_float;
8885 int sign = ' ';
8886
8887 if (f < 0)
8888 {
8889 f = -f;
8890 sign = '-';
8891 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008892 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893 this_var->di_key, sign, f) < 0)
8894 || put_eol(fd) == FAIL)
8895 return FAIL;
8896 }
8897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 }
8900 return OK;
8901}
8902#endif
8903
Bram Moolenaar661b1822005-07-28 22:36:45 +00008904/*
8905 * Display script name where an item was last set.
8906 * Should only be invoked when 'verbose' is non-zero.
8907 */
8908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008909last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008910{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008911 char_u *p;
8912
Bram Moolenaar661b1822005-07-28 22:36:45 +00008913 if (scriptID != 0)
8914 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008915 p = home_replace_save(NULL, get_scriptname(scriptID));
8916 if (p != NULL)
8917 {
8918 verbose_enter();
8919 MSG_PUTS(_("\n\tLast set from "));
8920 MSG_PUTS(p);
8921 vim_free(p);
8922 verbose_leave();
8923 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008924 }
8925}
8926
Bram Moolenaard812df62008-11-09 12:46:09 +00008927/*
8928 * List v:oldfiles in a nice way.
8929 */
Bram Moolenaard812df62008-11-09 12:46:09 +00008930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008931ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +00008932{
8933 list_T *l = vimvars[VV_OLDFILES].vv_list;
8934 listitem_T *li;
8935 int nr = 0;
8936
8937 if (l == NULL)
8938 msg((char_u *)_("No old files"));
8939 else
8940 {
8941 msg_start();
8942 msg_scroll = TRUE;
8943 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
8944 {
8945 msg_outnum((long)++nr);
8946 MSG_PUTS(": ");
8947 msg_outtrans(get_tv_string(&li->li_tv));
8948 msg_putchar('\n');
8949 out_flush(); /* output one line at a time */
8950 ui_breakcheck();
8951 }
8952 /* Assume "got_int" was set to truncate the listing. */
8953 got_int = FALSE;
8954
8955#ifdef FEAT_BROWSE_CMD
8956 if (cmdmod.browse)
8957 {
8958 quit_more = FALSE;
8959 nr = prompt_for_number(FALSE);
8960 msg_starthere();
8961 if (nr > 0)
8962 {
8963 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
8964 (long)nr);
8965
8966 if (p != NULL)
8967 {
8968 p = expand_env_save(p);
8969 eap->arg = p;
8970 eap->cmdidx = CMD_edit;
8971 cmdmod.browse = FALSE;
8972 do_exedit(eap, NULL);
8973 vim_free(p);
8974 }
8975 }
8976 }
8977#endif
8978 }
8979}
8980
Bram Moolenaar53744302015-07-17 17:38:22 +02008981/* reset v:option_new, v:option_old and v:option_type */
8982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008983reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008984{
8985 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8986 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8987 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8988}
8989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990/*
8991 * Prepare "gap" for an assert error and add the sourcing position.
8992 */
8993 void
8994prepare_assert_error(garray_T *gap)
8995{
8996 char buf[NUMBUFLEN];
8997
8998 ga_init2(gap, 1, 100);
8999 if (sourcing_name != NULL)
9000 {
9001 ga_concat(gap, sourcing_name);
9002 if (sourcing_lnum > 0)
9003 ga_concat(gap, (char_u *)" ");
9004 }
9005 if (sourcing_lnum > 0)
9006 {
9007 sprintf(buf, "line %ld", (long)sourcing_lnum);
9008 ga_concat(gap, (char_u *)buf);
9009 }
9010 if (sourcing_name != NULL || sourcing_lnum > 0)
9011 ga_concat(gap, (char_u *)": ");
9012}
9013
9014/*
9015 * Add an assert error to v:errors.
9016 */
9017 void
9018assert_error(garray_T *gap)
9019{
9020 struct vimvar *vp = &vimvars[VV_ERRORS];
9021
9022 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9023 /* Make sure v:errors is a list. */
9024 set_vim_var_list(VV_ERRORS, list_alloc());
9025 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9026}
9027
9028 void
9029assert_equal_common(typval_T *argvars, assert_type_T atype)
9030{
9031 garray_T ga;
9032
9033 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9034 != (atype == ASSERT_EQUAL))
9035 {
9036 prepare_assert_error(&ga);
9037 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9038 atype);
9039 assert_error(&ga);
9040 ga_clear(&ga);
9041 }
9042}
9043
9044 void
9045assert_match_common(typval_T *argvars, assert_type_T atype)
9046{
9047 garray_T ga;
9048 char_u buf1[NUMBUFLEN];
9049 char_u buf2[NUMBUFLEN];
9050 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9051 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9052
9053 if (pat == NULL || text == NULL)
9054 EMSG(_(e_invarg));
9055 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9056 {
9057 prepare_assert_error(&ga);
9058 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9059 atype);
9060 assert_error(&ga);
9061 ga_clear(&ga);
9062 }
9063}
9064
Bram Moolenaar61c04492016-07-23 15:35:35 +02009065 void
9066assert_inrange(typval_T *argvars)
9067{
9068 garray_T ga;
9069 int error = FALSE;
9070 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9071 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9072 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9073 char_u *tofree;
9074 char msg[200];
9075 char_u numbuf[NUMBUFLEN];
9076
9077 if (error)
9078 return;
9079 if (actual < lower || actual > upper)
9080 {
9081 prepare_assert_error(&ga);
9082 if (argvars[3].v_type != VAR_UNKNOWN)
9083 {
9084 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9085 vim_free(tofree);
9086 }
9087 else
9088 {
9089 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9090 (long)lower, (long)upper, (long)actual);
9091 ga_concat(&ga, (char_u *)msg);
9092 }
9093 assert_error(&ga);
9094 ga_clear(&ga);
9095 }
9096}
9097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098/*
9099 * Common for assert_true() and assert_false().
9100 */
9101 void
9102assert_bool(typval_T *argvars, int isTrue)
9103{
9104 int error = FALSE;
9105 garray_T ga;
9106
9107 if (argvars[0].v_type == VAR_SPECIAL
9108 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9109 return;
9110 if (argvars[0].v_type != VAR_NUMBER
9111 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9112 || error)
9113 {
9114 prepare_assert_error(&ga);
9115 fill_assert_error(&ga, &argvars[1],
9116 (char_u *)(isTrue ? "True" : "False"),
9117 NULL, &argvars[0], ASSERT_OTHER);
9118 assert_error(&ga);
9119 ga_clear(&ga);
9120 }
9121}
9122
9123 void
9124assert_exception(typval_T *argvars)
9125{
9126 garray_T ga;
9127 char_u *error = get_tv_string_chk(&argvars[0]);
9128
9129 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9130 {
9131 prepare_assert_error(&ga);
9132 ga_concat(&ga, (char_u *)"v:exception is not set");
9133 assert_error(&ga);
9134 ga_clear(&ga);
9135 }
9136 else if (error != NULL
9137 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9138 {
9139 prepare_assert_error(&ga);
9140 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9141 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9142 assert_error(&ga);
9143 ga_clear(&ga);
9144 }
9145}
9146
9147 void
9148assert_fails(typval_T *argvars)
9149{
9150 char_u *cmd = get_tv_string_chk(&argvars[0]);
9151 garray_T ga;
9152
9153 called_emsg = FALSE;
9154 suppress_errthrow = TRUE;
9155 emsg_silent = TRUE;
9156 do_cmdline_cmd(cmd);
9157 if (!called_emsg)
9158 {
9159 prepare_assert_error(&ga);
9160 ga_concat(&ga, (char_u *)"command did not fail: ");
9161 ga_concat(&ga, cmd);
9162 assert_error(&ga);
9163 ga_clear(&ga);
9164 }
9165 else if (argvars[1].v_type != VAR_UNKNOWN)
9166 {
9167 char_u buf[NUMBUFLEN];
9168 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9169
9170 if (error == NULL
9171 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9172 {
9173 prepare_assert_error(&ga);
9174 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9175 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9176 assert_error(&ga);
9177 ga_clear(&ga);
9178 }
9179 }
9180
9181 called_emsg = FALSE;
9182 suppress_errthrow = FALSE;
9183 emsg_silent = FALSE;
9184 emsg_on_display = FALSE;
9185 set_vim_var_string(VV_ERRMSG, NULL, 0);
9186}
9187
9188/*
9189 * Append "str" to "gap", escaping unprintable characters.
9190 * Changes NL to \n, CR to \r, etc.
9191 */
9192 static void
9193ga_concat_esc(garray_T *gap, char_u *str)
9194{
9195 char_u *p;
9196 char_u buf[NUMBUFLEN];
9197
9198 if (str == NULL)
9199 {
9200 ga_concat(gap, (char_u *)"NULL");
9201 return;
9202 }
9203
9204 for (p = str; *p != NUL; ++p)
9205 switch (*p)
9206 {
9207 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9208 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9209 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9210 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9211 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9212 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9213 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9214 default:
9215 if (*p < ' ')
9216 {
9217 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9218 ga_concat(gap, buf);
9219 }
9220 else
9221 ga_append(gap, *p);
9222 break;
9223 }
9224}
9225
9226/*
9227 * Fill "gap" with information about an assert error.
9228 */
9229 void
9230fill_assert_error(
9231 garray_T *gap,
9232 typval_T *opt_msg_tv,
9233 char_u *exp_str,
9234 typval_T *exp_tv,
9235 typval_T *got_tv,
9236 assert_type_T atype)
9237{
9238 char_u numbuf[NUMBUFLEN];
9239 char_u *tofree;
9240
9241 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9242 {
9243 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9244 vim_free(tofree);
9245 }
9246 else
9247 {
9248 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9249 ga_concat(gap, (char_u *)"Pattern ");
9250 else
9251 ga_concat(gap, (char_u *)"Expected ");
9252 if (exp_str == NULL)
9253 {
9254 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9255 vim_free(tofree);
9256 }
9257 else
9258 ga_concat_esc(gap, exp_str);
9259 if (atype == ASSERT_MATCH)
9260 ga_concat(gap, (char_u *)" does not match ");
9261 else if (atype == ASSERT_NOTMATCH)
9262 ga_concat(gap, (char_u *)" does match ");
9263 else if (atype == ASSERT_NOTEQUAL)
9264 ga_concat(gap, (char_u *)" differs from ");
9265 else
9266 ga_concat(gap, (char_u *)" but got ");
9267 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9268 vim_free(tofree);
9269 }
9270}
9271
Bram Moolenaar53744302015-07-17 17:38:22 +02009272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#endif /* FEAT_EVAL */
9274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009276#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277
9278#ifdef WIN3264
9279/*
9280 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9281 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009282static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9283static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9284static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285
9286/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009287 * Get the short path (8.3) for the filename in "fnamep".
9288 * Only works for a valid file name.
9289 * When the path gets longer "fnamep" is changed and the allocated buffer
9290 * is put in "bufp".
9291 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9292 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 */
9294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009295get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009297 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 char_u *newbuf;
9299
9300 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009301 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (l > len - 1)
9303 {
9304 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009305 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 newbuf = vim_strnsave(*fnamep, l);
9307 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309
9310 vim_free(*bufp);
9311 *fnamep = *bufp = newbuf;
9312
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009313 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009314 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 }
9316
9317 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009318 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319}
9320
9321/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009322 * Get the short path (8.3) for the filename in "fname". The converted
9323 * path is returned in "bufp".
9324 *
9325 * Some of the directories specified in "fname" may not exist. This function
9326 * will shorten the existing directories at the beginning of the path and then
9327 * append the remaining non-existing path.
9328 *
9329 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009330 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009331 * bufp - Pointer to an allocated buffer for the filename.
9332 * fnamelen - Length of the filename pointed to by fname
9333 *
9334 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 */
9336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009337shortpath_for_invalid_fname(
9338 char_u **fname,
9339 char_u **bufp,
9340 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009342 char_u *short_fname, *save_fname, *pbuf_unused;
9343 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009345 int old_len, len;
9346 int new_len, sfx_len;
9347 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
9349 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009350 old_len = *fnamelen;
9351 save_fname = vim_strnsave(*fname, old_len);
9352 pbuf_unused = NULL;
9353 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009355 endp = save_fname + old_len - 1; /* Find the end of the copy */
9356 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009358 /*
9359 * Try shortening the supplied path till it succeeds by removing one
9360 * directory at a time from the tail of the path.
9361 */
9362 len = 0;
9363 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009365 /* go back one path-separator */
9366 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9367 --endp;
9368 if (endp <= save_fname)
9369 break; /* processed the complete path */
9370
9371 /*
9372 * Replace the path separator with a NUL and try to shorten the
9373 * resulting path.
9374 */
9375 ch = *endp;
9376 *endp = 0;
9377 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009378 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009379 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9380 {
9381 retval = FAIL;
9382 goto theend;
9383 }
9384 *endp = ch; /* preserve the string */
9385
9386 if (len > 0)
9387 break; /* successfully shortened the path */
9388
9389 /* failed to shorten the path. Skip the path separator */
9390 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 }
9392
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009393 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009395 /*
9396 * Succeeded in shortening the path. Now concatenate the shortened
9397 * path with the remaining path at the tail.
9398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009400 /* Compute the length of the new path. */
9401 sfx_len = (int)(save_endp - endp) + 1;
9402 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009404 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009406 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009408 /* There is not enough space in the currently allocated string,
9409 * copy it to a buffer big enough. */
9410 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009412 {
9413 retval = FAIL;
9414 goto theend;
9415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 }
9417 else
9418 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009419 /* Transfer short_fname to the main buffer (it's big enough),
9420 * unless get_short_pathname() did its work in-place. */
9421 *fname = *bufp = save_fname;
9422 if (short_fname != save_fname)
9423 vim_strncpy(save_fname, short_fname, len);
9424 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009426
9427 /* concat the not-shortened part of the path */
9428 vim_strncpy(*fname + len, endp, sfx_len);
9429 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009431
9432theend:
9433 vim_free(pbuf_unused);
9434 vim_free(save_fname);
9435
9436 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
9439/*
9440 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009441 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 */
9443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009444shortpath_for_partial(
9445 char_u **fnamep,
9446 char_u **bufp,
9447 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
9449 int sepcount, len, tflen;
9450 char_u *p;
9451 char_u *pbuf, *tfname;
9452 int hasTilde;
9453
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009454 /* Count up the path separators from the RHS.. so we know which part
9455 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009457 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (vim_ispathsep(*p))
9459 ++sepcount;
9460
9461 /* Need full path first (use expand_env() to remove a "~/") */
9462 hasTilde = (**fnamep == '~');
9463 if (hasTilde)
9464 pbuf = tfname = expand_env_save(*fnamep);
9465 else
9466 pbuf = tfname = FullName_save(*fnamep, FALSE);
9467
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009468 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009470 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9471 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
9473 if (len == 0)
9474 {
9475 /* Don't have a valid filename, so shorten the rest of the
9476 * path if we can. This CAN give us invalid 8.3 filenames, but
9477 * there's not a lot of point in guessing what it might be.
9478 */
9479 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009480 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 }
9483
9484 /* Count the paths backward to find the beginning of the desired string. */
9485 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009486 {
9487#ifdef FEAT_MBYTE
9488 if (has_mbyte)
9489 p -= mb_head_off(tfname, p);
9490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 if (vim_ispathsep(*p))
9492 {
9493 if (sepcount == 0 || (hasTilde && sepcount == 1))
9494 break;
9495 else
9496 sepcount --;
9497 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (hasTilde)
9500 {
9501 --p;
9502 if (p >= tfname)
9503 *p = '~';
9504 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 }
9507 else
9508 ++p;
9509
9510 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9511 vim_free(*bufp);
9512 *fnamelen = (int)STRLEN(p);
9513 *bufp = pbuf;
9514 *fnamep = p;
9515
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009516 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517}
9518#endif /* WIN3264 */
9519
9520/*
9521 * Adjust a filename, according to a string of modifiers.
9522 * *fnamep must be NUL terminated when called. When returning, the length is
9523 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009524 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 * When there is an error, *fnamep is set to NULL.
9526 */
9527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528modify_fname(
9529 char_u *src, /* string with modifiers */
9530 int *usedlen, /* characters after src that are used */
9531 char_u **fnamep, /* file name so far */
9532 char_u **bufp, /* buffer for allocated file name or NULL */
9533 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 int valid = 0;
9536 char_u *tail;
9537 char_u *s, *p, *pbuf;
9538 char_u dirname[MAXPATHL];
9539 int c;
9540 int has_fullname = 0;
9541#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009542 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 int has_shortname = 0;
9544#endif
9545
9546repeat:
9547 /* ":p" - full path/file_name */
9548 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9549 {
9550 has_fullname = 1;
9551
9552 valid |= VALID_PATH;
9553 *usedlen += 2;
9554
9555 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9556 if ((*fnamep)[0] == '~'
9557#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9558 && ((*fnamep)[1] == '/'
9559# ifdef BACKSLASH_IN_FILENAME
9560 || (*fnamep)[1] == '\\'
9561# endif
9562 || (*fnamep)[1] == NUL)
9563
9564#endif
9565 )
9566 {
9567 *fnamep = expand_env_save(*fnamep);
9568 vim_free(*bufp); /* free any allocated file name */
9569 *bufp = *fnamep;
9570 if (*fnamep == NULL)
9571 return -1;
9572 }
9573
9574 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009575 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 {
9577 if (vim_ispathsep(*p)
9578 && p[1] == '.'
9579 && (p[2] == NUL
9580 || vim_ispathsep(p[2])
9581 || (p[2] == '.'
9582 && (p[3] == NUL || vim_ispathsep(p[3])))))
9583 break;
9584 }
9585
9586 /* FullName_save() is slow, don't use it when not needed. */
9587 if (*p != NUL || !vim_isAbsName(*fnamep))
9588 {
9589 *fnamep = FullName_save(*fnamep, *p != NUL);
9590 vim_free(*bufp); /* free any allocated file name */
9591 *bufp = *fnamep;
9592 if (*fnamep == NULL)
9593 return -1;
9594 }
9595
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009596#ifdef WIN3264
9597# if _WIN32_WINNT >= 0x0500
9598 if (vim_strchr(*fnamep, '~') != NULL)
9599 {
9600 /* Expand 8.3 filename to full path. Needed to make sure the same
9601 * file does not have two different names.
9602 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9603 p = alloc(_MAX_PATH + 1);
9604 if (p != NULL)
9605 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009606 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009607 {
9608 vim_free(*bufp);
9609 *bufp = *fnamep = p;
9610 }
9611 else
9612 vim_free(p);
9613 }
9614 }
9615# endif
9616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 /* Append a path separator to a directory. */
9618 if (mch_isdir(*fnamep))
9619 {
9620 /* Make room for one or two extra characters. */
9621 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9622 vim_free(*bufp); /* free any allocated file name */
9623 *bufp = *fnamep;
9624 if (*fnamep == NULL)
9625 return -1;
9626 add_pathsep(*fnamep);
9627 }
9628 }
9629
9630 /* ":." - path relative to the current directory */
9631 /* ":~" - path relative to the home directory */
9632 /* ":8" - shortname path - postponed till after */
9633 while (src[*usedlen] == ':'
9634 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9635 {
9636 *usedlen += 2;
9637 if (c == '8')
9638 {
9639#ifdef WIN3264
9640 has_shortname = 1; /* Postpone this. */
9641#endif
9642 continue;
9643 }
9644 pbuf = NULL;
9645 /* Need full path first (use expand_env() to remove a "~/") */
9646 if (!has_fullname)
9647 {
9648 if (c == '.' && **fnamep == '~')
9649 p = pbuf = expand_env_save(*fnamep);
9650 else
9651 p = pbuf = FullName_save(*fnamep, FALSE);
9652 }
9653 else
9654 p = *fnamep;
9655
9656 has_fullname = 0;
9657
9658 if (p != NULL)
9659 {
9660 if (c == '.')
9661 {
9662 mch_dirname(dirname, MAXPATHL);
9663 s = shorten_fname(p, dirname);
9664 if (s != NULL)
9665 {
9666 *fnamep = s;
9667 if (pbuf != NULL)
9668 {
9669 vim_free(*bufp); /* free any allocated file name */
9670 *bufp = pbuf;
9671 pbuf = NULL;
9672 }
9673 }
9674 }
9675 else
9676 {
9677 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9678 /* Only replace it when it starts with '~' */
9679 if (*dirname == '~')
9680 {
9681 s = vim_strsave(dirname);
9682 if (s != NULL)
9683 {
9684 *fnamep = s;
9685 vim_free(*bufp);
9686 *bufp = s;
9687 }
9688 }
9689 }
9690 vim_free(pbuf);
9691 }
9692 }
9693
9694 tail = gettail(*fnamep);
9695 *fnamelen = (int)STRLEN(*fnamep);
9696
9697 /* ":h" - head, remove "/file_name", can be repeated */
9698 /* Don't remove the first "/" or "c:\" */
9699 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9700 {
9701 valid |= VALID_HEAD;
9702 *usedlen += 2;
9703 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009704 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009705 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 *fnamelen = (int)(tail - *fnamep);
9707#ifdef VMS
9708 if (*fnamelen > 0)
9709 *fnamelen += 1; /* the path separator is part of the path */
9710#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009711 if (*fnamelen == 0)
9712 {
9713 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9714 p = vim_strsave((char_u *)".");
9715 if (p == NULL)
9716 return -1;
9717 vim_free(*bufp);
9718 *bufp = *fnamep = tail = p;
9719 *fnamelen = 1;
9720 }
9721 else
9722 {
9723 while (tail > s && !after_pathsep(s, tail))
9724 mb_ptr_back(*fnamep, tail);
9725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 }
9727
9728 /* ":8" - shortname */
9729 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9730 {
9731 *usedlen += 2;
9732#ifdef WIN3264
9733 has_shortname = 1;
9734#endif
9735 }
9736
9737#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009738 /*
9739 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 */
9741 if (has_shortname)
9742 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009743 /* Copy the string if it is shortened by :h and when it wasn't copied
9744 * yet, because we are going to change it in place. Avoids changing
9745 * the buffer name for "%:8". */
9746 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 {
9748 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009749 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 return -1;
9751 vim_free(*bufp);
9752 *bufp = *fnamep = p;
9753 }
9754
9755 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009756 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (!has_fullname && !vim_isAbsName(*fnamep))
9758 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009759 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 return -1;
9761 }
9762 else
9763 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009764 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
Bram Moolenaardc935552011-08-17 15:23:23 +02009766 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009768 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 return -1;
9770
9771 if (l == 0)
9772 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009773 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009775 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 return -1;
9777 }
9778 *fnamelen = l;
9779 }
9780 }
9781#endif /* WIN3264 */
9782
9783 /* ":t" - tail, just the basename */
9784 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9785 {
9786 *usedlen += 2;
9787 *fnamelen -= (int)(tail - *fnamep);
9788 *fnamep = tail;
9789 }
9790
9791 /* ":e" - extension, can be repeated */
9792 /* ":r" - root, without extension, can be repeated */
9793 while (src[*usedlen] == ':'
9794 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9795 {
9796 /* find a '.' in the tail:
9797 * - for second :e: before the current fname
9798 * - otherwise: The last '.'
9799 */
9800 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9801 s = *fnamep - 2;
9802 else
9803 s = *fnamep + *fnamelen - 1;
9804 for ( ; s > tail; --s)
9805 if (s[0] == '.')
9806 break;
9807 if (src[*usedlen + 1] == 'e') /* :e */
9808 {
9809 if (s > tail)
9810 {
9811 *fnamelen += (int)(*fnamep - (s + 1));
9812 *fnamep = s + 1;
9813#ifdef VMS
9814 /* cut version from the extension */
9815 s = *fnamep + *fnamelen - 1;
9816 for ( ; s > *fnamep; --s)
9817 if (s[0] == ';')
9818 break;
9819 if (s > *fnamep)
9820 *fnamelen = s - *fnamep;
9821#endif
9822 }
9823 else if (*fnamep <= tail)
9824 *fnamelen = 0;
9825 }
9826 else /* :r */
9827 {
9828 if (s > tail) /* remove one extension */
9829 *fnamelen = (int)(s - *fnamep);
9830 }
9831 *usedlen += 2;
9832 }
9833
9834 /* ":s?pat?foo?" - substitute */
9835 /* ":gs?pat?foo?" - global substitute */
9836 if (src[*usedlen] == ':'
9837 && (src[*usedlen + 1] == 's'
9838 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9839 {
9840 char_u *str;
9841 char_u *pat;
9842 char_u *sub;
9843 int sep;
9844 char_u *flags;
9845 int didit = FALSE;
9846
9847 flags = (char_u *)"";
9848 s = src + *usedlen + 2;
9849 if (src[*usedlen + 1] == 'g')
9850 {
9851 flags = (char_u *)"g";
9852 ++s;
9853 }
9854
9855 sep = *s++;
9856 if (sep)
9857 {
9858 /* find end of pattern */
9859 p = vim_strchr(s, sep);
9860 if (p != NULL)
9861 {
9862 pat = vim_strnsave(s, (int)(p - s));
9863 if (pat != NULL)
9864 {
9865 s = p + 1;
9866 /* find end of substitution */
9867 p = vim_strchr(s, sep);
9868 if (p != NULL)
9869 {
9870 sub = vim_strnsave(s, (int)(p - s));
9871 str = vim_strnsave(*fnamep, *fnamelen);
9872 if (sub != NULL && str != NULL)
9873 {
9874 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009875 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 if (s != NULL)
9877 {
9878 *fnamep = s;
9879 *fnamelen = (int)STRLEN(s);
9880 vim_free(*bufp);
9881 *bufp = s;
9882 didit = TRUE;
9883 }
9884 }
9885 vim_free(sub);
9886 vim_free(str);
9887 }
9888 vim_free(pat);
9889 }
9890 }
9891 /* after using ":s", repeat all the modifiers */
9892 if (didit)
9893 goto repeat;
9894 }
9895 }
9896
Bram Moolenaar26df0922014-02-23 23:39:13 +01009897 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9898 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009899 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009900 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009901 if (c != NUL)
9902 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009903 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009904 if (c != NUL)
9905 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009906 if (p == NULL)
9907 return -1;
9908 vim_free(*bufp);
9909 *bufp = *fnamep = p;
9910 *fnamelen = (int)STRLEN(p);
9911 *usedlen += 2;
9912 }
9913
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 return valid;
9915}
9916
9917/*
9918 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009919 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 * "flags" can be "g" to do a global substitute.
9921 * Returns an allocated string, NULL for error.
9922 */
9923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009924do_string_sub(
9925 char_u *str,
9926 char_u *pat,
9927 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009928 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009929 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931 int sublen;
9932 regmatch_T regmatch;
9933 int i;
9934 int do_all;
9935 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009936 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 garray_T ga;
9938 char_u *ret;
9939 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009940 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
9942 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9943 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009944 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
9946 ga_init2(&ga, 1, 200);
9947
9948 do_all = (flags[0] == 'g');
9949
9950 regmatch.rm_ic = p_ic;
9951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9952 if (regmatch.regprog != NULL)
9953 {
9954 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009955 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9957 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009958 /* Skip empty match except for first match. */
9959 if (regmatch.startp[0] == regmatch.endp[0])
9960 {
9961 if (zero_width == regmatch.startp[0])
9962 {
9963 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009964 i = MB_PTR2LEN(tail);
9965 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9966 (size_t)i);
9967 ga.ga_len += i;
9968 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009969 continue;
9970 }
9971 zero_width = regmatch.startp[0];
9972 }
9973
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 /*
9975 * Get some space for a temporary buffer to do the substitution
9976 * into. It will contain:
9977 * - The text up to where the match is.
9978 * - The substituted text.
9979 * - The text after the match.
9980 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009981 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009982 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9984 {
9985 ga_clear(&ga);
9986 break;
9987 }
9988
9989 /* copy the text up to where the match is */
9990 i = (int)(regmatch.startp[0] - tail);
9991 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9992 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009993 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 + ga.ga_len + i, TRUE, TRUE, FALSE);
9995 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009996 tail = regmatch.endp[0];
9997 if (*tail == NUL)
9998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 if (!do_all)
10000 break;
10001 }
10002
10003 if (ga.ga_data != NULL)
10004 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10005
Bram Moolenaar473de612013-06-08 18:19:48 +020010006 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 }
10008
10009 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10010 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010011 if (p_cpo == empty_option)
10012 p_cpo = save_cpo;
10013 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010014 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010015 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
10017 return ret;
10018}
10019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020 static int
10021filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10022{
10023 typval_T rettv;
10024 typval_T argv[3];
10025 char_u buf[NUMBUFLEN];
10026 char_u *s;
10027 int retval = FAIL;
10028 int dummy;
10029
10030 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10031 argv[0] = vimvars[VV_KEY].vv_tv;
10032 argv[1] = vimvars[VV_VAL].vv_tv;
10033 if (expr->v_type == VAR_FUNC)
10034 {
10035 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010036 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10037 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 goto theend;
10039 }
10040 else if (expr->v_type == VAR_PARTIAL)
10041 {
10042 partial_T *partial = expr->vval.v_partial;
10043
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010044 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010045 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10046 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010047 goto theend;
10048 }
10049 else
10050 {
10051 s = get_tv_string_buf_chk(expr, buf);
10052 if (s == NULL)
10053 goto theend;
10054 s = skipwhite(s);
10055 if (eval1(&s, &rettv, TRUE) == FAIL)
10056 goto theend;
10057 if (*s != NUL) /* check for trailing chars after expr */
10058 {
10059 EMSG2(_(e_invexpr2), s);
10060 goto theend;
10061 }
10062 }
10063 if (map)
10064 {
10065 /* map(): replace the list item value */
10066 clear_tv(tv);
10067 rettv.v_lock = 0;
10068 *tv = rettv;
10069 }
10070 else
10071 {
10072 int error = FALSE;
10073
10074 /* filter(): when expr is zero remove the item */
10075 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10076 clear_tv(&rettv);
10077 /* On type error, nothing has been removed; return FAIL to stop the
10078 * loop. The error message was given by get_tv_number_chk(). */
10079 if (error)
10080 goto theend;
10081 }
10082 retval = OK;
10083theend:
10084 clear_tv(&vimvars[VV_VAL].vv_tv);
10085 return retval;
10086}
10087
10088
10089/*
10090 * Implementation of map() and filter().
10091 */
10092 void
10093filter_map(typval_T *argvars, typval_T *rettv, int map)
10094{
10095 typval_T *expr;
10096 listitem_T *li, *nli;
10097 list_T *l = NULL;
10098 dictitem_T *di;
10099 hashtab_T *ht;
10100 hashitem_T *hi;
10101 dict_T *d = NULL;
10102 typval_T save_val;
10103 typval_T save_key;
10104 int rem;
10105 int todo;
10106 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10107 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10108 : N_("filter() argument"));
10109 int save_did_emsg;
10110 int idx = 0;
10111
10112 if (argvars[0].v_type == VAR_LIST)
10113 {
10114 if ((l = argvars[0].vval.v_list) == NULL
10115 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10116 return;
10117 }
10118 else if (argvars[0].v_type == VAR_DICT)
10119 {
10120 if ((d = argvars[0].vval.v_dict) == NULL
10121 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10122 return;
10123 }
10124 else
10125 {
10126 EMSG2(_(e_listdictarg), ermsg);
10127 return;
10128 }
10129
10130 expr = &argvars[1];
10131 /* On type errors, the preceding call has already displayed an error
10132 * message. Avoid a misleading error message for an empty string that
10133 * was not passed as argument. */
10134 if (expr->v_type != VAR_UNKNOWN)
10135 {
10136 prepare_vimvar(VV_VAL, &save_val);
10137
10138 /* We reset "did_emsg" to be able to detect whether an error
10139 * occurred during evaluation of the expression. */
10140 save_did_emsg = did_emsg;
10141 did_emsg = FALSE;
10142
10143 prepare_vimvar(VV_KEY, &save_key);
10144 if (argvars[0].v_type == VAR_DICT)
10145 {
10146 vimvars[VV_KEY].vv_type = VAR_STRING;
10147
10148 ht = &d->dv_hashtab;
10149 hash_lock(ht);
10150 todo = (int)ht->ht_used;
10151 for (hi = ht->ht_array; todo > 0; ++hi)
10152 {
10153 if (!HASHITEM_EMPTY(hi))
10154 {
10155 int r;
10156
10157 --todo;
10158 di = HI2DI(hi);
10159 if (map &&
10160 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10161 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10162 break;
10163 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10164 r = filter_map_one(&di->di_tv, expr, map, &rem);
10165 clear_tv(&vimvars[VV_KEY].vv_tv);
10166 if (r == FAIL || did_emsg)
10167 break;
10168 if (!map && rem)
10169 {
10170 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10171 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10172 break;
10173 dictitem_remove(d, di);
10174 }
10175 }
10176 }
10177 hash_unlock(ht);
10178 }
10179 else
10180 {
10181 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10182
10183 for (li = l->lv_first; li != NULL; li = nli)
10184 {
10185 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10186 break;
10187 nli = li->li_next;
10188 vimvars[VV_KEY].vv_nr = idx;
10189 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10190 || did_emsg)
10191 break;
10192 if (!map && rem)
10193 listitem_remove(l, li);
10194 ++idx;
10195 }
10196 }
10197
10198 restore_vimvar(VV_KEY, &save_key);
10199 restore_vimvar(VV_VAL, &save_val);
10200
10201 did_emsg |= save_did_emsg;
10202 }
10203
10204 copy_tv(&argvars[0], rettv);
10205}
10206
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */