blob: b8e606443646d4c36070054fa2bcc78f29e89dec [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram 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 Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static 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 +0200238static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static void delete_var(hashtab_T *ht, hashitem_T *hi);
241static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
242static 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 +0100243static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245/* for VIM_VERSION_ defines */
246#include "version.h"
247
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100248
249#if defined(EBCDIC) || defined(PROTO)
250/*
251 * Compare struct fst by function name.
252 */
253 static int
254compare_func_name(const void *s1, const void *s2)
255{
256 struct fst *p1 = (struct fst *)s1;
257 struct fst *p2 = (struct fst *)s2;
258
259 return STRCMP(p1->f_name, p2->f_name);
260}
261
262/*
263 * Sort the function table by function name.
264 * The sorting of the table above is ASCII dependant.
265 * On machines using EBCDIC we have to sort it.
266 */
267 static void
268sortFunctions(void)
269{
270 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
271
272 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
273}
274#endif
275
276
Bram Moolenaar33570922005-01-25 22:26:29 +0000277/*
278 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000279 */
280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100281eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000282{
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 int i;
284 struct vimvar *p;
285
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200286 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
287 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200288 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000289 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200290 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
292 for (i = 0; i < VV_LEN; ++i)
293 {
294 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200295 if (STRLEN(p->vv_name) > 16)
296 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100297 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 getout(1);
299 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 STRCPY(p->vv_di.di_key, p->vv_name);
301 if (p->vv_flags & VV_RO)
302 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
303 else if (p->vv_flags & VV_RO_SBX)
304 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
305 else
306 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000307
308 /* add to v: scope dict, unless the value is not always available */
309 if (p->vv_type != VAR_UNKNOWN)
310 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000312 /* add to compat scope dict */
313 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100315 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
316
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000317 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100318 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200319 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100320 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100321
322 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
323 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
324 set_vim_var_nr(VV_NONE, VVAL_NONE);
325 set_vim_var_nr(VV_NULL, VVAL_NULL);
326
Bram Moolenaarf562e722016-07-19 17:25:25 +0200327 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
328 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
329 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
330 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
331 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
332 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
333 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
334 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
335 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
336 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
337
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200338 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200339
340#ifdef EBCDIC
341 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100342 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200343 */
344 sortFunctions();
345#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000346}
347
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000348#if defined(EXITFREE) || defined(PROTO)
349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100350eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000351{
352 int i;
353 struct vimvar *p;
354
355 for (i = 0; i < VV_LEN; ++i)
356 {
357 p = &vimvars[i];
358 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000359 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000360 vim_free(p->vv_str);
361 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 }
363 else if (p->vv_di.di_tv.v_type == VAR_LIST)
364 {
365 list_unref(p->vv_list);
366 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000367 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000368 }
369 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000370 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000371 hash_clear(&compat_hashtab);
372
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100374# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200375 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377
378 /* global variables */
379 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000380
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000381 /* autoloaded script names */
382 ga_clear_strings(&ga_loaded);
383
Bram Moolenaarcca74132013-09-25 21:00:28 +0200384 /* Script-local variables. First clear all the variables and in a second
385 * loop free the scriptvar_T, because a variable in one script might hold
386 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200387 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200388 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 ga_clear(&ga_scripts);
392
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000393 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200394 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000395
396 /* functions */
397 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000398}
399#endif
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 * Set an internal variable to a string value. Creates the variable if it does
404 * not already exist.
405 */
406 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100407set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000409 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000410 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411
412 val = vim_strsave(value);
413 if (val != NULL)
414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000415 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000416 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000419 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 }
422}
423
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000424static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200425#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000426static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427static char_u *redir_endp = NULL;
428static char_u *redir_varname = NULL;
429
430/*
431 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100432 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433 * Returns OK if successfully completed the setup. FAIL otherwise.
434 */
435 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100436var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000437{
438 int save_emsg;
439 int err;
440 typval_T tv;
441
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000442 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000443 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000444 {
445 EMSG(_(e_invarg));
446 return FAIL;
447 }
448
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000449 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 redir_varname = vim_strsave(name);
451 if (redir_varname == NULL)
452 return FAIL;
453
454 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
455 if (redir_lval == NULL)
456 {
457 var_redir_stop();
458 return FAIL;
459 }
460
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000461 /* The output is stored in growarray "redir_ga" until redirection ends. */
462 ga_init2(&redir_ga, (int)sizeof(char), 500);
463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000464 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100465 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000466 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
468 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200469 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 if (redir_endp != NULL && *redir_endp != NUL)
471 /* Trailing characters are present after the variable name */
472 EMSG(_(e_trailing));
473 else
474 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000475 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 var_redir_stop();
477 return FAIL;
478 }
479
480 /* check if we can write to the variable: set it to or append an empty
481 * string */
482 save_emsg = did_emsg;
483 did_emsg = FALSE;
484 tv.v_type = VAR_STRING;
485 tv.vval.v_string = (char_u *)"";
486 if (append)
487 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
488 else
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200490 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000492 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 if (err)
494 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000495 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 var_redir_stop();
497 return FAIL;
498 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499
500 return OK;
501}
502
503/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000504 * Append "value[value_len]" to the variable set by var_redir_start().
505 * The actual appending is postponed until redirection ends, because the value
506 * appended may in fact be the string we write to, changing it may cause freed
507 * memory to be used:
508 * :redir => foo
509 * :let foo
510 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 */
512 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100513var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000515 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516
517 if (redir_lval == NULL)
518 return;
519
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000520 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 {
527 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000529 }
530 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000532}
533
534/*
535 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000536 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 */
538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000541 typval_T tv;
542
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200543 if (EVALCMD_BUSY)
544 {
545 redir_lval = NULL;
546 return;
547 }
548
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000549 if (redir_lval != NULL)
550 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000551 /* If there was no error: assign the text to the variable. */
552 if (redir_endp != NULL)
553 {
554 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
555 tv.v_type = VAR_STRING;
556 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200557 /* Call get_lval() again, if it's inside a Dict or List it may
558 * have changed. */
559 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100560 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200561 if (redir_endp != NULL && redir_lval->ll_name != NULL)
562 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
563 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000564 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000565
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 /* free the collected output */
567 vim_free(redir_ga.ga_data);
568 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570 vim_free(redir_lval);
571 redir_lval = NULL;
572 }
573 vim_free(redir_varname);
574 redir_varname = NULL;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577# if defined(FEAT_MBYTE) || defined(PROTO)
578 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100579eval_charconvert(
580 char_u *enc_from,
581 char_u *enc_to,
582 char_u *fname_from,
583 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int err = FALSE;
586
587 set_vim_var_string(VV_CC_FROM, enc_from, -1);
588 set_vim_var_string(VV_CC_TO, enc_to, -1);
589 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
590 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
591 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
592 err = TRUE;
593 set_vim_var_string(VV_CC_FROM, NULL, -1);
594 set_vim_var_string(VV_CC_TO, NULL, -1);
595 set_vim_var_string(VV_FNAME_IN, NULL, -1);
596 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
597
598 if (err)
599 return FAIL;
600 return OK;
601}
602# endif
603
604# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 int err = FALSE;
609
610 set_vim_var_string(VV_FNAME_IN, fname, -1);
611 set_vim_var_string(VV_CMDARG, args, -1);
612 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
613 err = TRUE;
614 set_vim_var_string(VV_FNAME_IN, NULL, -1);
615 set_vim_var_string(VV_CMDARG, NULL, -1);
616
617 if (err)
618 {
619 mch_remove(fname);
620 return FAIL;
621 }
622 return OK;
623}
624# endif
625
626# if defined(FEAT_DIFF) || defined(PROTO)
627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628eval_diff(
629 char_u *origfile,
630 char_u *newfile,
631 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 int err = FALSE;
634
635 set_vim_var_string(VV_FNAME_IN, origfile, -1);
636 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
637 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
638 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642}
643
644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100645eval_patch(
646 char_u *origfile,
647 char_u *difffile,
648 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 int err;
651
652 set_vim_var_string(VV_FNAME_IN, origfile, -1);
653 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
654 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
655 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
656 set_vim_var_string(VV_FNAME_IN, NULL, -1);
657 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
658 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
659}
660# endif
661
662/*
663 * Top level evaluation function, returning a boolean.
664 * Sets "error" to TRUE if there was an error.
665 * Return TRUE or FALSE.
666 */
667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100668eval_to_bool(
669 char_u *arg,
670 int *error,
671 char_u **nextcmd,
672 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar33570922005-01-25 22:26:29 +0000674 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677 if (skip)
678 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000679 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 else
682 {
683 *error = FALSE;
684 if (!skip)
685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 }
690 if (skip)
691 --emsg_skip;
692
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200693 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696/*
697 * Top level evaluation function, returning a string. If "skip" is TRUE,
698 * only parsing to "nextcmd" is done, without reporting errors. Return
699 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
700 */
701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100702eval_to_string_skip(
703 char_u *arg,
704 char_u **nextcmd,
705 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
Bram Moolenaar33570922005-01-25 22:26:29 +0000707 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 char_u *retval;
709
710 if (skip)
711 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 retval = NULL;
714 else
715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 retval = vim_strsave(get_tv_string(&tv));
717 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 if (skip)
720 --emsg_skip;
721
722 return retval;
723}
724
725/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000726 * Skip over an expression at "*pp".
727 * Return FAIL for an error, OK otherwise.
728 */
729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000731{
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000733
734 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000740 * When "convert" is TRUE convert a List into a sequence of lines and convert
741 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * Return pointer to allocated memory, or NULL for failure.
743 */
744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745eval_to_string(
746 char_u *arg,
747 char_u **nextcmd,
748 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000752 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000753#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000754 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000757 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 retval = NULL;
759 else
760 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000761 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000762 {
763 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000764 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200765 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200766 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200767 if (tv.vval.v_list->lv_len > 0)
768 ga_append(&ga, NL);
769 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000770 ga_append(&ga, NUL);
771 retval = (char_u *)ga.ga_data;
772 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000773#ifdef FEAT_FLOAT
774 else if (convert && tv.v_type == VAR_FLOAT)
775 {
776 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
777 retval = vim_strsave(numbuf);
778 }
779#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000780 else
781 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784
785 return retval;
786}
787
788/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000789 * Call eval_to_string() without using current local variables and using
790 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
792 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793eval_to_string_safe(
794 char_u *arg,
795 char_u **nextcmd,
796 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 char_u *retval;
799 void *save_funccalp;
800
801 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000802 if (use_sandbox)
803 ++sandbox;
804 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000805 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000806 if (use_sandbox)
807 --sandbox;
808 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 restore_funccal(save_funccalp);
810 return retval;
811}
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
814 * Top level evaluation function, returning a number.
815 * Evaluates "expr" silently.
816 * Returns -1 for an error.
817 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200818 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100819eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200822 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000823 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 ++emsg_off;
826
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000827 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 retval = -1;
829 else
830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000831 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 --emsg_off;
835
836 return retval;
837}
838
Bram Moolenaara40058a2005-07-11 22:42:07 +0000839/*
840 * Prepare v: variable "idx" to be used.
841 * Save the current typeval in "save_tv".
842 * When not used yet add the variable to the v: hashtable.
843 */
844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100845prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000846{
847 *save_tv = vimvars[idx].vv_tv;
848 if (vimvars[idx].vv_type == VAR_UNKNOWN)
849 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
850}
851
852/*
853 * Restore v: variable "idx" to typeval "save_tv".
854 * When no longer defined, remove the variable from the v: hashtable.
855 */
856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000858{
859 hashitem_T *hi;
860
Bram Moolenaara40058a2005-07-11 22:42:07 +0000861 vimvars[idx].vv_tv = *save_tv;
862 if (vimvars[idx].vv_type == VAR_UNKNOWN)
863 {
864 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
865 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100866 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000867 else
868 hash_remove(&vimvarht, hi);
869 }
870}
871
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000872#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000873/*
874 * Evaluate an expression to a list with suggestions.
875 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000876 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000877 */
878 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000880{
881 typval_T save_val;
882 typval_T rettv;
883 list_T *list = NULL;
884 char_u *p = skipwhite(expr);
885
886 /* Set "v:val" to the bad word. */
887 prepare_vimvar(VV_VAL, &save_val);
888 vimvars[VV_VAL].vv_type = VAR_STRING;
889 vimvars[VV_VAL].vv_str = badword;
890 if (p_verbose == 0)
891 ++emsg_off;
892
893 if (eval1(&p, &rettv, TRUE) == OK)
894 {
895 if (rettv.v_type != VAR_LIST)
896 clear_tv(&rettv);
897 else
898 list = rettv.vval.v_list;
899 }
900
901 if (p_verbose == 0)
902 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000903 restore_vimvar(VV_VAL, &save_val);
904
905 return list;
906}
907
908/*
909 * "list" is supposed to contain two items: a word and a number. Return the
910 * word in "pp" and the number as the return value.
911 * Return -1 if anything isn't right.
912 * Used to get the good word and score from the eval_spell_expr() result.
913 */
914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000916{
917 listitem_T *li;
918
919 li = list->lv_first;
920 if (li == NULL)
921 return -1;
922 *pp = get_tv_string(&li->li_tv);
923
924 li = li->li_next;
925 if (li == NULL)
926 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200927 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000928}
929#endif
930
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000931/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000932 * Top level evaluation function.
933 * Returns an allocated typval_T with the result.
934 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000935 */
936 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000938{
939 typval_T *tv;
940
941 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000942 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000943 {
944 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000945 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000946 }
947
948 return tv;
949}
950
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100953 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000954 * Uses argv[argc] for the function arguments. Only Number and String
955 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000956 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959call_vim_function(
960 char_u *func,
961 int argc,
962 char_u **argv,
963 int safe, /* use the sandbox */
964 int str_arg_only, /* all arguments are strings */
965 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200968 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 int len;
970 int i;
971 int doesrange;
972 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000973 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000975 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 for (i = 0; i < argc; i++)
980 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000981 /* Pass a NULL or empty argument as an empty string */
982 if (argv[i] == NULL || *argv[i] == NUL)
983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000984 argvars[i].v_type = VAR_STRING;
985 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000986 continue;
987 }
988
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200989 if (str_arg_only)
990 len = 0;
991 else
992 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100993 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (len != 0 && len == (int)STRLEN(argv[i]))
995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 argvars[i].v_type = VAR_NUMBER;
997 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
999 else
1000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 argvars[i].v_type = VAR_STRING;
1002 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 }
1005
1006 if (safe)
1007 {
1008 save_funccalp = save_funccal();
1009 ++sandbox;
1010 }
1011
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001012 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001013 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001015 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (safe)
1017 {
1018 --sandbox;
1019 restore_funccal(save_funccalp);
1020 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001021 vim_free(argvars);
1022
1023 if (ret == FAIL)
1024 clear_tv(rettv);
1025
1026 return ret;
1027}
1028
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001030 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001031 * Returns -1 when calling the function fails.
1032 * Uses argv[argc] for the function arguments.
1033 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001034 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001035call_func_retnr(
1036 char_u *func,
1037 int argc,
1038 char_u **argv,
1039 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001040{
1041 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001042 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043
1044 /* All arguments are passed as strings, no conversion to number. */
1045 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1046 return -1;
1047
1048 retval = get_tv_number_chk(&rettv, NULL);
1049 clear_tv(&rettv);
1050 return retval;
1051}
1052
1053#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1054 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1055
Bram Moolenaar4f688582007-07-24 12:34:30 +00001056# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001057/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001058 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001059 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001060 * Uses argv[argc] for the function arguments.
1061 */
1062 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063call_func_retstr(
1064 char_u *func,
1065 int argc,
1066 char_u **argv,
1067 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001068{
1069 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001070 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001072 /* All arguments are passed as strings, no conversion to number. */
1073 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001074 return NULL;
1075
1076 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001077 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 return retval;
1079}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001080# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001082/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001083 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001085 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 */
1087 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001088call_func_retlist(
1089 char_u *func,
1090 int argc,
1091 char_u **argv,
1092 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093{
1094 typval_T rettv;
1095
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001096 /* All arguments are passed as strings, no conversion to number. */
1097 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 return NULL;
1099
1100 if (rettv.v_type != VAR_LIST)
1101 {
1102 clear_tv(&rettv);
1103 return NULL;
1104 }
1105
1106 return rettv.vval.v_list;
1107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#endif
1109
Bram Moolenaar05159a02005-02-26 23:04:13 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#ifdef FEAT_FOLDING
1112/*
1113 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1114 * it in "*cp". Doesn't give error messages.
1115 */
1116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
Bram Moolenaar33570922005-01-25 22:26:29 +00001119 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001120 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001122 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1123 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001126 if (use_sandbox)
1127 ++sandbox;
1128 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 retval = 0;
1132 else
1133 {
1134 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (tv.v_type == VAR_NUMBER)
1136 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001137 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 retval = 0;
1139 else
1140 {
1141 /* If the result is a string, check if there is a non-digit before
1142 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (!VIM_ISDIGIT(*s) && *s != '-')
1145 *cp = *s++;
1146 retval = atol((char *)s);
1147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001148 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001151 if (use_sandbox)
1152 --sandbox;
1153 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001155 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156}
1157#endif
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 * ":let" list all variable values
1161 * ":let var1 var2" list variable values
1162 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001163 * ":let var += expr" assignment command.
1164 * ":let var -= expr" assignment command.
1165 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
1168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001169ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001173 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 int var_count = 0;
1176 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001177 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001178 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001179 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaardb552d602006-03-23 22:59:57 +00001181 argend = skip_var_list(arg, &var_count, &semicolon);
1182 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001183 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001184 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1185 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001186 expr = skipwhite(argend);
1187 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1188 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001190 /*
1191 * ":let" without "=": list variables
1192 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001193 if (*arg == '[')
1194 EMSG(_(e_invarg));
1195 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001197 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001201 list_glob_vars(&first);
1202 list_buf_vars(&first);
1203 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001204#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001205 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001206#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001207 list_script_vars(&first);
1208 list_func_vars(&first);
1209 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 eap->nextcmd = check_nextcmd(arg);
1212 }
1213 else
1214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001215 op[0] = '=';
1216 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001217 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001219 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1220 op[0] = *expr; /* +=, -= or .= */
1221 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001223 else
1224 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (eap->skip)
1227 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (eap->skip)
1230 {
1231 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 --emsg_skip;
1234 }
1235 else if (i != FAIL)
1236 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
1242}
1243
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001244/*
1245 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1246 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 * When "nextchars" is not NULL it points to a string with characters that
1248 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1249 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001250 * Returns OK or FAIL;
1251 */
1252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253ex_let_vars(
1254 char_u *arg_start,
1255 typval_T *tv,
1256 int copy, /* copy values from "tv", don't move */
1257 int semicolon, /* from skip_var_list() */
1258 int var_count, /* from skip_var_list() */
1259 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260{
1261 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001263 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 listitem_T *item;
1265 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266
1267 if (*arg != '[')
1268 {
1269 /*
1270 * ":let var = expr" or ":for var in list"
1271 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 return FAIL;
1274 return OK;
1275 }
1276
1277 /*
1278 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1279 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001280 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001281 {
1282 EMSG(_(e_listreq));
1283 return FAIL;
1284 }
1285
1286 i = list_len(l);
1287 if (semicolon == 0 && var_count < i)
1288 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001289 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 return FAIL;
1291 }
1292 if (var_count - semicolon > i)
1293 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001294 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 return FAIL;
1296 }
1297
1298 item = l->lv_first;
1299 while (*arg != ']')
1300 {
1301 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001303 item = item->li_next;
1304 if (arg == NULL)
1305 return FAIL;
1306
1307 arg = skipwhite(arg);
1308 if (*arg == ';')
1309 {
1310 /* Put the rest of the list (may be empty) in the var after ';'.
1311 * Create a new list for this. */
1312 l = list_alloc();
1313 if (l == NULL)
1314 return FAIL;
1315 while (item != NULL)
1316 {
1317 list_append_tv(l, &item->li_tv);
1318 item = item->li_next;
1319 }
1320
1321 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001322 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001323 ltv.vval.v_list = l;
1324 l->lv_refcount = 1;
1325
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001326 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1327 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 clear_tv(&ltv);
1329 if (arg == NULL)
1330 return FAIL;
1331 break;
1332 }
1333 else if (*arg != ',' && *arg != ']')
1334 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001335 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336 return FAIL;
1337 }
1338 }
1339
1340 return OK;
1341}
1342
1343/*
1344 * Skip over assignable variable "var" or list of variables "[var, var]".
1345 * Used for ":let varvar = expr" and ":for varvar in expr".
1346 * For "[var, var]" increment "*var_count" for each variable.
1347 * for "[var, var; var]" set "semicolon".
1348 * Return NULL for an error.
1349 */
1350 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001351skip_var_list(
1352 char_u *arg,
1353 int *var_count,
1354 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355{
1356 char_u *p, *s;
1357
1358 if (*arg == '[')
1359 {
1360 /* "[var, var]": find the matching ']'. */
1361 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001363 {
1364 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1365 s = skip_var_one(p);
1366 if (s == p)
1367 {
1368 EMSG2(_(e_invarg2), p);
1369 return NULL;
1370 }
1371 ++*var_count;
1372
1373 p = skipwhite(s);
1374 if (*p == ']')
1375 break;
1376 else if (*p == ';')
1377 {
1378 if (*semicolon == 1)
1379 {
1380 EMSG(_("Double ; in list of variables"));
1381 return NULL;
1382 }
1383 *semicolon = 1;
1384 }
1385 else if (*p != ',')
1386 {
1387 EMSG2(_(e_invarg2), p);
1388 return NULL;
1389 }
1390 }
1391 return p + 1;
1392 }
1393 else
1394 return skip_var_one(arg);
1395}
1396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001397/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001398 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001399 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001400 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001404 if (*arg == '@' && arg[1] != NUL)
1405 return arg + 2;
1406 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1407 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408}
1409
Bram Moolenaara7043832005-01-21 11:56:39 +00001410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 * List variables for hashtab "ht" with prefix "prefix".
1412 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415list_hashtable_vars(
1416 hashtab_T *ht,
1417 char_u *prefix,
1418 int empty,
1419 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 hashitem_T *hi;
1422 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001423 int todo;
1424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001425 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001426 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1427 {
1428 if (!HASHITEM_EMPTY(hi))
1429 {
1430 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 di = HI2DI(hi);
1432 if (empty || di->di_tv.v_type != VAR_STRING
1433 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001434 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001435 }
1436 }
1437}
1438
1439/*
1440 * List global variables.
1441 */
1442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001443list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001444{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001445 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001446}
1447
1448/*
1449 * List buffer variables.
1450 */
1451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001453{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001454 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001455 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001456}
1457
1458/*
1459 * List window variables.
1460 */
1461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001462list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001463{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001464 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001465 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001466}
1467
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001468#ifdef FEAT_WINDOWS
1469/*
1470 * List tab page variables.
1471 */
1472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001474{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001475 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477}
1478#endif
1479
Bram Moolenaara7043832005-01-21 11:56:39 +00001480/*
1481 * List Vim variables.
1482 */
1483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001486 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487}
1488
1489/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001490 * List script-local variables, if there is a script.
1491 */
1492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001494{
1495 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001496 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1497 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001498}
1499
1500/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 * List variables in "arg".
1502 */
1503 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001505{
1506 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001507 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001509 char_u *name_start;
1510 char_u *arg_subsc;
1511 char_u *tofree;
1512 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001513
1514 while (!ends_excmd(*arg) && !got_int)
1515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001516 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001518 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001519 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001520 {
1521 emsg_severe = TRUE;
1522 EMSG(_(e_trailing));
1523 break;
1524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001528 /* get_name_len() takes care of expanding curly braces */
1529 name_start = name = arg;
1530 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1531 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001533 /* This is mainly to keep test 49 working: when expanding
1534 * curly braces fails overrule the exception error message. */
1535 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001537 emsg_severe = TRUE;
1538 EMSG2(_(e_invarg2), arg);
1539 break;
1540 }
1541 error = TRUE;
1542 }
1543 else
1544 {
1545 if (tofree != NULL)
1546 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001547 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 else
1550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001551 /* handle d.key, l[idx], f(expr) */
1552 arg_subsc = arg;
1553 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001561 case 'g': list_glob_vars(first); break;
1562 case 'b': list_buf_vars(first); break;
1563 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001564#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001565 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001566#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001567 case 'v': list_vim_vars(first); break;
1568 case 's': list_script_vars(first); break;
1569 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001570 default:
1571 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001572 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 }
1574 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 {
1576 char_u numbuf[NUMBUFLEN];
1577 char_u *tf;
1578 int c;
1579 char_u *s;
1580
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001581 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001582 c = *arg;
1583 *arg = NUL;
1584 list_one_var_a((char_u *)"",
1585 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001586 tv.v_type,
1587 s == NULL ? (char_u *)"" : s,
1588 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001589 *arg = c;
1590 vim_free(tf);
1591 }
1592 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 }
1595 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596
1597 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599
1600 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 }
1602
1603 return arg;
1604}
1605
1606/*
1607 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1608 * Returns a pointer to the char just after the var name.
1609 * Returns NULL if there is an error.
1610 */
1611 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001612ex_let_one(
1613 char_u *arg, /* points to variable name */
1614 typval_T *tv, /* value to assign to variable */
1615 int copy, /* copy value from "tv" */
1616 char_u *endchars, /* valid chars after variable name or NULL */
1617 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618{
1619 int c1;
1620 char_u *name;
1621 char_u *p;
1622 char_u *arg_end = NULL;
1623 int len;
1624 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626
1627 /*
1628 * ":let $VAR = expr": Set environment variable.
1629 */
1630 if (*arg == '$')
1631 {
1632 /* Find the end of the name. */
1633 ++arg;
1634 name = arg;
1635 len = get_env_len(&arg);
1636 if (len == 0)
1637 EMSG2(_(e_invarg2), name - 1);
1638 else
1639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 if (op != NULL && (*op == '+' || *op == '-'))
1641 EMSG2(_(e_letwrong), op);
1642 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001645 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 {
1647 c1 = name[len];
1648 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649 p = get_tv_string_chk(tv);
1650 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001651 {
1652 int mustfree = FALSE;
1653 char_u *s = vim_getenv(name, &mustfree);
1654
1655 if (s != NULL)
1656 {
1657 p = tofree = concat_str(s, p);
1658 if (mustfree)
1659 vim_free(s);
1660 }
1661 }
1662 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001665 if (STRICMP(name, "HOME") == 0)
1666 init_homedir();
1667 else if (didset_vim && STRICMP(name, "VIM") == 0)
1668 didset_vim = FALSE;
1669 else if (didset_vimruntime
1670 && STRICMP(name, "VIMRUNTIME") == 0)
1671 didset_vimruntime = FALSE;
1672 arg_end = arg;
1673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 }
1677 }
1678 }
1679
1680 /*
1681 * ":let &option = expr": Set option value.
1682 * ":let &l:option = expr": Set local option value.
1683 * ":let &g:option = expr": Set global option value.
1684 */
1685 else if (*arg == '&')
1686 {
1687 /* Find the end of the name. */
1688 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 if (p == NULL || (endchars != NULL
1690 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 EMSG(_(e_letunexp));
1692 else
1693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001694 long n;
1695 int opt_type;
1696 long numval;
1697 char_u *stringval = NULL;
1698 char_u *s;
1699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 c1 = *p;
1701 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001703 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001704 s = get_tv_string_chk(tv); /* != NULL if number or string */
1705 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 {
1707 opt_type = get_option_value(arg, &numval,
1708 &stringval, opt_flags);
1709 if ((opt_type == 1 && *op == '.')
1710 || (opt_type == 0 && *op != '.'))
1711 EMSG2(_(e_letwrong), op);
1712 else
1713 {
1714 if (opt_type == 1) /* number */
1715 {
1716 if (*op == '+')
1717 n = numval + n;
1718 else
1719 n = numval - n;
1720 }
1721 else if (opt_type == 0 && stringval != NULL) /* string */
1722 {
1723 s = concat_str(stringval, s);
1724 vim_free(stringval);
1725 stringval = s;
1726 }
1727 }
1728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 if (s != NULL)
1730 {
1731 set_option_value(arg, n, s, opt_flags);
1732 arg_end = p;
1733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 }
1737 }
1738
1739 /*
1740 * ":let @r = expr": Set register contents.
1741 */
1742 else if (*arg == '@')
1743 {
1744 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 if (op != NULL && (*op == '+' || *op == '-'))
1746 EMSG2(_(e_letwrong), op);
1747 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 EMSG(_(e_letunexp));
1750 else
1751 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 char_u *s;
1754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 p = get_tv_string_chk(tv);
1756 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001758 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 if (s != NULL)
1760 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 vim_free(s);
1763 }
1764 }
1765 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001768 arg_end = arg + 1;
1769 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 }
1772 }
1773
1774 /*
1775 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001776 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001778 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001782 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1786 EMSG(_(e_letunexp));
1787 else
1788 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 arg_end = p;
1791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 }
1795
1796 else
1797 EMSG2(_(e_invarg2), arg);
1798
1799 return arg_end;
1800}
1801
1802/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803 * Get an lval: variable, Dict item or List item that can be assigned a value
1804 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1805 * "name.key", "name.key[expr]" etc.
1806 * Indexing only works if "name" is an existing List or Dictionary.
1807 * "name" points to the start of the name.
1808 * If "rettv" is not NULL it points to the value to be assigned.
1809 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1810 * wrong; must end in space or cmd separator.
1811 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001812 * flags:
1813 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001814 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001815 * GLV_NO_AUTOLOAD: do not use script autoloading
1816 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822get_lval(
1823 char_u *name,
1824 typval_T *rettv,
1825 lval_T *lp,
1826 int unlet,
1827 int skip,
1828 int flags, /* GLV_ values */
1829 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001832 char_u *expr_start, *expr_end;
1833 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001834 dictitem_T *v;
1835 typval_T var1;
1836 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001839 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001840 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001842 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001844 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001846
1847 if (skip)
1848 {
1849 /* When skipping just find the end of the name. */
1850 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001851 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 }
1853
1854 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001855 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 if (expr_start != NULL)
1857 {
1858 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 && *p != '[' && *p != '.')
1861 {
1862 EMSG(_(e_trailing));
1863 return NULL;
1864 }
1865
1866 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1867 if (lp->ll_exp_name == NULL)
1868 {
1869 /* Report an invalid expression in braces, unless the
1870 * expression evaluation has been cancelled due to an
1871 * aborting error, an interrupt, or an exception. */
1872 if (!aborting() && !quiet)
1873 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001874 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 EMSG2(_(e_invarg2), name);
1876 return NULL;
1877 }
1878 }
1879 lp->ll_name = lp->ll_exp_name;
1880 }
1881 else
1882 lp->ll_name = name;
1883
1884 /* Without [idx] or .key we are done. */
1885 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1886 return p;
1887
1888 cc = *p;
1889 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001890 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 if (v == NULL && !quiet)
1892 EMSG2(_(e_undefvar), lp->ll_name);
1893 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 if (v == NULL)
1895 return NULL;
1896
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 /*
1898 * Loop until no more [idx] or .key is following.
1899 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001901 var1.v_type = VAR_UNKNOWN;
1902 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001903 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1906 && !(lp->ll_tv->v_type == VAR_DICT
1907 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 if (!quiet)
1910 EMSG(_("E689: Can only index a List or Dictionary"));
1911 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 if (!quiet)
1916 EMSG(_("E708: [:] must come last"));
1917 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001919
Bram Moolenaar8c711452005-01-14 21:53:12 +00001920 len = -1;
1921 if (*p == '.')
1922 {
1923 key = p + 1;
1924 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1925 ;
1926 if (len == 0)
1927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 if (!quiet)
1929 EMSG(_(e_emptykey));
1930 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 }
1932 p = key + len;
1933 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001934 else
1935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001936 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001937 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001938 if (*p == ':')
1939 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001940 else
1941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001942 empty1 = FALSE;
1943 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001945 if (get_tv_string_chk(&var1) == NULL)
1946 {
1947 /* not a number or string */
1948 clear_tv(&var1);
1949 return NULL;
1950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001951 }
1952
1953 /* Optionally get the second index [ :expr]. */
1954 if (*p == ':')
1955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001960 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001962 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (rettv != NULL && (rettv->v_type != VAR_LIST
1964 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 if (!quiet)
1967 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001968 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 }
1971 p = skipwhite(p + 1);
1972 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 else
1975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1978 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001979 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 if (get_tv_string_chk(&var2) == NULL)
1983 {
1984 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001985 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 clear_tv(&var2);
1987 return NULL;
1988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001994
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 if (!quiet)
1998 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001999 clear_tv(&var1);
2000 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002002 }
2003
2004 /* Skip to past ']'. */
2005 ++p;
2006 }
2007
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 {
2010 if (len == -1)
2011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002013 key = get_tv_string_chk(&var1); /* is number or string */
2014 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 }
2019 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 lp->ll_list = NULL;
2021 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002022 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002023
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002024 /* When assigning to a scope dictionary check that a function and
2025 * variable name is valid (only variable name unless it is l: or
2026 * g: dictionary). Disallow overwriting a builtin function. */
2027 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002028 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002029 int prevval;
2030 int wrong;
2031
2032 if (len != -1)
2033 {
2034 prevval = key[len];
2035 key[len] = NUL;
2036 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002037 else
2038 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002039 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2040 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002041 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002042 || !valid_varname(key);
2043 if (len != -1)
2044 key[len] = prevval;
2045 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002046 return NULL;
2047 }
2048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002051 /* Can't add "v:" variable. */
2052 if (lp->ll_dict == &vimvardict)
2053 {
2054 EMSG2(_(e_illvar), name);
2055 return NULL;
2056 }
2057
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002058 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002063 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002065 }
2066 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002068 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002070 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 p = NULL;
2073 break;
2074 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002076 else if ((flags & GLV_READ_ONLY) == 0
2077 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002078 {
2079 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002081 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002082
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002083 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
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002094 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002095 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002096 clear_tv(&var1);
2097
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 lp->ll_list = lp->ll_tv->vval.v_list;
2100 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2101 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002102 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002103 if (lp->ll_n1 < 0)
2104 {
2105 lp->ll_n1 = 0;
2106 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2107 }
2108 }
2109 if (lp->ll_li == NULL)
2110 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002111 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002112 if (!quiet)
2113 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 }
2116
2117 /*
2118 * May need to find the item or absolute index for the second
2119 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 * When no index given: "lp->ll_empty2" is TRUE.
2121 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002125 lp->ll_n2 = (long)get_tv_number(&var2);
2126 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002127 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002132 {
2133 if (!quiet)
2134 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
2139
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2141 if (lp->ll_n1 < 0)
2142 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2143 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002144 {
2145 if (!quiet)
2146 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002148 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002149 }
2150
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 }
2154
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002155 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 return p;
2157}
2158
2159/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164{
2165 vim_free(lp->ll_exp_name);
2166 vim_free(lp->ll_newkey);
2167}
2168
2169/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002170 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175set_var_lval(
2176 lval_T *lp,
2177 char_u *endp,
2178 typval_T *rettv,
2179 int copy,
2180 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181{
2182 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 listitem_T *ri;
2184 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185
2186 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002188 cc = *endp;
2189 *endp = NUL;
2190 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002192 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193
Bram Moolenaar79518e22017-02-17 16:31:35 +01002194 /* handle +=, -= and .= */
2195 di = NULL;
2196 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2197 &tv, &di, TRUE, FALSE) == OK)
2198 {
2199 if ((di == NULL
2200 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2201 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2202 FALSE)))
2203 && tv_op(&tv, rettv, op) == OK)
2204 set_var(lp->ll_name, &tv, FALSE);
2205 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002208 else
2209 set_var(lp->ll_name, rettv, copy);
2210 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002212 else if (tv_check_lock(lp->ll_newkey == NULL
2213 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002214 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002215 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 else if (lp->ll_range)
2217 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002218 listitem_T *ll_li = lp->ll_li;
2219 int ll_n1 = lp->ll_n1;
2220
2221 /*
2222 * Check whether any of the list items is locked
2223 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002224 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002225 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002226 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002227 return;
2228 ri = ri->li_next;
2229 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2230 break;
2231 ll_li = ll_li->li_next;
2232 ++ll_n1;
2233 }
2234
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 /*
2236 * Assign the List values to the list items.
2237 */
2238 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 if (op != NULL && *op != '=')
2241 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2242 else
2243 {
2244 clear_tv(&lp->ll_li->li_tv);
2245 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 ri = ri->li_next;
2248 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2249 break;
2250 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002253 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 ri = NULL;
2256 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002257 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002258 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 lp->ll_li = lp->ll_li->li_next;
2260 ++lp->ll_n1;
2261 }
2262 if (ri != NULL)
2263 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 else if (lp->ll_empty2
2265 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 : lp->ll_n1 != lp->ll_n2)
2267 EMSG(_("E711: List value has not enough items"));
2268 }
2269 else
2270 {
2271 /*
2272 * Assign to a List or Dictionary item.
2273 */
2274 if (lp->ll_newkey != NULL)
2275 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 if (op != NULL && *op != '=')
2277 {
2278 EMSG2(_(e_letwrong), op);
2279 return;
2280 }
2281
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002283 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (di == NULL)
2285 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002286 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2287 {
2288 vim_free(di);
2289 return;
2290 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002292 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 else if (op != NULL && *op != '=')
2294 {
2295 tv_op(lp->ll_tv, rettv, op);
2296 return;
2297 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002300
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 /*
2302 * Assign the value to the variable or list item.
2303 */
2304 if (copy)
2305 copy_tv(rettv, lp->ll_tv);
2306 else
2307 {
2308 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002309 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
2312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313}
2314
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002315/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2317 * Returns OK or FAIL.
2318 */
2319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002320tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002322 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u numbuf[NUMBUFLEN];
2324 char_u *s;
2325
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002326 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2327 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2328 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 {
2330 switch (tv1->v_type)
2331 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002332 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 case VAR_DICT:
2334 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002335 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002336 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002337 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002338 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 break;
2340
2341 case VAR_LIST:
2342 if (*op != '+' || tv2->v_type != VAR_LIST)
2343 break;
2344 /* List += List */
2345 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2346 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2347 return OK;
2348
2349 case VAR_NUMBER:
2350 case VAR_STRING:
2351 if (tv2->v_type == VAR_LIST)
2352 break;
2353 if (*op == '+' || *op == '-')
2354 {
2355 /* nr += nr or nr -= nr*/
2356 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002357#ifdef FEAT_FLOAT
2358 if (tv2->v_type == VAR_FLOAT)
2359 {
2360 float_T f = n;
2361
2362 if (*op == '+')
2363 f += tv2->vval.v_float;
2364 else
2365 f -= tv2->vval.v_float;
2366 clear_tv(tv1);
2367 tv1->v_type = VAR_FLOAT;
2368 tv1->vval.v_float = f;
2369 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002371#endif
2372 {
2373 if (*op == '+')
2374 n += get_tv_number(tv2);
2375 else
2376 n -= get_tv_number(tv2);
2377 clear_tv(tv1);
2378 tv1->v_type = VAR_NUMBER;
2379 tv1->vval.v_number = n;
2380 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 }
2382 else
2383 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002384 if (tv2->v_type == VAR_FLOAT)
2385 break;
2386
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 /* str .= str */
2388 s = get_tv_string(tv1);
2389 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2390 clear_tv(tv1);
2391 tv1->v_type = VAR_STRING;
2392 tv1->vval.v_string = s;
2393 }
2394 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002395
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002396 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002397#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398 {
2399 float_T f;
2400
2401 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2402 && tv2->v_type != VAR_NUMBER
2403 && tv2->v_type != VAR_STRING))
2404 break;
2405 if (tv2->v_type == VAR_FLOAT)
2406 f = tv2->vval.v_float;
2407 else
2408 f = get_tv_number(tv2);
2409 if (*op == '+')
2410 tv1->vval.v_float += f;
2411 else
2412 tv1->vval.v_float -= f;
2413 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002414#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002415 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 }
2417 }
2418
2419 EMSG2(_(e_letwrong), op);
2420 return FAIL;
2421}
2422
2423/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 * Evaluate the expression used in a ":for var in expr" command.
2425 * "arg" points to "var".
2426 * Set "*errp" to TRUE for an error, FALSE otherwise;
2427 * Return a pointer that holds the info. Null when there is an error.
2428 */
2429 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002430eval_for_line(
2431 char_u *arg,
2432 int *errp,
2433 char_u **nextcmdp,
2434 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435{
Bram Moolenaar33570922005-01-25 22:26:29 +00002436 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002438 typval_T tv;
2439 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440
2441 *errp = TRUE; /* default: there is an error */
2442
Bram Moolenaar33570922005-01-25 22:26:29 +00002443 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 if (fi == NULL)
2445 return NULL;
2446
2447 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2448 if (expr == NULL)
2449 return fi;
2450
2451 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002452 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002453 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002454 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 return fi;
2456 }
2457
2458 if (skip)
2459 ++emsg_skip;
2460 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2461 {
2462 *errp = FALSE;
2463 if (!skip)
2464 {
2465 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002466 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002467 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002469 clear_tv(&tv);
2470 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002471 else if (l == NULL)
2472 {
2473 /* a null list is like an empty list: do nothing */
2474 clear_tv(&tv);
2475 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002476 else
2477 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002478 /* No need to increment the refcount, it's already set for the
2479 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 fi->fi_list = l;
2481 list_add_watch(l, &fi->fi_lw);
2482 fi->fi_lw.lw_item = l->lv_first;
2483 }
2484 }
2485 }
2486 if (skip)
2487 --emsg_skip;
2488
2489 return fi;
2490}
2491
2492/*
2493 * Use the first item in a ":for" list. Advance to the next.
2494 * Assign the values to the variable (list). "arg" points to the first one.
2495 * Return TRUE when a valid item was found, FALSE when at end of list or
2496 * something wrong.
2497 */
2498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002500{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002501 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002502 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504
2505 item = fi->fi_lw.lw_item;
2506 if (item == NULL)
2507 result = FALSE;
2508 else
2509 {
2510 fi->fi_lw.lw_item = item->li_next;
2511 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2512 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2513 }
2514 return result;
2515}
2516
2517/*
2518 * Free the structure used to store info used by ":for".
2519 */
2520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002521free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002522{
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002525 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002526 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002528 list_unref(fi->fi_list);
2529 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002530 vim_free(fi);
2531}
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2534
2535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536set_context_for_expression(
2537 expand_T *xp,
2538 char_u *arg,
2539 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int got_eq = FALSE;
2542 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002543 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545 if (cmdidx == CMD_let)
2546 {
2547 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002548 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 {
2550 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002551 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 {
2553 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002554 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002555 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 break;
2557 }
2558 return;
2559 }
2560 }
2561 else
2562 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2563 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 while ((xp->xp_pattern = vim_strpbrk(arg,
2565 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2566 {
2567 c = *xp->xp_pattern;
2568 if (c == '&')
2569 {
2570 c = xp->xp_pattern[1];
2571 if (c == '&')
2572 {
2573 ++xp->xp_pattern;
2574 xp->xp_context = cmdidx != CMD_let || got_eq
2575 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2576 }
2577 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002580 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2581 xp->xp_pattern += 2;
2582
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585 else if (c == '$')
2586 {
2587 /* environment variable */
2588 xp->xp_context = EXPAND_ENV_VARS;
2589 }
2590 else if (c == '=')
2591 {
2592 got_eq = TRUE;
2593 xp->xp_context = EXPAND_EXPRESSION;
2594 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002595 else if (c == '#'
2596 && xp->xp_context == EXPAND_EXPRESSION)
2597 {
2598 /* Autoload function/variable contains '#'. */
2599 break;
2600 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002601 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 && xp->xp_context == EXPAND_FUNCTIONS
2603 && vim_strchr(xp->xp_pattern, '(') == NULL)
2604 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002605 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 break;
2607 }
2608 else if (cmdidx != CMD_let || got_eq)
2609 {
2610 if (c == '"') /* string */
2611 {
2612 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2613 if (c == '\\' && xp->xp_pattern[1] != NUL)
2614 ++xp->xp_pattern;
2615 xp->xp_context = EXPAND_NOTHING;
2616 }
2617 else if (c == '\'') /* literal string */
2618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2621 /* skip */ ;
2622 xp->xp_context = EXPAND_NOTHING;
2623 }
2624 else if (c == '|')
2625 {
2626 if (xp->xp_pattern[1] == '|')
2627 {
2628 ++xp->xp_pattern;
2629 xp->xp_context = EXPAND_EXPRESSION;
2630 }
2631 else
2632 xp->xp_context = EXPAND_COMMANDS;
2633 }
2634 else
2635 xp->xp_context = EXPAND_EXPRESSION;
2636 }
2637 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002638 /* Doesn't look like something valid, expand as an expression
2639 * anyway. */
2640 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 arg = xp->xp_pattern;
2642 if (*arg != NUL)
2643 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2644 /* skip */ ;
2645 }
2646 xp->xp_pattern = arg;
2647}
2648
2649#endif /* FEAT_CMDL_COMPL */
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * ":unlet[!] var1 ... " command.
2653 */
2654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002655ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002657 ex_unletlock(eap, eap->arg, 0);
2658}
2659
2660/*
2661 * ":lockvar" and ":unlockvar" commands
2662 */
2663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002665{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002667 int deep = 2;
2668
2669 if (eap->forceit)
2670 deep = -1;
2671 else if (vim_isdigit(*arg))
2672 {
2673 deep = getdigits(&arg);
2674 arg = skipwhite(arg);
2675 }
2676
2677 ex_unletlock(eap, arg, deep);
2678}
2679
2680/*
2681 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2682 */
2683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002684ex_unletlock(
2685 exarg_T *eap,
2686 char_u *argstart,
2687 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688{
2689 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002692 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 do
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002697 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002698 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lv.ll_name == NULL)
2700 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002701 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (name_end != NULL)
2705 {
2706 emsg_severe = TRUE;
2707 EMSG(_(e_trailing));
2708 }
2709 if (!(eap->skip || error))
2710 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712 }
2713
2714 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002715 {
2716 if (eap->cmdidx == CMD_unlet)
2717 {
2718 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2719 error = TRUE;
2720 }
2721 else
2722 {
2723 if (do_lock_var(&lv, name_end, deep,
2724 eap->cmdidx == CMD_lockvar) == FAIL)
2725 error = TRUE;
2726 }
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (!eap->skip)
2730 clear_lval(&lv);
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 arg = skipwhite(name_end);
2733 } while (!ends_excmd(*arg));
2734
2735 eap->nextcmd = check_nextcmd(arg);
2736}
2737
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002739do_unlet_var(
2740 lval_T *lp,
2741 char_u *name_end,
2742 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 int ret = OK;
2745 int cc;
2746
2747 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 cc = *name_end;
2750 *name_end = NUL;
2751
2752 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002753 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002757 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002758 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002759 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002760 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002761 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 else if (lp->ll_range)
2763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002764 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002765 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002766 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002767
2768 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2769 {
2770 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002771 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002772 return FAIL;
2773 ll_li = li;
2774 ++ll_n1;
2775 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776
2777 /* Delete a range of List items. */
2778 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2779 {
2780 li = lp->ll_li->li_next;
2781 listitem_remove(lp->ll_list, lp->ll_li);
2782 lp->ll_li = li;
2783 ++lp->ll_n1;
2784 }
2785 }
2786 else
2787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 /* unlet a List item. */
2790 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002793 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 }
2795
2796 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797}
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799/*
2800 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002801 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
2803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002804do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 hashtab_T *ht;
2807 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002808 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002809 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002810 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 ht = find_var_ht(name, &varname);
2813 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002816 if (d == NULL)
2817 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 if (ht == &globvarht)
2819 d = &globvardict;
2820 else if (ht == &compat_hashtab)
2821 d = &vimvardict;
2822 else
2823 {
2824 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2825 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2826 }
2827 if (d == NULL)
2828 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002829 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 return FAIL;
2831 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002832 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002834 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002835 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002836 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002837 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002838 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002839 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002840 || var_check_ro(di->di_flags, name, FALSE)
2841 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002842 return FAIL;
2843
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 delete_var(ht, hi);
2845 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 if (forceit)
2849 return OK;
2850 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 return FAIL;
2852}
2853
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002854/*
2855 * Lock or unlock variable indicated by "lp".
2856 * "deep" is the levels to go (-1 for unlimited);
2857 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2858 */
2859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002860do_lock_var(
2861 lval_T *lp,
2862 char_u *name_end,
2863 int deep,
2864 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002865{
2866 int ret = OK;
2867 int cc;
2868 dictitem_T *di;
2869
2870 if (deep == 0) /* nothing to do */
2871 return OK;
2872
2873 if (lp->ll_tv == NULL)
2874 {
2875 cc = *name_end;
2876 *name_end = NUL;
2877
2878 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002879 di = find_var(lp->ll_name, NULL, TRUE);
2880 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002882 else if ((di->di_flags & DI_FLAGS_FIX)
2883 && di->di_tv.v_type != VAR_DICT
2884 && di->di_tv.v_type != VAR_LIST)
2885 /* For historic reasons this error is not given for a list or dict.
2886 * E.g., the b: dict could be locked/unlocked. */
2887 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002888 else
2889 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002890 if (lock)
2891 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002893 di->di_flags &= ~DI_FLAGS_LOCK;
2894 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 }
2896 *name_end = cc;
2897 }
2898 else if (lp->ll_range)
2899 {
2900 listitem_T *li = lp->ll_li;
2901
2902 /* (un)lock a range of List items. */
2903 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2904 {
2905 item_lock(&li->li_tv, deep, lock);
2906 li = li->li_next;
2907 ++lp->ll_n1;
2908 }
2909 }
2910 else if (lp->ll_list != NULL)
2911 /* (un)lock a List item. */
2912 item_lock(&lp->ll_li->li_tv, deep, lock);
2913 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002914 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 item_lock(&lp->ll_di->di_tv, deep, lock);
2916
2917 return ret;
2918}
2919
2920/*
2921 * Lock or unlock an item. "deep" is nr of levels to go.
2922 */
2923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925{
2926 static int recurse = 0;
2927 list_T *l;
2928 listitem_T *li;
2929 dict_T *d;
2930 hashitem_T *hi;
2931 int todo;
2932
2933 if (recurse >= DICT_MAXNEST)
2934 {
2935 EMSG(_("E743: variable nested too deep for (un)lock"));
2936 return;
2937 }
2938 if (deep == 0)
2939 return;
2940 ++recurse;
2941
2942 /* lock/unlock the item itself */
2943 if (lock)
2944 tv->v_lock |= VAR_LOCKED;
2945 else
2946 tv->v_lock &= ~VAR_LOCKED;
2947
2948 switch (tv->v_type)
2949 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002950 case VAR_UNKNOWN:
2951 case VAR_NUMBER:
2952 case VAR_STRING:
2953 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002954 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 case VAR_FLOAT:
2956 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002957 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002958 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002959 break;
2960
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 case VAR_LIST:
2962 if ((l = tv->vval.v_list) != NULL)
2963 {
2964 if (lock)
2965 l->lv_lock |= VAR_LOCKED;
2966 else
2967 l->lv_lock &= ~VAR_LOCKED;
2968 if (deep < 0 || deep > 1)
2969 /* recursive: lock/unlock the items the List contains */
2970 for (li = l->lv_first; li != NULL; li = li->li_next)
2971 item_lock(&li->li_tv, deep - 1, lock);
2972 }
2973 break;
2974 case VAR_DICT:
2975 if ((d = tv->vval.v_dict) != NULL)
2976 {
2977 if (lock)
2978 d->dv_lock |= VAR_LOCKED;
2979 else
2980 d->dv_lock &= ~VAR_LOCKED;
2981 if (deep < 0 || deep > 1)
2982 {
2983 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002984 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002985 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2986 {
2987 if (!HASHITEM_EMPTY(hi))
2988 {
2989 --todo;
2990 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2991 }
2992 }
2993 }
2994 }
2995 }
2996 --recurse;
2997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3000/*
3001 * Delete all "menutrans_" variables.
3002 */
3003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003004del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003007 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003010 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003012 {
3013 if (!HASHITEM_EMPTY(hi))
3014 {
3015 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3017 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003018 }
3019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021}
3022#endif
3023
3024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3025
3026/*
3027 * Local string buffer for the next two functions to store a variable name
3028 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3029 * get_user_var_name().
3030 */
3031
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003032static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034static char_u *varnamebuf = NULL;
3035static int varnamebuflen = 0;
3036
3037/*
3038 * Function to concatenate a prefix and a variable name.
3039 */
3040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003041cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 int len;
3044
3045 len = (int)STRLEN(name) + 3;
3046 if (len > varnamebuflen)
3047 {
3048 vim_free(varnamebuf);
3049 len += 10; /* some additional space */
3050 varnamebuf = alloc(len);
3051 if (varnamebuf == NULL)
3052 {
3053 varnamebuflen = 0;
3054 return NULL;
3055 }
3056 varnamebuflen = len;
3057 }
3058 *varnamebuf = prefix;
3059 varnamebuf[1] = ':';
3060 STRCPY(varnamebuf + 2, name);
3061 return varnamebuf;
3062}
3063
3064/*
3065 * Function given to ExpandGeneric() to obtain the list of user defined
3066 * (global/buffer/window/built-in) variable names.
3067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003069get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003071 static long_u gdone;
3072 static long_u bdone;
3073 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003074#ifdef FEAT_WINDOWS
3075 static long_u tdone;
3076#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003077 static int vidx;
3078 static hashitem_T *hi;
3079 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003083 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003084#ifdef FEAT_WINDOWS
3085 tdone = 0;
3086#endif
3087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003088
3089 /* Global variables */
3090 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003092 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003094 else
3095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003096 while (HASHITEM_EMPTY(hi))
3097 ++hi;
3098 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3099 return cat_prefix_varname('g', hi->hi_key);
3100 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003102
3103 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003104 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003107 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003109 else
3110 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003111 while (HASHITEM_EMPTY(hi))
3112 ++hi;
3113 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003115
3116 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003117 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003120 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003122 else
3123 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003124 while (HASHITEM_EMPTY(hi))
3125 ++hi;
3126 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003128
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003129#ifdef FEAT_WINDOWS
3130 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003131 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003132 if (tdone < ht->ht_used)
3133 {
3134 if (tdone++ == 0)
3135 hi = ht->ht_array;
3136 else
3137 ++hi;
3138 while (HASHITEM_EMPTY(hi))
3139 ++hi;
3140 return cat_prefix_varname('t', hi->hi_key);
3141 }
3142#endif
3143
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 /* v: variables */
3145 if (vidx < VV_LEN)
3146 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 vim_free(varnamebuf);
3149 varnamebuf = NULL;
3150 varnamebuflen = 0;
3151 return NULL;
3152}
3153
3154#endif /* FEAT_CMDL_COMPL */
3155
3156/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003157 * Return TRUE if "pat" matches "text".
3158 * Does not use 'cpo' and always uses 'magic'.
3159 */
3160 static int
3161pattern_match(char_u *pat, char_u *text, int ic)
3162{
3163 int matches = FALSE;
3164 char_u *save_cpo;
3165 regmatch_T regmatch;
3166
3167 /* avoid 'l' flag in 'cpoptions' */
3168 save_cpo = p_cpo;
3169 p_cpo = (char_u *)"";
3170 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3171 if (regmatch.regprog != NULL)
3172 {
3173 regmatch.rm_ic = ic;
3174 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3175 vim_regfree(regmatch.regprog);
3176 }
3177 p_cpo = save_cpo;
3178 return matches;
3179}
3180
3181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 * types for expressions.
3183 */
3184typedef enum
3185{
3186 TYPE_UNKNOWN = 0
3187 , TYPE_EQUAL /* == */
3188 , TYPE_NEQUAL /* != */
3189 , TYPE_GREATER /* > */
3190 , TYPE_GEQUAL /* >= */
3191 , TYPE_SMALLER /* < */
3192 , TYPE_SEQUAL /* <= */
3193 , TYPE_MATCH /* =~ */
3194 , TYPE_NOMATCH /* !~ */
3195} exptype_T;
3196
3197/*
3198 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003199 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3201 */
3202
3203/*
3204 * Handle zero level expression.
3205 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003206 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003207 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 * Return OK or FAIL.
3209 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211eval0(
3212 char_u *arg,
3213 typval_T *rettv,
3214 char_u **nextcmd,
3215 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216{
3217 int ret;
3218 char_u *p;
3219
3220 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003221 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (ret == FAIL || !ends_excmd(*p))
3223 {
3224 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003225 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 /*
3227 * Report the invalid expression unless the expression evaluation has
3228 * been cancelled due to an aborting error, an interrupt, or an
3229 * exception.
3230 */
3231 if (!aborting())
3232 EMSG2(_(e_invexpr2), arg);
3233 ret = FAIL;
3234 }
3235 if (nextcmd != NULL)
3236 *nextcmd = check_nextcmd(p);
3237
3238 return ret;
3239}
3240
3241/*
3242 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003243 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 *
3245 * "arg" must point to the first non-white of the expression.
3246 * "arg" is advanced to the next non-white after the recognized expression.
3247 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003248 * Note: "rettv.v_lock" is not set.
3249 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * Return OK or FAIL.
3251 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 /*
3259 * Get the first variable.
3260 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003261 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 return FAIL;
3263
3264 if ((*arg)[0] == '?')
3265 {
3266 result = FALSE;
3267 if (evaluate)
3268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003269 int error = FALSE;
3270
3271 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003274 if (error)
3275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277
3278 /*
3279 * Get the second variable.
3280 */
3281 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 return FAIL;
3284
3285 /*
3286 * Check for the ":".
3287 */
3288 if ((*arg)[0] != ':')
3289 {
3290 EMSG(_("E109: Missing ':' after '?'"));
3291 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003292 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return FAIL;
3294 }
3295
3296 /*
3297 * Get the third variable.
3298 */
3299 *arg = skipwhite(*arg + 1);
3300 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3301 {
3302 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003303 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return FAIL;
3305 }
3306 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003307 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309
3310 return OK;
3311}
3312
3313/*
3314 * Handle first level expression:
3315 * expr2 || expr2 || expr2 logical OR
3316 *
3317 * "arg" must point to the first non-white of the expression.
3318 * "arg" is advanced to the next non-white after the recognized expression.
3319 *
3320 * Return OK or FAIL.
3321 */
3322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003323eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 long result;
3327 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003328 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
3330 /*
3331 * Get the first variable.
3332 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003333 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 return FAIL;
3335
3336 /*
3337 * Repeat until there is no following "||".
3338 */
3339 first = TRUE;
3340 result = FALSE;
3341 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3342 {
3343 if (evaluate && first)
3344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348 if (error)
3349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 first = FALSE;
3351 }
3352
3353 /*
3354 * Get the second variable.
3355 */
3356 *arg = skipwhite(*arg + 2);
3357 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3358 return FAIL;
3359
3360 /*
3361 * Compute the result.
3362 */
3363 if (evaluate && !result)
3364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003367 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368 if (error)
3369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371 if (evaluate)
3372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 rettv->v_type = VAR_NUMBER;
3374 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 }
3377
3378 return OK;
3379}
3380
3381/*
3382 * Handle second level expression:
3383 * expr3 && expr3 && expr3 logical AND
3384 *
3385 * "arg" must point to the first non-white of the expression.
3386 * "arg" is advanced to the next non-white after the recognized expression.
3387 *
3388 * Return OK or FAIL.
3389 */
3390 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 long result;
3395 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003396 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 /*
3399 * Get the first variable.
3400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return FAIL;
3403
3404 /*
3405 * Repeat until there is no following "&&".
3406 */
3407 first = TRUE;
3408 result = TRUE;
3409 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3410 {
3411 if (evaluate && first)
3412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 if (error)
3417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 first = FALSE;
3419 }
3420
3421 /*
3422 * Get the second variable.
3423 */
3424 *arg = skipwhite(*arg + 2);
3425 if (eval4(arg, &var2, evaluate && result) == FAIL)
3426 return FAIL;
3427
3428 /*
3429 * Compute the result.
3430 */
3431 if (evaluate && result)
3432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003436 if (error)
3437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 if (evaluate)
3440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 rettv->v_type = VAR_NUMBER;
3442 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 }
3445
3446 return OK;
3447}
3448
3449/*
3450 * Handle third level expression:
3451 * var1 == var2
3452 * var1 =~ var2
3453 * var1 != var2
3454 * var1 !~ var2
3455 * var1 > var2
3456 * var1 >= var2
3457 * var1 < var2
3458 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003459 * var1 is var2
3460 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 *
3462 * "arg" must point to the first non-white of the expression.
3463 * "arg" is advanced to the next non-white after the recognized expression.
3464 *
3465 * Return OK or FAIL.
3466 */
3467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003468eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 char_u *p;
3472 int i;
3473 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003474 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003476 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 char_u *s1, *s2;
3478 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481 /*
3482 * Get the first variable.
3483 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 return FAIL;
3486
3487 p = *arg;
3488 switch (p[0])
3489 {
3490 case '=': if (p[1] == '=')
3491 type = TYPE_EQUAL;
3492 else if (p[1] == '~')
3493 type = TYPE_MATCH;
3494 break;
3495 case '!': if (p[1] == '=')
3496 type = TYPE_NEQUAL;
3497 else if (p[1] == '~')
3498 type = TYPE_NOMATCH;
3499 break;
3500 case '>': if (p[1] != '=')
3501 {
3502 type = TYPE_GREATER;
3503 len = 1;
3504 }
3505 else
3506 type = TYPE_GEQUAL;
3507 break;
3508 case '<': if (p[1] != '=')
3509 {
3510 type = TYPE_SMALLER;
3511 len = 1;
3512 }
3513 else
3514 type = TYPE_SEQUAL;
3515 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003516 case 'i': if (p[1] == 's')
3517 {
3518 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3519 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003520 i = p[len];
3521 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003522 {
3523 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3524 type_is = TRUE;
3525 }
3526 }
3527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529
3530 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003531 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 if (type != TYPE_UNKNOWN)
3534 {
3535 /* extra question mark appended: ignore case */
3536 if (p[len] == '?')
3537 {
3538 ic = TRUE;
3539 ++len;
3540 }
3541 /* extra '#' appended: match case */
3542 else if (p[len] == '#')
3543 {
3544 ic = FALSE;
3545 ++len;
3546 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003547 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else
3549 ic = p_ic;
3550
3551 /*
3552 * Get the second variable.
3553 */
3554 *arg = skipwhite(p + len);
3555 if (eval5(arg, &var2, evaluate) == FAIL)
3556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 return FAIL;
3559 }
3560
3561 if (evaluate)
3562 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003563 if (type_is && rettv->v_type != var2.v_type)
3564 {
3565 /* For "is" a different type always means FALSE, for "notis"
3566 * it means TRUE. */
3567 n1 = (type == TYPE_NEQUAL);
3568 }
3569 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3570 {
3571 if (type_is)
3572 {
3573 n1 = (rettv->v_type == var2.v_type
3574 && rettv->vval.v_list == var2.vval.v_list);
3575 if (type == TYPE_NEQUAL)
3576 n1 = !n1;
3577 }
3578 else if (rettv->v_type != var2.v_type
3579 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3580 {
3581 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003582 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003583 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003584 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003585 clear_tv(rettv);
3586 clear_tv(&var2);
3587 return FAIL;
3588 }
3589 else
3590 {
3591 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003592 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3593 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003594 if (type == TYPE_NEQUAL)
3595 n1 = !n1;
3596 }
3597 }
3598
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003599 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3600 {
3601 if (type_is)
3602 {
3603 n1 = (rettv->v_type == var2.v_type
3604 && rettv->vval.v_dict == var2.vval.v_dict);
3605 if (type == TYPE_NEQUAL)
3606 n1 = !n1;
3607 }
3608 else if (rettv->v_type != var2.v_type
3609 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3610 {
3611 if (rettv->v_type != var2.v_type)
3612 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3613 else
3614 EMSG(_("E736: Invalid operation for Dictionary"));
3615 clear_tv(rettv);
3616 clear_tv(&var2);
3617 return FAIL;
3618 }
3619 else
3620 {
3621 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003622 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3623 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003624 if (type == TYPE_NEQUAL)
3625 n1 = !n1;
3626 }
3627 }
3628
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003629 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3630 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003631 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003632 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003633 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003634 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003635 clear_tv(rettv);
3636 clear_tv(&var2);
3637 return FAIL;
3638 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003639 if ((rettv->v_type == VAR_PARTIAL
3640 && rettv->vval.v_partial == NULL)
3641 || (var2.v_type == VAR_PARTIAL
3642 && var2.vval.v_partial == NULL))
3643 /* when a partial is NULL assume not equal */
3644 n1 = FALSE;
3645 else if (type_is)
3646 {
3647 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3648 /* strings are considered the same if their value is
3649 * the same */
3650 n1 = tv_equal(rettv, &var2, ic, FALSE);
3651 else if (rettv->v_type == VAR_PARTIAL
3652 && var2.v_type == VAR_PARTIAL)
3653 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3654 else
3655 n1 = FALSE;
3656 }
3657 else
3658 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003659 if (type == TYPE_NEQUAL)
3660 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003661 }
3662
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003663#ifdef FEAT_FLOAT
3664 /*
3665 * If one of the two variables is a float, compare as a float.
3666 * When using "=~" or "!~", always compare as string.
3667 */
3668 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3669 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3670 {
3671 float_T f1, f2;
3672
3673 if (rettv->v_type == VAR_FLOAT)
3674 f1 = rettv->vval.v_float;
3675 else
3676 f1 = get_tv_number(rettv);
3677 if (var2.v_type == VAR_FLOAT)
3678 f2 = var2.vval.v_float;
3679 else
3680 f2 = get_tv_number(&var2);
3681 n1 = FALSE;
3682 switch (type)
3683 {
3684 case TYPE_EQUAL: n1 = (f1 == f2); break;
3685 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3686 case TYPE_GREATER: n1 = (f1 > f2); break;
3687 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3688 case TYPE_SMALLER: n1 = (f1 < f2); break;
3689 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3690 case TYPE_UNKNOWN:
3691 case TYPE_MATCH:
3692 case TYPE_NOMATCH: break; /* avoid gcc warning */
3693 }
3694 }
3695#endif
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 /*
3698 * If one of the two variables is a number, compare as a number.
3699 * When using "=~" or "!~", always compare as string.
3700 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003701 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003704 n1 = get_tv_number(rettv);
3705 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 switch (type)
3707 {
3708 case TYPE_EQUAL: n1 = (n1 == n2); break;
3709 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3710 case TYPE_GREATER: n1 = (n1 > n2); break;
3711 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3712 case TYPE_SMALLER: n1 = (n1 < n2); break;
3713 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3714 case TYPE_UNKNOWN:
3715 case TYPE_MATCH:
3716 case TYPE_NOMATCH: break; /* avoid gcc warning */
3717 }
3718 }
3719 else
3720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 s1 = get_tv_string_buf(rettv, buf1);
3722 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3724 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3725 else
3726 i = 0;
3727 n1 = FALSE;
3728 switch (type)
3729 {
3730 case TYPE_EQUAL: n1 = (i == 0); break;
3731 case TYPE_NEQUAL: n1 = (i != 0); break;
3732 case TYPE_GREATER: n1 = (i > 0); break;
3733 case TYPE_GEQUAL: n1 = (i >= 0); break;
3734 case TYPE_SMALLER: n1 = (i < 0); break;
3735 case TYPE_SEQUAL: n1 = (i <= 0); break;
3736
3737 case TYPE_MATCH:
3738 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003739 n1 = pattern_match(s2, s1, ic);
3740 if (type == TYPE_NOMATCH)
3741 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 break;
3743
3744 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3745 }
3746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003747 clear_tv(rettv);
3748 clear_tv(&var2);
3749 rettv->v_type = VAR_NUMBER;
3750 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 }
3753
3754 return OK;
3755}
3756
3757/*
3758 * Handle fourth level expression:
3759 * + number addition
3760 * - number subtraction
3761 * . string concatenation
3762 *
3763 * "arg" must point to the first non-white of the expression.
3764 * "arg" is advanced to the next non-white after the recognized expression.
3765 *
3766 * Return OK or FAIL.
3767 */
3768 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 typval_T var2;
3772 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003774 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775#ifdef FEAT_FLOAT
3776 float_T f1 = 0, f2 = 0;
3777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 char_u *s1, *s2;
3779 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3780 char_u *p;
3781
3782 /*
3783 * Get the first variable.
3784 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003785 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 /*
3789 * Repeat computing, until no '+', '-' or '.' is following.
3790 */
3791 for (;;)
3792 {
3793 op = **arg;
3794 if (op != '+' && op != '-' && op != '.')
3795 break;
3796
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003797 if ((op != '+' || rettv->v_type != VAR_LIST)
3798#ifdef FEAT_FLOAT
3799 && (op == '.' || rettv->v_type != VAR_FLOAT)
3800#endif
3801 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 {
3803 /* For "list + ...", an illegal use of the first operand as
3804 * a number cannot be determined before evaluating the 2nd
3805 * operand: if this is also a list, all is ok.
3806 * For "something . ...", "something - ..." or "non-list + ...",
3807 * we know that the first operand needs to be a string or number
3808 * without evaluating the 2nd operand. So check before to avoid
3809 * side effects after an error. */
3810 if (evaluate && get_tv_string_chk(rettv) == NULL)
3811 {
3812 clear_tv(rettv);
3813 return FAIL;
3814 }
3815 }
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 /*
3818 * Get the second variable.
3819 */
3820 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003821 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 return FAIL;
3825 }
3826
3827 if (evaluate)
3828 {
3829 /*
3830 * Compute the result.
3831 */
3832 if (op == '.')
3833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3835 s2 = get_tv_string_buf_chk(&var2, buf2);
3836 if (s2 == NULL) /* type error ? */
3837 {
3838 clear_tv(rettv);
3839 clear_tv(&var2);
3840 return FAIL;
3841 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003842 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003843 clear_tv(rettv);
3844 rettv->v_type = VAR_STRING;
3845 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003847 else if (op == '+' && rettv->v_type == VAR_LIST
3848 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003849 {
3850 /* concatenate Lists */
3851 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3852 &var3) == FAIL)
3853 {
3854 clear_tv(rettv);
3855 clear_tv(&var2);
3856 return FAIL;
3857 }
3858 clear_tv(rettv);
3859 *rettv = var3;
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 else
3862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 int error = FALSE;
3864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865#ifdef FEAT_FLOAT
3866 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 f1 = rettv->vval.v_float;
3869 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 else
3872#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003874 n1 = get_tv_number_chk(rettv, &error);
3875 if (error)
3876 {
3877 /* This can only happen for "list + non-list". For
3878 * "non-list + ..." or "something - ...", we returned
3879 * before evaluating the 2nd operand. */
3880 clear_tv(rettv);
3881 return FAIL;
3882 }
3883#ifdef FEAT_FLOAT
3884 if (var2.v_type == VAR_FLOAT)
3885 f1 = n1;
3886#endif
3887 }
3888#ifdef FEAT_FLOAT
3889 if (var2.v_type == VAR_FLOAT)
3890 {
3891 f2 = var2.vval.v_float;
3892 n2 = 0;
3893 }
3894 else
3895#endif
3896 {
3897 n2 = get_tv_number_chk(&var2, &error);
3898 if (error)
3899 {
3900 clear_tv(rettv);
3901 clear_tv(&var2);
3902 return FAIL;
3903 }
3904#ifdef FEAT_FLOAT
3905 if (rettv->v_type == VAR_FLOAT)
3906 f2 = n2;
3907#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003910
3911#ifdef FEAT_FLOAT
3912 /* If there is a float on either side the result is a float. */
3913 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3914 {
3915 if (op == '+')
3916 f1 = f1 + f2;
3917 else
3918 f1 = f1 - f2;
3919 rettv->v_type = VAR_FLOAT;
3920 rettv->vval.v_float = f1;
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003923#endif
3924 {
3925 if (op == '+')
3926 n1 = n1 + n2;
3927 else
3928 n1 = n1 - n2;
3929 rettv->v_type = VAR_NUMBER;
3930 rettv->vval.v_number = n1;
3931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935 }
3936 return OK;
3937}
3938
3939/*
3940 * Handle fifth level expression:
3941 * * number multiplication
3942 * / number division
3943 * % number modulo
3944 *
3945 * "arg" must point to the first non-white of the expression.
3946 * "arg" is advanced to the next non-white after the recognized expression.
3947 *
3948 * Return OK or FAIL.
3949 */
3950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003951eval6(
3952 char_u **arg,
3953 typval_T *rettv,
3954 int evaluate,
3955 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003959 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003960#ifdef FEAT_FLOAT
3961 int use_float = FALSE;
3962 float_T f1 = 0, f2;
3963#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003964 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
3966 /*
3967 * Get the first variable.
3968 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003969 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 return FAIL;
3971
3972 /*
3973 * Repeat computing, until no '*', '/' or '%' is following.
3974 */
3975 for (;;)
3976 {
3977 op = **arg;
3978 if (op != '*' && op != '/' && op != '%')
3979 break;
3980
3981 if (evaluate)
3982 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003983#ifdef FEAT_FLOAT
3984 if (rettv->v_type == VAR_FLOAT)
3985 {
3986 f1 = rettv->vval.v_float;
3987 use_float = TRUE;
3988 n1 = 0;
3989 }
3990 else
3991#endif
3992 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 if (error)
3995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997 else
3998 n1 = 0;
3999
4000 /*
4001 * Get the second variable.
4002 */
4003 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004004 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return FAIL;
4006
4007 if (evaluate)
4008 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004009#ifdef FEAT_FLOAT
4010 if (var2.v_type == VAR_FLOAT)
4011 {
4012 if (!use_float)
4013 {
4014 f1 = n1;
4015 use_float = TRUE;
4016 }
4017 f2 = var2.vval.v_float;
4018 n2 = 0;
4019 }
4020 else
4021#endif
4022 {
4023 n2 = get_tv_number_chk(&var2, &error);
4024 clear_tv(&var2);
4025 if (error)
4026 return FAIL;
4027#ifdef FEAT_FLOAT
4028 if (use_float)
4029 f2 = n2;
4030#endif
4031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033 /*
4034 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004035 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004037#ifdef FEAT_FLOAT
4038 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040 if (op == '*')
4041 f1 = f1 * f2;
4042 else if (op == '/')
4043 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004044# ifdef VMS
4045 /* VMS crashes on divide by zero, work around it */
4046 if (f2 == 0.0)
4047 {
4048 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004049 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004050 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004051 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004052 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004053 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004054 }
4055 else
4056 f1 = f1 / f2;
4057# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 /* We rely on the floating point library to handle divide
4059 * by zero to result in "inf" and not a crash. */
4060 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004061# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004064 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004065 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066 return FAIL;
4067 }
4068 rettv->v_type = VAR_FLOAT;
4069 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 if (op == '*')
4075 n1 = n1 * n2;
4076 else if (op == '/')
4077 {
4078 if (n2 == 0) /* give an error message? */
4079 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004080 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004081 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004082 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004083 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004084 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004085 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004086 }
4087 else
4088 n1 = n1 / n2;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004091 {
4092 if (n2 == 0) /* give an error message? */
4093 n1 = 0;
4094 else
4095 n1 = n1 % n2;
4096 }
4097 rettv->v_type = VAR_NUMBER;
4098 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 }
4102
4103 return OK;
4104}
4105
4106/*
4107 * Handle sixth level expression:
4108 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004109 * "string" string constant
4110 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 * &option-name option value
4112 * @r register contents
4113 * identifier variable value
4114 * function() function call
4115 * $VAR environment variable
4116 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004117 * [expr, expr] List
4118 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * Also handle:
4121 * ! in front logical NOT
4122 * - in front unary minus
4123 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004124 * trailing [] subscript in String or List
4125 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 *
4127 * "arg" must point to the first non-white of the expression.
4128 * "arg" is advanced to the next non-white after the recognized expression.
4129 *
4130 * Return OK or FAIL.
4131 */
4132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004133eval7(
4134 char_u **arg,
4135 typval_T *rettv,
4136 int evaluate,
4137 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004139 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int len;
4141 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 char_u *start_leader, *end_leader;
4143 int ret = OK;
4144 char_u *alias;
4145
4146 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004148 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004153 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 */
4155 start_leader = *arg;
4156 while (**arg == '!' || **arg == '-' || **arg == '+')
4157 *arg = skipwhite(*arg + 1);
4158 end_leader = *arg;
4159
4160 switch (**arg)
4161 {
4162 /*
4163 * Number constant.
4164 */
4165 case '0':
4166 case '1':
4167 case '2':
4168 case '3':
4169 case '4':
4170 case '5':
4171 case '6':
4172 case '7':
4173 case '8':
4174 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004175 {
4176#ifdef FEAT_FLOAT
4177 char_u *p = skipdigits(*arg + 1);
4178 int get_float = FALSE;
4179
4180 /* We accept a float when the format matches
4181 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004182 * strict to avoid backwards compatibility problems.
4183 * Don't look for a float after the "." operator, so that
4184 * ":let vers = 1.2.3" doesn't fail. */
4185 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004187 get_float = TRUE;
4188 p = skipdigits(p + 2);
4189 if (*p == 'e' || *p == 'E')
4190 {
4191 ++p;
4192 if (*p == '-' || *p == '+')
4193 ++p;
4194 if (!vim_isdigit(*p))
4195 get_float = FALSE;
4196 else
4197 p = skipdigits(p + 1);
4198 }
4199 if (ASCII_ISALPHA(*p) || *p == '.')
4200 get_float = FALSE;
4201 }
4202 if (get_float)
4203 {
4204 float_T f;
4205
4206 *arg += string2float(*arg, &f);
4207 if (evaluate)
4208 {
4209 rettv->v_type = VAR_FLOAT;
4210 rettv->vval.v_float = f;
4211 }
4212 }
4213 else
4214#endif
4215 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004216 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004217 *arg += len;
4218 if (evaluate)
4219 {
4220 rettv->v_type = VAR_NUMBER;
4221 rettv->vval.v_number = n;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 /*
4228 * String constant: "string".
4229 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 break;
4232
4233 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004234 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004237 break;
4238
4239 /*
4240 * List: [expr, expr]
4241 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 break;
4244
4245 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004246 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004247 * Dictionary: {key: val, key: val}
4248 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004249 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4250 if (ret == NOTDONE)
4251 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 break;
4253
4254 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004255 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004257 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 break;
4259
4260 /*
4261 * Environment variable: $VAR.
4262 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 break;
4265
4266 /*
4267 * Register contents: @r.
4268 */
4269 case '@': ++*arg;
4270 if (evaluate)
4271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004273 rettv->vval.v_string = get_reg_contents(**arg,
4274 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 if (**arg != NUL)
4277 ++*arg;
4278 break;
4279
4280 /*
4281 * nested expression: (expression).
4282 */
4283 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (**arg == ')')
4286 ++*arg;
4287 else if (ret == OK)
4288 {
4289 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 ret = FAIL;
4292 }
4293 break;
4294
Bram Moolenaar8c711452005-01-14 21:53:12 +00004295 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 break;
4297 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004298
4299 if (ret == NOTDONE)
4300 {
4301 /*
4302 * Must be a variable or function name.
4303 * Can also be a curly-braces kind of name: {expr}.
4304 */
4305 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004306 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 if (alias != NULL)
4308 s = alias;
4309
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004310 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 ret = FAIL;
4312 else
4313 {
4314 if (**arg == '(') /* recursive! */
4315 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004316 partial_T *partial;
4317
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004318 if (!evaluate)
4319 check_vars(s, len);
4320
Bram Moolenaar8c711452005-01-14 21:53:12 +00004321 /* If "s" is the name of a variable of type VAR_FUNC
4322 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004323 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004325 /* Need to make a copy, in case evaluating the arguments makes
4326 * the name invalid. */
4327 s = vim_strsave(s);
4328 if (s == NULL)
4329 ret = FAIL;
4330 else
4331 /* Invoke the function. */
4332 ret = get_func_tv(s, len, rettv, arg,
4333 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4334 &len, evaluate, partial, NULL);
4335 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004336
4337 /* If evaluate is FALSE rettv->v_type was not set in
4338 * get_func_tv, but it's needed in handle_subscript() to parse
4339 * what follows. So set it here. */
4340 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4341 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004342 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004343 rettv->v_type = VAR_FUNC;
4344 }
4345
Bram Moolenaar8c711452005-01-14 21:53:12 +00004346 /* Stop the expression evaluation when immediately
4347 * aborting on error, or when an interrupt occurred or
4348 * an exception was thrown but not caught. */
4349 if (aborting())
4350 {
4351 if (ret == OK)
4352 clear_tv(rettv);
4353 ret = FAIL;
4354 }
4355 }
4356 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004357 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004358 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004359 {
4360 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004361 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004362 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004363 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004364 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004365 }
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 *arg = skipwhite(*arg);
4368
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004369 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4370 * expr(expr). */
4371 if (ret == OK)
4372 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /*
4375 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4376 */
4377 if (ret == OK && evaluate && end_leader > start_leader)
4378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004379 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004380 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381#ifdef FEAT_FLOAT
4382 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004384 if (rettv->v_type == VAR_FLOAT)
4385 f = rettv->vval.v_float;
4386 else
4387#endif
4388 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 clear_tv(rettv);
4392 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004394 else
4395 {
4396 while (end_leader > start_leader)
4397 {
4398 --end_leader;
4399 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004400 {
4401#ifdef FEAT_FLOAT
4402 if (rettv->v_type == VAR_FLOAT)
4403 f = !f;
4404 else
4405#endif
4406 val = !val;
4407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004408 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004409 {
4410#ifdef FEAT_FLOAT
4411 if (rettv->v_type == VAR_FLOAT)
4412 f = -f;
4413 else
4414#endif
4415 val = -val;
4416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004418#ifdef FEAT_FLOAT
4419 if (rettv->v_type == VAR_FLOAT)
4420 {
4421 clear_tv(rettv);
4422 rettv->vval.v_float = f;
4423 }
4424 else
4425#endif
4426 {
4427 clear_tv(rettv);
4428 rettv->v_type = VAR_NUMBER;
4429 rettv->vval.v_number = val;
4430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433
4434 return ret;
4435}
4436
4437/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004438 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4439 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4441 */
4442 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004443eval_index(
4444 char_u **arg,
4445 typval_T *rettv,
4446 int evaluate,
4447 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448{
4449 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004450 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 long len = -1;
4453 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004454 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456
Bram Moolenaara03f2332016-02-06 18:09:59 +01004457 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004459 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004460 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004461 if (verbose)
4462 EMSG(_("E695: Cannot index a Funcref"));
4463 return FAIL;
4464 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004465#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004466 if (verbose)
4467 EMSG(_(e_float_as_string));
4468 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004469#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004470 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004471 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004472 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004473 if (verbose)
4474 EMSG(_("E909: Cannot index a special variable"));
4475 return FAIL;
4476 case VAR_UNKNOWN:
4477 if (evaluate)
4478 return FAIL;
4479 /* FALLTHROUGH */
4480
4481 case VAR_STRING:
4482 case VAR_NUMBER:
4483 case VAR_LIST:
4484 case VAR_DICT:
4485 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004486 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004488 init_tv(&var1);
4489 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492 /*
4493 * dict.name
4494 */
4495 key = *arg + 1;
4496 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4497 ;
4498 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501 }
4502 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 /*
4505 * something[idx]
4506 *
4507 * Get the (first) variable from inside the [].
4508 */
4509 *arg = skipwhite(*arg + 1);
4510 if (**arg == ':')
4511 empty1 = TRUE;
4512 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4513 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4515 {
4516 /* not a number or string */
4517 clear_tv(&var1);
4518 return FAIL;
4519 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520
4521 /*
4522 * Get the second variable from inside the [:].
4523 */
4524 if (**arg == ':')
4525 {
4526 range = TRUE;
4527 *arg = skipwhite(*arg + 1);
4528 if (**arg == ']')
4529 empty2 = TRUE;
4530 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004532 if (!empty1)
4533 clear_tv(&var1);
4534 return FAIL;
4535 }
4536 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4537 {
4538 /* not a number or string */
4539 if (!empty1)
4540 clear_tv(&var1);
4541 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 return FAIL;
4543 }
4544 }
4545
4546 /* Check for the ']'. */
4547 if (**arg != ']')
4548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004549 if (verbose)
4550 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 clear_tv(&var1);
4552 if (range)
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 }
4558
4559 if (evaluate)
4560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 n1 = 0;
4562 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 n1 = get_tv_number(&var1);
4565 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 }
4567 if (range)
4568 {
4569 if (empty2)
4570 n2 = -1;
4571 else
4572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 n2 = get_tv_number(&var2);
4574 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 }
4576 }
4577
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004580 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004581 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004582 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004583 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004584 case VAR_SPECIAL:
4585 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004586 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004587 break; /* not evaluating, skipping over subscript */
4588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 case VAR_NUMBER:
4590 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 len = (long)STRLEN(s);
4593 if (range)
4594 {
4595 /* The resulting variable is a substring. If the indexes
4596 * are out of range the result is empty. */
4597 if (n1 < 0)
4598 {
4599 n1 = len + n1;
4600 if (n1 < 0)
4601 n1 = 0;
4602 }
4603 if (n2 < 0)
4604 n2 = len + n2;
4605 else if (n2 >= len)
4606 n2 = len;
4607 if (n1 >= len || n2 < 0 || n1 > n2)
4608 s = NULL;
4609 else
4610 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4611 }
4612 else
4613 {
4614 /* The resulting variable is a string of a single
4615 * character. If the index is too big or negative the
4616 * result is empty. */
4617 if (n1 >= len || n1 < 0)
4618 s = NULL;
4619 else
4620 s = vim_strnsave(s + n1, 1);
4621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 clear_tv(rettv);
4623 rettv->v_type = VAR_STRING;
4624 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 break;
4626
4627 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 if (n1 < 0)
4630 n1 = len + n1;
4631 if (!empty1 && (n1 < 0 || n1 >= len))
4632 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004633 /* For a range we allow invalid values and return an empty
4634 * list. A list index out of range is an error. */
4635 if (!range)
4636 {
4637 if (verbose)
4638 EMSGN(_(e_listidx), n1);
4639 return FAIL;
4640 }
4641 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 }
4643 if (range)
4644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004645 list_T *l;
4646 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647
4648 if (n2 < 0)
4649 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004650 else if (n2 >= len)
4651 n2 = len - 1;
4652 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004653 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 l = list_alloc();
4655 if (l == NULL)
4656 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 n1 <= n2; ++n1)
4659 {
4660 if (list_append_tv(l, &item->li_tv) == FAIL)
4661 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004662 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004663 return FAIL;
4664 }
4665 item = item->li_next;
4666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004668 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 }
4670 else
4671 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004672 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673 clear_tv(rettv);
4674 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 }
4676 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677
4678 case VAR_DICT:
4679 if (range)
4680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004681 if (verbose)
4682 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 if (len == -1)
4684 clear_tv(&var1);
4685 return FAIL;
4686 }
4687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004688 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689
4690 if (len == -1)
4691 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004692 key = get_tv_string_chk(&var1);
4693 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695 clear_tv(&var1);
4696 return FAIL;
4697 }
4698 }
4699
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004700 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004702 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004703 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 if (len == -1)
4705 clear_tv(&var1);
4706 if (item == NULL)
4707 return FAIL;
4708
4709 copy_tv(&item->di_tv, &var1);
4710 clear_tv(rettv);
4711 *rettv = var1;
4712 }
4713 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 }
4715 }
4716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 return OK;
4718}
4719
4720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 * Get an option value.
4722 * "arg" points to the '&' or '+' before the option name.
4723 * "arg" is advanced to character after the option name.
4724 * Return OK or FAIL.
4725 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004727get_option_tv(
4728 char_u **arg,
4729 typval_T *rettv, /* when NULL, only check if option exists */
4730 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 char_u *option_end;
4733 long numval;
4734 char_u *stringval;
4735 int opt_type;
4736 int c;
4737 int working = (**arg == '+'); /* has("+option") */
4738 int ret = OK;
4739 int opt_flags;
4740
4741 /*
4742 * Isolate the option name and find its value.
4743 */
4744 option_end = find_option_end(arg, &opt_flags);
4745 if (option_end == NULL)
4746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004747 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 EMSG2(_("E112: Option name missing: %s"), *arg);
4749 return FAIL;
4750 }
4751
4752 if (!evaluate)
4753 {
4754 *arg = option_end;
4755 return OK;
4756 }
4757
4758 c = *option_end;
4759 *option_end = NUL;
4760 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 if (opt_type == -3) /* invalid name */
4764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 EMSG2(_("E113: Unknown option: %s"), *arg);
4767 ret = FAIL;
4768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 if (opt_type == -2) /* hidden string option */
4772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 rettv->v_type = VAR_STRING;
4774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 else if (opt_type == -1) /* hidden number option */
4777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 rettv->v_type = VAR_NUMBER;
4779 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 else if (opt_type == 1) /* number option */
4782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 rettv->v_type = VAR_NUMBER;
4784 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786 else /* string option */
4787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 rettv->v_type = VAR_STRING;
4789 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 }
4792 else if (working && (opt_type == -2 || opt_type == -1))
4793 ret = FAIL;
4794
4795 *option_end = c; /* put back for error messages */
4796 *arg = option_end;
4797
4798 return ret;
4799}
4800
4801/*
4802 * Allocate a variable for a string constant.
4803 * Return OK or FAIL.
4804 */
4805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004806get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 char_u *p;
4809 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 int extra = 0;
4811
4812 /*
4813 * Find the end of the string, skipping backslashed characters.
4814 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004815 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 if (*p == '\\' && p[1] != NUL)
4818 {
4819 ++p;
4820 /* A "\<x>" form occupies at least 4 characters, and produces up
4821 * to 6 characters: reserve space for 2 extra */
4822 if (*p == '<')
4823 extra += 2;
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826
4827 if (*p != '"')
4828 {
4829 EMSG2(_("E114: Missing quote: %s"), *arg);
4830 return FAIL;
4831 }
4832
4833 /* If only parsing, set *arg and return here */
4834 if (!evaluate)
4835 {
4836 *arg = p + 1;
4837 return OK;
4838 }
4839
4840 /*
4841 * Copy the string into allocated memory, handling backslashed
4842 * characters.
4843 */
4844 name = alloc((unsigned)(p - *arg + extra));
4845 if (name == NULL)
4846 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 rettv->v_type = VAR_STRING;
4848 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar8c711452005-01-14 21:53:12 +00004850 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
4852 if (*p == '\\')
4853 {
4854 switch (*++p)
4855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004856 case 'b': *name++ = BS; ++p; break;
4857 case 'e': *name++ = ESC; ++p; break;
4858 case 'f': *name++ = FF; ++p; break;
4859 case 'n': *name++ = NL; ++p; break;
4860 case 'r': *name++ = CAR; ++p; break;
4861 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
4863 case 'X': /* hex: "\x1", "\x12" */
4864 case 'x':
4865 case 'u': /* Unicode: "\u0023" */
4866 case 'U':
4867 if (vim_isxdigit(p[1]))
4868 {
4869 int n, nr;
4870 int c = toupper(*p);
4871
4872 if (c == 'X')
4873 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004874 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004876 else
4877 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 nr = 0;
4879 while (--n >= 0 && vim_isxdigit(p[1]))
4880 {
4881 ++p;
4882 nr = (nr << 4) + hex2nr(*p);
4883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004884 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#ifdef FEAT_MBYTE
4886 /* For "\u" store the number according to
4887 * 'encoding'. */
4888 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 else
4891#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 break;
4895
4896 /* octal: "\1", "\12", "\123" */
4897 case '0':
4898 case '1':
4899 case '2':
4900 case '3':
4901 case '4':
4902 case '5':
4903 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 case '7': *name = *p++ - '0';
4905 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004907 *name = (*name << 3) + *p++ - '0';
4908 if (*p >= '0' && *p <= '7')
4909 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004911 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 break;
4913
4914 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004915 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (extra != 0)
4917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 break;
4920 }
4921 /* FALLTHROUGH */
4922
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925 }
4926 }
4927 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004931 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004932 if (*p != NUL) /* just in case */
4933 ++p;
4934 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 return OK;
4937}
4938
4939/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 * Return OK or FAIL.
4942 */
4943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004944get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004947 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004948 int reduce = 0;
4949
4950 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004952 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004953 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004958 break;
4959 ++reduce;
4960 ++p;
4961 }
4962 }
4963
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004967 return FAIL;
4968 }
4969
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004971 if (!evaluate)
4972 {
4973 *arg = p + 1;
4974 return OK;
4975 }
4976
4977 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004979 */
4980 str = alloc((unsigned)((p - *arg) - reduce));
4981 if (str == NULL)
4982 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 rettv->v_type = VAR_STRING;
4984 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004985
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004991 break;
4992 ++p;
4993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004997 *arg = p + 1;
4998
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 return OK;
5000}
5001
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005002/*
5003 * Return the function name of the partial.
5004 */
5005 char_u *
5006partial_name(partial_T *pt)
5007{
5008 if (pt->pt_name != NULL)
5009 return pt->pt_name;
5010 return pt->pt_func->uf_name;
5011}
5012
Bram Moolenaarddecc252016-04-06 22:59:37 +02005013 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005014partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005015{
5016 int i;
5017
5018 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005019 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005020 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005021 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005022 if (pt->pt_name != NULL)
5023 {
5024 func_unref(pt->pt_name);
5025 vim_free(pt->pt_name);
5026 }
5027 else
5028 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005029 vim_free(pt);
5030}
5031
5032/*
5033 * Unreference a closure: decrement the reference count and free it when it
5034 * becomes zero.
5035 */
5036 void
5037partial_unref(partial_T *pt)
5038{
5039 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005040 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005041}
5042
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005043static int tv_equal_recurse_limit;
5044
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005045 static int
5046func_equal(
5047 typval_T *tv1,
5048 typval_T *tv2,
5049 int ic) /* ignore case */
5050{
5051 char_u *s1, *s2;
5052 dict_T *d1, *d2;
5053 int a1, a2;
5054 int i;
5055
5056 /* empty and NULL function name considered the same */
5057 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005058 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005059 if (s1 != NULL && *s1 == NUL)
5060 s1 = NULL;
5061 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005062 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005063 if (s2 != NULL && *s2 == NUL)
5064 s2 = NULL;
5065 if (s1 == NULL || s2 == NULL)
5066 {
5067 if (s1 != s2)
5068 return FALSE;
5069 }
5070 else if (STRCMP(s1, s2) != 0)
5071 return FALSE;
5072
5073 /* empty dict and NULL dict is different */
5074 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5075 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5076 if (d1 == NULL || d2 == NULL)
5077 {
5078 if (d1 != d2)
5079 return FALSE;
5080 }
5081 else if (!dict_equal(d1, d2, ic, TRUE))
5082 return FALSE;
5083
5084 /* empty list and no list considered the same */
5085 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5086 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5087 if (a1 != a2)
5088 return FALSE;
5089 for (i = 0; i < a1; ++i)
5090 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5091 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5092 return FALSE;
5093
5094 return TRUE;
5095}
5096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005097/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005098 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005099 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005101 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005103tv_equal(
5104 typval_T *tv1,
5105 typval_T *tv2,
5106 int ic, /* ignore case */
5107 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005108{
5109 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005110 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005111 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005112 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005113
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005114 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005115 * recursiveness to a limit. We guess they are equal then.
5116 * A fixed limit has the problem of still taking an awful long time.
5117 * Reduce the limit every time running into it. That should work fine for
5118 * deeply linked structures that are not recursively linked and catch
5119 * recursiveness quickly. */
5120 if (!recursive)
5121 tv_equal_recurse_limit = 1000;
5122 if (recursive_cnt >= tv_equal_recurse_limit)
5123 {
5124 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005125 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005126 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005127
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005128 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5129 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005130 if ((tv1->v_type == VAR_FUNC
5131 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5132 && (tv2->v_type == VAR_FUNC
5133 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5134 {
5135 ++recursive_cnt;
5136 r = func_equal(tv1, tv2, ic);
5137 --recursive_cnt;
5138 return r;
5139 }
5140
5141 if (tv1->v_type != tv2->v_type)
5142 return FALSE;
5143
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005144 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005145 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005146 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005147 ++recursive_cnt;
5148 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5149 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005150 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005151
5152 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005153 ++recursive_cnt;
5154 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5155 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005156 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005157
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005158 case VAR_NUMBER:
5159 return tv1->vval.v_number == tv2->vval.v_number;
5160
5161 case VAR_STRING:
5162 s1 = get_tv_string_buf(tv1, buf1);
5163 s2 = get_tv_string_buf(tv2, buf2);
5164 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005165
5166 case VAR_SPECIAL:
5167 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005168
5169 case VAR_FLOAT:
5170#ifdef FEAT_FLOAT
5171 return tv1->vval.v_float == tv2->vval.v_float;
5172#endif
5173 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005174#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005175 return tv1->vval.v_job == tv2->vval.v_job;
5176#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005177 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005178#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005179 return tv1->vval.v_channel == tv2->vval.v_channel;
5180#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005181 case VAR_FUNC:
5182 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005183 case VAR_UNKNOWN:
5184 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005185 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005186
Bram Moolenaara03f2332016-02-06 18:09:59 +01005187 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5188 * does not equal anything, not even itself. */
5189 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005190}
5191
5192/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005193 * Return the next (unique) copy ID.
5194 * Used for serializing nested structures.
5195 */
5196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005197get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005198{
5199 current_copyID += COPYID_INC;
5200 return current_copyID;
5201}
5202
5203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005204 * Garbage collection for lists and dictionaries.
5205 *
5206 * We use reference counts to be able to free most items right away when they
5207 * are no longer used. But for composite items it's possible that it becomes
5208 * unused while the reference count is > 0: When there is a recursive
5209 * reference. Example:
5210 * :let l = [1, 2, 3]
5211 * :let d = {9: l}
5212 * :let l[1] = d
5213 *
5214 * Since this is quite unusual we handle this with garbage collection: every
5215 * once in a while find out which lists and dicts are not referenced from any
5216 * variable.
5217 *
5218 * Here is a good reference text about garbage collection (refers to Python
5219 * but it applies to all reference-counting mechanisms):
5220 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005221 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005222
5223/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005224 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005225 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005227 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005229garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005230{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005231 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005232 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005233 buf_T *buf;
5234 win_T *wp;
5235 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005236 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005237#ifdef FEAT_WINDOWS
5238 tabpage_T *tp;
5239#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005240
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005241 if (!testing)
5242 {
5243 /* Only do this once. */
5244 want_garbage_collect = FALSE;
5245 may_garbage_collect = FALSE;
5246 garbage_collect_at_exit = FALSE;
5247 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005248
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005249 /* We advance by two because we add one for items referenced through
5250 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005251 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005252
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005253 /*
5254 * 1. Go through all accessible variables and mark all lists and dicts
5255 * with copyID.
5256 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005257
5258 /* Don't free variables in the previous_funccal list unless they are only
5259 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005260 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005261 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005262
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005263 /* script-local variables */
5264 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005266
5267 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005268 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005269 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5270 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005271
5272 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005273 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005274 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5275 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005276#ifdef FEAT_AUTOCMD
5277 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5279 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005280#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005281
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005282#ifdef FEAT_WINDOWS
5283 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005284 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5286 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005287#endif
5288
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005289 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005291
5292 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005294
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005295 /* named functions (matters for closures) */
5296 abort = abort || set_ref_in_functions(copyID);
5297
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005298 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005300
Bram Moolenaard812df62008-11-09 12:46:09 +00005301 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005303
Bram Moolenaar1dced572012-04-05 16:54:08 +02005304#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005306#endif
5307
Bram Moolenaardb913952012-06-29 12:54:53 +02005308#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005309 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005310#endif
5311
5312#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005313 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005314#endif
5315
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005316#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005317 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005318 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005319#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005320#ifdef FEAT_NETBEANS_INTG
5321 abort = abort || set_ref_in_nb_channel(copyID);
5322#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005323
Bram Moolenaare3188e22016-05-31 21:13:04 +02005324#ifdef FEAT_TIMERS
5325 abort = abort || set_ref_in_timer(copyID);
5326#endif
5327
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005328#ifdef FEAT_QUICKFIX
5329 abort = abort || set_ref_in_quickfix(copyID);
5330#endif
5331
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005332#ifdef FEAT_TERMINAL
5333 abort = abort || set_ref_in_term(copyID);
5334#endif
5335
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005336 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005337 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005338 /*
5339 * 2. Free lists and dictionaries that are not referenced.
5340 */
5341 did_free = free_unref_items(copyID);
5342
5343 /*
5344 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005345 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005346 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005347 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005348 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005349 else if (p_verbose > 0)
5350 {
5351 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5352 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005353
5354 return did_free;
5355}
5356
5357/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005358 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005359 */
5360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005361free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005362{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005363 int did_free = FALSE;
5364
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365 /* Let all "free" functions know that we are here. This means no
5366 * dictionaries, lists, channels or jobs are to be freed, because we will
5367 * do that here. */
5368 in_free_unref_items = TRUE;
5369
5370 /*
5371 * PASS 1: free the contents of the items. We don't free the items
5372 * themselves yet, so that it is possible to decrement refcount counters
5373 */
5374
Bram Moolenaarcd524592016-07-17 14:57:05 +02005375 /* Go through the list of dicts and free items without the copyID. */
5376 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005377
Bram Moolenaarda861d62016-07-17 15:46:27 +02005378 /* Go through the list of lists and free items without the copyID. */
5379 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380
5381#ifdef FEAT_JOB_CHANNEL
5382 /* Go through the list of jobs and free items without the copyID. This
5383 * must happen before doing channels, because jobs refer to channels, but
5384 * the reference from the channel to the job isn't tracked. */
5385 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5386
5387 /* Go through the list of channels and free items without the copyID. */
5388 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5389#endif
5390
5391 /*
5392 * PASS 2: free the items themselves.
5393 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005394 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005395 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005396
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005397#ifdef FEAT_JOB_CHANNEL
5398 /* Go through the list of jobs and free items without the copyID. This
5399 * must happen before doing channels, because jobs refer to channels, but
5400 * the reference from the channel to the job isn't tracked. */
5401 free_unused_jobs(copyID, COPYID_MASK);
5402
5403 /* Go through the list of channels and free items without the copyID. */
5404 free_unused_channels(copyID, COPYID_MASK);
5405#endif
5406
5407 in_free_unref_items = FALSE;
5408
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005409 return did_free;
5410}
5411
5412/*
5413 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005414 * "list_stack" is used to add lists to be marked. Can be NULL.
5415 *
5416 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005417 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420{
5421 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 hashtab_T *cur_ht;
5425 ht_stack_T *ht_stack = NULL;
5426 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005427
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005428 cur_ht = ht;
5429 for (;;)
5430 {
5431 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 /* Mark each item in the hashtab. If the item contains a hashtab
5434 * it is added to ht_stack, if it contains a list it is added to
5435 * list_stack. */
5436 todo = (int)cur_ht->ht_used;
5437 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5438 if (!HASHITEM_EMPTY(hi))
5439 {
5440 --todo;
5441 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5442 &ht_stack, list_stack);
5443 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005444 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005445
5446 if (ht_stack == NULL)
5447 break;
5448
5449 /* take an item from the stack */
5450 cur_ht = ht_stack->ht;
5451 tempitem = ht_stack;
5452 ht_stack = ht_stack->prev;
5453 free(tempitem);
5454 }
5455
5456 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005457}
5458
5459/*
5460 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005461 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5462 *
5463 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005466set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005467{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005468 listitem_T *li;
5469 int abort = FALSE;
5470 list_T *cur_l;
5471 list_stack_T *list_stack = NULL;
5472 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005473
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005474 cur_l = l;
5475 for (;;)
5476 {
5477 if (!abort)
5478 /* Mark each item in the list. If the item contains a hashtab
5479 * it is added to ht_stack, if it contains a list it is added to
5480 * list_stack. */
5481 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5482 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5483 ht_stack, &list_stack);
5484 if (list_stack == NULL)
5485 break;
5486
5487 /* take an item from the stack */
5488 cur_l = list_stack->list;
5489 tempitem = list_stack;
5490 list_stack = list_stack->prev;
5491 free(tempitem);
5492 }
5493
5494 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005495}
5496
5497/*
5498 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005499 * "list_stack" is used to add lists to be marked. Can be NULL.
5500 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5501 *
5502 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005503 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005505set_ref_in_item(
5506 typval_T *tv,
5507 int copyID,
5508 ht_stack_T **ht_stack,
5509 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005512
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005513 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005514 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005515 dict_T *dd = tv->vval.v_dict;
5516
Bram Moolenaara03f2332016-02-06 18:09:59 +01005517 if (dd != NULL && dd->dv_copyID != copyID)
5518 {
5519 /* Didn't see this dict yet. */
5520 dd->dv_copyID = copyID;
5521 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005522 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005523 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5524 }
5525 else
5526 {
5527 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5528 if (newitem == NULL)
5529 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 else
5531 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005532 newitem->ht = &dd->dv_hashtab;
5533 newitem->prev = *ht_stack;
5534 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005535 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005536 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005537 }
5538 }
5539 else if (tv->v_type == VAR_LIST)
5540 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005541 list_T *ll = tv->vval.v_list;
5542
Bram Moolenaara03f2332016-02-06 18:09:59 +01005543 if (ll != NULL && ll->lv_copyID != copyID)
5544 {
5545 /* Didn't see this list yet. */
5546 ll->lv_copyID = copyID;
5547 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005548 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005549 abort = set_ref_in_list(ll, copyID, ht_stack);
5550 }
5551 else
5552 {
5553 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005554 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005555 if (newitem == NULL)
5556 abort = TRUE;
5557 else
5558 {
5559 newitem->list = ll;
5560 newitem->prev = *list_stack;
5561 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005562 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005563 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005564 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005565 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005566 else if (tv->v_type == VAR_FUNC)
5567 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005568 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005569 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005570 else if (tv->v_type == VAR_PARTIAL)
5571 {
5572 partial_T *pt = tv->vval.v_partial;
5573 int i;
5574
5575 /* A partial does not have a copyID, because it cannot contain itself.
5576 */
5577 if (pt != NULL)
5578 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005579 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005580
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005581 if (pt->pt_dict != NULL)
5582 {
5583 typval_T dtv;
5584
5585 dtv.v_type = VAR_DICT;
5586 dtv.vval.v_dict = pt->pt_dict;
5587 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5588 }
5589
5590 for (i = 0; i < pt->pt_argc; ++i)
5591 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5592 ht_stack, list_stack);
5593 }
5594 }
5595#ifdef FEAT_JOB_CHANNEL
5596 else if (tv->v_type == VAR_JOB)
5597 {
5598 job_T *job = tv->vval.v_job;
5599 typval_T dtv;
5600
5601 if (job != NULL && job->jv_copyID != copyID)
5602 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005603 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005604 if (job->jv_channel != NULL)
5605 {
5606 dtv.v_type = VAR_CHANNEL;
5607 dtv.vval.v_channel = job->jv_channel;
5608 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5609 }
5610 if (job->jv_exit_partial != NULL)
5611 {
5612 dtv.v_type = VAR_PARTIAL;
5613 dtv.vval.v_partial = job->jv_exit_partial;
5614 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5615 }
5616 }
5617 }
5618 else if (tv->v_type == VAR_CHANNEL)
5619 {
5620 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005621 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005622 typval_T dtv;
5623 jsonq_T *jq;
5624 cbq_T *cq;
5625
5626 if (ch != NULL && ch->ch_copyID != copyID)
5627 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005628 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005629 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005630 {
5631 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5632 jq = jq->jq_next)
5633 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5634 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5635 cq = cq->cq_next)
5636 if (cq->cq_partial != NULL)
5637 {
5638 dtv.v_type = VAR_PARTIAL;
5639 dtv.vval.v_partial = cq->cq_partial;
5640 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5641 }
5642 if (ch->ch_part[part].ch_partial != NULL)
5643 {
5644 dtv.v_type = VAR_PARTIAL;
5645 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5646 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5647 }
5648 }
5649 if (ch->ch_partial != NULL)
5650 {
5651 dtv.v_type = VAR_PARTIAL;
5652 dtv.vval.v_partial = ch->ch_partial;
5653 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5654 }
5655 if (ch->ch_close_partial != NULL)
5656 {
5657 dtv.v_type = VAR_PARTIAL;
5658 dtv.vval.v_partial = ch->ch_close_partial;
5659 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5660 }
5661 }
5662 }
5663#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005664 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005665}
5666
Bram Moolenaar17a13432016-01-24 14:22:10 +01005667 static char *
5668get_var_special_name(int nr)
5669{
5670 switch (nr)
5671 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005672 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005673 case VVAL_TRUE: return "v:true";
5674 case VVAL_NONE: return "v:none";
5675 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005676 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005677 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005678 return "42";
5679}
5680
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 * Return a string with the string representation of a variable.
5683 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005684 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005685 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005686 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5687 * stings as "string()", otherwise does not put quotes around strings, as
5688 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005689 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5690 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005691 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005693 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005694echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005695 typval_T *tv,
5696 char_u **tofree,
5697 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005698 int copyID,
5699 int echo_style,
5700 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005701 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005703 static int recurse = 0;
5704 char_u *r = NULL;
5705
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005707 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005708 if (!did_echo_string_emsg)
5709 {
5710 /* Only give this message once for a recursive call to avoid
5711 * flooding the user with errors. And stop iterating over lists
5712 * and dicts. */
5713 did_echo_string_emsg = TRUE;
5714 EMSG(_("E724: variable nested too deep for displaying"));
5715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005716 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005717 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 }
5719 ++recurse;
5720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 switch (tv->v_type)
5722 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005723 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005724 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005725 {
5726 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005727 r = tv->vval.v_string;
5728 if (r == NULL)
5729 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005730 }
5731 else
5732 {
5733 *tofree = string_quote(tv->vval.v_string, FALSE);
5734 r = *tofree;
5735 }
5736 break;
5737
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005739 if (echo_style)
5740 {
5741 *tofree = NULL;
5742 r = tv->vval.v_string;
5743 }
5744 else
5745 {
5746 *tofree = string_quote(tv->vval.v_string, TRUE);
5747 r = *tofree;
5748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005749 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005750
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005751 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005752 {
5753 partial_T *pt = tv->vval.v_partial;
5754 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005755 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005756 garray_T ga;
5757 int i;
5758 char_u *tf;
5759
5760 ga_init2(&ga, 1, 100);
5761 ga_concat(&ga, (char_u *)"function(");
5762 if (fname != NULL)
5763 {
5764 ga_concat(&ga, fname);
5765 vim_free(fname);
5766 }
5767 if (pt != NULL && pt->pt_argc > 0)
5768 {
5769 ga_concat(&ga, (char_u *)", [");
5770 for (i = 0; i < pt->pt_argc; ++i)
5771 {
5772 if (i > 0)
5773 ga_concat(&ga, (char_u *)", ");
5774 ga_concat(&ga,
5775 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5776 vim_free(tf);
5777 }
5778 ga_concat(&ga, (char_u *)"]");
5779 }
5780 if (pt != NULL && pt->pt_dict != NULL)
5781 {
5782 typval_T dtv;
5783
5784 ga_concat(&ga, (char_u *)", ");
5785 dtv.v_type = VAR_DICT;
5786 dtv.vval.v_dict = pt->pt_dict;
5787 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5788 vim_free(tf);
5789 }
5790 ga_concat(&ga, (char_u *)")");
5791
5792 *tofree = ga.ga_data;
5793 r = *tofree;
5794 break;
5795 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005796
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005798 if (tv->vval.v_list == NULL)
5799 {
5800 *tofree = NULL;
5801 r = NULL;
5802 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005803 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5804 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005805 {
5806 *tofree = NULL;
5807 r = (char_u *)"[...]";
5808 }
5809 else
5810 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005811 int old_copyID = tv->vval.v_list->lv_copyID;
5812
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005813 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005814 *tofree = list2string(tv, copyID, restore_copyID);
5815 if (restore_copyID)
5816 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005817 r = *tofree;
5818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005819 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005820
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005822 if (tv->vval.v_dict == NULL)
5823 {
5824 *tofree = NULL;
5825 r = NULL;
5826 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005827 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5828 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005829 {
5830 *tofree = NULL;
5831 r = (char_u *)"{...}";
5832 }
5833 else
5834 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005835 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005836 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005837 *tofree = dict2string(tv, copyID, restore_copyID);
5838 if (restore_copyID)
5839 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005840 r = *tofree;
5841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005842 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005844 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005845 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005846 *tofree = NULL;
5847 r = get_tv_string_buf(tv, numbuf);
5848 break;
5849
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005851 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005852 *tofree = NULL;
5853 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005854 if (composite_val)
5855 {
5856 *tofree = string_quote(r, FALSE);
5857 r = *tofree;
5858 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005861 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005862#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005863 *tofree = NULL;
5864 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5865 r = numbuf;
5866 break;
5867#endif
5868
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005869 case VAR_SPECIAL:
5870 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005871 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005872 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874
Bram Moolenaar8502c702014-06-17 12:51:16 +02005875 if (--recurse == 0)
5876 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878}
5879
5880/*
5881 * Return a string with the string representation of a variable.
5882 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5883 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005884 * Does not put quotes around strings, as ":echo" displays values.
5885 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5886 * May return NULL.
5887 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005888 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005889echo_string(
5890 typval_T *tv,
5891 char_u **tofree,
5892 char_u *numbuf,
5893 int copyID)
5894{
5895 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5896}
5897
5898/*
5899 * Return a string with the string representation of a variable.
5900 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5901 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005903 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005905 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005906tv2string(
5907 typval_T *tv,
5908 char_u **tofree,
5909 char_u *numbuf,
5910 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005912 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913}
5914
5915/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 * Return string "str" in ' quotes, doubling ' characters.
5917 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005920 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922{
Bram Moolenaar33570922005-01-25 22:26:29 +00005923 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 char_u *p, *r, *s;
5925
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 len = (function ? 13 : 3);
5927 if (str != NULL)
5928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005929 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005930 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 if (*p == '\'')
5932 ++len;
5933 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 s = r = alloc(len);
5935 if (r != NULL)
5936 {
5937 if (function)
5938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 r += 10;
5941 }
5942 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 if (str != NULL)
5945 for (p = str; *p != NUL; )
5946 {
5947 if (*p == '\'')
5948 *r++ = '\'';
5949 MB_COPY_CHAR(p, r);
5950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 if (function)
5953 *r++ = ')';
5954 *r++ = NUL;
5955 }
5956 return s;
5957}
5958
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005959#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005960/*
5961 * Convert the string "text" to a floating point number.
5962 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5963 * this always uses a decimal point.
5964 * Returns the length of the text that was consumed.
5965 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967string2float(
5968 char_u *text,
5969 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970{
5971 char *s = (char *)text;
5972 float_T f;
5973
Bram Moolenaar62473612017-01-08 19:25:40 +01005974 /* MS-Windows does not deal with "inf" and "nan" properly. */
5975 if (STRNICMP(text, "inf", 3) == 0)
5976 {
5977 *value = INFINITY;
5978 return 3;
5979 }
5980 if (STRNICMP(text, "-inf", 3) == 0)
5981 {
5982 *value = -INFINITY;
5983 return 4;
5984 }
5985 if (STRNICMP(text, "nan", 3) == 0)
5986 {
5987 *value = NAN;
5988 return 3;
5989 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005990 f = strtod(s, &s);
5991 *value = f;
5992 return (int)((char_u *)s - text);
5993}
5994#endif
5995
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * Get the value of an environment variable.
5998 * "arg" is pointing to the '$'. It is advanced to after the name.
5999 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006000 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 */
6002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 char_u *string = NULL;
6006 int len;
6007 int cc;
6008 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006009 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
6011 ++*arg;
6012 name = *arg;
6013 len = get_env_len(arg);
6014 if (evaluate)
6015 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006016 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006017 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006018
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006019 cc = name[len];
6020 name[len] = NUL;
6021 /* first try vim_getenv(), fast for normal environment vars */
6022 string = vim_getenv(name, &mustfree);
6023 if (string != NULL && *string != NUL)
6024 {
6025 if (!mustfree)
6026 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006028 else
6029 {
6030 if (mustfree)
6031 vim_free(string);
6032
6033 /* next try expanding things like $VIM and ${HOME} */
6034 string = expand_env_save(name - 1);
6035 if (string != NULL && *string == '$')
6036 {
6037 vim_free(string);
6038 string = NULL;
6039 }
6040 }
6041 name[len] = cc;
6042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006043 rettv->v_type = VAR_STRING;
6044 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046
6047 return OK;
6048}
6049
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006050
6051
6052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006054 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057var2fpos(
6058 typval_T *varp,
6059 int dollar_lnum, /* TRUE when $ is last line */
6060 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006062 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006064 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaara5525202006-03-02 22:52:09 +00006066 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006067 if (varp->v_type == VAR_LIST)
6068 {
6069 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006070 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006071 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006072 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006073
6074 l = varp->vval.v_list;
6075 if (l == NULL)
6076 return NULL;
6077
6078 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006079 pos.lnum = list_find_nr(l, 0L, &error);
6080 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006081 return NULL; /* invalid line number */
6082
6083 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006084 pos.col = list_find_nr(l, 1L, &error);
6085 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006086 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006087 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006088
6089 /* We accept "$" for the column number: last column. */
6090 li = list_find(l, 1L);
6091 if (li != NULL && li->li_tv.v_type == VAR_STRING
6092 && li->li_tv.vval.v_string != NULL
6093 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6094 pos.col = len + 1;
6095
Bram Moolenaara5525202006-03-02 22:52:09 +00006096 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006097 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006098 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006099 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006100
Bram Moolenaara5525202006-03-02 22:52:09 +00006101#ifdef FEAT_VIRTUALEDIT
6102 /* Get the virtual offset. Defaults to zero. */
6103 pos.coladd = list_find_nr(l, 2L, &error);
6104 if (error)
6105 pos.coladd = 0;
6106#endif
6107
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006108 return &pos;
6109 }
6110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006111 name = get_tv_string_chk(varp);
6112 if (name == NULL)
6113 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006114 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006116 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6117 {
6118 if (VIsual_active)
6119 return &VIsual;
6120 return &curwin->w_cursor;
6121 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006122 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006124 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6126 return NULL;
6127 return pp;
6128 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006129
6130#ifdef FEAT_VIRTUALEDIT
6131 pos.coladd = 0;
6132#endif
6133
Bram Moolenaar477933c2007-07-17 14:32:23 +00006134 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006135 {
6136 pos.col = 0;
6137 if (name[1] == '0') /* "w0": first visible line */
6138 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006139 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006140 /* In silent Ex mode topline is zero, but that's not a valid line
6141 * number; use one instead. */
6142 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006143 return &pos;
6144 }
6145 else if (name[1] == '$') /* "w$": last visible line */
6146 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006147 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006148 /* In silent Ex mode botline is zero, return zero then. */
6149 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006150 return &pos;
6151 }
6152 }
6153 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006155 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
6157 pos.lnum = curbuf->b_ml.ml_line_count;
6158 pos.col = 0;
6159 }
6160 else
6161 {
6162 pos.lnum = curwin->w_cursor.lnum;
6163 pos.col = (colnr_T)STRLEN(ml_get_curline());
6164 }
6165 return &pos;
6166 }
6167 return NULL;
6168}
6169
6170/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006171 * Convert list in "arg" into a position and optional file number.
6172 * When "fnump" is NULL there is no file number, only 3 items.
6173 * Note that the column is passed on as-is, the caller may want to decrement
6174 * it to use 1 for the first column.
6175 * Return FAIL when conversion is not possible, doesn't check the position for
6176 * validity.
6177 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006178 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006179list2fpos(
6180 typval_T *arg,
6181 pos_T *posp,
6182 int *fnump,
6183 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006184{
6185 list_T *l = arg->vval.v_list;
6186 long i = 0;
6187 long n;
6188
Bram Moolenaar493c1782014-05-28 14:34:46 +02006189 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6190 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006191 if (arg->v_type != VAR_LIST
6192 || l == NULL
6193 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006194 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006195 return FAIL;
6196
6197 if (fnump != NULL)
6198 {
6199 n = list_find_nr(l, i++, NULL); /* fnum */
6200 if (n < 0)
6201 return FAIL;
6202 if (n == 0)
6203 n = curbuf->b_fnum; /* current buffer */
6204 *fnump = n;
6205 }
6206
6207 n = list_find_nr(l, i++, NULL); /* lnum */
6208 if (n < 0)
6209 return FAIL;
6210 posp->lnum = n;
6211
6212 n = list_find_nr(l, i++, NULL); /* col */
6213 if (n < 0)
6214 return FAIL;
6215 posp->col = n;
6216
6217#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006218 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006219 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006220 posp->coladd = 0;
6221 else
6222 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006223#endif
6224
Bram Moolenaar493c1782014-05-28 14:34:46 +02006225 if (curswantp != NULL)
6226 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6227
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006228 return OK;
6229}
6230
6231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 * Get the length of an environment variable name.
6233 * Advance "arg" to the first character after the name.
6234 * Return 0 for error.
6235 */
6236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006237get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 char_u *p;
6240 int len;
6241
6242 for (p = *arg; vim_isIDc(*p); ++p)
6243 ;
6244 if (p == *arg) /* no name found */
6245 return 0;
6246
6247 len = (int)(p - *arg);
6248 *arg = p;
6249 return len;
6250}
6251
6252/*
6253 * Get the length of the name of a function or internal variable.
6254 * "arg" is advanced to the first non-white character after the name.
6255 * Return 0 if something is wrong.
6256 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 char_u *p;
6261 int len;
6262
6263 /* Find the end of the name. */
6264 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006265 {
6266 if (*p == ':')
6267 {
6268 /* "s:" is start of "s:var", but "n:" is not and can be used in
6269 * slice "[n:]". Also "xx:" is not a namespace. */
6270 len = (int)(p - *arg);
6271 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6272 || len > 1)
6273 break;
6274 }
6275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (p == *arg) /* no name found */
6277 return 0;
6278
6279 len = (int)(p - *arg);
6280 *arg = skipwhite(p);
6281
6282 return len;
6283}
6284
6285/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006286 * Get the length of the name of a variable or function.
6287 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006289 * Return -1 if curly braces expansion failed.
6290 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 * If the name contains 'magic' {}'s, expand them and return the
6292 * expanded name in an allocated string via 'alias' - caller must free.
6293 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006295get_name_len(
6296 char_u **arg,
6297 char_u **alias,
6298 int evaluate,
6299 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 char_u *p;
6303 char_u *expr_start;
6304 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 *alias = NULL; /* default to no alias */
6307
6308 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6309 && (*arg)[2] == (int)KE_SNR)
6310 {
6311 /* hard coded <SNR>, already translated */
6312 *arg += 3;
6313 return get_id_len(arg) + 3;
6314 }
6315 len = eval_fname_script(*arg);
6316 if (len > 0)
6317 {
6318 /* literal "<SID>", "s:" or "<SNR>" */
6319 *arg += len;
6320 }
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006323 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006325 p = find_name_end(*arg, &expr_start, &expr_end,
6326 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (expr_start != NULL)
6328 {
6329 char_u *temp_string;
6330
6331 if (!evaluate)
6332 {
6333 len += (int)(p - *arg);
6334 *arg = skipwhite(p);
6335 return len;
6336 }
6337
6338 /*
6339 * Include any <SID> etc in the expanded string:
6340 * Thus the -len here.
6341 */
6342 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6343 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006344 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 *alias = temp_string;
6346 *arg = skipwhite(p);
6347 return (int)STRLEN(temp_string);
6348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006351 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 EMSG2(_(e_invexpr2), *arg);
6353
6354 return len;
6355}
6356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006357/*
6358 * Find the end of a variable or function name, taking care of magic braces.
6359 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6360 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006361 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 * Return a pointer to just after the name. Equal to "arg" if there is no
6363 * valid name.
6364 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006366find_name_end(
6367 char_u *arg,
6368 char_u **expr_start,
6369 char_u **expr_end,
6370 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372 int mb_nest = 0;
6373 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006375 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006377 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006379 *expr_start = NULL;
6380 *expr_end = NULL;
6381 }
6382
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006383 /* Quick check for valid starting character. */
6384 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6385 return arg;
6386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006387 for (p = arg; *p != NUL
6388 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006390 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006392 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006393 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006394 if (*p == '\'')
6395 {
6396 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006397 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006398 ;
6399 if (*p == NUL)
6400 break;
6401 }
6402 else if (*p == '"')
6403 {
6404 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006405 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006406 if (*p == '\\' && p[1] != NUL)
6407 ++p;
6408 if (*p == NUL)
6409 break;
6410 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006411 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6412 {
6413 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006414 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006415 len = (int)(p - arg);
6416 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006417 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006418 break;
6419 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006421 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006423 if (*p == '[')
6424 ++br_nest;
6425 else if (*p == ']')
6426 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006429 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431 if (*p == '{')
6432 {
6433 mb_nest++;
6434 if (expr_start != NULL && *expr_start == NULL)
6435 *expr_start = p;
6436 }
6437 else if (*p == '}')
6438 {
6439 mb_nest--;
6440 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6441 *expr_end = p;
6442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 }
6445
6446 return p;
6447}
6448
6449/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006450 * Expands out the 'magic' {}'s in a variable/function name.
6451 * Note that this can call itself recursively, to deal with
6452 * constructs like foo{bar}{baz}{bam}
6453 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6454 * "in_start" ^
6455 * "expr_start" ^
6456 * "expr_end" ^
6457 * "in_end" ^
6458 *
6459 * Returns a new allocated string, which the caller must free.
6460 * Returns NULL for failure.
6461 */
6462 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463make_expanded_name(
6464 char_u *in_start,
6465 char_u *expr_start,
6466 char_u *expr_end,
6467 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006468{
6469 char_u c1;
6470 char_u *retval = NULL;
6471 char_u *temp_result;
6472 char_u *nextcmd = NULL;
6473
6474 if (expr_end == NULL || in_end == NULL)
6475 return NULL;
6476 *expr_start = NUL;
6477 *expr_end = NUL;
6478 c1 = *in_end;
6479 *in_end = NUL;
6480
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006481 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006482 if (temp_result != NULL && nextcmd == NULL)
6483 {
6484 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6485 + (in_end - expr_end) + 1));
6486 if (retval != NULL)
6487 {
6488 STRCPY(retval, in_start);
6489 STRCAT(retval, temp_result);
6490 STRCAT(retval, expr_end + 1);
6491 }
6492 }
6493 vim_free(temp_result);
6494
6495 *in_end = c1; /* put char back for error messages */
6496 *expr_start = '{';
6497 *expr_end = '}';
6498
6499 if (retval != NULL)
6500 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006501 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006502 if (expr_start != NULL)
6503 {
6504 /* Further expansion! */
6505 temp_result = make_expanded_name(retval, expr_start,
6506 expr_end, temp_result);
6507 vim_free(retval);
6508 retval = temp_result;
6509 }
6510 }
6511
6512 return retval;
6513}
6514
6515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006522 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6523}
6524
6525/*
6526 * Return TRUE if character "c" can be used as the first character in a
6527 * variable or function name (excluding '{' and '}').
6528 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006530eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006531{
6532 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533}
6534
6535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 * Set number v: variable to "val".
6537 */
6538 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006539set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006541 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542}
6543
6544/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006545 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006547 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006548get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551}
6552
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006553/*
6554 * Get string v: variable value. Uses a static buffer, can only be used once.
6555 */
6556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006558{
6559 return get_tv_string(&vimvars[idx].vv_tv);
6560}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006563 * Get List v: variable value. Caller must take care of reference count when
6564 * needed.
6565 */
6566 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006567get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006568{
6569 return vimvars[idx].vv_list;
6570}
6571
6572/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006573 * Set v:char to character "c".
6574 */
6575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006576set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006577{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006578 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006579
6580#ifdef FEAT_MBYTE
6581 if (has_mbyte)
6582 buf[(*mb_char2bytes)(c, buf)] = NUL;
6583 else
6584#endif
6585 {
6586 buf[0] = c;
6587 buf[1] = NUL;
6588 }
6589 set_vim_var_string(VV_CHAR, buf, -1);
6590}
6591
6592/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006593 * Set v:count to "count" and v:count1 to "count1".
6594 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 */
6596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597set_vcount(
6598 long count,
6599 long count1,
6600 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006602 if (set_prevcount)
6603 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 vimvars[VV_COUNT].vv_nr = count;
6605 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
6607
6608/*
6609 * Set string v: variable to a copy of "val".
6610 */
6611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612set_vim_var_string(
6613 int idx,
6614 char_u *val,
6615 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
Bram Moolenaara542c682016-01-31 16:28:04 +01006617 clear_tv(&vimvars[idx].vv_di.di_tv);
6618 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006622 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006624 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626
6627/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006628 * Set List v: variable to "val".
6629 */
6630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006632{
Bram Moolenaara542c682016-01-31 16:28:04 +01006633 clear_tv(&vimvars[idx].vv_di.di_tv);
6634 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006635 vimvars[idx].vv_list = val;
6636 if (val != NULL)
6637 ++val->lv_refcount;
6638}
6639
6640/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006641 * Set Dictionary v: variable to "val".
6642 */
6643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006645{
6646 int todo;
6647 hashitem_T *hi;
6648
Bram Moolenaara542c682016-01-31 16:28:04 +01006649 clear_tv(&vimvars[idx].vv_di.di_tv);
6650 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006651 vimvars[idx].vv_dict = val;
6652 if (val != NULL)
6653 {
6654 ++val->dv_refcount;
6655
6656 /* Set readonly */
6657 todo = (int)val->dv_hashtab.ht_used;
6658 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6659 {
6660 if (HASHITEM_EMPTY(hi))
6661 continue;
6662 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006663 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006664 }
6665 }
6666}
6667
6668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 * Set v:register if needed.
6670 */
6671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006672set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 char_u regname;
6675
6676 if (c == 0 || c == ' ')
6677 regname = '"';
6678 else
6679 regname = c;
6680 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 set_vim_var_string(VV_REG, &regname, 1);
6683}
6684
6685/*
6686 * Get or set v:exception. If "oldval" == NULL, return the current value.
6687 * Otherwise, restore the value to "oldval" and return NULL.
6688 * Must always be called in pairs to save and restore v:exception! Does not
6689 * take care of memory allocations.
6690 */
6691 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006695 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
Bram Moolenaare9a41262005-01-15 22:18:47 +00006697 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 return NULL;
6699}
6700
6701/*
6702 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6703 * Otherwise, restore the value to "oldval" and return NULL.
6704 * Must always be called in pairs to save and restore v:throwpoint! Does not
6705 * take care of memory allocations.
6706 */
6707 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006708v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
Bram Moolenaare9a41262005-01-15 22:18:47 +00006713 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 return NULL;
6715}
6716
6717#if defined(FEAT_AUTOCMD) || defined(PROTO)
6718/*
6719 * Set v:cmdarg.
6720 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6721 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6722 * Must always be called in pairs!
6723 */
6724 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006725set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 char_u *oldval;
6728 char_u *newval;
6729 unsigned len;
6730
Bram Moolenaare9a41262005-01-15 22:18:47 +00006731 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006732 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006734 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006735 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006736 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006739 if (eap->force_bin == FORCE_BIN)
6740 len = 6;
6741 else if (eap->force_bin == FORCE_NOBIN)
6742 len = 8;
6743 else
6744 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006745
6746 if (eap->read_edit)
6747 len += 7;
6748
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006749 if (eap->force_ff != 0)
6750 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6751# ifdef FEAT_MBYTE
6752 if (eap->force_enc != 0)
6753 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006754 if (eap->bad_char != 0)
6755 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006756# endif
6757
6758 newval = alloc(len + 1);
6759 if (newval == NULL)
6760 return NULL;
6761
6762 if (eap->force_bin == FORCE_BIN)
6763 sprintf((char *)newval, " ++bin");
6764 else if (eap->force_bin == FORCE_NOBIN)
6765 sprintf((char *)newval, " ++nobin");
6766 else
6767 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006768
6769 if (eap->read_edit)
6770 STRCAT(newval, " ++edit");
6771
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006772 if (eap->force_ff != 0)
6773 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6774 eap->cmd + eap->force_ff);
6775# ifdef FEAT_MBYTE
6776 if (eap->force_enc != 0)
6777 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6778 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006779 if (eap->bad_char == BAD_KEEP)
6780 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6781 else if (eap->bad_char == BAD_DROP)
6782 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6783 else if (eap->bad_char != 0)
6784 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006785# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006786 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006787 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788}
6789#endif
6790
6791/*
6792 * Get the value of internal variable "name".
6793 * Return OK or FAIL.
6794 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006796get_var_tv(
6797 char_u *name,
6798 int len, /* length of "name" */
6799 typval_T *rettv, /* NULL when only checking existence */
6800 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6801 int verbose, /* may give error message */
6802 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
6804 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
6809 /* truncate the name, so that we can use strcmp() */
6810 cc = name[len];
6811 name[len] = NUL;
6812
6813 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 * Check for user-defined variables.
6815 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006816 v = find_var(name, NULL, no_autoload);
6817 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006819 tv = &v->di_tv;
6820 if (dip != NULL)
6821 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006826 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006827 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 ret = FAIL;
6829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006830 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006831 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
6833 name[len] = cc;
6834
6835 return ret;
6836}
6837
6838/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006839 * Check if variable "name[len]" is a local variable or an argument.
6840 * If so, "*eval_lavars_used" is set to TRUE.
6841 */
6842 static void
6843check_vars(char_u *name, int len)
6844{
6845 int cc;
6846 char_u *varname;
6847 hashtab_T *ht;
6848
6849 if (eval_lavars_used == NULL)
6850 return;
6851
6852 /* truncate the name, so that we can use strcmp() */
6853 cc = name[len];
6854 name[len] = NUL;
6855
6856 ht = find_var_ht(name, &varname);
6857 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6858 {
6859 if (find_var(name, NULL, TRUE) != NULL)
6860 *eval_lavars_used = TRUE;
6861 }
6862
6863 name[len] = cc;
6864}
6865
6866/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006867 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6868 * Also handle function call with Funcref variable: func(expr)
6869 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6870 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872handle_subscript(
6873 char_u **arg,
6874 typval_T *rettv,
6875 int evaluate, /* do more than finding the end */
6876 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006877{
6878 int ret = OK;
6879 dict_T *selfdict = NULL;
6880 char_u *s;
6881 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006882 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006883
6884 while (ret == OK
6885 && (**arg == '['
6886 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006887 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6888 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006889 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006890 {
6891 if (**arg == '(')
6892 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006893 partial_T *pt = NULL;
6894
Bram Moolenaard9fba312005-06-26 22:34:35 +00006895 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006896 if (evaluate)
6897 {
6898 functv = *rettv;
6899 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006900
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006901 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006902 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006903 {
6904 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006905 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006906 }
6907 else
6908 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006909 }
6910 else
6911 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006912 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006913 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006914 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006915
6916 /* Clear the funcref afterwards, so that deleting it while
6917 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006918 if (evaluate)
6919 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006920
6921 /* Stop the expression evaluation when immediately aborting on
6922 * error, or when an interrupt occurred or an exception was thrown
6923 * but not caught. */
6924 if (aborting())
6925 {
6926 if (ret == OK)
6927 clear_tv(rettv);
6928 ret = FAIL;
6929 }
6930 dict_unref(selfdict);
6931 selfdict = NULL;
6932 }
6933 else /* **arg == '[' || **arg == '.' */
6934 {
6935 dict_unref(selfdict);
6936 if (rettv->v_type == VAR_DICT)
6937 {
6938 selfdict = rettv->vval.v_dict;
6939 if (selfdict != NULL)
6940 ++selfdict->dv_refcount;
6941 }
6942 else
6943 selfdict = NULL;
6944 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6945 {
6946 clear_tv(rettv);
6947 ret = FAIL;
6948 }
6949 }
6950 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006951
Bram Moolenaar1d429612016-05-24 15:44:17 +02006952 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6953 * Don't do this when "Func" is already a partial that was bound
6954 * explicitly (pt_auto is FALSE). */
6955 if (selfdict != NULL
6956 && (rettv->v_type == VAR_FUNC
6957 || (rettv->v_type == VAR_PARTIAL
6958 && (rettv->vval.v_partial->pt_auto
6959 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006960 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006962 dict_unref(selfdict);
6963 return ret;
6964}
6965
6966/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006967 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006968 * value).
6969 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006970 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006974}
6975
6976/*
6977 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 * The string "s" must have been allocated, it is consumed.
6979 * Return NULL for out of memory, the variable otherwise.
6980 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006986 rettv = alloc_tv();
6987 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006989 rettv->v_type = VAR_STRING;
6990 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
6992 else
6993 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995}
6996
6997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006998 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007001free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002{
7003 if (varp != NULL)
7004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007005 switch (varp->v_type)
7006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007007 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007008 func_unref(varp->vval.v_string);
7009 /*FALLTHROUGH*/
7010 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 vim_free(varp->vval.v_string);
7012 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007013 case VAR_PARTIAL:
7014 partial_unref(varp->vval.v_partial);
7015 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007016 case VAR_LIST:
7017 list_unref(varp->vval.v_list);
7018 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007019 case VAR_DICT:
7020 dict_unref(varp->vval.v_dict);
7021 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007022 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007024 job_unref(varp->vval.v_job);
7025 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007026#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007027 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007028#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007029 channel_unref(varp->vval.v_channel);
7030 break;
7031#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007032 case VAR_NUMBER:
7033 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007034 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007035 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007036 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 vim_free(varp);
7039 }
7040}
7041
7042/*
7043 * Free the memory for a variable value and set the value to NULL or 0.
7044 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 if (varp != NULL)
7049 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007050 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007052 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007053 func_unref(varp->vval.v_string);
7054 /*FALLTHROUGH*/
7055 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007056 vim_free(varp->vval.v_string);
7057 varp->vval.v_string = NULL;
7058 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007059 case VAR_PARTIAL:
7060 partial_unref(varp->vval.v_partial);
7061 varp->vval.v_partial = NULL;
7062 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007063 case VAR_LIST:
7064 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007066 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067 case VAR_DICT:
7068 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007071 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007072 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007073 varp->vval.v_number = 0;
7074 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007075 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007076#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007077 varp->vval.v_float = 0.0;
7078 break;
7079#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007080 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007081#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007082 job_unref(varp->vval.v_job);
7083 varp->vval.v_job = NULL;
7084#endif
7085 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007086 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007087#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007088 channel_unref(varp->vval.v_channel);
7089 varp->vval.v_channel = NULL;
7090#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007091 case VAR_UNKNOWN:
7092 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007094 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 }
7096}
7097
7098/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007099 * Set the value of a variable to NULL without freeing items.
7100 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007103{
7104 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007106}
7107
7108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 * Get the number value of a variable.
7110 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007111 * For incompatible types, return 0.
7112 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7113 * caller of incompatible types: it sets *denote to TRUE if "denote"
7114 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007116 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007117get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007119 int error = FALSE;
7120
7121 return get_tv_number_chk(varp, &error); /* return 0L on error */
7122}
7123
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007124 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007126{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007127 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007129 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007131 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007132 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007133 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007134#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007135 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007136 break;
7137#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007138 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007139 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007140 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 break;
7142 case VAR_STRING:
7143 if (varp->vval.v_string != NULL)
7144 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007145 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007146 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007147 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007148 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007149 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007150 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007151 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007152 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007153 case VAR_SPECIAL:
7154 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7155 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007156 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007157#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007158 EMSG(_("E910: Using a Job as a Number"));
7159 break;
7160#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007161 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007162#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007163 EMSG(_("E913: Using a Channel as a Number"));
7164 break;
7165#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007166 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007167 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007170 if (denote == NULL) /* useful for values that must be unsigned */
7171 n = -1;
7172 else
7173 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175}
7176
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007177#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007178 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007180{
7181 switch (varp->v_type)
7182 {
7183 case VAR_NUMBER:
7184 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007185 case VAR_FLOAT:
7186 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007187 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007188 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007189 EMSG(_("E891: Using a Funcref as a Float"));
7190 break;
7191 case VAR_STRING:
7192 EMSG(_("E892: Using a String as a Float"));
7193 break;
7194 case VAR_LIST:
7195 EMSG(_("E893: Using a List as a Float"));
7196 break;
7197 case VAR_DICT:
7198 EMSG(_("E894: Using a Dictionary as a Float"));
7199 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007200 case VAR_SPECIAL:
7201 EMSG(_("E907: Using a special value as a Float"));
7202 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007203 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007204# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007205 EMSG(_("E911: Using a Job as a Float"));
7206 break;
7207# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007208 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007209# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007210 EMSG(_("E914: Using a Channel as a Float"));
7211 break;
7212# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007213 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007214 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007215 break;
7216 }
7217 return 0;
7218}
7219#endif
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 * Get the string value of a variable.
7223 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007224 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7225 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 * If the String variable has never been set, return an empty string.
7227 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007228 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7229 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007231 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
7234 static char_u mybuf[NUMBUFLEN];
7235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237}
7238
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007239 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007240get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007242 char_u *res = get_tv_string_buf_chk(varp, buf);
7243
7244 return res != NULL ? res : (char_u *)"";
7245}
7246
Bram Moolenaar7d647822014-04-05 21:28:56 +02007247/*
7248 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7249 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007250 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007251get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252{
7253 static char_u mybuf[NUMBUFLEN];
7254
7255 return get_tv_string_buf_chk(varp, mybuf);
7256}
7257
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007258 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007259get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007260{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007261 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007263 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007264 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7265 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007266 return buf;
7267 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007268 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007269 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007270 break;
7271 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007272 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 break;
7274 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007275 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007276 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007277 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007278#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007279 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007280 break;
7281#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 case VAR_STRING:
7283 if (varp->vval.v_string != NULL)
7284 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007285 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007286 case VAR_SPECIAL:
7287 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7288 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007289 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007290#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007291 {
7292 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007293 char *status;
7294
7295 if (job == NULL)
7296 return (char_u *)"no process";
7297 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007298 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007299 : "run";
7300# ifdef UNIX
7301 vim_snprintf((char *)buf, NUMBUFLEN,
7302 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007303# elif defined(WIN32)
7304 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007305 "process %ld %s",
7306 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007307 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007308# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007309 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007310 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7311# endif
7312 return buf;
7313 }
7314#endif
7315 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007316 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007317#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007318 {
7319 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007320 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007321
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007322 if (channel == NULL)
7323 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7324 else
7325 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007326 "channel %d %s", channel->ch_id, status);
7327 return buf;
7328 }
7329#endif
7330 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007331 case VAR_UNKNOWN:
7332 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007335 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336}
7337
7338/*
7339 * Find variable "name" in the list of variables.
7340 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007342 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007345 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007346find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007350 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351
Bram Moolenaara7043832005-01-21 11:56:39 +00007352 ht = find_var_ht(name, &varname);
7353 if (htp != NULL)
7354 *htp = ht;
7355 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007357 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7358 if (ret != NULL)
7359 return ret;
7360
7361 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007362 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
7364
7365/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007366 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007367 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007370find_var_in_ht(
7371 hashtab_T *ht,
7372 int htname,
7373 char_u *varname,
7374 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007375{
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 hashitem_T *hi;
7377
7378 if (*varname == NUL)
7379 {
7380 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007381 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007383 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 case 'g': return &globvars_var;
7385 case 'v': return &vimvars_var;
7386 case 'b': return &curbuf->b_bufvar;
7387 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007388#ifdef FEAT_WINDOWS
7389 case 't': return &curtab->tp_winvar;
7390#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007391 case 'l': return get_funccal_local_var();
7392 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 }
7394 return NULL;
7395 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007396
7397 hi = hash_find(ht, varname);
7398 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007399 {
7400 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007401 * worked find the variable again. Don't auto-load a script if it was
7402 * loaded already, otherwise it would be loaded every time when
7403 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007404 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007405 {
7406 /* Note: script_autoload() may make "hi" invalid. It must either
7407 * be obtained again or not used. */
7408 if (!script_autoload(varname, FALSE) || aborting())
7409 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007410 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007411 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007412 if (HASHITEM_EMPTY(hi))
7413 return NULL;
7414 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007416}
7417
7418/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007420 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007421 * Set "varname" to the start of name without ':'.
7422 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007423 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007426 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007427 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007428
Bram Moolenaar73627d02015-08-11 15:46:09 +02007429 if (name[0] == NUL)
7430 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (name[1] != ':')
7432 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007433 /* The name must not start with a colon or #. */
7434 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 return NULL;
7436 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007437
7438 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007439 hi = hash_find(&compat_hashtab, name);
7440 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007441 return &compat_hashtab;
7442
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007443 ht = get_funccal_local_ht();
7444 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007446 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
7448 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007449 if (*name == 'g') /* global variable */
7450 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007451 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7452 */
7453 if (vim_strchr(name + 2, ':') != NULL
7454 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007455 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007457 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007459 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007460#ifdef FEAT_WINDOWS
7461 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007462 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007464 if (*name == 'v') /* v: variable */
7465 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007466 if (*name == 'a') /* a: function argument */
7467 return get_funccal_args_ht();
7468 if (*name == 'l') /* l: local function variable */
7469 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 if (*name == 's' /* script variable */
7471 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7472 return &SCRIPT_VARS(current_SID);
7473 return NULL;
7474}
7475
7476/*
7477 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007478 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 * Returns NULL when it doesn't exist.
7480 */
7481 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007486 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (v == NULL)
7488 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007489 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490}
7491
7492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 * sourcing this script and when executing functions defined in the script.
7495 */
7496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
Bram Moolenaara7043832005-01-21 11:56:39 +00007499 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hashtab_T *ht;
7501 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7504 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007505 /* Re-allocating ga_data means that an ht_array pointing to
7506 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007508 for (i = 1; i <= ga_scripts.ga_len; ++i)
7509 {
7510 ht = &SCRIPT_VARS(i);
7511 if (ht->ht_mask == HT_INIT_SIZE - 1)
7512 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007513 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007515 }
7516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 while (ga_scripts.ga_len < id)
7518 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007519 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007520 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007521 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524 }
7525}
7526
7527/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7529 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 */
7531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533{
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007535 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007536 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007537 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007538 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 dict_var->di_tv.vval.v_dict = dict;
7540 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007541 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7543 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544}
7545
7546/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007547 * Unreference a dictionary initialized by init_var_dict().
7548 */
7549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007551{
7552 /* Now the dict needs to be freed if no one else is using it, go back to
7553 * normal reference counting. */
7554 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7555 dict_unref(dict);
7556}
7557
7558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 * Frees all allocated variables and the value they contain.
7561 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 */
7563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007565{
7566 vars_clear_ext(ht, TRUE);
7567}
7568
7569/*
7570 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7571 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007573vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
Bram Moolenaara7043832005-01-21 11:56:39 +00007575 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 hashitem_T *hi;
7577 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007581 for (hi = ht->ht_array; todo > 0; ++hi)
7582 {
7583 if (!HASHITEM_EMPTY(hi))
7584 {
7585 --todo;
7586
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007588 * ht_array might change then. hash_clear() takes care of it
7589 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 v = HI2DI(hi);
7591 if (free_val)
7592 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007593 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007595 }
7596 }
7597 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007598 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599}
7600
Bram Moolenaara7043832005-01-21 11:56:39 +00007601/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 * Delete a variable from hashtab "ht" at item "hi".
7603 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007606delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007609
7610 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 clear_tv(&di->di_tv);
7612 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613}
7614
7615/*
7616 * List the value of one internal variable.
7617 */
7618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 char_u *tofree;
7622 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007623 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007625 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007627 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629}
7630
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632list_one_var_a(
7633 char_u *prefix,
7634 char_u *name,
7635 int type,
7636 char_u *string,
7637 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
Bram Moolenaar31859182007-08-14 20:41:13 +00007639 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7640 msg_start();
7641 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (name != NULL) /* "a:" vars don't have a name stored */
7643 msg_puts(name);
7644 msg_putchar(' ');
7645 msg_advance(22);
7646 if (type == VAR_NUMBER)
7647 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007648 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 msg_putchar('*');
7650 else if (type == VAR_LIST)
7651 {
7652 msg_putchar('[');
7653 if (*string == '[')
7654 ++string;
7655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 else if (type == VAR_DICT)
7657 {
7658 msg_putchar('{');
7659 if (*string == '{')
7660 ++string;
7661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 else
7663 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007666
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007667 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007668 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007669 if (*first)
7670 {
7671 msg_clr_eos();
7672 *first = FALSE;
7673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674}
7675
7676/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007677 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 * If the variable already exists, the value is updated.
7679 * Otherwise the variable is created.
7680 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007682set_var(
7683 char_u *name,
7684 typval_T *tv,
7685 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007691 ht = find_var_ht(name, &varname);
7692 if (ht == NULL || *varname == NUL)
7693 {
7694 EMSG2(_(e_illvar), name);
7695 return;
7696 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007697 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007698
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007699 /* Search in parent scope which is possible to reference from lambda */
7700 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007701 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007702
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007703 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7704 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007705 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007706
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007710 if (var_check_ro(v->di_flags, name, FALSE)
7711 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007713
7714 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007715 * Handle setting internal v: variables separately where needed to
7716 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 */
7718 if (ht == &vimvarht)
7719 {
7720 if (v->di_tv.v_type == VAR_STRING)
7721 {
7722 vim_free(v->di_tv.vval.v_string);
7723 if (copy || tv->v_type != VAR_STRING)
7724 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7725 else
7726 {
7727 /* Take over the string to avoid an extra alloc/free. */
7728 v->di_tv.vval.v_string = tv->vval.v_string;
7729 tv->vval.v_string = NULL;
7730 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007731 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007733 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007736 if (STRCMP(varname, "searchforward") == 0)
7737 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007738#ifdef FEAT_SEARCH_EXTRA
7739 else if (STRCMP(varname, "hlsearch") == 0)
7740 {
7741 no_hlsearch = !v->di_tv.vval.v_number;
7742 redraw_all_later(SOME_VALID);
7743 }
7744#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007745 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007746 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007747 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007748 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 }
7750
7751 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 else /* add a new variable */
7754 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007755 /* Can't add "v:" variable. */
7756 if (ht == &vimvarht)
7757 {
7758 EMSG2(_(e_illvar), name);
7759 return;
7760 }
7761
Bram Moolenaar92124a32005-06-17 22:03:40 +00007762 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007763 if (!valid_varname(varname))
7764 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007766 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7767 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007768 if (v == NULL)
7769 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007773 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 return;
7775 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007776 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007781 else
7782 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007784 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007790 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 * Also give an error message.
7792 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007795{
7796 if (flags & DI_FLAGS_RO)
7797 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007798 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 return TRUE;
7800 }
7801 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7802 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007803 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007804 return TRUE;
7805 }
7806 return FALSE;
7807}
7808
7809/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007810 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7811 * Also give an error message.
7812 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007815{
7816 if (flags & DI_FLAGS_FIX)
7817 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007818 EMSG2(_("E795: Cannot delete variable %s"),
7819 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007820 return TRUE;
7821 }
7822 return FALSE;
7823}
7824
7825/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007826 * Check if a funcref is assigned to a valid variable name.
7827 * Return TRUE and give an error if not.
7828 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007830var_check_func_name(
7831 char_u *name, /* points to start of variable name */
7832 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007833{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007834 /* Allow for w: b: s: and t:. */
7835 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007836 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7837 ? name[2] : name[0]))
7838 {
7839 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7840 name);
7841 return TRUE;
7842 }
7843 /* Don't allow hiding a function. When "v" is not NULL we might be
7844 * assigning another function to the same var, the type is checked
7845 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007846 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007847 {
7848 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7849 name);
7850 return TRUE;
7851 }
7852 return FALSE;
7853}
7854
7855/*
7856 * Check if a variable name is valid.
7857 * Return FALSE and give an error if not.
7858 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007859 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007860valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007861{
7862 char_u *p;
7863
7864 for (p = varname; *p != NUL; ++p)
7865 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7866 && *p != AUTOLOAD_CHAR)
7867 {
7868 EMSG2(_(e_illvar), varname);
7869 return FALSE;
7870 }
7871 return TRUE;
7872}
7873
7874/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007875 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007876 * Also give an error message, using "name" or _("name") when use_gettext is
7877 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007878 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007881{
7882 if (lock & VAR_LOCKED)
7883 {
7884 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007885 name == NULL ? (char_u *)_("Unknown")
7886 : use_gettext ? (char_u *)_(name)
7887 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007888 return TRUE;
7889 }
7890 if (lock & VAR_FIXED)
7891 {
7892 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007893 name == NULL ? (char_u *)_("Unknown")
7894 : use_gettext ? (char_u *)_(name)
7895 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007896 return TRUE;
7897 }
7898 return FALSE;
7899}
7900
7901/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007905 * It is OK for "from" and "to" to point to the same item. This is used to
7906 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007907 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007909copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007912 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 switch (from->v_type)
7914 {
7915 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007916 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 to->vval.v_number = from->vval.v_number;
7918 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007920#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007921 to->vval.v_float = from->vval.v_float;
7922 break;
7923#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007924 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007925#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007926 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007927 if (to->vval.v_job != NULL)
7928 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007929 break;
7930#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007931 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007932#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007933 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007934 if (to->vval.v_channel != NULL)
7935 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007936 break;
7937#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 case VAR_STRING:
7939 case VAR_FUNC:
7940 if (from->vval.v_string == NULL)
7941 to->vval.v_string = NULL;
7942 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007945 if (from->v_type == VAR_FUNC)
7946 func_ref(to->vval.v_string);
7947 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007949 case VAR_PARTIAL:
7950 if (from->vval.v_partial == NULL)
7951 to->vval.v_partial = NULL;
7952 else
7953 {
7954 to->vval.v_partial = from->vval.v_partial;
7955 ++to->vval.v_partial->pt_refcount;
7956 }
7957 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 case VAR_LIST:
7959 if (from->vval.v_list == NULL)
7960 to->vval.v_list = NULL;
7961 else
7962 {
7963 to->vval.v_list = from->vval.v_list;
7964 ++to->vval.v_list->lv_refcount;
7965 }
7966 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 case VAR_DICT:
7968 if (from->vval.v_dict == NULL)
7969 to->vval.v_dict = NULL;
7970 else
7971 {
7972 to->vval.v_dict = from->vval.v_dict;
7973 ++to->vval.v_dict->dv_refcount;
7974 }
7975 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007976 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007977 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 break;
7979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980}
7981
7982/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 * Make a copy of an item.
7984 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007985 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7986 * reference to an already copied list/dict can be used.
7987 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007990item_copy(
7991 typval_T *from,
7992 typval_T *to,
7993 int deep,
7994 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007995{
7996 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007997 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007998
Bram Moolenaar33570922005-01-25 22:26:29 +00007999 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000 {
8001 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008002 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008003 }
8004 ++recurse;
8005
8006 switch (from->v_type)
8007 {
8008 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008009 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 case VAR_STRING:
8011 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008012 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008013 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008014 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008015 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 copy_tv(from, to);
8017 break;
8018 case VAR_LIST:
8019 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008020 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008021 if (from->vval.v_list == NULL)
8022 to->vval.v_list = NULL;
8023 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8024 {
8025 /* use the copy made earlier */
8026 to->vval.v_list = from->vval.v_list->lv_copylist;
8027 ++to->vval.v_list->lv_refcount;
8028 }
8029 else
8030 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8031 if (to->vval.v_list == NULL)
8032 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008033 break;
8034 case VAR_DICT:
8035 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008036 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008037 if (from->vval.v_dict == NULL)
8038 to->vval.v_dict = NULL;
8039 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8040 {
8041 /* use the copy made earlier */
8042 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8043 ++to->vval.v_dict->dv_refcount;
8044 }
8045 else
8046 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8047 if (to->vval.v_dict == NULL)
8048 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008049 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008050 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008051 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008052 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 }
8054 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008055 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008056}
8057
8058/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 * This function is used by f_input() and f_inputdialog() functions. The third
8060 * argument to f_input() specifies the type of completion to use at the
8061 * prompt. The third argument to f_inputdialog() specifies the value to return
8062 * when the user cancels the prompt.
8063 */
8064 void
8065get_user_input(
8066 typval_T *argvars,
8067 typval_T *rettv,
8068 int inputdialog,
8069 int secret)
8070{
8071 char_u *prompt = get_tv_string_chk(&argvars[0]);
8072 char_u *p = NULL;
8073 int c;
8074 char_u buf[NUMBUFLEN];
8075 int cmd_silent_save = cmd_silent;
8076 char_u *defstr = (char_u *)"";
8077 int xp_type = EXPAND_NOTHING;
8078 char_u *xp_arg = NULL;
8079
8080 rettv->v_type = VAR_STRING;
8081 rettv->vval.v_string = NULL;
8082
8083#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008084 /* While starting up, there is no place to enter text. When running tests
8085 * with --not-a-term we assume feedkeys() will be used. */
8086 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 return;
8088#endif
8089
8090 cmd_silent = FALSE; /* Want to see the prompt. */
8091 if (prompt != NULL)
8092 {
8093 /* Only the part of the message after the last NL is considered as
8094 * prompt for the command line */
8095 p = vim_strrchr(prompt, '\n');
8096 if (p == NULL)
8097 p = prompt;
8098 else
8099 {
8100 ++p;
8101 c = *p;
8102 *p = NUL;
8103 msg_start();
8104 msg_clr_eos();
8105 msg_puts_attr(prompt, echo_attr);
8106 msg_didout = FALSE;
8107 msg_starthere();
8108 *p = c;
8109 }
8110 cmdline_row = msg_row;
8111
8112 if (argvars[1].v_type != VAR_UNKNOWN)
8113 {
8114 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8115 if (defstr != NULL)
8116 stuffReadbuffSpec(defstr);
8117
8118 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8119 {
8120 char_u *xp_name;
8121 int xp_namelen;
8122 long argt;
8123
8124 /* input() with a third argument: completion */
8125 rettv->vval.v_string = NULL;
8126
8127 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8128 if (xp_name == NULL)
8129 return;
8130
8131 xp_namelen = (int)STRLEN(xp_name);
8132
8133 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8134 &xp_arg) == FAIL)
8135 return;
8136 }
8137 }
8138
8139 if (defstr != NULL)
8140 {
8141 int save_ex_normal_busy = ex_normal_busy;
8142 ex_normal_busy = 0;
8143 rettv->vval.v_string =
8144 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8145 xp_type, xp_arg);
8146 ex_normal_busy = save_ex_normal_busy;
8147 }
8148 if (inputdialog && rettv->vval.v_string == NULL
8149 && argvars[1].v_type != VAR_UNKNOWN
8150 && argvars[2].v_type != VAR_UNKNOWN)
8151 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8152 &argvars[2], buf));
8153
8154 vim_free(xp_arg);
8155
8156 /* since the user typed this, no need to wait for return */
8157 need_wait_return = FALSE;
8158 msg_didout = FALSE;
8159 }
8160 cmd_silent = cmd_silent_save;
8161}
8162
8163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 * ":echo expr1 ..." print each argument separated with a space, add a
8165 * newline at the end.
8166 * ":echon expr1 ..." print each argument plain.
8167 */
8168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008169ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 char_u *p;
8175 int needclr = TRUE;
8176 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008177 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
8179 if (eap->skip)
8180 ++emsg_skip;
8181 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8182 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008183 /* If eval1() causes an error message the text from the command may
8184 * still need to be cleared. E.g., "echo 22,44". */
8185 need_clr_eos = needclr;
8186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008188 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
8190 /*
8191 * Report the invalid expression unless the expression evaluation
8192 * has been cancelled due to an aborting error, an interrupt, or an
8193 * exception.
8194 */
8195 if (!aborting())
8196 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008197 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 break;
8199 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008200 need_clr_eos = FALSE;
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (!eap->skip)
8203 {
8204 if (atstart)
8205 {
8206 atstart = FALSE;
8207 /* Call msg_start() after eval1(), evaluating the expression
8208 * may cause a message to appear. */
8209 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008210 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008211 /* Mark the saved text as finishing the line, so that what
8212 * follows is displayed on a new line when scrolling back
8213 * at the more prompt. */
8214 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
8218 else if (eap->cmdidx == CMD_echo)
8219 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008220 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008221 if (p != NULL)
8222 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008224 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008226 if (*p != TAB && needclr)
8227 {
8228 /* remove any text still there from the command */
8229 msg_clr_eos();
8230 needclr = FALSE;
8231 }
8232 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008235 {
8236#ifdef FEAT_MBYTE
8237 if (has_mbyte)
8238 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008239 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008240
8241 (void)msg_outtrans_len_attr(p, i, echo_attr);
8242 p += i - 1;
8243 }
8244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008246 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008249 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 arg = skipwhite(arg);
8253 }
8254 eap->nextcmd = check_nextcmd(arg);
8255
8256 if (eap->skip)
8257 --emsg_skip;
8258 else
8259 {
8260 /* remove text that may still be there from the command */
8261 if (needclr)
8262 msg_clr_eos();
8263 if (eap->cmdidx == CMD_echo)
8264 msg_end();
8265 }
8266}
8267
8268/*
8269 * ":echohl {name}".
8270 */
8271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008272ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 int id;
8275
8276 id = syn_name2id(eap->arg);
8277 if (id == 0)
8278 echo_attr = 0;
8279 else
8280 echo_attr = syn_id2attr(id);
8281}
8282
8283/*
8284 * ":execute expr1 ..." execute the result of an expression.
8285 * ":echomsg expr1 ..." Print a message
8286 * ":echoerr expr1 ..." Print an error
8287 * Each gets spaces around each argument and a newline at the end for
8288 * echo commands
8289 */
8290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008291ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 int ret = OK;
8296 char_u *p;
8297 garray_T ga;
8298 int len;
8299 int save_did_emsg;
8300
8301 ga_init2(&ga, 1, 80);
8302
8303 if (eap->skip)
8304 ++emsg_skip;
8305 while (*arg != NUL && *arg != '|' && *arg != '\n')
8306 {
8307 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 /*
8311 * Report the invalid expression unless the expression evaluation
8312 * has been cancelled due to an aborting error, an interrupt, or an
8313 * exception.
8314 */
8315 if (!aborting())
8316 EMSG2(_(e_invexpr2), p);
8317 ret = FAIL;
8318 break;
8319 }
8320
8321 if (!eap->skip)
8322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008323 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 len = (int)STRLEN(p);
8325 if (ga_grow(&ga, len + 2) == FAIL)
8326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008327 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 ret = FAIL;
8329 break;
8330 }
8331 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 ga.ga_len += len;
8335 }
8336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 arg = skipwhite(arg);
8339 }
8340
8341 if (ret != FAIL && ga.ga_data != NULL)
8342 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008343 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8344 {
8345 /* Mark the already saved text as finishing the line, so that what
8346 * follows is displayed on a new line when scrolling back at the
8347 * more prompt. */
8348 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008349 }
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008354 out_flush();
8355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 else if (eap->cmdidx == CMD_echoerr)
8357 {
8358 /* We don't want to abort following commands, restore did_emsg. */
8359 save_did_emsg = did_emsg;
8360 EMSG((char_u *)ga.ga_data);
8361 if (!force_abort)
8362 did_emsg = save_did_emsg;
8363 }
8364 else if (eap->cmdidx == CMD_execute)
8365 do_cmdline((char_u *)ga.ga_data,
8366 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8367 }
8368
8369 ga_clear(&ga);
8370
8371 if (eap->skip)
8372 --emsg_skip;
8373
8374 eap->nextcmd = check_nextcmd(arg);
8375}
8376
8377/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 * Find window specified by "vp" in tabpage "tp".
8379 */
8380 win_T *
8381find_win_by_nr(
8382 typval_T *vp,
8383 tabpage_T *tp UNUSED) /* NULL for current tab page */
8384{
8385#ifdef FEAT_WINDOWS
8386 win_T *wp;
8387#endif
8388 int nr;
8389
8390 nr = (int)get_tv_number_chk(vp, NULL);
8391
8392#ifdef FEAT_WINDOWS
8393 if (nr < 0)
8394 return NULL;
8395 if (nr == 0)
8396 return curwin;
8397
Bram Moolenaar29323592016-07-24 22:04:11 +02008398 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8399 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 if (nr >= LOWEST_WIN_ID)
8401 {
8402 if (wp->w_id == nr)
8403 return wp;
8404 }
8405 else if (--nr <= 0)
8406 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008407 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (nr >= LOWEST_WIN_ID)
8409 return NULL;
8410 return wp;
8411#else
8412 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8413 return curwin;
8414 return NULL;
8415#endif
8416}
8417
8418/*
8419 * Find window specified by "wvp" in tabpage "tvp".
8420 */
8421 win_T *
8422find_tabwin(
8423 typval_T *wvp, /* VAR_UNKNOWN for current window */
8424 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8425{
8426 win_T *wp = NULL;
8427 tabpage_T *tp = NULL;
8428 long n;
8429
8430 if (wvp->v_type != VAR_UNKNOWN)
8431 {
8432 if (tvp->v_type != VAR_UNKNOWN)
8433 {
8434 n = (long)get_tv_number(tvp);
8435 if (n >= 0)
8436 tp = find_tabpage(n);
8437 }
8438 else
8439 tp = curtab;
8440
8441 if (tp != NULL)
8442 wp = find_win_by_nr(wvp, tp);
8443 }
8444 else
8445 wp = curwin;
8446
8447 return wp;
8448}
8449
8450/*
8451 * getwinvar() and gettabwinvar()
8452 */
8453 void
8454getwinvar(
8455 typval_T *argvars,
8456 typval_T *rettv,
8457 int off) /* 1 for gettabwinvar() */
8458{
8459 win_T *win;
8460 char_u *varname;
8461 dictitem_T *v;
8462 tabpage_T *tp = NULL;
8463 int done = FALSE;
8464#ifdef FEAT_WINDOWS
8465 win_T *oldcurwin;
8466 tabpage_T *oldtabpage;
8467 int need_switch_win;
8468#endif
8469
8470#ifdef FEAT_WINDOWS
8471 if (off == 1)
8472 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8473 else
8474 tp = curtab;
8475#endif
8476 win = find_win_by_nr(&argvars[off], tp);
8477 varname = get_tv_string_chk(&argvars[off + 1]);
8478 ++emsg_off;
8479
8480 rettv->v_type = VAR_STRING;
8481 rettv->vval.v_string = NULL;
8482
8483 if (win != NULL && varname != NULL)
8484 {
8485#ifdef FEAT_WINDOWS
8486 /* Set curwin to be our win, temporarily. Also set the tabpage,
8487 * otherwise the window is not valid. Only do this when needed,
8488 * autocommands get blocked. */
8489 need_switch_win = !(tp == curtab && win == curwin);
8490 if (!need_switch_win
8491 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8492#endif
8493 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008494 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008496 if (varname[1] == NUL)
8497 {
8498 /* get all window-local options in a dict */
8499 dict_T *opts = get_winbuf_options(FALSE);
8500
8501 if (opts != NULL)
8502 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008503 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008504 done = TRUE;
8505 }
8506 }
8507 else if (get_option_tv(&varname, rettv, 1) == OK)
8508 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 done = TRUE;
8510 }
8511 else
8512 {
8513 /* Look up the variable. */
8514 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8515 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8516 varname, FALSE);
8517 if (v != NULL)
8518 {
8519 copy_tv(&v->di_tv, rettv);
8520 done = TRUE;
8521 }
8522 }
8523 }
8524
8525#ifdef FEAT_WINDOWS
8526 if (need_switch_win)
8527 /* restore previous notion of curwin */
8528 restore_win(oldcurwin, oldtabpage, TRUE);
8529#endif
8530 }
8531
8532 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8533 /* use the default return value */
8534 copy_tv(&argvars[off + 2], rettv);
8535
8536 --emsg_off;
8537}
8538
8539/*
8540 * "setwinvar()" and "settabwinvar()" functions
8541 */
8542 void
8543setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8544{
8545 win_T *win;
8546#ifdef FEAT_WINDOWS
8547 win_T *save_curwin;
8548 tabpage_T *save_curtab;
8549 int need_switch_win;
8550#endif
8551 char_u *varname, *winvarname;
8552 typval_T *varp;
8553 char_u nbuf[NUMBUFLEN];
8554 tabpage_T *tp = NULL;
8555
8556 if (check_restricted() || check_secure())
8557 return;
8558
8559#ifdef FEAT_WINDOWS
8560 if (off == 1)
8561 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8562 else
8563 tp = curtab;
8564#endif
8565 win = find_win_by_nr(&argvars[off], tp);
8566 varname = get_tv_string_chk(&argvars[off + 1]);
8567 varp = &argvars[off + 2];
8568
8569 if (win != NULL && varname != NULL && varp != NULL)
8570 {
8571#ifdef FEAT_WINDOWS
8572 need_switch_win = !(tp == curtab && win == curwin);
8573 if (!need_switch_win
8574 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8575#endif
8576 {
8577 if (*varname == '&')
8578 {
8579 long numval;
8580 char_u *strval;
8581 int error = FALSE;
8582
8583 ++varname;
8584 numval = (long)get_tv_number_chk(varp, &error);
8585 strval = get_tv_string_buf_chk(varp, nbuf);
8586 if (!error && strval != NULL)
8587 set_option_value(varname, numval, strval, OPT_LOCAL);
8588 }
8589 else
8590 {
8591 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8592 if (winvarname != NULL)
8593 {
8594 STRCPY(winvarname, "w:");
8595 STRCPY(winvarname + 2, varname);
8596 set_var(winvarname, varp, TRUE);
8597 vim_free(winvarname);
8598 }
8599 }
8600 }
8601#ifdef FEAT_WINDOWS
8602 if (need_switch_win)
8603 restore_win(save_curwin, save_curtab, TRUE);
8604#endif
8605 }
8606}
8607
8608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8610 * "arg" points to the "&" or '+' when called, to "option" when returning.
8611 * Returns NULL when no option name found. Otherwise pointer to the char
8612 * after the option name.
8613 */
8614 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008615find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 char_u *p = *arg;
8618
8619 ++p;
8620 if (*p == 'g' && p[1] == ':')
8621 {
8622 *opt_flags = OPT_GLOBAL;
8623 p += 2;
8624 }
8625 else if (*p == 'l' && p[1] == ':')
8626 {
8627 *opt_flags = OPT_LOCAL;
8628 p += 2;
8629 }
8630 else
8631 *opt_flags = 0;
8632
8633 if (!ASCII_ISALPHA(*p))
8634 return NULL;
8635 *arg = p;
8636
8637 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8638 p += 4; /* termcap option */
8639 else
8640 while (ASCII_ISALPHA(*p))
8641 ++p;
8642 return p;
8643}
8644
8645/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008646 * Return the autoload script name for a function or variable name.
8647 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008649 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008650autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008651{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008652 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008653 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008654
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008655 /* Get the script file name: replace '#' with '/', append ".vim". */
8656 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8657 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008658 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008659 STRCPY(scriptname, "autoload/");
8660 STRCAT(scriptname, name);
8661 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8662 STRCAT(scriptname, ".vim");
8663 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8664 *p = '/';
8665 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008666}
8667
8668/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008669 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008670 * Return TRUE if a package was loaded.
8671 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008673script_autoload(
8674 char_u *name,
8675 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008676{
8677 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008678 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008679 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008680 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008681
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008682 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008683 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008684 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685 return FALSE;
8686
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008687 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008688
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008689 /* Find the name in the list of previously loaded package names. Skip
8690 * "autoload/", it's always the same. */
8691 for (i = 0; i < ga_loaded.ga_len; ++i)
8692 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8693 break;
8694 if (!reload && i < ga_loaded.ga_len)
8695 ret = FALSE; /* was loaded already */
8696 else
8697 {
8698 /* Remember the name if it wasn't loaded already. */
8699 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8700 {
8701 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8702 tofree = NULL;
8703 }
8704
8705 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008706 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008707 ret = TRUE;
8708 }
8709
8710 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008711 return ret;
8712}
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8715typedef enum
8716{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008717 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8718 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8719 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720} var_flavour_T;
8721
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008722static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008725var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727 char_u *p = varname;
8728
8729 if (ASCII_ISUPPER(*p))
8730 {
8731 while (*(++p))
8732 if (ASCII_ISLOWER(*p))
8733 return VAR_FLAVOUR_SESSION;
8734 return VAR_FLAVOUR_VIMINFO;
8735 }
8736 else
8737 return VAR_FLAVOUR_DEFAULT;
8738}
8739#endif
8740
8741#if defined(FEAT_VIMINFO) || defined(PROTO)
8742/*
8743 * Restore global vars that start with a capital from the viminfo file
8744 */
8745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008746read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
8748 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008749 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008751 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753 if (!writing && (find_viminfo_parameter('!') != NULL))
8754 {
8755 tab = vim_strchr(virp->vir_line + 1, '\t');
8756 if (tab != NULL)
8757 {
8758 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008759 switch (*tab)
8760 {
8761 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008762#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008763 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008764#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008765 case 'D': type = VAR_DICT; break;
8766 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008767 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
8770 tab = vim_strchr(tab, '\t');
8771 if (tab != NULL)
8772 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008773 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008774 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008775 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008777#ifdef FEAT_FLOAT
8778 else if (type == VAR_FLOAT)
8779 (void)string2float(tab + 1, &tv.vval.v_float);
8780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008782 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008783 if (type == VAR_DICT || type == VAR_LIST)
8784 {
8785 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8786
8787 if (etv == NULL)
8788 /* Failed to parse back the dict or list, use it as a
8789 * string. */
8790 tv.v_type = VAR_STRING;
8791 else
8792 {
8793 vim_free(tv.vval.v_string);
8794 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008795 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008796 }
8797 }
8798
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008799 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008800 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008801 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008802 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008803
8804 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008805 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008806 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
8809 }
8810 }
8811
8812 return viminfo_readline(virp);
8813}
8814
8815/*
8816 * Write global vars that start with a capital to the viminfo file
8817 */
8818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 hashitem_T *hi;
8822 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008823 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008824 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008825 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008827 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 if (find_viminfo_parameter('!') == NULL)
8830 return;
8831
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008832 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008833
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008834 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008837 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008839 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 this_var = HI2DI(hi);
8841 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008843 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008844 {
8845 case VAR_STRING: s = "STR"; break;
8846 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008847 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008848 case VAR_DICT: s = "DIC"; break;
8849 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008850 case VAR_SPECIAL: s = "XPL"; break;
8851
8852 case VAR_UNKNOWN:
8853 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008854 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008855 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008856 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008857 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008858 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008860 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008861 if (p != NULL)
8862 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008863 vim_free(tofree);
8864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 }
8867}
8868#endif
8869
8870#if defined(FEAT_SESSION) || defined(PROTO)
8871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008872store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 hashitem_T *hi;
8875 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008876 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 char_u *p, *t;
8878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008879 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008882 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008884 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008885 this_var = HI2DI(hi);
8886 if ((this_var->di_tv.v_type == VAR_NUMBER
8887 || this_var->di_tv.v_type == VAR_STRING)
8888 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008889 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008890 /* Escape special characters with a backslash. Turn a LF and
8891 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008893 (char_u *)"\\\"\n\r");
8894 if (p == NULL) /* out of memory */
8895 break;
8896 for (t = p; *t != NUL; ++t)
8897 if (*t == '\n')
8898 *t = 'n';
8899 else if (*t == '\r')
8900 *t = 'r';
8901 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 this_var->di_key,
8903 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8904 : ' ',
8905 p,
8906 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8907 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008908 || put_eol(fd) == FAIL)
8909 {
8910 vim_free(p);
8911 return FAIL;
8912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 vim_free(p);
8914 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008915#ifdef FEAT_FLOAT
8916 else if (this_var->di_tv.v_type == VAR_FLOAT
8917 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8918 {
8919 float_T f = this_var->di_tv.vval.v_float;
8920 int sign = ' ';
8921
8922 if (f < 0)
8923 {
8924 f = -f;
8925 sign = '-';
8926 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008927 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008928 this_var->di_key, sign, f) < 0)
8929 || put_eol(fd) == FAIL)
8930 return FAIL;
8931 }
8932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934 }
8935 return OK;
8936}
8937#endif
8938
Bram Moolenaar661b1822005-07-28 22:36:45 +00008939/*
8940 * Display script name where an item was last set.
8941 * Should only be invoked when 'verbose' is non-zero.
8942 */
8943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008944last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008945{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008946 char_u *p;
8947
Bram Moolenaar661b1822005-07-28 22:36:45 +00008948 if (scriptID != 0)
8949 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008950 p = home_replace_save(NULL, get_scriptname(scriptID));
8951 if (p != NULL)
8952 {
8953 verbose_enter();
8954 MSG_PUTS(_("\n\tLast set from "));
8955 MSG_PUTS(p);
8956 vim_free(p);
8957 verbose_leave();
8958 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008959 }
8960}
8961
Bram Moolenaar53744302015-07-17 17:38:22 +02008962/* reset v:option_new, v:option_old and v:option_type */
8963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008964reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008965{
8966 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8967 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8968 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8969}
8970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971/*
8972 * Prepare "gap" for an assert error and add the sourcing position.
8973 */
8974 void
8975prepare_assert_error(garray_T *gap)
8976{
8977 char buf[NUMBUFLEN];
8978
8979 ga_init2(gap, 1, 100);
8980 if (sourcing_name != NULL)
8981 {
8982 ga_concat(gap, sourcing_name);
8983 if (sourcing_lnum > 0)
8984 ga_concat(gap, (char_u *)" ");
8985 }
8986 if (sourcing_lnum > 0)
8987 {
8988 sprintf(buf, "line %ld", (long)sourcing_lnum);
8989 ga_concat(gap, (char_u *)buf);
8990 }
8991 if (sourcing_name != NULL || sourcing_lnum > 0)
8992 ga_concat(gap, (char_u *)": ");
8993}
8994
8995/*
8996 * Add an assert error to v:errors.
8997 */
8998 void
8999assert_error(garray_T *gap)
9000{
9001 struct vimvar *vp = &vimvars[VV_ERRORS];
9002
9003 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9004 /* Make sure v:errors is a list. */
9005 set_vim_var_list(VV_ERRORS, list_alloc());
9006 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9007}
9008
9009 void
9010assert_equal_common(typval_T *argvars, assert_type_T atype)
9011{
9012 garray_T ga;
9013
9014 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9015 != (atype == ASSERT_EQUAL))
9016 {
9017 prepare_assert_error(&ga);
9018 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9019 atype);
9020 assert_error(&ga);
9021 ga_clear(&ga);
9022 }
9023}
9024
9025 void
9026assert_match_common(typval_T *argvars, assert_type_T atype)
9027{
9028 garray_T ga;
9029 char_u buf1[NUMBUFLEN];
9030 char_u buf2[NUMBUFLEN];
9031 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9032 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9033
9034 if (pat == NULL || text == NULL)
9035 EMSG(_(e_invarg));
9036 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9037 {
9038 prepare_assert_error(&ga);
9039 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9040 atype);
9041 assert_error(&ga);
9042 ga_clear(&ga);
9043 }
9044}
9045
Bram Moolenaar61c04492016-07-23 15:35:35 +02009046 void
9047assert_inrange(typval_T *argvars)
9048{
9049 garray_T ga;
9050 int error = FALSE;
9051 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9052 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9053 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9054 char_u *tofree;
9055 char msg[200];
9056 char_u numbuf[NUMBUFLEN];
9057
9058 if (error)
9059 return;
9060 if (actual < lower || actual > upper)
9061 {
9062 prepare_assert_error(&ga);
9063 if (argvars[3].v_type != VAR_UNKNOWN)
9064 {
9065 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9066 vim_free(tofree);
9067 }
9068 else
9069 {
9070 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9071 (long)lower, (long)upper, (long)actual);
9072 ga_concat(&ga, (char_u *)msg);
9073 }
9074 assert_error(&ga);
9075 ga_clear(&ga);
9076 }
9077}
9078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079/*
9080 * Common for assert_true() and assert_false().
9081 */
9082 void
9083assert_bool(typval_T *argvars, int isTrue)
9084{
9085 int error = FALSE;
9086 garray_T ga;
9087
9088 if (argvars[0].v_type == VAR_SPECIAL
9089 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9090 return;
9091 if (argvars[0].v_type != VAR_NUMBER
9092 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9093 || error)
9094 {
9095 prepare_assert_error(&ga);
9096 fill_assert_error(&ga, &argvars[1],
9097 (char_u *)(isTrue ? "True" : "False"),
9098 NULL, &argvars[0], ASSERT_OTHER);
9099 assert_error(&ga);
9100 ga_clear(&ga);
9101 }
9102}
9103
9104 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009105assert_report(typval_T *argvars)
9106{
9107 garray_T ga;
9108
9109 prepare_assert_error(&ga);
9110 ga_concat(&ga, get_tv_string(&argvars[0]));
9111 assert_error(&ga);
9112 ga_clear(&ga);
9113}
9114
9115 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116assert_exception(typval_T *argvars)
9117{
9118 garray_T ga;
9119 char_u *error = get_tv_string_chk(&argvars[0]);
9120
9121 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9122 {
9123 prepare_assert_error(&ga);
9124 ga_concat(&ga, (char_u *)"v:exception is not set");
9125 assert_error(&ga);
9126 ga_clear(&ga);
9127 }
9128 else if (error != NULL
9129 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9130 {
9131 prepare_assert_error(&ga);
9132 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9133 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9134 assert_error(&ga);
9135 ga_clear(&ga);
9136 }
9137}
9138
9139 void
9140assert_fails(typval_T *argvars)
9141{
9142 char_u *cmd = get_tv_string_chk(&argvars[0]);
9143 garray_T ga;
9144
9145 called_emsg = FALSE;
9146 suppress_errthrow = TRUE;
9147 emsg_silent = TRUE;
9148 do_cmdline_cmd(cmd);
9149 if (!called_emsg)
9150 {
9151 prepare_assert_error(&ga);
9152 ga_concat(&ga, (char_u *)"command did not fail: ");
9153 ga_concat(&ga, cmd);
9154 assert_error(&ga);
9155 ga_clear(&ga);
9156 }
9157 else if (argvars[1].v_type != VAR_UNKNOWN)
9158 {
9159 char_u buf[NUMBUFLEN];
9160 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9161
9162 if (error == NULL
9163 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9164 {
9165 prepare_assert_error(&ga);
9166 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9167 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9168 assert_error(&ga);
9169 ga_clear(&ga);
9170 }
9171 }
9172
9173 called_emsg = FALSE;
9174 suppress_errthrow = FALSE;
9175 emsg_silent = FALSE;
9176 emsg_on_display = FALSE;
9177 set_vim_var_string(VV_ERRMSG, NULL, 0);
9178}
9179
9180/*
9181 * Append "str" to "gap", escaping unprintable characters.
9182 * Changes NL to \n, CR to \r, etc.
9183 */
9184 static void
9185ga_concat_esc(garray_T *gap, char_u *str)
9186{
9187 char_u *p;
9188 char_u buf[NUMBUFLEN];
9189
9190 if (str == NULL)
9191 {
9192 ga_concat(gap, (char_u *)"NULL");
9193 return;
9194 }
9195
9196 for (p = str; *p != NUL; ++p)
9197 switch (*p)
9198 {
9199 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9200 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9201 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9202 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9203 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9204 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9205 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9206 default:
9207 if (*p < ' ')
9208 {
9209 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9210 ga_concat(gap, buf);
9211 }
9212 else
9213 ga_append(gap, *p);
9214 break;
9215 }
9216}
9217
9218/*
9219 * Fill "gap" with information about an assert error.
9220 */
9221 void
9222fill_assert_error(
9223 garray_T *gap,
9224 typval_T *opt_msg_tv,
9225 char_u *exp_str,
9226 typval_T *exp_tv,
9227 typval_T *got_tv,
9228 assert_type_T atype)
9229{
9230 char_u numbuf[NUMBUFLEN];
9231 char_u *tofree;
9232
9233 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9234 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009235 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9236 vim_free(tofree);
9237 ga_concat(gap, (char_u *)": ");
9238 }
9239
9240 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9241 ga_concat(gap, (char_u *)"Pattern ");
9242 else if (atype == ASSERT_NOTEQUAL)
9243 ga_concat(gap, (char_u *)"Expected not equal to ");
9244 else
9245 ga_concat(gap, (char_u *)"Expected ");
9246 if (exp_str == NULL)
9247 {
9248 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009249 vim_free(tofree);
9250 }
9251 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009252 ga_concat_esc(gap, exp_str);
9253 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009254 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009255 if (atype == ASSERT_MATCH)
9256 ga_concat(gap, (char_u *)" does not match ");
9257 else if (atype == ASSERT_NOTMATCH)
9258 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009259 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009260 ga_concat(gap, (char_u *)" but got ");
9261 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9262 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 }
9264}
9265
Bram Moolenaar53744302015-07-17 17:38:22 +02009266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267#endif /* FEAT_EVAL */
9268
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009270#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
9272#ifdef WIN3264
9273/*
9274 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9275 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009276static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9277static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9278static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
9280/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009281 * Get the short path (8.3) for the filename in "fnamep".
9282 * Only works for a valid file name.
9283 * When the path gets longer "fnamep" is changed and the allocated buffer
9284 * is put in "bufp".
9285 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9286 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 */
9288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009289get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009291 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 char_u *newbuf;
9293
9294 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009295 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 if (l > len - 1)
9297 {
9298 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009299 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 newbuf = vim_strnsave(*fnamep, l);
9301 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009302 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303
9304 vim_free(*bufp);
9305 *fnamep = *bufp = newbuf;
9306
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009307 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009308 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 }
9310
9311 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009312 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
9314
9315/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009316 * Get the short path (8.3) for the filename in "fname". The converted
9317 * path is returned in "bufp".
9318 *
9319 * Some of the directories specified in "fname" may not exist. This function
9320 * will shorten the existing directories at the beginning of the path and then
9321 * append the remaining non-existing path.
9322 *
9323 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009324 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009325 * bufp - Pointer to an allocated buffer for the filename.
9326 * fnamelen - Length of the filename pointed to by fname
9327 *
9328 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 */
9330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009331shortpath_for_invalid_fname(
9332 char_u **fname,
9333 char_u **bufp,
9334 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009336 char_u *short_fname, *save_fname, *pbuf_unused;
9337 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009339 int old_len, len;
9340 int new_len, sfx_len;
9341 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
9343 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009344 old_len = *fnamelen;
9345 save_fname = vim_strnsave(*fname, old_len);
9346 pbuf_unused = NULL;
9347 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009349 endp = save_fname + old_len - 1; /* Find the end of the copy */
9350 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009352 /*
9353 * Try shortening the supplied path till it succeeds by removing one
9354 * directory at a time from the tail of the path.
9355 */
9356 len = 0;
9357 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009359 /* go back one path-separator */
9360 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9361 --endp;
9362 if (endp <= save_fname)
9363 break; /* processed the complete path */
9364
9365 /*
9366 * Replace the path separator with a NUL and try to shorten the
9367 * resulting path.
9368 */
9369 ch = *endp;
9370 *endp = 0;
9371 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009372 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009373 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9374 {
9375 retval = FAIL;
9376 goto theend;
9377 }
9378 *endp = ch; /* preserve the string */
9379
9380 if (len > 0)
9381 break; /* successfully shortened the path */
9382
9383 /* failed to shorten the path. Skip the path separator */
9384 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 }
9386
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009387 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009389 /*
9390 * Succeeded in shortening the path. Now concatenate the shortened
9391 * path with the remaining path at the tail.
9392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009394 /* Compute the length of the new path. */
9395 sfx_len = (int)(save_endp - endp) + 1;
9396 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009398 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009400 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009402 /* There is not enough space in the currently allocated string,
9403 * copy it to a buffer big enough. */
9404 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009406 {
9407 retval = FAIL;
9408 goto theend;
9409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 else
9412 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009413 /* Transfer short_fname to the main buffer (it's big enough),
9414 * unless get_short_pathname() did its work in-place. */
9415 *fname = *bufp = save_fname;
9416 if (short_fname != save_fname)
9417 vim_strncpy(save_fname, short_fname, len);
9418 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009420
9421 /* concat the not-shortened part of the path */
9422 vim_strncpy(*fname + len, endp, sfx_len);
9423 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009425
9426theend:
9427 vim_free(pbuf_unused);
9428 vim_free(save_fname);
9429
9430 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431}
9432
9433/*
9434 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009435 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 */
9437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009438shortpath_for_partial(
9439 char_u **fnamep,
9440 char_u **bufp,
9441 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 int sepcount, len, tflen;
9444 char_u *p;
9445 char_u *pbuf, *tfname;
9446 int hasTilde;
9447
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448 /* Count up the path separators from the RHS.. so we know which part
9449 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009451 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (vim_ispathsep(*p))
9453 ++sepcount;
9454
9455 /* Need full path first (use expand_env() to remove a "~/") */
9456 hasTilde = (**fnamep == '~');
9457 if (hasTilde)
9458 pbuf = tfname = expand_env_save(*fnamep);
9459 else
9460 pbuf = tfname = FullName_save(*fnamep, FALSE);
9461
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009462 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009464 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9465 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466
9467 if (len == 0)
9468 {
9469 /* Don't have a valid filename, so shorten the rest of the
9470 * path if we can. This CAN give us invalid 8.3 filenames, but
9471 * there's not a lot of point in guessing what it might be.
9472 */
9473 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009474 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 }
9477
9478 /* Count the paths backward to find the beginning of the desired string. */
9479 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009480 {
9481#ifdef FEAT_MBYTE
9482 if (has_mbyte)
9483 p -= mb_head_off(tfname, p);
9484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (vim_ispathsep(*p))
9486 {
9487 if (sepcount == 0 || (hasTilde && sepcount == 1))
9488 break;
9489 else
9490 sepcount --;
9491 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 if (hasTilde)
9494 {
9495 --p;
9496 if (p >= tfname)
9497 *p = '~';
9498 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009499 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
9501 else
9502 ++p;
9503
9504 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9505 vim_free(*bufp);
9506 *fnamelen = (int)STRLEN(p);
9507 *bufp = pbuf;
9508 *fnamep = p;
9509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009510 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511}
9512#endif /* WIN3264 */
9513
9514/*
9515 * Adjust a filename, according to a string of modifiers.
9516 * *fnamep must be NUL terminated when called. When returning, the length is
9517 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009518 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 * When there is an error, *fnamep is set to NULL.
9520 */
9521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009522modify_fname(
9523 char_u *src, /* string with modifiers */
9524 int *usedlen, /* characters after src that are used */
9525 char_u **fnamep, /* file name so far */
9526 char_u **bufp, /* buffer for allocated file name or NULL */
9527 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529 int valid = 0;
9530 char_u *tail;
9531 char_u *s, *p, *pbuf;
9532 char_u dirname[MAXPATHL];
9533 int c;
9534 int has_fullname = 0;
9535#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009536 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 int has_shortname = 0;
9538#endif
9539
9540repeat:
9541 /* ":p" - full path/file_name */
9542 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9543 {
9544 has_fullname = 1;
9545
9546 valid |= VALID_PATH;
9547 *usedlen += 2;
9548
9549 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9550 if ((*fnamep)[0] == '~'
9551#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9552 && ((*fnamep)[1] == '/'
9553# ifdef BACKSLASH_IN_FILENAME
9554 || (*fnamep)[1] == '\\'
9555# endif
9556 || (*fnamep)[1] == NUL)
9557
9558#endif
9559 )
9560 {
9561 *fnamep = expand_env_save(*fnamep);
9562 vim_free(*bufp); /* free any allocated file name */
9563 *bufp = *fnamep;
9564 if (*fnamep == NULL)
9565 return -1;
9566 }
9567
9568 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009569 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
9571 if (vim_ispathsep(*p)
9572 && p[1] == '.'
9573 && (p[2] == NUL
9574 || vim_ispathsep(p[2])
9575 || (p[2] == '.'
9576 && (p[3] == NUL || vim_ispathsep(p[3])))))
9577 break;
9578 }
9579
9580 /* FullName_save() is slow, don't use it when not needed. */
9581 if (*p != NUL || !vim_isAbsName(*fnamep))
9582 {
9583 *fnamep = FullName_save(*fnamep, *p != NUL);
9584 vim_free(*bufp); /* free any allocated file name */
9585 *bufp = *fnamep;
9586 if (*fnamep == NULL)
9587 return -1;
9588 }
9589
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009590#ifdef WIN3264
9591# if _WIN32_WINNT >= 0x0500
9592 if (vim_strchr(*fnamep, '~') != NULL)
9593 {
9594 /* Expand 8.3 filename to full path. Needed to make sure the same
9595 * file does not have two different names.
9596 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9597 p = alloc(_MAX_PATH + 1);
9598 if (p != NULL)
9599 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009600 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009601 {
9602 vim_free(*bufp);
9603 *bufp = *fnamep = p;
9604 }
9605 else
9606 vim_free(p);
9607 }
9608 }
9609# endif
9610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 /* Append a path separator to a directory. */
9612 if (mch_isdir(*fnamep))
9613 {
9614 /* Make room for one or two extra characters. */
9615 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9616 vim_free(*bufp); /* free any allocated file name */
9617 *bufp = *fnamep;
9618 if (*fnamep == NULL)
9619 return -1;
9620 add_pathsep(*fnamep);
9621 }
9622 }
9623
9624 /* ":." - path relative to the current directory */
9625 /* ":~" - path relative to the home directory */
9626 /* ":8" - shortname path - postponed till after */
9627 while (src[*usedlen] == ':'
9628 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9629 {
9630 *usedlen += 2;
9631 if (c == '8')
9632 {
9633#ifdef WIN3264
9634 has_shortname = 1; /* Postpone this. */
9635#endif
9636 continue;
9637 }
9638 pbuf = NULL;
9639 /* Need full path first (use expand_env() to remove a "~/") */
9640 if (!has_fullname)
9641 {
9642 if (c == '.' && **fnamep == '~')
9643 p = pbuf = expand_env_save(*fnamep);
9644 else
9645 p = pbuf = FullName_save(*fnamep, FALSE);
9646 }
9647 else
9648 p = *fnamep;
9649
9650 has_fullname = 0;
9651
9652 if (p != NULL)
9653 {
9654 if (c == '.')
9655 {
9656 mch_dirname(dirname, MAXPATHL);
9657 s = shorten_fname(p, dirname);
9658 if (s != NULL)
9659 {
9660 *fnamep = s;
9661 if (pbuf != NULL)
9662 {
9663 vim_free(*bufp); /* free any allocated file name */
9664 *bufp = pbuf;
9665 pbuf = NULL;
9666 }
9667 }
9668 }
9669 else
9670 {
9671 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9672 /* Only replace it when it starts with '~' */
9673 if (*dirname == '~')
9674 {
9675 s = vim_strsave(dirname);
9676 if (s != NULL)
9677 {
9678 *fnamep = s;
9679 vim_free(*bufp);
9680 *bufp = s;
9681 }
9682 }
9683 }
9684 vim_free(pbuf);
9685 }
9686 }
9687
9688 tail = gettail(*fnamep);
9689 *fnamelen = (int)STRLEN(*fnamep);
9690
9691 /* ":h" - head, remove "/file_name", can be repeated */
9692 /* Don't remove the first "/" or "c:\" */
9693 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9694 {
9695 valid |= VALID_HEAD;
9696 *usedlen += 2;
9697 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009698 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009699 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 *fnamelen = (int)(tail - *fnamep);
9701#ifdef VMS
9702 if (*fnamelen > 0)
9703 *fnamelen += 1; /* the path separator is part of the path */
9704#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009705 if (*fnamelen == 0)
9706 {
9707 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9708 p = vim_strsave((char_u *)".");
9709 if (p == NULL)
9710 return -1;
9711 vim_free(*bufp);
9712 *bufp = *fnamep = tail = p;
9713 *fnamelen = 1;
9714 }
9715 else
9716 {
9717 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009718 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 }
9721
9722 /* ":8" - shortname */
9723 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9724 {
9725 *usedlen += 2;
9726#ifdef WIN3264
9727 has_shortname = 1;
9728#endif
9729 }
9730
9731#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009732 /*
9733 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 */
9735 if (has_shortname)
9736 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009737 /* Copy the string if it is shortened by :h and when it wasn't copied
9738 * yet, because we are going to change it in place. Avoids changing
9739 * the buffer name for "%:8". */
9740 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 {
9742 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009743 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 return -1;
9745 vim_free(*bufp);
9746 *bufp = *fnamep = p;
9747 }
9748
9749 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009750 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (!has_fullname && !vim_isAbsName(*fnamep))
9752 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009753 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 return -1;
9755 }
9756 else
9757 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009758 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759
Bram Moolenaardc935552011-08-17 15:23:23 +02009760 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009762 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 return -1;
9764
9765 if (l == 0)
9766 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009767 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009769 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 return -1;
9771 }
9772 *fnamelen = l;
9773 }
9774 }
9775#endif /* WIN3264 */
9776
9777 /* ":t" - tail, just the basename */
9778 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9779 {
9780 *usedlen += 2;
9781 *fnamelen -= (int)(tail - *fnamep);
9782 *fnamep = tail;
9783 }
9784
9785 /* ":e" - extension, can be repeated */
9786 /* ":r" - root, without extension, can be repeated */
9787 while (src[*usedlen] == ':'
9788 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9789 {
9790 /* find a '.' in the tail:
9791 * - for second :e: before the current fname
9792 * - otherwise: The last '.'
9793 */
9794 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9795 s = *fnamep - 2;
9796 else
9797 s = *fnamep + *fnamelen - 1;
9798 for ( ; s > tail; --s)
9799 if (s[0] == '.')
9800 break;
9801 if (src[*usedlen + 1] == 'e') /* :e */
9802 {
9803 if (s > tail)
9804 {
9805 *fnamelen += (int)(*fnamep - (s + 1));
9806 *fnamep = s + 1;
9807#ifdef VMS
9808 /* cut version from the extension */
9809 s = *fnamep + *fnamelen - 1;
9810 for ( ; s > *fnamep; --s)
9811 if (s[0] == ';')
9812 break;
9813 if (s > *fnamep)
9814 *fnamelen = s - *fnamep;
9815#endif
9816 }
9817 else if (*fnamep <= tail)
9818 *fnamelen = 0;
9819 }
9820 else /* :r */
9821 {
9822 if (s > tail) /* remove one extension */
9823 *fnamelen = (int)(s - *fnamep);
9824 }
9825 *usedlen += 2;
9826 }
9827
9828 /* ":s?pat?foo?" - substitute */
9829 /* ":gs?pat?foo?" - global substitute */
9830 if (src[*usedlen] == ':'
9831 && (src[*usedlen + 1] == 's'
9832 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9833 {
9834 char_u *str;
9835 char_u *pat;
9836 char_u *sub;
9837 int sep;
9838 char_u *flags;
9839 int didit = FALSE;
9840
9841 flags = (char_u *)"";
9842 s = src + *usedlen + 2;
9843 if (src[*usedlen + 1] == 'g')
9844 {
9845 flags = (char_u *)"g";
9846 ++s;
9847 }
9848
9849 sep = *s++;
9850 if (sep)
9851 {
9852 /* find end of pattern */
9853 p = vim_strchr(s, sep);
9854 if (p != NULL)
9855 {
9856 pat = vim_strnsave(s, (int)(p - s));
9857 if (pat != NULL)
9858 {
9859 s = p + 1;
9860 /* find end of substitution */
9861 p = vim_strchr(s, sep);
9862 if (p != NULL)
9863 {
9864 sub = vim_strnsave(s, (int)(p - s));
9865 str = vim_strnsave(*fnamep, *fnamelen);
9866 if (sub != NULL && str != NULL)
9867 {
9868 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009869 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 if (s != NULL)
9871 {
9872 *fnamep = s;
9873 *fnamelen = (int)STRLEN(s);
9874 vim_free(*bufp);
9875 *bufp = s;
9876 didit = TRUE;
9877 }
9878 }
9879 vim_free(sub);
9880 vim_free(str);
9881 }
9882 vim_free(pat);
9883 }
9884 }
9885 /* after using ":s", repeat all the modifiers */
9886 if (didit)
9887 goto repeat;
9888 }
9889 }
9890
Bram Moolenaar26df0922014-02-23 23:39:13 +01009891 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9892 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009893 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009894 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009895 if (c != NUL)
9896 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009897 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009898 if (c != NUL)
9899 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009900 if (p == NULL)
9901 return -1;
9902 vim_free(*bufp);
9903 *bufp = *fnamep = p;
9904 *fnamelen = (int)STRLEN(p);
9905 *usedlen += 2;
9906 }
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 return valid;
9909}
9910
9911/*
9912 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009913 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 * "flags" can be "g" to do a global substitute.
9915 * Returns an allocated string, NULL for error.
9916 */
9917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918do_string_sub(
9919 char_u *str,
9920 char_u *pat,
9921 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009922 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009923 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 int sublen;
9926 regmatch_T regmatch;
9927 int i;
9928 int do_all;
9929 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009930 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 garray_T ga;
9932 char_u *ret;
9933 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009934 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935
9936 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9937 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009938 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 ga_init2(&ga, 1, 200);
9941
9942 do_all = (flags[0] == 'g');
9943
9944 regmatch.rm_ic = p_ic;
9945 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9946 if (regmatch.regprog != NULL)
9947 {
9948 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009949 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9951 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009952 /* Skip empty match except for first match. */
9953 if (regmatch.startp[0] == regmatch.endp[0])
9954 {
9955 if (zero_width == regmatch.startp[0])
9956 {
9957 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009958 i = MB_PTR2LEN(tail);
9959 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9960 (size_t)i);
9961 ga.ga_len += i;
9962 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009963 continue;
9964 }
9965 zero_width = regmatch.startp[0];
9966 }
9967
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 /*
9969 * Get some space for a temporary buffer to do the substitution
9970 * into. It will contain:
9971 * - The text up to where the match is.
9972 * - The substituted text.
9973 * - The text after the match.
9974 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009975 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009976 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9978 {
9979 ga_clear(&ga);
9980 break;
9981 }
9982
9983 /* copy the text up to where the match is */
9984 i = (int)(regmatch.startp[0] - tail);
9985 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9986 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009987 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 + ga.ga_len + i, TRUE, TRUE, FALSE);
9989 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009990 tail = regmatch.endp[0];
9991 if (*tail == NUL)
9992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 if (!do_all)
9994 break;
9995 }
9996
9997 if (ga.ga_data != NULL)
9998 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9999
Bram Moolenaar473de612013-06-08 18:19:48 +020010000 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 }
10002
10003 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10004 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010005 if (p_cpo == empty_option)
10006 p_cpo = save_cpo;
10007 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010008 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010009 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
10011 return ret;
10012}
10013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 static int
10015filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10016{
10017 typval_T rettv;
10018 typval_T argv[3];
10019 char_u buf[NUMBUFLEN];
10020 char_u *s;
10021 int retval = FAIL;
10022 int dummy;
10023
10024 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10025 argv[0] = vimvars[VV_KEY].vv_tv;
10026 argv[1] = vimvars[VV_VAL].vv_tv;
10027 if (expr->v_type == VAR_FUNC)
10028 {
10029 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010030 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10031 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010032 goto theend;
10033 }
10034 else if (expr->v_type == VAR_PARTIAL)
10035 {
10036 partial_T *partial = expr->vval.v_partial;
10037
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010038 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010039 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10040 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010041 goto theend;
10042 }
10043 else
10044 {
10045 s = get_tv_string_buf_chk(expr, buf);
10046 if (s == NULL)
10047 goto theend;
10048 s = skipwhite(s);
10049 if (eval1(&s, &rettv, TRUE) == FAIL)
10050 goto theend;
10051 if (*s != NUL) /* check for trailing chars after expr */
10052 {
10053 EMSG2(_(e_invexpr2), s);
10054 goto theend;
10055 }
10056 }
10057 if (map)
10058 {
10059 /* map(): replace the list item value */
10060 clear_tv(tv);
10061 rettv.v_lock = 0;
10062 *tv = rettv;
10063 }
10064 else
10065 {
10066 int error = FALSE;
10067
10068 /* filter(): when expr is zero remove the item */
10069 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10070 clear_tv(&rettv);
10071 /* On type error, nothing has been removed; return FAIL to stop the
10072 * loop. The error message was given by get_tv_number_chk(). */
10073 if (error)
10074 goto theend;
10075 }
10076 retval = OK;
10077theend:
10078 clear_tv(&vimvars[VV_VAL].vv_tv);
10079 return retval;
10080}
10081
10082
10083/*
10084 * Implementation of map() and filter().
10085 */
10086 void
10087filter_map(typval_T *argvars, typval_T *rettv, int map)
10088{
10089 typval_T *expr;
10090 listitem_T *li, *nli;
10091 list_T *l = NULL;
10092 dictitem_T *di;
10093 hashtab_T *ht;
10094 hashitem_T *hi;
10095 dict_T *d = NULL;
10096 typval_T save_val;
10097 typval_T save_key;
10098 int rem;
10099 int todo;
10100 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10101 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10102 : N_("filter() argument"));
10103 int save_did_emsg;
10104 int idx = 0;
10105
10106 if (argvars[0].v_type == VAR_LIST)
10107 {
10108 if ((l = argvars[0].vval.v_list) == NULL
10109 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10110 return;
10111 }
10112 else if (argvars[0].v_type == VAR_DICT)
10113 {
10114 if ((d = argvars[0].vval.v_dict) == NULL
10115 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10116 return;
10117 }
10118 else
10119 {
10120 EMSG2(_(e_listdictarg), ermsg);
10121 return;
10122 }
10123
10124 expr = &argvars[1];
10125 /* On type errors, the preceding call has already displayed an error
10126 * message. Avoid a misleading error message for an empty string that
10127 * was not passed as argument. */
10128 if (expr->v_type != VAR_UNKNOWN)
10129 {
10130 prepare_vimvar(VV_VAL, &save_val);
10131
10132 /* We reset "did_emsg" to be able to detect whether an error
10133 * occurred during evaluation of the expression. */
10134 save_did_emsg = did_emsg;
10135 did_emsg = FALSE;
10136
10137 prepare_vimvar(VV_KEY, &save_key);
10138 if (argvars[0].v_type == VAR_DICT)
10139 {
10140 vimvars[VV_KEY].vv_type = VAR_STRING;
10141
10142 ht = &d->dv_hashtab;
10143 hash_lock(ht);
10144 todo = (int)ht->ht_used;
10145 for (hi = ht->ht_array; todo > 0; ++hi)
10146 {
10147 if (!HASHITEM_EMPTY(hi))
10148 {
10149 int r;
10150
10151 --todo;
10152 di = HI2DI(hi);
10153 if (map &&
10154 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10155 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10156 break;
10157 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10158 r = filter_map_one(&di->di_tv, expr, map, &rem);
10159 clear_tv(&vimvars[VV_KEY].vv_tv);
10160 if (r == FAIL || did_emsg)
10161 break;
10162 if (!map && rem)
10163 {
10164 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10165 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10166 break;
10167 dictitem_remove(d, di);
10168 }
10169 }
10170 }
10171 hash_unlock(ht);
10172 }
10173 else
10174 {
10175 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10176
10177 for (li = l->lv_first; li != NULL; li = nli)
10178 {
10179 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10180 break;
10181 nli = li->li_next;
10182 vimvars[VV_KEY].vv_nr = idx;
10183 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10184 || did_emsg)
10185 break;
10186 if (!map && rem)
10187 listitem_remove(l, li);
10188 ++idx;
10189 }
10190 }
10191
10192 restore_vimvar(VV_KEY, &save_key);
10193 restore_vimvar(VV_VAL, &save_val);
10194
10195 did_emsg |= save_did_emsg;
10196 }
10197
10198 copy_tv(&argvars[0], rettv);
10199}
10200
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */