blob: 1cced57ec0196c1eb1c60a4ddf16d44d747c275f [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 Moolenaar65e4c4f2017-10-14 23:24:25 +0200190 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
191 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200192 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
193 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
194 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000195};
196
197/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000198#define vv_type vv_di.di_tv.v_type
199#define vv_nr vv_di.di_tv.vval.v_number
200#define vv_float vv_di.di_tv.vval.v_float
201#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000202#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200203#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000204#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000205
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200206static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207#define vimvarht vimvardict.dv_hashtab
208
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100209static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
210static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
211static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100212static void list_glob_vars(int *first);
213static void list_buf_vars(int *first);
214static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100216static void list_vim_vars(int *first);
217static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100218static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
219static 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 +0100220static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
221static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
223static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
224static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
225static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000226
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static int eval2(char_u **arg, typval_T *rettv, int evaluate);
228static int eval3(char_u **arg, typval_T *rettv, int evaluate);
229static int eval4(char_u **arg, typval_T *rettv, int evaluate);
230static int eval5(char_u **arg, typval_T *rettv, int evaluate);
231static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
232static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000233
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
236static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static 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 +0200241static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static void delete_var(hashtab_T *ht, hashitem_T *hi);
244static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
245static 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 +0100246static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248/* for VIM_VERSION_ defines */
249#include "version.h"
250
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100251
252#if defined(EBCDIC) || defined(PROTO)
253/*
254 * Compare struct fst by function name.
255 */
256 static int
257compare_func_name(const void *s1, const void *s2)
258{
259 struct fst *p1 = (struct fst *)s1;
260 struct fst *p2 = (struct fst *)s2;
261
262 return STRCMP(p1->f_name, p2->f_name);
263}
264
265/*
266 * Sort the function table by function name.
267 * The sorting of the table above is ASCII dependant.
268 * On machines using EBCDIC we have to sort it.
269 */
270 static void
271sortFunctions(void)
272{
273 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
274
275 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
276}
277#endif
278
279
Bram Moolenaar33570922005-01-25 22:26:29 +0000280/*
281 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000282 */
283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100284eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000285{
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 int i;
287 struct vimvar *p;
288
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200289 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
290 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200291 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000292 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200293 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295 for (i = 0; i < VV_LEN; ++i)
296 {
297 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 if (STRLEN(p->vv_name) > 16)
299 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100300 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200301 getout(1);
302 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000303 STRCPY(p->vv_di.di_key, p->vv_name);
304 if (p->vv_flags & VV_RO)
305 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
306 else if (p->vv_flags & VV_RO_SBX)
307 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
308 else
309 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000310
311 /* add to v: scope dict, unless the value is not always available */
312 if (p->vv_type != VAR_UNKNOWN)
313 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000315 /* add to compat scope dict */
316 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000317 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100318 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
319
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100321 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200322 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100323 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100324
325 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
326 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
327 set_vim_var_nr(VV_NONE, VVAL_NONE);
328 set_vim_var_nr(VV_NULL, VVAL_NULL);
329
Bram Moolenaarf562e722016-07-19 17:25:25 +0200330 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
331 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
332 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
333 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
334 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
335 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
336 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
337 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
338 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
339 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
340
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200341 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200342
343#ifdef EBCDIC
344 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100345 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200346 */
347 sortFunctions();
348#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000349}
350
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000351#if defined(EXITFREE) || defined(PROTO)
352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000354{
355 int i;
356 struct vimvar *p;
357
358 for (i = 0; i < VV_LEN; ++i)
359 {
360 p = &vimvars[i];
361 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000362 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000363 vim_free(p->vv_str);
364 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 }
366 else if (p->vv_di.di_tv.v_type == VAR_LIST)
367 {
368 list_unref(p->vv_list);
369 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000370 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000371 }
372 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000373 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 hash_clear(&compat_hashtab);
375
Bram Moolenaard9fba312005-06-26 22:34:35 +0000376 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100377# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200378 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100379# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000380
381 /* global variables */
382 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000383
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000384 /* autoloaded script names */
385 ga_clear_strings(&ga_loaded);
386
Bram Moolenaarcca74132013-09-25 21:00:28 +0200387 /* Script-local variables. First clear all the variables and in a second
388 * loop free the scriptvar_T, because a variable in one script might hold
389 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200392 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 ga_clear(&ga_scripts);
395
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000396 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200397 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000398
399 /* functions */
400 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000401}
402#endif
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 * Set an internal variable to a string value. Creates the variable if it does
407 * not already exist.
408 */
409 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100410set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000412 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000413 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415 val = vim_strsave(value);
416 if (val != NULL)
417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000418 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000419 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000421 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000422 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 }
424 }
425}
426
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200428#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000429static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static char_u *redir_endp = NULL;
431static char_u *redir_varname = NULL;
432
433/*
434 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100435 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000436 * Returns OK if successfully completed the setup. FAIL otherwise.
437 */
438 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100439var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000440{
441 int save_emsg;
442 int err;
443 typval_T tv;
444
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000445 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000446 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000447 {
448 EMSG(_(e_invarg));
449 return FAIL;
450 }
451
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000452 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000453 redir_varname = vim_strsave(name);
454 if (redir_varname == NULL)
455 return FAIL;
456
457 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
458 if (redir_lval == NULL)
459 {
460 var_redir_stop();
461 return FAIL;
462 }
463
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000464 /* The output is stored in growarray "redir_ga" until redirection ends. */
465 ga_init2(&redir_ga, (int)sizeof(char), 500);
466
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100468 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000469 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
471 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200472 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp != NULL && *redir_endp != NUL)
474 /* Trailing characters are present after the variable name */
475 EMSG(_(e_trailing));
476 else
477 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000478 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 var_redir_stop();
480 return FAIL;
481 }
482
483 /* check if we can write to the variable: set it to or append an empty
484 * string */
485 save_emsg = did_emsg;
486 did_emsg = FALSE;
487 tv.v_type = VAR_STRING;
488 tv.vval.v_string = (char_u *)"";
489 if (append)
490 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
491 else
492 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200493 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000495 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 if (err)
497 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000498 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 var_redir_stop();
500 return FAIL;
501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502
503 return OK;
504}
505
506/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000507 * Append "value[value_len]" to the variable set by var_redir_start().
508 * The actual appending is postponed until redirection ends, because the value
509 * appended may in fact be the string we write to, changing it may cause freed
510 * memory to be used:
511 * :redir => foo
512 * :let foo
513 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 */
515 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100516var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000518 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000519
520 if (redir_lval == NULL)
521 return;
522
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000523 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000524 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000526 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000529 {
530 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 }
533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535}
536
537/*
538 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000539 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 */
541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100542var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000544 typval_T tv;
545
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200546 if (EVALCMD_BUSY)
547 {
548 redir_lval = NULL;
549 return;
550 }
551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000552 if (redir_lval != NULL)
553 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000554 /* If there was no error: assign the text to the variable. */
555 if (redir_endp != NULL)
556 {
557 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
558 tv.v_type = VAR_STRING;
559 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200560 /* Call get_lval() again, if it's inside a Dict or List it may
561 * have changed. */
562 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100563 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200564 if (redir_endp != NULL && redir_lval->ll_name != NULL)
565 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
566 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000567 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000569 /* free the collected output */
570 vim_free(redir_ga.ga_data);
571 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000572
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573 vim_free(redir_lval);
574 redir_lval = NULL;
575 }
576 vim_free(redir_varname);
577 redir_varname = NULL;
578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaar48570482017-10-30 21:48:41 +0100699 static int
700eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
701{
702 char_u *s;
703 int dummy;
704 char_u buf[NUMBUFLEN];
705
706 if (expr->v_type == VAR_FUNC)
707 {
708 s = expr->vval.v_string;
709 if (s == NULL || *s == NUL)
710 return FAIL;
711 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
712 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
713 return FAIL;
714 }
715 else if (expr->v_type == VAR_PARTIAL)
716 {
717 partial_T *partial = expr->vval.v_partial;
718
719 s = partial_name(partial);
720 if (s == NULL || *s == NUL)
721 return FAIL;
722 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
723 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
724 return FAIL;
725 }
726 else
727 {
728 s = get_tv_string_buf_chk(expr, buf);
729 if (s == NULL)
730 return FAIL;
731 s = skipwhite(s);
732 if (eval1(&s, rettv, TRUE) == FAIL)
733 return FAIL;
734 if (*s != NUL) /* check for trailing chars after expr */
735 {
736 EMSG2(_(e_invexpr2), s);
737 return FAIL;
738 }
739 }
740 return OK;
741}
742
743/*
744 * Like eval_to_bool() but using a typval_T instead of a string.
745 * Works for string, funcref and partial.
746 */
747 int
748eval_expr_to_bool(typval_T *expr, int *error)
749{
750 typval_T rettv;
751 int res;
752
753 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
754 {
755 *error = TRUE;
756 return FALSE;
757 }
758 res = (get_tv_number_chk(&rettv, error) != 0);
759 clear_tv(&rettv);
760 return res;
761}
762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763/*
764 * Top level evaluation function, returning a string. If "skip" is TRUE,
765 * only parsing to "nextcmd" is done, without reporting errors. Return
766 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
767 */
768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100769eval_to_string_skip(
770 char_u *arg,
771 char_u **nextcmd,
772 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 char_u *retval;
776
777 if (skip)
778 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 retval = NULL;
781 else
782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783 retval = vim_strsave(get_tv_string(&tv));
784 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 if (skip)
787 --emsg_skip;
788
789 return retval;
790}
791
792/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000793 * Skip over an expression at "*pp".
794 * Return FAIL for an error, OK otherwise.
795 */
796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800
801 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000803}
804
805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000807 * When "convert" is TRUE convert a List into a sequence of lines and convert
808 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 * Return pointer to allocated memory, or NULL for failure.
810 */
811 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100812eval_to_string(
813 char_u *arg,
814 char_u **nextcmd,
815 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000819 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000820#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 retval = NULL;
826 else
827 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000828 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829 {
830 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000831 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200832 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200833 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200834 if (tv.vval.v_list->lv_len > 0)
835 ga_append(&ga, NL);
836 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837 ga_append(&ga, NUL);
838 retval = (char_u *)ga.ga_data;
839 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000840#ifdef FEAT_FLOAT
841 else if (convert && tv.v_type == VAR_FLOAT)
842 {
843 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
844 retval = vim_strsave(numbuf);
845 }
846#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000847 else
848 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
852 return retval;
853}
854
855/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000856 * Call eval_to_string() without using current local variables and using
857 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 */
859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100860eval_to_string_safe(
861 char_u *arg,
862 char_u **nextcmd,
863 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 char_u *retval;
866 void *save_funccalp;
867
868 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000869 if (use_sandbox)
870 ++sandbox;
871 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000872 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000873 if (use_sandbox)
874 --sandbox;
875 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 restore_funccal(save_funccalp);
877 return retval;
878}
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880/*
881 * Top level evaluation function, returning a number.
882 * Evaluates "expr" silently.
883 * Returns -1 for an error.
884 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200885 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200889 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000890 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 ++emsg_off;
893
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 retval = -1;
896 else
897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000898 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901 --emsg_off;
902
903 return retval;
904}
905
Bram Moolenaara40058a2005-07-11 22:42:07 +0000906/*
907 * Prepare v: variable "idx" to be used.
908 * Save the current typeval in "save_tv".
909 * When not used yet add the variable to the v: hashtable.
910 */
911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000913{
914 *save_tv = vimvars[idx].vv_tv;
915 if (vimvars[idx].vv_type == VAR_UNKNOWN)
916 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
917}
918
919/*
920 * Restore v: variable "idx" to typeval "save_tv".
921 * When no longer defined, remove the variable from the v: hashtable.
922 */
923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100924restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000925{
926 hashitem_T *hi;
927
Bram Moolenaara40058a2005-07-11 22:42:07 +0000928 vimvars[idx].vv_tv = *save_tv;
929 if (vimvars[idx].vv_type == VAR_UNKNOWN)
930 {
931 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
932 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100933 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000934 else
935 hash_remove(&vimvarht, hi);
936 }
937}
938
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000939#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000940/*
941 * Evaluate an expression to a list with suggestions.
942 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000943 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000944 */
945 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000947{
948 typval_T save_val;
949 typval_T rettv;
950 list_T *list = NULL;
951 char_u *p = skipwhite(expr);
952
953 /* Set "v:val" to the bad word. */
954 prepare_vimvar(VV_VAL, &save_val);
955 vimvars[VV_VAL].vv_type = VAR_STRING;
956 vimvars[VV_VAL].vv_str = badword;
957 if (p_verbose == 0)
958 ++emsg_off;
959
960 if (eval1(&p, &rettv, TRUE) == OK)
961 {
962 if (rettv.v_type != VAR_LIST)
963 clear_tv(&rettv);
964 else
965 list = rettv.vval.v_list;
966 }
967
968 if (p_verbose == 0)
969 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000970 restore_vimvar(VV_VAL, &save_val);
971
972 return list;
973}
974
975/*
976 * "list" is supposed to contain two items: a word and a number. Return the
977 * word in "pp" and the number as the return value.
978 * Return -1 if anything isn't right.
979 * Used to get the good word and score from the eval_spell_expr() result.
980 */
981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000983{
984 listitem_T *li;
985
986 li = list->lv_first;
987 if (li == NULL)
988 return -1;
989 *pp = get_tv_string(&li->li_tv);
990
991 li = li->li_next;
992 if (li == NULL)
993 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200994 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000995}
996#endif
997
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000999 * Top level evaluation function.
1000 * Returns an allocated typval_T with the result.
1001 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001002 */
1003 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001005{
1006 typval_T *tv;
1007
1008 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001009 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001010 {
1011 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001013 }
1014
1015 return tv;
1016}
1017
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001020 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001021 * Uses argv[argc] for the function arguments. Only Number and String
1022 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001023 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026call_vim_function(
1027 char_u *func,
1028 int argc,
1029 char_u **argv,
1030 int safe, /* use the sandbox */
1031 int str_arg_only, /* all arguments are strings */
1032 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001035 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int len;
1037 int i;
1038 int doesrange;
1039 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001040 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001042 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001044 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 for (i = 0; i < argc; i++)
1047 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001048 /* Pass a NULL or empty argument as an empty string */
1049 if (argv[i] == NULL || *argv[i] == NUL)
1050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001051 argvars[i].v_type = VAR_STRING;
1052 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001053 continue;
1054 }
1055
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001056 if (str_arg_only)
1057 len = 0;
1058 else
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001059 {
1060 /* Recognize a number argument, the others must be strings. A dash
1061 * is a string too. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001062 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001063 if (len == 1 && *argv[i] == '-')
1064 len = 0;
1065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (len != 0 && len == (int)STRLEN(argv[i]))
1067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001068 argvars[i].v_type = VAR_NUMBER;
1069 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
1071 else
1072 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 argvars[i].v_type = VAR_STRING;
1074 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 }
1077
1078 if (safe)
1079 {
1080 save_funccalp = save_funccal();
1081 ++sandbox;
1082 }
1083
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001085 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001087 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (safe)
1089 {
1090 --sandbox;
1091 restore_funccal(save_funccalp);
1092 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093 vim_free(argvars);
1094
1095 if (ret == FAIL)
1096 clear_tv(rettv);
1097
1098 return ret;
1099}
1100
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001101/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001102 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001103 * Returns -1 when calling the function fails.
1104 * Uses argv[argc] for the function arguments.
1105 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001106 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001107call_func_retnr(
1108 char_u *func,
1109 int argc,
1110 char_u **argv,
1111 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001112{
1113 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001114 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001115
1116 /* All arguments are passed as strings, no conversion to number. */
1117 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1118 return -1;
1119
1120 retval = get_tv_number_chk(&rettv, NULL);
1121 clear_tv(&rettv);
1122 return retval;
1123}
1124
1125#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1126 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1127
Bram Moolenaar4f688582007-07-24 12:34:30 +00001128# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001129/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001130 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001131 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001132 * Uses argv[argc] for the function arguments.
1133 */
1134 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001135call_func_retstr(
1136 char_u *func,
1137 int argc,
1138 char_u **argv,
1139 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001140{
1141 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001142 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001143
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001144 /* All arguments are passed as strings, no conversion to number. */
1145 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146 return NULL;
1147
1148 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001149 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 return retval;
1151}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001152# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001153
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001154/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001155 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001157 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158 */
1159 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001160call_func_retlist(
1161 char_u *func,
1162 int argc,
1163 char_u **argv,
1164 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165{
1166 typval_T rettv;
1167
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001168 /* All arguments are passed as strings, no conversion to number. */
1169 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001170 return NULL;
1171
1172 if (rettv.v_type != VAR_LIST)
1173 {
1174 clear_tv(&rettv);
1175 return NULL;
1176 }
1177
1178 return rettv.vval.v_list;
1179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181
Bram Moolenaar05159a02005-02-26 23:04:13 +00001182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#ifdef FEAT_FOLDING
1184/*
1185 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1186 * it in "*cp". Doesn't give error messages.
1187 */
1188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001189eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001192 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001194 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1195 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001198 if (use_sandbox)
1199 ++sandbox;
1200 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 retval = 0;
1204 else
1205 {
1206 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (tv.v_type == VAR_NUMBER)
1208 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001209 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = 0;
1211 else
1212 {
1213 /* If the result is a string, check if there is a non-digit before
1214 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (!VIM_ISDIGIT(*s) && *s != '-')
1217 *cp = *s++;
1218 retval = atol((char *)s);
1219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001223 if (use_sandbox)
1224 --sandbox;
1225 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001227 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228}
1229#endif
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 * ":let" list all variable values
1233 * ":let var1 var2" list variable values
1234 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001235 * ":let var += expr" assignment command.
1236 * ":let var -= expr" assignment command.
1237 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
1240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001245 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247 int var_count = 0;
1248 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001250 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001251 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaardb552d602006-03-23 22:59:57 +00001253 argend = skip_var_list(arg, &var_count, &semicolon);
1254 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001255 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001256 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1257 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001258 expr = skipwhite(argend);
1259 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1260 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001262 /*
1263 * ":let" without "=": list variables
1264 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001265 if (*arg == '[')
1266 EMSG(_(e_invarg));
1267 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001269 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001273 list_glob_vars(&first);
1274 list_buf_vars(&first);
1275 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001276 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001277 list_script_vars(&first);
1278 list_func_vars(&first);
1279 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 eap->nextcmd = check_nextcmd(arg);
1282 }
1283 else
1284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 op[0] = '=';
1286 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001287 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001289 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1290 op[0] = *expr; /* +=, -= or .= */
1291 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001292 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001293 else
1294 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (eap->skip)
1297 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001298 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (eap->skip)
1300 {
1301 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 --emsg_skip;
1304 }
1305 else if (i != FAIL)
1306 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001308 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312}
1313
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001314/*
1315 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1316 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 * When "nextchars" is not NULL it points to a string with characters that
1318 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1319 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001320 * Returns OK or FAIL;
1321 */
1322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001323ex_let_vars(
1324 char_u *arg_start,
1325 typval_T *tv,
1326 int copy, /* copy values from "tv", don't move */
1327 int semicolon, /* from skip_var_list() */
1328 int var_count, /* from skip_var_list() */
1329 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001330{
1331 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001333 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 listitem_T *item;
1335 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336
1337 if (*arg != '[')
1338 {
1339 /*
1340 * ":let var = expr" or ":for var in list"
1341 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001342 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001343 return FAIL;
1344 return OK;
1345 }
1346
1347 /*
1348 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1349 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001350 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001351 {
1352 EMSG(_(e_listreq));
1353 return FAIL;
1354 }
1355
1356 i = list_len(l);
1357 if (semicolon == 0 && var_count < i)
1358 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001359 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001360 return FAIL;
1361 }
1362 if (var_count - semicolon > i)
1363 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001364 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001365 return FAIL;
1366 }
1367
1368 item = l->lv_first;
1369 while (*arg != ']')
1370 {
1371 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001373 item = item->li_next;
1374 if (arg == NULL)
1375 return FAIL;
1376
1377 arg = skipwhite(arg);
1378 if (*arg == ';')
1379 {
1380 /* Put the rest of the list (may be empty) in the var after ';'.
1381 * Create a new list for this. */
1382 l = list_alloc();
1383 if (l == NULL)
1384 return FAIL;
1385 while (item != NULL)
1386 {
1387 list_append_tv(l, &item->li_tv);
1388 item = item->li_next;
1389 }
1390
1391 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001392 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 ltv.vval.v_list = l;
1394 l->lv_refcount = 1;
1395
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1397 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001398 clear_tv(&ltv);
1399 if (arg == NULL)
1400 return FAIL;
1401 break;
1402 }
1403 else if (*arg != ',' && *arg != ']')
1404 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001405 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001406 return FAIL;
1407 }
1408 }
1409
1410 return OK;
1411}
1412
1413/*
1414 * Skip over assignable variable "var" or list of variables "[var, var]".
1415 * Used for ":let varvar = expr" and ":for varvar in expr".
1416 * For "[var, var]" increment "*var_count" for each variable.
1417 * for "[var, var; var]" set "semicolon".
1418 * Return NULL for an error.
1419 */
1420 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001421skip_var_list(
1422 char_u *arg,
1423 int *var_count,
1424 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425{
1426 char_u *p, *s;
1427
1428 if (*arg == '[')
1429 {
1430 /* "[var, var]": find the matching ']'. */
1431 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 {
1434 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1435 s = skip_var_one(p);
1436 if (s == p)
1437 {
1438 EMSG2(_(e_invarg2), p);
1439 return NULL;
1440 }
1441 ++*var_count;
1442
1443 p = skipwhite(s);
1444 if (*p == ']')
1445 break;
1446 else if (*p == ';')
1447 {
1448 if (*semicolon == 1)
1449 {
1450 EMSG(_("Double ; in list of variables"));
1451 return NULL;
1452 }
1453 *semicolon = 1;
1454 }
1455 else if (*p != ',')
1456 {
1457 EMSG2(_(e_invarg2), p);
1458 return NULL;
1459 }
1460 }
1461 return p + 1;
1462 }
1463 else
1464 return skip_var_one(arg);
1465}
1466
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001467/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001468 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001469 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001470 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001473{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001474 if (*arg == '@' && arg[1] != NUL)
1475 return arg + 2;
1476 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1477 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001478}
1479
Bram Moolenaara7043832005-01-21 11:56:39 +00001480/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001481 * List variables for hashtab "ht" with prefix "prefix".
1482 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001483 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485list_hashtable_vars(
1486 hashtab_T *ht,
1487 char_u *prefix,
1488 int empty,
1489 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001490{
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 hashitem_T *hi;
1492 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001493 int todo;
1494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001495 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001496 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1497 {
1498 if (!HASHITEM_EMPTY(hi))
1499 {
1500 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 di = HI2DI(hi);
1502 if (empty || di->di_tv.v_type != VAR_STRING
1503 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001504 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001505 }
1506 }
1507}
1508
1509/*
1510 * List global variables.
1511 */
1512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001513list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001514{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001515 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001516}
1517
1518/*
1519 * List buffer variables.
1520 */
1521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001523{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001524 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001525 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001526}
1527
1528/*
1529 * List window variables.
1530 */
1531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001533{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001534 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001535 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001536}
1537
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001538/*
1539 * List tab page variables.
1540 */
1541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001542list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001543{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001544 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001545 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001546}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001547
Bram Moolenaara7043832005-01-21 11:56:39 +00001548/*
1549 * List Vim variables.
1550 */
1551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001554 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555}
1556
1557/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001558 * List script-local variables, if there is a script.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001562{
1563 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001564 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1565 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001566}
1567
1568/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 * List variables in "arg".
1570 */
1571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573{
1574 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 char_u *name_start;
1578 char_u *arg_subsc;
1579 char_u *tofree;
1580 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581
1582 while (!ends_excmd(*arg) && !got_int)
1583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001584 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001586 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001587 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 {
1589 emsg_severe = TRUE;
1590 EMSG(_(e_trailing));
1591 break;
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596 /* get_name_len() takes care of expanding curly braces */
1597 name_start = name = arg;
1598 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1599 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001601 /* This is mainly to keep test 49 working: when expanding
1602 * curly braces fails overrule the exception error message. */
1603 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001605 emsg_severe = TRUE;
1606 EMSG2(_(e_invarg2), arg);
1607 break;
1608 }
1609 error = TRUE;
1610 }
1611 else
1612 {
1613 if (tofree != NULL)
1614 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001615 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 else
1618 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001619 /* handle d.key, l[idx], f(expr) */
1620 arg_subsc = arg;
1621 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001622 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001624 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001625 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001626 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001628 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001629 case 'g': list_glob_vars(first); break;
1630 case 'b': list_buf_vars(first); break;
1631 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001632 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001633 case 'v': list_vim_vars(first); break;
1634 case 's': list_script_vars(first); break;
1635 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636 default:
1637 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001638 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001639 }
1640 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001641 {
1642 char_u numbuf[NUMBUFLEN];
1643 char_u *tf;
1644 int c;
1645 char_u *s;
1646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001647 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001648 c = *arg;
1649 *arg = NUL;
1650 list_one_var_a((char_u *)"",
1651 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001652 tv.v_type,
1653 s == NULL ? (char_u *)"" : s,
1654 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 *arg = c;
1656 vim_free(tf);
1657 }
1658 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 }
1661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001662
1663 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001665
1666 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001667 }
1668
1669 return arg;
1670}
1671
1672/*
1673 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1674 * Returns a pointer to the char just after the var name.
1675 * Returns NULL if there is an error.
1676 */
1677 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001678ex_let_one(
1679 char_u *arg, /* points to variable name */
1680 typval_T *tv, /* value to assign to variable */
1681 int copy, /* copy value from "tv" */
1682 char_u *endchars, /* valid chars after variable name or NULL */
1683 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684{
1685 int c1;
1686 char_u *name;
1687 char_u *p;
1688 char_u *arg_end = NULL;
1689 int len;
1690 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692
1693 /*
1694 * ":let $VAR = expr": Set environment variable.
1695 */
1696 if (*arg == '$')
1697 {
1698 /* Find the end of the name. */
1699 ++arg;
1700 name = arg;
1701 len = get_env_len(&arg);
1702 if (len == 0)
1703 EMSG2(_(e_invarg2), name - 1);
1704 else
1705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 if (op != NULL && (*op == '+' || *op == '-'))
1707 EMSG2(_(e_letwrong), op);
1708 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001710 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001711 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 {
1713 c1 = name[len];
1714 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001715 p = get_tv_string_chk(tv);
1716 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 {
1718 int mustfree = FALSE;
1719 char_u *s = vim_getenv(name, &mustfree);
1720
1721 if (s != NULL)
1722 {
1723 p = tofree = concat_str(s, p);
1724 if (mustfree)
1725 vim_free(s);
1726 }
1727 }
1728 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731 if (STRICMP(name, "HOME") == 0)
1732 init_homedir();
1733 else if (didset_vim && STRICMP(name, "VIM") == 0)
1734 didset_vim = FALSE;
1735 else if (didset_vimruntime
1736 && STRICMP(name, "VIMRUNTIME") == 0)
1737 didset_vimruntime = FALSE;
1738 arg_end = arg;
1739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 }
1743 }
1744 }
1745
1746 /*
1747 * ":let &option = expr": Set option value.
1748 * ":let &l:option = expr": Set local option value.
1749 * ":let &g:option = expr": Set global option value.
1750 */
1751 else if (*arg == '&')
1752 {
1753 /* Find the end of the name. */
1754 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 if (p == NULL || (endchars != NULL
1756 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 EMSG(_(e_letunexp));
1758 else
1759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 long n;
1761 int opt_type;
1762 long numval;
1763 char_u *stringval = NULL;
1764 char_u *s;
1765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766 c1 = *p;
1767 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001769 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 s = get_tv_string_chk(tv); /* != NULL if number or string */
1771 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 {
1773 opt_type = get_option_value(arg, &numval,
1774 &stringval, opt_flags);
1775 if ((opt_type == 1 && *op == '.')
1776 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001779 s = NULL; /* don't set the value */
1780 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 else
1782 {
1783 if (opt_type == 1) /* number */
1784 {
1785 if (*op == '+')
1786 n = numval + n;
1787 else
1788 n = numval - n;
1789 }
1790 else if (opt_type == 0 && stringval != NULL) /* string */
1791 {
1792 s = concat_str(stringval, s);
1793 vim_free(stringval);
1794 stringval = s;
1795 }
1796 }
1797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001798 if (s != NULL)
1799 {
1800 set_option_value(arg, n, s, opt_flags);
1801 arg_end = p;
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 }
1806 }
1807
1808 /*
1809 * ":let @r = expr": Set register contents.
1810 */
1811 else if (*arg == '@')
1812 {
1813 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814 if (op != NULL && (*op == '+' || *op == '-'))
1815 EMSG2(_(e_letwrong), op);
1816 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 EMSG(_(e_letunexp));
1819 else
1820 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001821 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 char_u *s;
1823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001824 p = get_tv_string_chk(tv);
1825 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001827 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 if (s != NULL)
1829 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001830 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 vim_free(s);
1832 }
1833 }
1834 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001835 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001837 arg_end = arg + 1;
1838 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001839 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 }
1841 }
1842
1843 /*
1844 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001847 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001851 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1855 EMSG(_(e_letunexp));
1856 else
1857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001858 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001859 arg_end = p;
1860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 }
1864
1865 else
1866 EMSG2(_(e_invarg2), arg);
1867
1868 return arg_end;
1869}
1870
1871/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 * Get an lval: variable, Dict item or List item that can be assigned a value
1873 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1874 * "name.key", "name.key[expr]" etc.
1875 * Indexing only works if "name" is an existing List or Dictionary.
1876 * "name" points to the start of the name.
1877 * If "rettv" is not NULL it points to the value to be assigned.
1878 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1879 * wrong; must end in space or cmd separator.
1880 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001881 * flags:
1882 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001883 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001884 * GLV_NO_AUTOLOAD: do not use script autoloading
1885 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891get_lval(
1892 char_u *name,
1893 typval_T *rettv,
1894 lval_T *lp,
1895 int unlet,
1896 int skip,
1897 int flags, /* GLV_ values */
1898 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 char_u *expr_start, *expr_end;
1902 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 dictitem_T *v;
1904 typval_T var1;
1905 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001908 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001909 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001911 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915
1916 if (skip)
1917 {
1918 /* When skipping just find the end of the name. */
1919 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001920 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 }
1922
1923 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001924 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 if (expr_start != NULL)
1926 {
1927 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001928 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 && *p != '[' && *p != '.')
1930 {
1931 EMSG(_(e_trailing));
1932 return NULL;
1933 }
1934
1935 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1936 if (lp->ll_exp_name == NULL)
1937 {
1938 /* Report an invalid expression in braces, unless the
1939 * expression evaluation has been cancelled due to an
1940 * aborting error, an interrupt, or an exception. */
1941 if (!aborting() && !quiet)
1942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001943 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 EMSG2(_(e_invarg2), name);
1945 return NULL;
1946 }
1947 }
1948 lp->ll_name = lp->ll_exp_name;
1949 }
1950 else
1951 lp->ll_name = name;
1952
1953 /* Without [idx] or .key we are done. */
1954 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1955 return p;
1956
1957 cc = *p;
1958 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001959 /* Only pass &ht when we would write to the variable, it prevents autoload
1960 * as well. */
1961 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1962 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (v == NULL && !quiet)
1964 EMSG2(_(e_undefvar), lp->ll_name);
1965 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 if (v == NULL)
1967 return NULL;
1968
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 /*
1970 * Loop until no more [idx] or .key is following.
1971 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001973 var1.v_type = VAR_UNKNOWN;
1974 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1978 && !(lp->ll_tv->v_type == VAR_DICT
1979 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001981 if (!quiet)
1982 EMSG(_("E689: Can only index a List or Dictionary"));
1983 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 if (!quiet)
1988 EMSG(_("E708: [:] must come last"));
1989 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001991
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 len = -1;
1993 if (*p == '.')
1994 {
1995 key = p + 1;
1996 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1997 ;
1998 if (len == 0)
1999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 if (!quiet)
2001 EMSG(_(e_emptykey));
2002 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 }
2004 p = key + len;
2005 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002006 else
2007 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002008 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002009 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002010 if (*p == ':')
2011 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002012 else
2013 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 empty1 = FALSE;
2015 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002017 if (get_tv_string_chk(&var1) == NULL)
2018 {
2019 /* not a number or string */
2020 clear_tv(&var1);
2021 return NULL;
2022 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 }
2024
2025 /* Optionally get the second index [ :expr]. */
2026 if (*p == ':')
2027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002034 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 if (rettv != NULL && (rettv->v_type != VAR_LIST
2036 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002037 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 if (!quiet)
2039 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 }
2043 p = skipwhite(p + 1);
2044 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 else
2047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2050 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002051 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002054 if (get_tv_string_chk(&var2) == NULL)
2055 {
2056 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002057 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002058 clear_tv(&var2);
2059 return NULL;
2060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002066
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002068 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 if (!quiet)
2070 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002071 clear_tv(&var1);
2072 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 }
2075
2076 /* Skip to past ']'. */
2077 ++p;
2078 }
2079
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002081 {
2082 if (len == -1)
2083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002085 key = get_tv_string_chk(&var1); /* is number or string */
2086 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002090 }
2091 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002092 lp->ll_list = NULL;
2093 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002094 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002095
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002096 /* When assigning to a scope dictionary check that a function and
2097 * variable name is valid (only variable name unless it is l: or
2098 * g: dictionary). Disallow overwriting a builtin function. */
2099 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002100 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002101 int prevval;
2102 int wrong;
2103
2104 if (len != -1)
2105 {
2106 prevval = key[len];
2107 key[len] = NUL;
2108 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002109 else
2110 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002111 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2112 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002113 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002114 || !valid_varname(key);
2115 if (len != -1)
2116 key[len] = prevval;
2117 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002118 return NULL;
2119 }
2120
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002123 /* Can't add "v:" variable. */
2124 if (lp->ll_dict == &vimvardict)
2125 {
2126 EMSG2(_(e_illvar), name);
2127 return NULL;
2128 }
2129
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002130 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002134 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 }
2138 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002142 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 p = NULL;
2145 break;
2146 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002147 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002148 else if ((flags & GLV_READ_ONLY) == 0
2149 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002150 {
2151 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002152 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002153 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002154
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002155 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002157 }
2158 else
2159 {
2160 /*
2161 * Get the number and item for the only or first index of the List.
2162 */
2163 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002165 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002166 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002167 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002168 clear_tv(&var1);
2169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002170 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 lp->ll_list = lp->ll_tv->vval.v_list;
2172 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2173 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002174 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002175 if (lp->ll_n1 < 0)
2176 {
2177 lp->ll_n1 = 0;
2178 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2179 }
2180 }
2181 if (lp->ll_li == NULL)
2182 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002183 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002184 if (!quiet)
2185 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 }
2188
2189 /*
2190 * May need to find the item or absolute index for the second
2191 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 * When no index given: "lp->ll_empty2" is TRUE.
2193 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002196 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002197 lp->ll_n2 = (long)get_tv_number(&var2);
2198 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002199 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002204 {
2205 if (!quiet)
2206 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002208 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 }
2211
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2213 if (lp->ll_n1 < 0)
2214 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2215 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002216 {
2217 if (!quiet)
2218 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002220 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002221 }
2222
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 }
2226
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002227 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 return p;
2229}
2230
2231/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002232 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002235clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236{
2237 vim_free(lp->ll_exp_name);
2238 vim_free(lp->ll_newkey);
2239}
2240
2241/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002242 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 */
2246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002247set_var_lval(
2248 lval_T *lp,
2249 char_u *endp,
2250 typval_T *rettv,
2251 int copy,
2252 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253{
2254 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 listitem_T *ri;
2256 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257
2258 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002260 cc = *endp;
2261 *endp = NUL;
2262 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002264 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265
Bram Moolenaar79518e22017-02-17 16:31:35 +01002266 /* handle +=, -= and .= */
2267 di = NULL;
2268 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2269 &tv, &di, TRUE, FALSE) == OK)
2270 {
2271 if ((di == NULL
2272 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2273 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2274 FALSE)))
2275 && tv_op(&tv, rettv, op) == OK)
2276 set_var(lp->ll_name, &tv, FALSE);
2277 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002280 else
2281 set_var(lp->ll_name, rettv, copy);
2282 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002284 else if (tv_check_lock(lp->ll_newkey == NULL
2285 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002286 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002287 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 else if (lp->ll_range)
2289 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002290 listitem_T *ll_li = lp->ll_li;
2291 int ll_n1 = lp->ll_n1;
2292
2293 /*
2294 * Check whether any of the list items is locked
2295 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002296 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002297 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002298 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002299 return;
2300 ri = ri->li_next;
2301 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2302 break;
2303 ll_li = ll_li->li_next;
2304 ++ll_n1;
2305 }
2306
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 /*
2308 * Assign the List values to the list items.
2309 */
2310 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002311 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 if (op != NULL && *op != '=')
2313 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2314 else
2315 {
2316 clear_tv(&lp->ll_li->li_tv);
2317 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2318 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 ri = ri->li_next;
2320 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2321 break;
2322 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002323 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002325 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002326 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 ri = NULL;
2328 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002329 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002330 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 lp->ll_li = lp->ll_li->li_next;
2332 ++lp->ll_n1;
2333 }
2334 if (ri != NULL)
2335 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 else if (lp->ll_empty2
2337 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 : lp->ll_n1 != lp->ll_n2)
2339 EMSG(_("E711: List value has not enough items"));
2340 }
2341 else
2342 {
2343 /*
2344 * Assign to a List or Dictionary item.
2345 */
2346 if (lp->ll_newkey != NULL)
2347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 if (op != NULL && *op != '=')
2349 {
2350 EMSG2(_(e_letwrong), op);
2351 return;
2352 }
2353
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002355 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 if (di == NULL)
2357 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002358 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2359 {
2360 vim_free(di);
2361 return;
2362 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 else if (op != NULL && *op != '=')
2366 {
2367 tv_op(lp->ll_tv, rettv, op);
2368 return;
2369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 /*
2374 * Assign the value to the variable or list item.
2375 */
2376 if (copy)
2377 copy_tv(rettv, lp->ll_tv);
2378 else
2379 {
2380 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002381 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 }
2384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385}
2386
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002387/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2389 * Returns OK or FAIL.
2390 */
2391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002392tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002394 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 char_u numbuf[NUMBUFLEN];
2396 char_u *s;
2397
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002398 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2399 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2400 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 {
2402 switch (tv1->v_type)
2403 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002404 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 case VAR_DICT:
2406 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002407 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002408 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002409 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002410 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 break;
2412
2413 case VAR_LIST:
2414 if (*op != '+' || tv2->v_type != VAR_LIST)
2415 break;
2416 /* List += List */
2417 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2418 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2419 return OK;
2420
2421 case VAR_NUMBER:
2422 case VAR_STRING:
2423 if (tv2->v_type == VAR_LIST)
2424 break;
2425 if (*op == '+' || *op == '-')
2426 {
2427 /* nr += nr or nr -= nr*/
2428 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002429#ifdef FEAT_FLOAT
2430 if (tv2->v_type == VAR_FLOAT)
2431 {
2432 float_T f = n;
2433
2434 if (*op == '+')
2435 f += tv2->vval.v_float;
2436 else
2437 f -= tv2->vval.v_float;
2438 clear_tv(tv1);
2439 tv1->v_type = VAR_FLOAT;
2440 tv1->vval.v_float = f;
2441 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002443#endif
2444 {
2445 if (*op == '+')
2446 n += get_tv_number(tv2);
2447 else
2448 n -= get_tv_number(tv2);
2449 clear_tv(tv1);
2450 tv1->v_type = VAR_NUMBER;
2451 tv1->vval.v_number = n;
2452 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 }
2454 else
2455 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002456 if (tv2->v_type == VAR_FLOAT)
2457 break;
2458
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 /* str .= str */
2460 s = get_tv_string(tv1);
2461 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2462 clear_tv(tv1);
2463 tv1->v_type = VAR_STRING;
2464 tv1->vval.v_string = s;
2465 }
2466 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002468 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002469#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002470 {
2471 float_T f;
2472
2473 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2474 && tv2->v_type != VAR_NUMBER
2475 && tv2->v_type != VAR_STRING))
2476 break;
2477 if (tv2->v_type == VAR_FLOAT)
2478 f = tv2->vval.v_float;
2479 else
2480 f = get_tv_number(tv2);
2481 if (*op == '+')
2482 tv1->vval.v_float += f;
2483 else
2484 tv1->vval.v_float -= f;
2485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002486#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002487 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 }
2489 }
2490
2491 EMSG2(_(e_letwrong), op);
2492 return FAIL;
2493}
2494
2495/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002496 * Evaluate the expression used in a ":for var in expr" command.
2497 * "arg" points to "var".
2498 * Set "*errp" to TRUE for an error, FALSE otherwise;
2499 * Return a pointer that holds the info. Null when there is an error.
2500 */
2501 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002502eval_for_line(
2503 char_u *arg,
2504 int *errp,
2505 char_u **nextcmdp,
2506 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507{
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 typval_T tv;
2511 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002512
2513 *errp = TRUE; /* default: there is an error */
2514
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002516 if (fi == NULL)
2517 return NULL;
2518
2519 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2520 if (expr == NULL)
2521 return fi;
2522
2523 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002524 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002526 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527 return fi;
2528 }
2529
2530 if (skip)
2531 ++emsg_skip;
2532 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2533 {
2534 *errp = FALSE;
2535 if (!skip)
2536 {
2537 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002538 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002539 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002540 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002541 clear_tv(&tv);
2542 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002543 else if (l == NULL)
2544 {
2545 /* a null list is like an empty list: do nothing */
2546 clear_tv(&tv);
2547 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002548 else
2549 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002550 /* No need to increment the refcount, it's already set for the
2551 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 fi->fi_list = l;
2553 list_add_watch(l, &fi->fi_lw);
2554 fi->fi_lw.lw_item = l->lv_first;
2555 }
2556 }
2557 }
2558 if (skip)
2559 --emsg_skip;
2560
2561 return fi;
2562}
2563
2564/*
2565 * Use the first item in a ":for" list. Advance to the next.
2566 * Assign the values to the variable (list). "arg" points to the first one.
2567 * Return TRUE when a valid item was found, FALSE when at end of list or
2568 * something wrong.
2569 */
2570 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002571next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002572{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002573 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002574 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002576
2577 item = fi->fi_lw.lw_item;
2578 if (item == NULL)
2579 result = FALSE;
2580 else
2581 {
2582 fi->fi_lw.lw_item = item->li_next;
2583 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2584 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2585 }
2586 return result;
2587}
2588
2589/*
2590 * Free the structure used to store info used by ":for".
2591 */
2592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002593free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594{
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002596
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002597 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002598 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002599 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002600 list_unref(fi->fi_list);
2601 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002602 vim_free(fi);
2603}
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2606
2607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002608set_context_for_expression(
2609 expand_T *xp,
2610 char_u *arg,
2611 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 int got_eq = FALSE;
2614 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002615 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002617 if (cmdidx == CMD_let)
2618 {
2619 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002620 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002621 {
2622 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002623 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002624 {
2625 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002626 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002627 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002628 break;
2629 }
2630 return;
2631 }
2632 }
2633 else
2634 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2635 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 while ((xp->xp_pattern = vim_strpbrk(arg,
2637 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2638 {
2639 c = *xp->xp_pattern;
2640 if (c == '&')
2641 {
2642 c = xp->xp_pattern[1];
2643 if (c == '&')
2644 {
2645 ++xp->xp_pattern;
2646 xp->xp_context = cmdidx != CMD_let || got_eq
2647 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2648 }
2649 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002652 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2653 xp->xp_pattern += 2;
2654
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657 else if (c == '$')
2658 {
2659 /* environment variable */
2660 xp->xp_context = EXPAND_ENV_VARS;
2661 }
2662 else if (c == '=')
2663 {
2664 got_eq = TRUE;
2665 xp->xp_context = EXPAND_EXPRESSION;
2666 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002667 else if (c == '#'
2668 && xp->xp_context == EXPAND_EXPRESSION)
2669 {
2670 /* Autoload function/variable contains '#'. */
2671 break;
2672 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002673 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 && xp->xp_context == EXPAND_FUNCTIONS
2675 && vim_strchr(xp->xp_pattern, '(') == NULL)
2676 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002677 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 break;
2679 }
2680 else if (cmdidx != CMD_let || got_eq)
2681 {
2682 if (c == '"') /* string */
2683 {
2684 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2685 if (c == '\\' && xp->xp_pattern[1] != NUL)
2686 ++xp->xp_pattern;
2687 xp->xp_context = EXPAND_NOTHING;
2688 }
2689 else if (c == '\'') /* literal string */
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2693 /* skip */ ;
2694 xp->xp_context = EXPAND_NOTHING;
2695 }
2696 else if (c == '|')
2697 {
2698 if (xp->xp_pattern[1] == '|')
2699 {
2700 ++xp->xp_pattern;
2701 xp->xp_context = EXPAND_EXPRESSION;
2702 }
2703 else
2704 xp->xp_context = EXPAND_COMMANDS;
2705 }
2706 else
2707 xp->xp_context = EXPAND_EXPRESSION;
2708 }
2709 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710 /* Doesn't look like something valid, expand as an expression
2711 * anyway. */
2712 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 arg = xp->xp_pattern;
2714 if (*arg != NUL)
2715 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2716 /* skip */ ;
2717 }
2718 xp->xp_pattern = arg;
2719}
2720
2721#endif /* FEAT_CMDL_COMPL */
2722
2723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * ":unlet[!] var1 ... " command.
2725 */
2726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002727ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002729 ex_unletlock(eap, eap->arg, 0);
2730}
2731
2732/*
2733 * ":lockvar" and ":unlockvar" commands
2734 */
2735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002736ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002737{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002739 int deep = 2;
2740
2741 if (eap->forceit)
2742 deep = -1;
2743 else if (vim_isdigit(*arg))
2744 {
2745 deep = getdigits(&arg);
2746 arg = skipwhite(arg);
2747 }
2748
2749 ex_unletlock(eap, arg, deep);
2750}
2751
2752/*
2753 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2754 */
2755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002756ex_unletlock(
2757 exarg_T *eap,
2758 char_u *argstart,
2759 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002760{
2761 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002764 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 do
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002769 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002770 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lv.ll_name == NULL)
2772 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002773 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (name_end != NULL)
2777 {
2778 emsg_severe = TRUE;
2779 EMSG(_(e_trailing));
2780 }
2781 if (!(eap->skip || error))
2782 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
2784 }
2785
2786 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002787 {
2788 if (eap->cmdidx == CMD_unlet)
2789 {
2790 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2791 error = TRUE;
2792 }
2793 else
2794 {
2795 if (do_lock_var(&lv, name_end, deep,
2796 eap->cmdidx == CMD_lockvar) == FAIL)
2797 error = TRUE;
2798 }
2799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (!eap->skip)
2802 clear_lval(&lv);
2803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 arg = skipwhite(name_end);
2805 } while (!ends_excmd(*arg));
2806
2807 eap->nextcmd = check_nextcmd(arg);
2808}
2809
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002811do_unlet_var(
2812 lval_T *lp,
2813 char_u *name_end,
2814 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 int ret = OK;
2817 int cc;
2818
2819 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 cc = *name_end;
2822 *name_end = NUL;
2823
2824 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002825 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002829 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002830 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002831 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002832 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002833 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 else if (lp->ll_range)
2835 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002836 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002837 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002838 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002839
2840 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2841 {
2842 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002844 return FAIL;
2845 ll_li = li;
2846 ++ll_n1;
2847 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848
2849 /* Delete a range of List items. */
2850 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2851 {
2852 li = lp->ll_li->li_next;
2853 listitem_remove(lp->ll_list, lp->ll_li);
2854 lp->ll_li = li;
2855 ++lp->ll_n1;
2856 }
2857 }
2858 else
2859 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 /* unlet a List item. */
2862 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002865 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 }
2867
2868 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869}
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
2872 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002873 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 */
2875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002876do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 hashtab_T *ht;
2879 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002880 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002881 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002882 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 ht = find_var_ht(name, &varname);
2885 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002888 if (d == NULL)
2889 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002890 if (ht == &globvarht)
2891 d = &globvardict;
2892 else if (ht == &compat_hashtab)
2893 d = &vimvardict;
2894 else
2895 {
2896 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2897 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2898 }
2899 if (d == NULL)
2900 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002901 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 return FAIL;
2903 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002904 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002906 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002907 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002908 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002909 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002910 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002911 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002912 || var_check_ro(di->di_flags, name, FALSE)
2913 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002914 return FAIL;
2915
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 delete_var(ht, hi);
2917 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002920 if (forceit)
2921 return OK;
2922 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 return FAIL;
2924}
2925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926/*
2927 * Lock or unlock variable indicated by "lp".
2928 * "deep" is the levels to go (-1 for unlimited);
2929 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2930 */
2931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932do_lock_var(
2933 lval_T *lp,
2934 char_u *name_end,
2935 int deep,
2936 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002937{
2938 int ret = OK;
2939 int cc;
2940 dictitem_T *di;
2941
2942 if (deep == 0) /* nothing to do */
2943 return OK;
2944
2945 if (lp->ll_tv == NULL)
2946 {
2947 cc = *name_end;
2948 *name_end = NUL;
2949
2950 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002951 di = find_var(lp->ll_name, NULL, TRUE);
2952 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002954 else if ((di->di_flags & DI_FLAGS_FIX)
2955 && di->di_tv.v_type != VAR_DICT
2956 && di->di_tv.v_type != VAR_LIST)
2957 /* For historic reasons this error is not given for a list or dict.
2958 * E.g., the b: dict could be locked/unlocked. */
2959 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 else
2961 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002962 if (lock)
2963 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002965 di->di_flags &= ~DI_FLAGS_LOCK;
2966 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 }
2968 *name_end = cc;
2969 }
2970 else if (lp->ll_range)
2971 {
2972 listitem_T *li = lp->ll_li;
2973
2974 /* (un)lock a range of List items. */
2975 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2976 {
2977 item_lock(&li->li_tv, deep, lock);
2978 li = li->li_next;
2979 ++lp->ll_n1;
2980 }
2981 }
2982 else if (lp->ll_list != NULL)
2983 /* (un)lock a List item. */
2984 item_lock(&lp->ll_li->li_tv, deep, lock);
2985 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002986 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002987 item_lock(&lp->ll_di->di_tv, deep, lock);
2988
2989 return ret;
2990}
2991
2992/*
2993 * Lock or unlock an item. "deep" is nr of levels to go.
2994 */
2995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002996item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002997{
2998 static int recurse = 0;
2999 list_T *l;
3000 listitem_T *li;
3001 dict_T *d;
3002 hashitem_T *hi;
3003 int todo;
3004
3005 if (recurse >= DICT_MAXNEST)
3006 {
3007 EMSG(_("E743: variable nested too deep for (un)lock"));
3008 return;
3009 }
3010 if (deep == 0)
3011 return;
3012 ++recurse;
3013
3014 /* lock/unlock the item itself */
3015 if (lock)
3016 tv->v_lock |= VAR_LOCKED;
3017 else
3018 tv->v_lock &= ~VAR_LOCKED;
3019
3020 switch (tv->v_type)
3021 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003022 case VAR_UNKNOWN:
3023 case VAR_NUMBER:
3024 case VAR_STRING:
3025 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003026 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003027 case VAR_FLOAT:
3028 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003029 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003030 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003031 break;
3032
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003033 case VAR_LIST:
3034 if ((l = tv->vval.v_list) != NULL)
3035 {
3036 if (lock)
3037 l->lv_lock |= VAR_LOCKED;
3038 else
3039 l->lv_lock &= ~VAR_LOCKED;
3040 if (deep < 0 || deep > 1)
3041 /* recursive: lock/unlock the items the List contains */
3042 for (li = l->lv_first; li != NULL; li = li->li_next)
3043 item_lock(&li->li_tv, deep - 1, lock);
3044 }
3045 break;
3046 case VAR_DICT:
3047 if ((d = tv->vval.v_dict) != NULL)
3048 {
3049 if (lock)
3050 d->dv_lock |= VAR_LOCKED;
3051 else
3052 d->dv_lock &= ~VAR_LOCKED;
3053 if (deep < 0 || deep > 1)
3054 {
3055 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003056 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003057 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3058 {
3059 if (!HASHITEM_EMPTY(hi))
3060 {
3061 --todo;
3062 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3063 }
3064 }
3065 }
3066 }
3067 }
3068 --recurse;
3069}
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3072/*
3073 * Delete all "menutrans_" variables.
3074 */
3075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaar33570922005-01-25 22:26:29 +00003078 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003079 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaar33570922005-01-25 22:26:29 +00003081 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003082 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003084 {
3085 if (!HASHITEM_EMPTY(hi))
3086 {
3087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3089 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003090 }
3091 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003092 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093}
3094#endif
3095
3096#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3097
3098/*
3099 * Local string buffer for the next two functions to store a variable name
3100 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3101 * get_user_var_name().
3102 */
3103
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003104static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106static char_u *varnamebuf = NULL;
3107static int varnamebuflen = 0;
3108
3109/*
3110 * Function to concatenate a prefix and a variable name.
3111 */
3112 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 int len;
3116
3117 len = (int)STRLEN(name) + 3;
3118 if (len > varnamebuflen)
3119 {
3120 vim_free(varnamebuf);
3121 len += 10; /* some additional space */
3122 varnamebuf = alloc(len);
3123 if (varnamebuf == NULL)
3124 {
3125 varnamebuflen = 0;
3126 return NULL;
3127 }
3128 varnamebuflen = len;
3129 }
3130 *varnamebuf = prefix;
3131 varnamebuf[1] = ':';
3132 STRCPY(varnamebuf + 2, name);
3133 return varnamebuf;
3134}
3135
3136/*
3137 * Function given to ExpandGeneric() to obtain the list of user defined
3138 * (global/buffer/window/built-in) variable names.
3139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003141get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003143 static long_u gdone;
3144 static long_u bdone;
3145 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003146 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003147 static int vidx;
3148 static hashitem_T *hi;
3149 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003152 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003153 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003154 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003155 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003156
3157 /* Global variables */
3158 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003160 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003162 else
3163 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003164 while (HASHITEM_EMPTY(hi))
3165 ++hi;
3166 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3167 return cat_prefix_varname('g', hi->hi_key);
3168 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003170
3171 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003172 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003175 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003177 else
3178 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003179 while (HASHITEM_EMPTY(hi))
3180 ++hi;
3181 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003183
3184 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003185 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003188 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003190 else
3191 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003192 while (HASHITEM_EMPTY(hi))
3193 ++hi;
3194 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003197 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003198 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003199 if (tdone < ht->ht_used)
3200 {
3201 if (tdone++ == 0)
3202 hi = ht->ht_array;
3203 else
3204 ++hi;
3205 while (HASHITEM_EMPTY(hi))
3206 ++hi;
3207 return cat_prefix_varname('t', hi->hi_key);
3208 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003209
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 /* v: variables */
3211 if (vidx < VV_LEN)
3212 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 vim_free(varnamebuf);
3215 varnamebuf = NULL;
3216 varnamebuflen = 0;
3217 return NULL;
3218}
3219
3220#endif /* FEAT_CMDL_COMPL */
3221
3222/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003223 * Return TRUE if "pat" matches "text".
3224 * Does not use 'cpo' and always uses 'magic'.
3225 */
3226 static int
3227pattern_match(char_u *pat, char_u *text, int ic)
3228{
3229 int matches = FALSE;
3230 char_u *save_cpo;
3231 regmatch_T regmatch;
3232
3233 /* avoid 'l' flag in 'cpoptions' */
3234 save_cpo = p_cpo;
3235 p_cpo = (char_u *)"";
3236 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3237 if (regmatch.regprog != NULL)
3238 {
3239 regmatch.rm_ic = ic;
3240 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3241 vim_regfree(regmatch.regprog);
3242 }
3243 p_cpo = save_cpo;
3244 return matches;
3245}
3246
3247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * types for expressions.
3249 */
3250typedef enum
3251{
3252 TYPE_UNKNOWN = 0
3253 , TYPE_EQUAL /* == */
3254 , TYPE_NEQUAL /* != */
3255 , TYPE_GREATER /* > */
3256 , TYPE_GEQUAL /* >= */
3257 , TYPE_SMALLER /* < */
3258 , TYPE_SEQUAL /* <= */
3259 , TYPE_MATCH /* =~ */
3260 , TYPE_NOMATCH /* !~ */
3261} exptype_T;
3262
3263/*
3264 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003265 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3267 */
3268
3269/*
3270 * Handle zero level expression.
3271 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003273 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * Return OK or FAIL.
3275 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003277eval0(
3278 char_u *arg,
3279 typval_T *rettv,
3280 char_u **nextcmd,
3281 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 int ret;
3284 char_u *p;
3285
3286 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (ret == FAIL || !ends_excmd(*p))
3289 {
3290 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003291 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 /*
3293 * Report the invalid expression unless the expression evaluation has
3294 * been cancelled due to an aborting error, an interrupt, or an
3295 * exception.
3296 */
3297 if (!aborting())
3298 EMSG2(_(e_invexpr2), arg);
3299 ret = FAIL;
3300 }
3301 if (nextcmd != NULL)
3302 *nextcmd = check_nextcmd(p);
3303
3304 return ret;
3305}
3306
3307/*
3308 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003309 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 *
3311 * "arg" must point to the first non-white of the expression.
3312 * "arg" is advanced to the next non-white after the recognized expression.
3313 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003314 * Note: "rettv.v_lock" is not set.
3315 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 * Return OK or FAIL.
3317 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003319eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 /*
3325 * Get the first variable.
3326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 return FAIL;
3329
3330 if ((*arg)[0] == '?')
3331 {
3332 result = FALSE;
3333 if (evaluate)
3334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 int error = FALSE;
3336
3337 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003339 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 if (error)
3341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343
3344 /*
3345 * Get the second variable.
3346 */
3347 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return FAIL;
3350
3351 /*
3352 * Check for the ":".
3353 */
3354 if ((*arg)[0] != ':')
3355 {
3356 EMSG(_("E109: Missing ':' after '?'"));
3357 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return FAIL;
3360 }
3361
3362 /*
3363 * Get the third variable.
3364 */
3365 *arg = skipwhite(*arg + 1);
3366 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3367 {
3368 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return FAIL;
3371 }
3372 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375
3376 return OK;
3377}
3378
3379/*
3380 * Handle first level expression:
3381 * expr2 || expr2 || expr2 logical OR
3382 *
3383 * "arg" must point to the first non-white of the expression.
3384 * "arg" is advanced to the next non-white after the recognized expression.
3385 *
3386 * Return OK or FAIL.
3387 */
3388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
Bram Moolenaar33570922005-01-25 22:26:29 +00003391 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 long result;
3393 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /*
3397 * Get the first variable.
3398 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 return FAIL;
3401
3402 /*
3403 * Repeat until there is no following "||".
3404 */
3405 first = TRUE;
3406 result = FALSE;
3407 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3408 {
3409 if (evaluate && first)
3410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003414 if (error)
3415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 first = FALSE;
3417 }
3418
3419 /*
3420 * Get the second variable.
3421 */
3422 *arg = skipwhite(*arg + 2);
3423 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3424 return FAIL;
3425
3426 /*
3427 * Compute the result.
3428 */
3429 if (evaluate && !result)
3430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003431 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003434 if (error)
3435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 if (evaluate)
3438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 rettv->v_type = VAR_NUMBER;
3440 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 }
3443
3444 return OK;
3445}
3446
3447/*
3448 * Handle second level expression:
3449 * expr3 && expr3 && expr3 logical AND
3450 *
3451 * "arg" must point to the first non-white of the expression.
3452 * "arg" is advanced to the next non-white after the recognized expression.
3453 *
3454 * Return OK or FAIL.
3455 */
3456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003457eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 long result;
3461 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003462 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 /*
3465 * Get the first variable.
3466 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return FAIL;
3469
3470 /*
3471 * Repeat until there is no following "&&".
3472 */
3473 first = TRUE;
3474 result = TRUE;
3475 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3476 {
3477 if (evaluate && first)
3478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003479 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003482 if (error)
3483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 first = FALSE;
3485 }
3486
3487 /*
3488 * Get the second variable.
3489 */
3490 *arg = skipwhite(*arg + 2);
3491 if (eval4(arg, &var2, evaluate && result) == FAIL)
3492 return FAIL;
3493
3494 /*
3495 * Compute the result.
3496 */
3497 if (evaluate && result)
3498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003502 if (error)
3503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 if (evaluate)
3506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003507 rettv->v_type = VAR_NUMBER;
3508 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 }
3511
3512 return OK;
3513}
3514
3515/*
3516 * Handle third level expression:
3517 * var1 == var2
3518 * var1 =~ var2
3519 * var1 != var2
3520 * var1 !~ var2
3521 * var1 > var2
3522 * var1 >= var2
3523 * var1 < var2
3524 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003525 * var1 is var2
3526 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 *
3528 * "arg" must point to the first non-white of the expression.
3529 * "arg" is advanced to the next non-white after the recognized expression.
3530 *
3531 * Return OK or FAIL.
3532 */
3533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 char_u *p;
3538 int i;
3539 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003540 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003542 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 char_u *s1, *s2;
3544 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 /*
3548 * Get the first variable.
3549 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return FAIL;
3552
3553 p = *arg;
3554 switch (p[0])
3555 {
3556 case '=': if (p[1] == '=')
3557 type = TYPE_EQUAL;
3558 else if (p[1] == '~')
3559 type = TYPE_MATCH;
3560 break;
3561 case '!': if (p[1] == '=')
3562 type = TYPE_NEQUAL;
3563 else if (p[1] == '~')
3564 type = TYPE_NOMATCH;
3565 break;
3566 case '>': if (p[1] != '=')
3567 {
3568 type = TYPE_GREATER;
3569 len = 1;
3570 }
3571 else
3572 type = TYPE_GEQUAL;
3573 break;
3574 case '<': if (p[1] != '=')
3575 {
3576 type = TYPE_SMALLER;
3577 len = 1;
3578 }
3579 else
3580 type = TYPE_SEQUAL;
3581 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003582 case 'i': if (p[1] == 's')
3583 {
3584 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3585 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003586 i = p[len];
3587 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003588 {
3589 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3590 type_is = TRUE;
3591 }
3592 }
3593 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595
3596 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003597 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 */
3599 if (type != TYPE_UNKNOWN)
3600 {
3601 /* extra question mark appended: ignore case */
3602 if (p[len] == '?')
3603 {
3604 ic = TRUE;
3605 ++len;
3606 }
3607 /* extra '#' appended: match case */
3608 else if (p[len] == '#')
3609 {
3610 ic = FALSE;
3611 ++len;
3612 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003613 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 else
3615 ic = p_ic;
3616
3617 /*
3618 * Get the second variable.
3619 */
3620 *arg = skipwhite(p + len);
3621 if (eval5(arg, &var2, evaluate) == FAIL)
3622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 return FAIL;
3625 }
3626
3627 if (evaluate)
3628 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003629 if (type_is && rettv->v_type != var2.v_type)
3630 {
3631 /* For "is" a different type always means FALSE, for "notis"
3632 * it means TRUE. */
3633 n1 = (type == TYPE_NEQUAL);
3634 }
3635 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3636 {
3637 if (type_is)
3638 {
3639 n1 = (rettv->v_type == var2.v_type
3640 && rettv->vval.v_list == var2.vval.v_list);
3641 if (type == TYPE_NEQUAL)
3642 n1 = !n1;
3643 }
3644 else if (rettv->v_type != var2.v_type
3645 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3646 {
3647 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003648 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003649 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003650 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003651 clear_tv(rettv);
3652 clear_tv(&var2);
3653 return FAIL;
3654 }
3655 else
3656 {
3657 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003658 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3659 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003660 if (type == TYPE_NEQUAL)
3661 n1 = !n1;
3662 }
3663 }
3664
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003665 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3666 {
3667 if (type_is)
3668 {
3669 n1 = (rettv->v_type == var2.v_type
3670 && rettv->vval.v_dict == var2.vval.v_dict);
3671 if (type == TYPE_NEQUAL)
3672 n1 = !n1;
3673 }
3674 else if (rettv->v_type != var2.v_type
3675 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3676 {
3677 if (rettv->v_type != var2.v_type)
3678 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3679 else
3680 EMSG(_("E736: Invalid operation for Dictionary"));
3681 clear_tv(rettv);
3682 clear_tv(&var2);
3683 return FAIL;
3684 }
3685 else
3686 {
3687 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003688 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3689 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003690 if (type == TYPE_NEQUAL)
3691 n1 = !n1;
3692 }
3693 }
3694
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003695 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3696 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003697 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003698 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003699 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003700 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003701 clear_tv(rettv);
3702 clear_tv(&var2);
3703 return FAIL;
3704 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003705 if ((rettv->v_type == VAR_PARTIAL
3706 && rettv->vval.v_partial == NULL)
3707 || (var2.v_type == VAR_PARTIAL
3708 && var2.vval.v_partial == NULL))
3709 /* when a partial is NULL assume not equal */
3710 n1 = FALSE;
3711 else if (type_is)
3712 {
3713 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3714 /* strings are considered the same if their value is
3715 * the same */
3716 n1 = tv_equal(rettv, &var2, ic, FALSE);
3717 else if (rettv->v_type == VAR_PARTIAL
3718 && var2.v_type == VAR_PARTIAL)
3719 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3720 else
3721 n1 = FALSE;
3722 }
3723 else
3724 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003725 if (type == TYPE_NEQUAL)
3726 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003727 }
3728
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003729#ifdef FEAT_FLOAT
3730 /*
3731 * If one of the two variables is a float, compare as a float.
3732 * When using "=~" or "!~", always compare as string.
3733 */
3734 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3735 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3736 {
3737 float_T f1, f2;
3738
3739 if (rettv->v_type == VAR_FLOAT)
3740 f1 = rettv->vval.v_float;
3741 else
3742 f1 = get_tv_number(rettv);
3743 if (var2.v_type == VAR_FLOAT)
3744 f2 = var2.vval.v_float;
3745 else
3746 f2 = get_tv_number(&var2);
3747 n1 = FALSE;
3748 switch (type)
3749 {
3750 case TYPE_EQUAL: n1 = (f1 == f2); break;
3751 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3752 case TYPE_GREATER: n1 = (f1 > f2); break;
3753 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3754 case TYPE_SMALLER: n1 = (f1 < f2); break;
3755 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3756 case TYPE_UNKNOWN:
3757 case TYPE_MATCH:
3758 case TYPE_NOMATCH: break; /* avoid gcc warning */
3759 }
3760 }
3761#endif
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 /*
3764 * If one of the two variables is a number, compare as a number.
3765 * When using "=~" or "!~", always compare as string.
3766 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003767 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 n1 = get_tv_number(rettv);
3771 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 switch (type)
3773 {
3774 case TYPE_EQUAL: n1 = (n1 == n2); break;
3775 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3776 case TYPE_GREATER: n1 = (n1 > n2); break;
3777 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3778 case TYPE_SMALLER: n1 = (n1 < n2); break;
3779 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3780 case TYPE_UNKNOWN:
3781 case TYPE_MATCH:
3782 case TYPE_NOMATCH: break; /* avoid gcc warning */
3783 }
3784 }
3785 else
3786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 s1 = get_tv_string_buf(rettv, buf1);
3788 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3790 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3791 else
3792 i = 0;
3793 n1 = FALSE;
3794 switch (type)
3795 {
3796 case TYPE_EQUAL: n1 = (i == 0); break;
3797 case TYPE_NEQUAL: n1 = (i != 0); break;
3798 case TYPE_GREATER: n1 = (i > 0); break;
3799 case TYPE_GEQUAL: n1 = (i >= 0); break;
3800 case TYPE_SMALLER: n1 = (i < 0); break;
3801 case TYPE_SEQUAL: n1 = (i <= 0); break;
3802
3803 case TYPE_MATCH:
3804 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003805 n1 = pattern_match(s2, s1, ic);
3806 if (type == TYPE_NOMATCH)
3807 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 break;
3809
3810 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3811 }
3812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
3814 clear_tv(&var2);
3815 rettv->v_type = VAR_NUMBER;
3816 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 }
3819
3820 return OK;
3821}
3822
3823/*
3824 * Handle fourth level expression:
3825 * + number addition
3826 * - number subtraction
3827 * . string concatenation
3828 *
3829 * "arg" must point to the first non-white of the expression.
3830 * "arg" is advanced to the next non-white after the recognized expression.
3831 *
3832 * Return OK or FAIL.
3833 */
3834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 typval_T var2;
3838 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003840 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003841#ifdef FEAT_FLOAT
3842 float_T f1 = 0, f2 = 0;
3843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 char_u *s1, *s2;
3845 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3846 char_u *p;
3847
3848 /*
3849 * Get the first variable.
3850 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003851 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return FAIL;
3853
3854 /*
3855 * Repeat computing, until no '+', '-' or '.' is following.
3856 */
3857 for (;;)
3858 {
3859 op = **arg;
3860 if (op != '+' && op != '-' && op != '.')
3861 break;
3862
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003863 if ((op != '+' || rettv->v_type != VAR_LIST)
3864#ifdef FEAT_FLOAT
3865 && (op == '.' || rettv->v_type != VAR_FLOAT)
3866#endif
3867 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868 {
3869 /* For "list + ...", an illegal use of the first operand as
3870 * a number cannot be determined before evaluating the 2nd
3871 * operand: if this is also a list, all is ok.
3872 * For "something . ...", "something - ..." or "non-list + ...",
3873 * we know that the first operand needs to be a string or number
3874 * without evaluating the 2nd operand. So check before to avoid
3875 * side effects after an error. */
3876 if (evaluate && get_tv_string_chk(rettv) == NULL)
3877 {
3878 clear_tv(rettv);
3879 return FAIL;
3880 }
3881 }
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 /*
3884 * Get the second variable.
3885 */
3886 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003887 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return FAIL;
3891 }
3892
3893 if (evaluate)
3894 {
3895 /*
3896 * Compute the result.
3897 */
3898 if (op == '.')
3899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3901 s2 = get_tv_string_buf_chk(&var2, buf2);
3902 if (s2 == NULL) /* type error ? */
3903 {
3904 clear_tv(rettv);
3905 clear_tv(&var2);
3906 return FAIL;
3907 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003908 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 clear_tv(rettv);
3910 rettv->v_type = VAR_STRING;
3911 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003913 else if (op == '+' && rettv->v_type == VAR_LIST
3914 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003915 {
3916 /* concatenate Lists */
3917 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3918 &var3) == FAIL)
3919 {
3920 clear_tv(rettv);
3921 clear_tv(&var2);
3922 return FAIL;
3923 }
3924 clear_tv(rettv);
3925 *rettv = var3;
3926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 else
3928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003929 int error = FALSE;
3930
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931#ifdef FEAT_FLOAT
3932 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003934 f1 = rettv->vval.v_float;
3935 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003936 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003937 else
3938#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003940 n1 = get_tv_number_chk(rettv, &error);
3941 if (error)
3942 {
3943 /* This can only happen for "list + non-list". For
3944 * "non-list + ..." or "something - ...", we returned
3945 * before evaluating the 2nd operand. */
3946 clear_tv(rettv);
3947 return FAIL;
3948 }
3949#ifdef FEAT_FLOAT
3950 if (var2.v_type == VAR_FLOAT)
3951 f1 = n1;
3952#endif
3953 }
3954#ifdef FEAT_FLOAT
3955 if (var2.v_type == VAR_FLOAT)
3956 {
3957 f2 = var2.vval.v_float;
3958 n2 = 0;
3959 }
3960 else
3961#endif
3962 {
3963 n2 = get_tv_number_chk(&var2, &error);
3964 if (error)
3965 {
3966 clear_tv(rettv);
3967 clear_tv(&var2);
3968 return FAIL;
3969 }
3970#ifdef FEAT_FLOAT
3971 if (rettv->v_type == VAR_FLOAT)
3972 f2 = n2;
3973#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003976
3977#ifdef FEAT_FLOAT
3978 /* If there is a float on either side the result is a float. */
3979 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3980 {
3981 if (op == '+')
3982 f1 = f1 + f2;
3983 else
3984 f1 = f1 - f2;
3985 rettv->v_type = VAR_FLOAT;
3986 rettv->vval.v_float = f1;
3987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003989#endif
3990 {
3991 if (op == '+')
3992 n1 = n1 + n2;
3993 else
3994 n1 = n1 - n2;
3995 rettv->v_type = VAR_NUMBER;
3996 rettv->vval.v_number = n1;
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 }
4002 return OK;
4003}
4004
4005/*
4006 * Handle fifth level expression:
4007 * * number multiplication
4008 * / number division
4009 * % number modulo
4010 *
4011 * "arg" must point to the first non-white of the expression.
4012 * "arg" is advanced to the next non-white after the recognized expression.
4013 *
4014 * Return OK or FAIL.
4015 */
4016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004017eval6(
4018 char_u **arg,
4019 typval_T *rettv,
4020 int evaluate,
4021 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004025 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004026#ifdef FEAT_FLOAT
4027 int use_float = FALSE;
4028 float_T f1 = 0, f2;
4029#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004030 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
4032 /*
4033 * Get the first variable.
4034 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004035 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return FAIL;
4037
4038 /*
4039 * Repeat computing, until no '*', '/' or '%' is following.
4040 */
4041 for (;;)
4042 {
4043 op = **arg;
4044 if (op != '*' && op != '/' && op != '%')
4045 break;
4046
4047 if (evaluate)
4048 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004049#ifdef FEAT_FLOAT
4050 if (rettv->v_type == VAR_FLOAT)
4051 {
4052 f1 = rettv->vval.v_float;
4053 use_float = TRUE;
4054 n1 = 0;
4055 }
4056 else
4057#endif
4058 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004060 if (error)
4061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 else
4064 n1 = 0;
4065
4066 /*
4067 * Get the second variable.
4068 */
4069 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004070 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return FAIL;
4072
4073 if (evaluate)
4074 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075#ifdef FEAT_FLOAT
4076 if (var2.v_type == VAR_FLOAT)
4077 {
4078 if (!use_float)
4079 {
4080 f1 = n1;
4081 use_float = TRUE;
4082 }
4083 f2 = var2.vval.v_float;
4084 n2 = 0;
4085 }
4086 else
4087#endif
4088 {
4089 n2 = get_tv_number_chk(&var2, &error);
4090 clear_tv(&var2);
4091 if (error)
4092 return FAIL;
4093#ifdef FEAT_FLOAT
4094 if (use_float)
4095 f2 = n2;
4096#endif
4097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 /*
4100 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004101 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103#ifdef FEAT_FLOAT
4104 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004106 if (op == '*')
4107 f1 = f1 * f2;
4108 else if (op == '/')
4109 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004110# ifdef VMS
4111 /* VMS crashes on divide by zero, work around it */
4112 if (f2 == 0.0)
4113 {
4114 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004115 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004116 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004117 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004118 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004119 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004120 }
4121 else
4122 f1 = f1 / f2;
4123# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004124 /* We rely on the floating point library to handle divide
4125 * by zero to result in "inf" and not a crash. */
4126 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004127# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004131 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004132 return FAIL;
4133 }
4134 rettv->v_type = VAR_FLOAT;
4135 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004140 if (op == '*')
4141 n1 = n1 * n2;
4142 else if (op == '/')
4143 {
4144 if (n2 == 0) /* give an error message? */
4145 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004146 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004147 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004148 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004149 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004150 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004151 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004152 }
4153 else
4154 n1 = n1 / n2;
4155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004157 {
4158 if (n2 == 0) /* give an error message? */
4159 n1 = 0;
4160 else
4161 n1 = n1 % n2;
4162 }
4163 rettv->v_type = VAR_NUMBER;
4164 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 }
4168
4169 return OK;
4170}
4171
4172/*
4173 * Handle sixth level expression:
4174 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004175 * "string" string constant
4176 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * &option-name option value
4178 * @r register contents
4179 * identifier variable value
4180 * function() function call
4181 * $VAR environment variable
4182 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004183 * [expr, expr] List
4184 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 *
4186 * Also handle:
4187 * ! in front logical NOT
4188 * - in front unary minus
4189 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004190 * trailing [] subscript in String or List
4191 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 *
4193 * "arg" must point to the first non-white of the expression.
4194 * "arg" is advanced to the next non-white after the recognized expression.
4195 *
4196 * Return OK or FAIL.
4197 */
4198 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004199eval7(
4200 char_u **arg,
4201 typval_T *rettv,
4202 int evaluate,
4203 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004205 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int len;
4207 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 char_u *start_leader, *end_leader;
4209 int ret = OK;
4210 char_u *alias;
4211
4212 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004214 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004219 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 */
4221 start_leader = *arg;
4222 while (**arg == '!' || **arg == '-' || **arg == '+')
4223 *arg = skipwhite(*arg + 1);
4224 end_leader = *arg;
4225
4226 switch (**arg)
4227 {
4228 /*
4229 * Number constant.
4230 */
4231 case '0':
4232 case '1':
4233 case '2':
4234 case '3':
4235 case '4':
4236 case '5':
4237 case '6':
4238 case '7':
4239 case '8':
4240 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004241 {
4242#ifdef FEAT_FLOAT
4243 char_u *p = skipdigits(*arg + 1);
4244 int get_float = FALSE;
4245
4246 /* We accept a float when the format matches
4247 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004248 * strict to avoid backwards compatibility problems.
4249 * Don't look for a float after the "." operator, so that
4250 * ":let vers = 1.2.3" doesn't fail. */
4251 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004253 get_float = TRUE;
4254 p = skipdigits(p + 2);
4255 if (*p == 'e' || *p == 'E')
4256 {
4257 ++p;
4258 if (*p == '-' || *p == '+')
4259 ++p;
4260 if (!vim_isdigit(*p))
4261 get_float = FALSE;
4262 else
4263 p = skipdigits(p + 1);
4264 }
4265 if (ASCII_ISALPHA(*p) || *p == '.')
4266 get_float = FALSE;
4267 }
4268 if (get_float)
4269 {
4270 float_T f;
4271
4272 *arg += string2float(*arg, &f);
4273 if (evaluate)
4274 {
4275 rettv->v_type = VAR_FLOAT;
4276 rettv->vval.v_float = f;
4277 }
4278 }
4279 else
4280#endif
4281 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004282 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004283 *arg += len;
4284 if (evaluate)
4285 {
4286 rettv->v_type = VAR_NUMBER;
4287 rettv->vval.v_number = n;
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 /*
4294 * String constant: "string".
4295 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 break;
4298
4299 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004303 break;
4304
4305 /*
4306 * List: [expr, expr]
4307 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 break;
4310
4311 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004312 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004313 * Dictionary: {key: val, key: val}
4314 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004315 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4316 if (ret == NOTDONE)
4317 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318 break;
4319
4320 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004321 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004323 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325
4326 /*
4327 * Environment variable: $VAR.
4328 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 break;
4331
4332 /*
4333 * Register contents: @r.
4334 */
4335 case '@': ++*arg;
4336 if (evaluate)
4337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004339 rettv->vval.v_string = get_reg_contents(**arg,
4340 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 if (**arg != NUL)
4343 ++*arg;
4344 break;
4345
4346 /*
4347 * nested expression: (expression).
4348 */
4349 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (**arg == ')')
4352 ++*arg;
4353 else if (ret == OK)
4354 {
4355 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 ret = FAIL;
4358 }
4359 break;
4360
Bram Moolenaar8c711452005-01-14 21:53:12 +00004361 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 break;
4363 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004364
4365 if (ret == NOTDONE)
4366 {
4367 /*
4368 * Must be a variable or function name.
4369 * Can also be a curly-braces kind of name: {expr}.
4370 */
4371 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004372 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 if (alias != NULL)
4374 s = alias;
4375
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004376 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 ret = FAIL;
4378 else
4379 {
4380 if (**arg == '(') /* recursive! */
4381 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004382 partial_T *partial;
4383
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004384 if (!evaluate)
4385 check_vars(s, len);
4386
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387 /* If "s" is the name of a variable of type VAR_FUNC
4388 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004389 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004391 /* Need to make a copy, in case evaluating the arguments makes
4392 * the name invalid. */
4393 s = vim_strsave(s);
4394 if (s == NULL)
4395 ret = FAIL;
4396 else
4397 /* Invoke the function. */
4398 ret = get_func_tv(s, len, rettv, arg,
4399 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4400 &len, evaluate, partial, NULL);
4401 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004402
4403 /* If evaluate is FALSE rettv->v_type was not set in
4404 * get_func_tv, but it's needed in handle_subscript() to parse
4405 * what follows. So set it here. */
4406 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4407 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004408 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004409 rettv->v_type = VAR_FUNC;
4410 }
4411
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 /* Stop the expression evaluation when immediately
4413 * aborting on error, or when an interrupt occurred or
4414 * an exception was thrown but not caught. */
4415 if (aborting())
4416 {
4417 if (ret == OK)
4418 clear_tv(rettv);
4419 ret = FAIL;
4420 }
4421 }
4422 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004423 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004424 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004425 {
4426 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004427 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004430 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 }
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 *arg = skipwhite(*arg);
4434
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004435 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4436 * expr(expr). */
4437 if (ret == OK)
4438 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 /*
4441 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4442 */
4443 if (ret == OK && evaluate && end_leader > start_leader)
4444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004446 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004447#ifdef FEAT_FLOAT
4448 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004450 if (rettv->v_type == VAR_FLOAT)
4451 f = rettv->vval.v_float;
4452 else
4453#endif
4454 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 clear_tv(rettv);
4458 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004460 else
4461 {
4462 while (end_leader > start_leader)
4463 {
4464 --end_leader;
4465 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004466 {
4467#ifdef FEAT_FLOAT
4468 if (rettv->v_type == VAR_FLOAT)
4469 f = !f;
4470 else
4471#endif
4472 val = !val;
4473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004474 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004475 {
4476#ifdef FEAT_FLOAT
4477 if (rettv->v_type == VAR_FLOAT)
4478 f = -f;
4479 else
4480#endif
4481 val = -val;
4482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004484#ifdef FEAT_FLOAT
4485 if (rettv->v_type == VAR_FLOAT)
4486 {
4487 clear_tv(rettv);
4488 rettv->vval.v_float = f;
4489 }
4490 else
4491#endif
4492 {
4493 clear_tv(rettv);
4494 rettv->v_type = VAR_NUMBER;
4495 rettv->vval.v_number = val;
4496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499
4500 return ret;
4501}
4502
4503/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004504 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4505 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4507 */
4508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004509eval_index(
4510 char_u **arg,
4511 typval_T *rettv,
4512 int evaluate,
4513 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514{
4515 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004516 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 long len = -1;
4519 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522
Bram Moolenaara03f2332016-02-06 18:09:59 +01004523 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004525 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004526 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004527 if (verbose)
4528 EMSG(_("E695: Cannot index a Funcref"));
4529 return FAIL;
4530 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004531#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004532 if (verbose)
4533 EMSG(_(e_float_as_string));
4534 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004535#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004536 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004537 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004538 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004539 if (verbose)
4540 EMSG(_("E909: Cannot index a special variable"));
4541 return FAIL;
4542 case VAR_UNKNOWN:
4543 if (evaluate)
4544 return FAIL;
4545 /* FALLTHROUGH */
4546
4547 case VAR_STRING:
4548 case VAR_NUMBER:
4549 case VAR_LIST:
4550 case VAR_DICT:
4551 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004552 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004554 init_tv(&var1);
4555 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 /*
4559 * dict.name
4560 */
4561 key = *arg + 1;
4562 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4563 ;
4564 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004566 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 }
4568 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 /*
4571 * something[idx]
4572 *
4573 * Get the (first) variable from inside the [].
4574 */
4575 *arg = skipwhite(*arg + 1);
4576 if (**arg == ':')
4577 empty1 = TRUE;
4578 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4579 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4581 {
4582 /* not a number or string */
4583 clear_tv(&var1);
4584 return FAIL;
4585 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004586
4587 /*
4588 * Get the second variable from inside the [:].
4589 */
4590 if (**arg == ':')
4591 {
4592 range = TRUE;
4593 *arg = skipwhite(*arg + 1);
4594 if (**arg == ']')
4595 empty2 = TRUE;
4596 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004598 if (!empty1)
4599 clear_tv(&var1);
4600 return FAIL;
4601 }
4602 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4603 {
4604 /* not a number or string */
4605 if (!empty1)
4606 clear_tv(&var1);
4607 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 return FAIL;
4609 }
4610 }
4611
4612 /* Check for the ']'. */
4613 if (**arg != ']')
4614 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004615 if (verbose)
4616 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 clear_tv(&var1);
4618 if (range)
4619 clear_tv(&var2);
4620 return FAIL;
4621 }
4622 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 }
4624
4625 if (evaluate)
4626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 n1 = 0;
4628 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 n1 = get_tv_number(&var1);
4631 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 }
4633 if (range)
4634 {
4635 if (empty2)
4636 n2 = -1;
4637 else
4638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004639 n2 = get_tv_number(&var2);
4640 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 }
4642 }
4643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004646 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004647 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004648 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004649 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004650 case VAR_SPECIAL:
4651 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004652 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004653 break; /* not evaluating, skipping over subscript */
4654
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 case VAR_NUMBER:
4656 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 len = (long)STRLEN(s);
4659 if (range)
4660 {
4661 /* The resulting variable is a substring. If the indexes
4662 * are out of range the result is empty. */
4663 if (n1 < 0)
4664 {
4665 n1 = len + n1;
4666 if (n1 < 0)
4667 n1 = 0;
4668 }
4669 if (n2 < 0)
4670 n2 = len + n2;
4671 else if (n2 >= len)
4672 n2 = len;
4673 if (n1 >= len || n2 < 0 || n1 > n2)
4674 s = NULL;
4675 else
4676 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4677 }
4678 else
4679 {
4680 /* The resulting variable is a string of a single
4681 * character. If the index is too big or negative the
4682 * result is empty. */
4683 if (n1 >= len || n1 < 0)
4684 s = NULL;
4685 else
4686 s = vim_strnsave(s + n1, 1);
4687 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004688 clear_tv(rettv);
4689 rettv->v_type = VAR_STRING;
4690 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 break;
4692
4693 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 if (n1 < 0)
4696 n1 = len + n1;
4697 if (!empty1 && (n1 < 0 || n1 >= len))
4698 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004699 /* For a range we allow invalid values and return an empty
4700 * list. A list index out of range is an error. */
4701 if (!range)
4702 {
4703 if (verbose)
4704 EMSGN(_(e_listidx), n1);
4705 return FAIL;
4706 }
4707 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 }
4709 if (range)
4710 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004711 list_T *l;
4712 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713
4714 if (n2 < 0)
4715 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004716 else if (n2 >= len)
4717 n2 = len - 1;
4718 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004719 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 l = list_alloc();
4721 if (l == NULL)
4722 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 n1 <= n2; ++n1)
4725 {
4726 if (list_append_tv(l, &item->li_tv) == FAIL)
4727 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 return FAIL;
4730 }
4731 item = item->li_next;
4732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004734 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 }
4736 else
4737 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004738 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 clear_tv(rettv);
4740 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 }
4742 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743
4744 case VAR_DICT:
4745 if (range)
4746 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004747 if (verbose)
4748 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749 if (len == -1)
4750 clear_tv(&var1);
4751 return FAIL;
4752 }
4753 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004754 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755
4756 if (len == -1)
4757 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004758 key = get_tv_string_chk(&var1);
4759 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004761 clear_tv(&var1);
4762 return FAIL;
4763 }
4764 }
4765
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004766 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004769 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770 if (len == -1)
4771 clear_tv(&var1);
4772 if (item == NULL)
4773 return FAIL;
4774
4775 copy_tv(&item->di_tv, &var1);
4776 clear_tv(rettv);
4777 *rettv = var1;
4778 }
4779 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 }
4781 }
4782
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 return OK;
4784}
4785
4786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 * Get an option value.
4788 * "arg" points to the '&' or '+' before the option name.
4789 * "arg" is advanced to character after the option name.
4790 * Return OK or FAIL.
4791 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004793get_option_tv(
4794 char_u **arg,
4795 typval_T *rettv, /* when NULL, only check if option exists */
4796 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 char_u *option_end;
4799 long numval;
4800 char_u *stringval;
4801 int opt_type;
4802 int c;
4803 int working = (**arg == '+'); /* has("+option") */
4804 int ret = OK;
4805 int opt_flags;
4806
4807 /*
4808 * Isolate the option name and find its value.
4809 */
4810 option_end = find_option_end(arg, &opt_flags);
4811 if (option_end == NULL)
4812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 EMSG2(_("E112: Option name missing: %s"), *arg);
4815 return FAIL;
4816 }
4817
4818 if (!evaluate)
4819 {
4820 *arg = option_end;
4821 return OK;
4822 }
4823
4824 c = *option_end;
4825 *option_end = NUL;
4826 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829 if (opt_type == -3) /* invalid name */
4830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 EMSG2(_("E113: Unknown option: %s"), *arg);
4833 ret = FAIL;
4834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
4837 if (opt_type == -2) /* hidden string option */
4838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 rettv->v_type = VAR_STRING;
4840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 else if (opt_type == -1) /* hidden number option */
4843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 rettv->v_type = VAR_NUMBER;
4845 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 else if (opt_type == 1) /* number option */
4848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 rettv->v_type = VAR_NUMBER;
4850 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 else /* string option */
4853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 rettv->v_type = VAR_STRING;
4855 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 }
4858 else if (working && (opt_type == -2 || opt_type == -1))
4859 ret = FAIL;
4860
4861 *option_end = c; /* put back for error messages */
4862 *arg = option_end;
4863
4864 return ret;
4865}
4866
4867/*
4868 * Allocate a variable for a string constant.
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 char_u *p;
4875 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int extra = 0;
4877
4878 /*
4879 * Find the end of the string, skipping backslashed characters.
4880 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004881 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 if (*p == '\\' && p[1] != NUL)
4884 {
4885 ++p;
4886 /* A "\<x>" form occupies at least 4 characters, and produces up
4887 * to 6 characters: reserve space for 2 extra */
4888 if (*p == '<')
4889 extra += 2;
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892
4893 if (*p != '"')
4894 {
4895 EMSG2(_("E114: Missing quote: %s"), *arg);
4896 return FAIL;
4897 }
4898
4899 /* If only parsing, set *arg and return here */
4900 if (!evaluate)
4901 {
4902 *arg = p + 1;
4903 return OK;
4904 }
4905
4906 /*
4907 * Copy the string into allocated memory, handling backslashed
4908 * characters.
4909 */
4910 name = alloc((unsigned)(p - *arg + extra));
4911 if (name == NULL)
4912 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913 rettv->v_type = VAR_STRING;
4914 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 if (*p == '\\')
4919 {
4920 switch (*++p)
4921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004922 case 'b': *name++ = BS; ++p; break;
4923 case 'e': *name++ = ESC; ++p; break;
4924 case 'f': *name++ = FF; ++p; break;
4925 case 'n': *name++ = NL; ++p; break;
4926 case 'r': *name++ = CAR; ++p; break;
4927 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 case 'X': /* hex: "\x1", "\x12" */
4930 case 'x':
4931 case 'u': /* Unicode: "\u0023" */
4932 case 'U':
4933 if (vim_isxdigit(p[1]))
4934 {
4935 int n, nr;
4936 int c = toupper(*p);
4937
4938 if (c == 'X')
4939 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004940 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004942 else
4943 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 nr = 0;
4945 while (--n >= 0 && vim_isxdigit(p[1]))
4946 {
4947 ++p;
4948 nr = (nr << 4) + hex2nr(*p);
4949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951#ifdef FEAT_MBYTE
4952 /* For "\u" store the number according to
4953 * 'encoding'. */
4954 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 else
4957#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 break;
4961
4962 /* octal: "\1", "\12", "\123" */
4963 case '0':
4964 case '1':
4965 case '2':
4966 case '3':
4967 case '4':
4968 case '5':
4969 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 case '7': *name = *p++ - '0';
4971 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 *name = (*name << 3) + *p++ - '0';
4974 if (*p >= '0' && *p <= '7')
4975 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 break;
4979
4980 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004981 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (extra != 0)
4983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
4986 }
4987 /* FALLTHROUGH */
4988
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 break;
4991 }
4992 }
4993 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004998 if (*p != NUL) /* just in case */
4999 ++p;
5000 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 return OK;
5003}
5004
5005/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * Return OK or FAIL.
5008 */
5009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005010get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005013 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005014 int reduce = 0;
5015
5016 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005018 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005019 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005024 break;
5025 ++reduce;
5026 ++p;
5027 }
5028 }
5029
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005031 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005033 return FAIL;
5034 }
5035
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037 if (!evaluate)
5038 {
5039 *arg = p + 1;
5040 return OK;
5041 }
5042
5043 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005045 */
5046 str = alloc((unsigned)((p - *arg) - reduce));
5047 if (str == NULL)
5048 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 rettv->v_type = VAR_STRING;
5050 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057 break;
5058 ++p;
5059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 *arg = p + 1;
5064
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 return OK;
5066}
5067
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005068/*
5069 * Return the function name of the partial.
5070 */
5071 char_u *
5072partial_name(partial_T *pt)
5073{
5074 if (pt->pt_name != NULL)
5075 return pt->pt_name;
5076 return pt->pt_func->uf_name;
5077}
5078
Bram Moolenaarddecc252016-04-06 22:59:37 +02005079 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005080partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005081{
5082 int i;
5083
5084 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005085 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005086 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005087 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005088 if (pt->pt_name != NULL)
5089 {
5090 func_unref(pt->pt_name);
5091 vim_free(pt->pt_name);
5092 }
5093 else
5094 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005095 vim_free(pt);
5096}
5097
5098/*
5099 * Unreference a closure: decrement the reference count and free it when it
5100 * becomes zero.
5101 */
5102 void
5103partial_unref(partial_T *pt)
5104{
5105 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005106 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005107}
5108
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005109static int tv_equal_recurse_limit;
5110
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005111 static int
5112func_equal(
5113 typval_T *tv1,
5114 typval_T *tv2,
5115 int ic) /* ignore case */
5116{
5117 char_u *s1, *s2;
5118 dict_T *d1, *d2;
5119 int a1, a2;
5120 int i;
5121
5122 /* empty and NULL function name considered the same */
5123 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005124 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005125 if (s1 != NULL && *s1 == NUL)
5126 s1 = NULL;
5127 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005128 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005129 if (s2 != NULL && *s2 == NUL)
5130 s2 = NULL;
5131 if (s1 == NULL || s2 == NULL)
5132 {
5133 if (s1 != s2)
5134 return FALSE;
5135 }
5136 else if (STRCMP(s1, s2) != 0)
5137 return FALSE;
5138
5139 /* empty dict and NULL dict is different */
5140 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5141 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5142 if (d1 == NULL || d2 == NULL)
5143 {
5144 if (d1 != d2)
5145 return FALSE;
5146 }
5147 else if (!dict_equal(d1, d2, ic, TRUE))
5148 return FALSE;
5149
5150 /* empty list and no list considered the same */
5151 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5152 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5153 if (a1 != a2)
5154 return FALSE;
5155 for (i = 0; i < a1; ++i)
5156 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5157 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5158 return FALSE;
5159
5160 return TRUE;
5161}
5162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005163/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005164 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005165 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005166 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005167 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005169tv_equal(
5170 typval_T *tv1,
5171 typval_T *tv2,
5172 int ic, /* ignore case */
5173 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005174{
5175 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005177 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005178 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005179
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005180 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005181 * recursiveness to a limit. We guess they are equal then.
5182 * A fixed limit has the problem of still taking an awful long time.
5183 * Reduce the limit every time running into it. That should work fine for
5184 * deeply linked structures that are not recursively linked and catch
5185 * recursiveness quickly. */
5186 if (!recursive)
5187 tv_equal_recurse_limit = 1000;
5188 if (recursive_cnt >= tv_equal_recurse_limit)
5189 {
5190 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005191 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005192 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005193
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005194 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5195 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005196 if ((tv1->v_type == VAR_FUNC
5197 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5198 && (tv2->v_type == VAR_FUNC
5199 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5200 {
5201 ++recursive_cnt;
5202 r = func_equal(tv1, tv2, ic);
5203 --recursive_cnt;
5204 return r;
5205 }
5206
5207 if (tv1->v_type != tv2->v_type)
5208 return FALSE;
5209
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005210 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005211 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005212 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005213 ++recursive_cnt;
5214 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5215 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005216 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005217
5218 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005219 ++recursive_cnt;
5220 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5221 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005222 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005223
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005224 case VAR_NUMBER:
5225 return tv1->vval.v_number == tv2->vval.v_number;
5226
5227 case VAR_STRING:
5228 s1 = get_tv_string_buf(tv1, buf1);
5229 s2 = get_tv_string_buf(tv2, buf2);
5230 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005231
5232 case VAR_SPECIAL:
5233 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005234
5235 case VAR_FLOAT:
5236#ifdef FEAT_FLOAT
5237 return tv1->vval.v_float == tv2->vval.v_float;
5238#endif
5239 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005240#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005241 return tv1->vval.v_job == tv2->vval.v_job;
5242#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005243 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005244#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005245 return tv1->vval.v_channel == tv2->vval.v_channel;
5246#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005247 case VAR_FUNC:
5248 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005249 case VAR_UNKNOWN:
5250 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005251 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005252
Bram Moolenaara03f2332016-02-06 18:09:59 +01005253 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5254 * does not equal anything, not even itself. */
5255 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005256}
5257
5258/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005259 * Return the next (unique) copy ID.
5260 * Used for serializing nested structures.
5261 */
5262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005263get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005264{
5265 current_copyID += COPYID_INC;
5266 return current_copyID;
5267}
5268
5269/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270 * Garbage collection for lists and dictionaries.
5271 *
5272 * We use reference counts to be able to free most items right away when they
5273 * are no longer used. But for composite items it's possible that it becomes
5274 * unused while the reference count is > 0: When there is a recursive
5275 * reference. Example:
5276 * :let l = [1, 2, 3]
5277 * :let d = {9: l}
5278 * :let l[1] = d
5279 *
5280 * Since this is quite unusual we handle this with garbage collection: every
5281 * once in a while find out which lists and dicts are not referenced from any
5282 * variable.
5283 *
5284 * Here is a good reference text about garbage collection (refers to Python
5285 * but it applies to all reference-counting mechanisms):
5286 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005287 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005288
5289/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005290 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005291 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005292 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005293 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005294 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005295garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005296{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005297 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005299 buf_T *buf;
5300 win_T *wp;
5301 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005302 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005303 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005304
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005305 if (!testing)
5306 {
5307 /* Only do this once. */
5308 want_garbage_collect = FALSE;
5309 may_garbage_collect = FALSE;
5310 garbage_collect_at_exit = FALSE;
5311 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005312
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005313 /* We advance by two because we add one for items referenced through
5314 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005315 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005316
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005317 /*
5318 * 1. Go through all accessible variables and mark all lists and dicts
5319 * with copyID.
5320 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005321
5322 /* Don't free variables in the previous_funccal list unless they are only
5323 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005324 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005325 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005326
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327 /* script-local variables */
5328 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005329 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005330
5331 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005332 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005333 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5334 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005335
5336 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005337 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005338 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5339 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005340#ifdef FEAT_AUTOCMD
5341 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005342 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5343 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005344#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005346 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005347 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5349 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005350 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005351 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005352
5353 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005356 /* named functions (matters for closures) */
5357 abort = abort || set_ref_in_functions(copyID);
5358
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005359 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005361
Bram Moolenaard812df62008-11-09 12:46:09 +00005362 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005363 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005364
Bram Moolenaar1dced572012-04-05 16:54:08 +02005365#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005367#endif
5368
Bram Moolenaardb913952012-06-29 12:54:53 +02005369#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005371#endif
5372
5373#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005375#endif
5376
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005378 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005379 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005380#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005381#ifdef FEAT_NETBEANS_INTG
5382 abort = abort || set_ref_in_nb_channel(copyID);
5383#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005384
Bram Moolenaare3188e22016-05-31 21:13:04 +02005385#ifdef FEAT_TIMERS
5386 abort = abort || set_ref_in_timer(copyID);
5387#endif
5388
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005389#ifdef FEAT_QUICKFIX
5390 abort = abort || set_ref_in_quickfix(copyID);
5391#endif
5392
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005393#ifdef FEAT_TERMINAL
5394 abort = abort || set_ref_in_term(copyID);
5395#endif
5396
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005397 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005398 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 /*
5400 * 2. Free lists and dictionaries that are not referenced.
5401 */
5402 did_free = free_unref_items(copyID);
5403
5404 /*
5405 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005409 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 else if (p_verbose > 0)
5411 {
5412 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5413 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005414
5415 return did_free;
5416}
5417
5418/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005419 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005420 */
5421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005423{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005424 int did_free = FALSE;
5425
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 /* Let all "free" functions know that we are here. This means no
5427 * dictionaries, lists, channels or jobs are to be freed, because we will
5428 * do that here. */
5429 in_free_unref_items = TRUE;
5430
5431 /*
5432 * PASS 1: free the contents of the items. We don't free the items
5433 * themselves yet, so that it is possible to decrement refcount counters
5434 */
5435
Bram Moolenaarcd524592016-07-17 14:57:05 +02005436 /* Go through the list of dicts and free items without the copyID. */
5437 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005438
Bram Moolenaarda861d62016-07-17 15:46:27 +02005439 /* Go through the list of lists and free items without the copyID. */
5440 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441
5442#ifdef FEAT_JOB_CHANNEL
5443 /* Go through the list of jobs and free items without the copyID. This
5444 * must happen before doing channels, because jobs refer to channels, but
5445 * the reference from the channel to the job isn't tracked. */
5446 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5447
5448 /* Go through the list of channels and free items without the copyID. */
5449 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5450#endif
5451
5452 /*
5453 * PASS 2: free the items themselves.
5454 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005455 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005456 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005457
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458#ifdef FEAT_JOB_CHANNEL
5459 /* Go through the list of jobs and free items without the copyID. This
5460 * must happen before doing channels, because jobs refer to channels, but
5461 * the reference from the channel to the job isn't tracked. */
5462 free_unused_jobs(copyID, COPYID_MASK);
5463
5464 /* Go through the list of channels and free items without the copyID. */
5465 free_unused_channels(copyID, COPYID_MASK);
5466#endif
5467
5468 in_free_unref_items = FALSE;
5469
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470 return did_free;
5471}
5472
5473/*
5474 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005475 * "list_stack" is used to add lists to be marked. Can be NULL.
5476 *
5477 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005480set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005481{
5482 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005483 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005484 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 hashtab_T *cur_ht;
5486 ht_stack_T *ht_stack = NULL;
5487 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005488
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 cur_ht = ht;
5490 for (;;)
5491 {
5492 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005493 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 /* Mark each item in the hashtab. If the item contains a hashtab
5495 * it is added to ht_stack, if it contains a list it is added to
5496 * list_stack. */
5497 todo = (int)cur_ht->ht_used;
5498 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5499 if (!HASHITEM_EMPTY(hi))
5500 {
5501 --todo;
5502 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5503 &ht_stack, list_stack);
5504 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005505 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005506
5507 if (ht_stack == NULL)
5508 break;
5509
5510 /* take an item from the stack */
5511 cur_ht = ht_stack->ht;
5512 tempitem = ht_stack;
5513 ht_stack = ht_stack->prev;
5514 free(tempitem);
5515 }
5516
5517 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005518}
5519
5520/*
5521 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005522 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5523 *
5524 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005525 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005528{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005529 listitem_T *li;
5530 int abort = FALSE;
5531 list_T *cur_l;
5532 list_stack_T *list_stack = NULL;
5533 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005534
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005535 cur_l = l;
5536 for (;;)
5537 {
5538 if (!abort)
5539 /* Mark each item in the list. If the item contains a hashtab
5540 * it is added to ht_stack, if it contains a list it is added to
5541 * list_stack. */
5542 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5543 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5544 ht_stack, &list_stack);
5545 if (list_stack == NULL)
5546 break;
5547
5548 /* take an item from the stack */
5549 cur_l = list_stack->list;
5550 tempitem = list_stack;
5551 list_stack = list_stack->prev;
5552 free(tempitem);
5553 }
5554
5555 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005556}
5557
5558/*
5559 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005560 * "list_stack" is used to add lists to be marked. Can be NULL.
5561 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5562 *
5563 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005564 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566set_ref_in_item(
5567 typval_T *tv,
5568 int copyID,
5569 ht_stack_T **ht_stack,
5570 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005571{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005573
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005574 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005575 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005576 dict_T *dd = tv->vval.v_dict;
5577
Bram Moolenaara03f2332016-02-06 18:09:59 +01005578 if (dd != NULL && dd->dv_copyID != copyID)
5579 {
5580 /* Didn't see this dict yet. */
5581 dd->dv_copyID = copyID;
5582 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005583 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005584 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5585 }
5586 else
5587 {
5588 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5589 if (newitem == NULL)
5590 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 else
5592 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005593 newitem->ht = &dd->dv_hashtab;
5594 newitem->prev = *ht_stack;
5595 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005596 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005597 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005598 }
5599 }
5600 else if (tv->v_type == VAR_LIST)
5601 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005602 list_T *ll = tv->vval.v_list;
5603
Bram Moolenaara03f2332016-02-06 18:09:59 +01005604 if (ll != NULL && ll->lv_copyID != copyID)
5605 {
5606 /* Didn't see this list yet. */
5607 ll->lv_copyID = copyID;
5608 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005609 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005610 abort = set_ref_in_list(ll, copyID, ht_stack);
5611 }
5612 else
5613 {
5614 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005616 if (newitem == NULL)
5617 abort = TRUE;
5618 else
5619 {
5620 newitem->list = ll;
5621 newitem->prev = *list_stack;
5622 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005624 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005625 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005626 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005627 else if (tv->v_type == VAR_FUNC)
5628 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005629 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005630 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005631 else if (tv->v_type == VAR_PARTIAL)
5632 {
5633 partial_T *pt = tv->vval.v_partial;
5634 int i;
5635
5636 /* A partial does not have a copyID, because it cannot contain itself.
5637 */
5638 if (pt != NULL)
5639 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005640 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005641
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005642 if (pt->pt_dict != NULL)
5643 {
5644 typval_T dtv;
5645
5646 dtv.v_type = VAR_DICT;
5647 dtv.vval.v_dict = pt->pt_dict;
5648 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5649 }
5650
5651 for (i = 0; i < pt->pt_argc; ++i)
5652 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5653 ht_stack, list_stack);
5654 }
5655 }
5656#ifdef FEAT_JOB_CHANNEL
5657 else if (tv->v_type == VAR_JOB)
5658 {
5659 job_T *job = tv->vval.v_job;
5660 typval_T dtv;
5661
5662 if (job != NULL && job->jv_copyID != copyID)
5663 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005664 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005665 if (job->jv_channel != NULL)
5666 {
5667 dtv.v_type = VAR_CHANNEL;
5668 dtv.vval.v_channel = job->jv_channel;
5669 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5670 }
5671 if (job->jv_exit_partial != NULL)
5672 {
5673 dtv.v_type = VAR_PARTIAL;
5674 dtv.vval.v_partial = job->jv_exit_partial;
5675 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5676 }
5677 }
5678 }
5679 else if (tv->v_type == VAR_CHANNEL)
5680 {
5681 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005682 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005683 typval_T dtv;
5684 jsonq_T *jq;
5685 cbq_T *cq;
5686
5687 if (ch != NULL && ch->ch_copyID != copyID)
5688 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005689 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005690 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005691 {
5692 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5693 jq = jq->jq_next)
5694 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5695 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5696 cq = cq->cq_next)
5697 if (cq->cq_partial != NULL)
5698 {
5699 dtv.v_type = VAR_PARTIAL;
5700 dtv.vval.v_partial = cq->cq_partial;
5701 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5702 }
5703 if (ch->ch_part[part].ch_partial != NULL)
5704 {
5705 dtv.v_type = VAR_PARTIAL;
5706 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5707 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5708 }
5709 }
5710 if (ch->ch_partial != NULL)
5711 {
5712 dtv.v_type = VAR_PARTIAL;
5713 dtv.vval.v_partial = ch->ch_partial;
5714 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5715 }
5716 if (ch->ch_close_partial != NULL)
5717 {
5718 dtv.v_type = VAR_PARTIAL;
5719 dtv.vval.v_partial = ch->ch_close_partial;
5720 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5721 }
5722 }
5723 }
5724#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005725 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005726}
5727
Bram Moolenaar17a13432016-01-24 14:22:10 +01005728 static char *
5729get_var_special_name(int nr)
5730{
5731 switch (nr)
5732 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005733 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005734 case VVAL_TRUE: return "v:true";
5735 case VVAL_NONE: return "v:none";
5736 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005737 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005738 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005739 return "42";
5740}
5741
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 * Return a string with the string representation of a variable.
5744 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005745 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005746 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005747 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5748 * stings as "string()", otherwise does not put quotes around strings, as
5749 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005750 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5751 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005752 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005754 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005755echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005756 typval_T *tv,
5757 char_u **tofree,
5758 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005759 int copyID,
5760 int echo_style,
5761 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005762 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005764 static int recurse = 0;
5765 char_u *r = NULL;
5766
Bram Moolenaar33570922005-01-25 22:26:29 +00005767 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005768 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005769 if (!did_echo_string_emsg)
5770 {
5771 /* Only give this message once for a recursive call to avoid
5772 * flooding the user with errors. And stop iterating over lists
5773 * and dicts. */
5774 did_echo_string_emsg = TRUE;
5775 EMSG(_("E724: variable nested too deep for displaying"));
5776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005777 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005778 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005779 }
5780 ++recurse;
5781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 switch (tv->v_type)
5783 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005784 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005785 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005786 {
5787 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005788 r = tv->vval.v_string;
5789 if (r == NULL)
5790 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005791 }
5792 else
5793 {
5794 *tofree = string_quote(tv->vval.v_string, FALSE);
5795 r = *tofree;
5796 }
5797 break;
5798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005800 if (echo_style)
5801 {
5802 *tofree = NULL;
5803 r = tv->vval.v_string;
5804 }
5805 else
5806 {
5807 *tofree = string_quote(tv->vval.v_string, TRUE);
5808 r = *tofree;
5809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005811
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005812 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005813 {
5814 partial_T *pt = tv->vval.v_partial;
5815 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005816 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005817 garray_T ga;
5818 int i;
5819 char_u *tf;
5820
5821 ga_init2(&ga, 1, 100);
5822 ga_concat(&ga, (char_u *)"function(");
5823 if (fname != NULL)
5824 {
5825 ga_concat(&ga, fname);
5826 vim_free(fname);
5827 }
5828 if (pt != NULL && pt->pt_argc > 0)
5829 {
5830 ga_concat(&ga, (char_u *)", [");
5831 for (i = 0; i < pt->pt_argc; ++i)
5832 {
5833 if (i > 0)
5834 ga_concat(&ga, (char_u *)", ");
5835 ga_concat(&ga,
5836 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5837 vim_free(tf);
5838 }
5839 ga_concat(&ga, (char_u *)"]");
5840 }
5841 if (pt != NULL && pt->pt_dict != NULL)
5842 {
5843 typval_T dtv;
5844
5845 ga_concat(&ga, (char_u *)", ");
5846 dtv.v_type = VAR_DICT;
5847 dtv.vval.v_dict = pt->pt_dict;
5848 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5849 vim_free(tf);
5850 }
5851 ga_concat(&ga, (char_u *)")");
5852
5853 *tofree = ga.ga_data;
5854 r = *tofree;
5855 break;
5856 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005857
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005859 if (tv->vval.v_list == NULL)
5860 {
5861 *tofree = NULL;
5862 r = NULL;
5863 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005864 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5865 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005866 {
5867 *tofree = NULL;
5868 r = (char_u *)"[...]";
5869 }
5870 else
5871 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005872 int old_copyID = tv->vval.v_list->lv_copyID;
5873
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005874 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005875 *tofree = list2string(tv, copyID, restore_copyID);
5876 if (restore_copyID)
5877 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005878 r = *tofree;
5879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005881
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005883 if (tv->vval.v_dict == NULL)
5884 {
5885 *tofree = NULL;
5886 r = NULL;
5887 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5889 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005890 {
5891 *tofree = NULL;
5892 r = (char_u *)"{...}";
5893 }
5894 else
5895 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005896 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005897 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 *tofree = dict2string(tv, copyID, restore_copyID);
5899 if (restore_copyID)
5900 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005901 r = *tofree;
5902 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005903 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005905 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005906 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005907 *tofree = NULL;
5908 r = get_tv_string_buf(tv, numbuf);
5909 break;
5910
Bram Moolenaar835dc632016-02-07 14:27:38 +01005911 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005912 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 *tofree = NULL;
5914 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005915 if (composite_val)
5916 {
5917 *tofree = string_quote(r, FALSE);
5918 r = *tofree;
5919 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005921
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005922 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005923#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005924 *tofree = NULL;
5925 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5926 r = numbuf;
5927 break;
5928#endif
5929
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005930 case VAR_SPECIAL:
5931 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005932 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005933 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935
Bram Moolenaar8502c702014-06-17 12:51:16 +02005936 if (--recurse == 0)
5937 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005939}
5940
5941/*
5942 * Return a string with the string representation of a variable.
5943 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5944 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005945 * Does not put quotes around strings, as ":echo" displays values.
5946 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5947 * May return NULL.
5948 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005949 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005950echo_string(
5951 typval_T *tv,
5952 char_u **tofree,
5953 char_u *numbuf,
5954 int copyID)
5955{
5956 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5957}
5958
5959/*
5960 * Return a string with the string representation of a variable.
5961 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5962 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005964 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005966 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967tv2string(
5968 typval_T *tv,
5969 char_u **tofree,
5970 char_u *numbuf,
5971 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005973 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974}
5975
5976/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 * Return string "str" in ' quotes, doubling ' characters.
5978 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005979 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005980 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005981 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005982string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983{
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 char_u *p, *r, *s;
5986
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 len = (function ? 13 : 3);
5988 if (str != NULL)
5989 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005990 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005991 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 if (*p == '\'')
5993 ++len;
5994 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 s = r = alloc(len);
5996 if (r != NULL)
5997 {
5998 if (function)
5999 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006000 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 r += 10;
6002 }
6003 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 if (str != NULL)
6006 for (p = str; *p != NUL; )
6007 {
6008 if (*p == '\'')
6009 *r++ = '\'';
6010 MB_COPY_CHAR(p, r);
6011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006012 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013 if (function)
6014 *r++ = ')';
6015 *r++ = NUL;
6016 }
6017 return s;
6018}
6019
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006020#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006021/*
6022 * Convert the string "text" to a floating point number.
6023 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6024 * this always uses a decimal point.
6025 * Returns the length of the text that was consumed.
6026 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028string2float(
6029 char_u *text,
6030 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006031{
6032 char *s = (char *)text;
6033 float_T f;
6034
Bram Moolenaar62473612017-01-08 19:25:40 +01006035 /* MS-Windows does not deal with "inf" and "nan" properly. */
6036 if (STRNICMP(text, "inf", 3) == 0)
6037 {
6038 *value = INFINITY;
6039 return 3;
6040 }
6041 if (STRNICMP(text, "-inf", 3) == 0)
6042 {
6043 *value = -INFINITY;
6044 return 4;
6045 }
6046 if (STRNICMP(text, "nan", 3) == 0)
6047 {
6048 *value = NAN;
6049 return 3;
6050 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006051 f = strtod(s, &s);
6052 *value = f;
6053 return (int)((char_u *)s - text);
6054}
6055#endif
6056
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 * Get the value of an environment variable.
6059 * "arg" is pointing to the '$'. It is advanced to after the name.
6060 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006061 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 */
6063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
6066 char_u *string = NULL;
6067 int len;
6068 int cc;
6069 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006070 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 ++*arg;
6073 name = *arg;
6074 len = get_env_len(arg);
6075 if (evaluate)
6076 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006077 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006078 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006079
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006080 cc = name[len];
6081 name[len] = NUL;
6082 /* first try vim_getenv(), fast for normal environment vars */
6083 string = vim_getenv(name, &mustfree);
6084 if (string != NULL && *string != NUL)
6085 {
6086 if (!mustfree)
6087 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006089 else
6090 {
6091 if (mustfree)
6092 vim_free(string);
6093
6094 /* next try expanding things like $VIM and ${HOME} */
6095 string = expand_env_save(name - 1);
6096 if (string != NULL && *string == '$')
6097 {
6098 vim_free(string);
6099 string = NULL;
6100 }
6101 }
6102 name[len] = cc;
6103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006104 rettv->v_type = VAR_STRING;
6105 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 }
6107
6108 return OK;
6109}
6110
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006111
6112
6113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006115 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006117 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118var2fpos(
6119 typval_T *varp,
6120 int dollar_lnum, /* TRUE when $ is last line */
6121 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006123 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006125 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
Bram Moolenaara5525202006-03-02 22:52:09 +00006127 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006128 if (varp->v_type == VAR_LIST)
6129 {
6130 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006131 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006132 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006133 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006134
6135 l = varp->vval.v_list;
6136 if (l == NULL)
6137 return NULL;
6138
6139 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006140 pos.lnum = list_find_nr(l, 0L, &error);
6141 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006142 return NULL; /* invalid line number */
6143
6144 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006145 pos.col = list_find_nr(l, 1L, &error);
6146 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006147 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006148 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006149
6150 /* We accept "$" for the column number: last column. */
6151 li = list_find(l, 1L);
6152 if (li != NULL && li->li_tv.v_type == VAR_STRING
6153 && li->li_tv.vval.v_string != NULL
6154 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6155 pos.col = len + 1;
6156
Bram Moolenaara5525202006-03-02 22:52:09 +00006157 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006158 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006159 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006160 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006161
Bram Moolenaara5525202006-03-02 22:52:09 +00006162#ifdef FEAT_VIRTUALEDIT
6163 /* Get the virtual offset. Defaults to zero. */
6164 pos.coladd = list_find_nr(l, 2L, &error);
6165 if (error)
6166 pos.coladd = 0;
6167#endif
6168
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006169 return &pos;
6170 }
6171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006172 name = get_tv_string_chk(varp);
6173 if (name == NULL)
6174 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006175 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006177 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6178 {
6179 if (VIsual_active)
6180 return &VIsual;
6181 return &curwin->w_cursor;
6182 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006183 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006185 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6187 return NULL;
6188 return pp;
6189 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006190
6191#ifdef FEAT_VIRTUALEDIT
6192 pos.coladd = 0;
6193#endif
6194
Bram Moolenaar477933c2007-07-17 14:32:23 +00006195 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006196 {
6197 pos.col = 0;
6198 if (name[1] == '0') /* "w0": first visible line */
6199 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006200 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006201 /* In silent Ex mode topline is zero, but that's not a valid line
6202 * number; use one instead. */
6203 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006204 return &pos;
6205 }
6206 else if (name[1] == '$') /* "w$": last visible line */
6207 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006208 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006209 /* In silent Ex mode botline is zero, return zero then. */
6210 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006211 return &pos;
6212 }
6213 }
6214 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006216 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
6218 pos.lnum = curbuf->b_ml.ml_line_count;
6219 pos.col = 0;
6220 }
6221 else
6222 {
6223 pos.lnum = curwin->w_cursor.lnum;
6224 pos.col = (colnr_T)STRLEN(ml_get_curline());
6225 }
6226 return &pos;
6227 }
6228 return NULL;
6229}
6230
6231/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006232 * Convert list in "arg" into a position and optional file number.
6233 * When "fnump" is NULL there is no file number, only 3 items.
6234 * Note that the column is passed on as-is, the caller may want to decrement
6235 * it to use 1 for the first column.
6236 * Return FAIL when conversion is not possible, doesn't check the position for
6237 * validity.
6238 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006240list2fpos(
6241 typval_T *arg,
6242 pos_T *posp,
6243 int *fnump,
6244 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006245{
6246 list_T *l = arg->vval.v_list;
6247 long i = 0;
6248 long n;
6249
Bram Moolenaar493c1782014-05-28 14:34:46 +02006250 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6251 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006252 if (arg->v_type != VAR_LIST
6253 || l == NULL
6254 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006255 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006256 return FAIL;
6257
6258 if (fnump != NULL)
6259 {
6260 n = list_find_nr(l, i++, NULL); /* fnum */
6261 if (n < 0)
6262 return FAIL;
6263 if (n == 0)
6264 n = curbuf->b_fnum; /* current buffer */
6265 *fnump = n;
6266 }
6267
6268 n = list_find_nr(l, i++, NULL); /* lnum */
6269 if (n < 0)
6270 return FAIL;
6271 posp->lnum = n;
6272
6273 n = list_find_nr(l, i++, NULL); /* col */
6274 if (n < 0)
6275 return FAIL;
6276 posp->col = n;
6277
6278#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006279 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006280 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006281 posp->coladd = 0;
6282 else
6283 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006284#endif
6285
Bram Moolenaar493c1782014-05-28 14:34:46 +02006286 if (curswantp != NULL)
6287 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6288
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006289 return OK;
6290}
6291
6292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 * Get the length of an environment variable name.
6294 * Advance "arg" to the first character after the name.
6295 * Return 0 for error.
6296 */
6297 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006298get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 char_u *p;
6301 int len;
6302
6303 for (p = *arg; vim_isIDc(*p); ++p)
6304 ;
6305 if (p == *arg) /* no name found */
6306 return 0;
6307
6308 len = (int)(p - *arg);
6309 *arg = p;
6310 return len;
6311}
6312
6313/*
6314 * Get the length of the name of a function or internal variable.
6315 * "arg" is advanced to the first non-white character after the name.
6316 * Return 0 if something is wrong.
6317 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006319get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 char_u *p;
6322 int len;
6323
6324 /* Find the end of the name. */
6325 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006326 {
6327 if (*p == ':')
6328 {
6329 /* "s:" is start of "s:var", but "n:" is not and can be used in
6330 * slice "[n:]". Also "xx:" is not a namespace. */
6331 len = (int)(p - *arg);
6332 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6333 || len > 1)
6334 break;
6335 }
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (p == *arg) /* no name found */
6338 return 0;
6339
6340 len = (int)(p - *arg);
6341 *arg = skipwhite(p);
6342
6343 return len;
6344}
6345
6346/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006347 * Get the length of the name of a variable or function.
6348 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006350 * Return -1 if curly braces expansion failed.
6351 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 * If the name contains 'magic' {}'s, expand them and return the
6353 * expanded name in an allocated string via 'alias' - caller must free.
6354 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356get_name_len(
6357 char_u **arg,
6358 char_u **alias,
6359 int evaluate,
6360 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 char_u *p;
6364 char_u *expr_start;
6365 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
6367 *alias = NULL; /* default to no alias */
6368
6369 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6370 && (*arg)[2] == (int)KE_SNR)
6371 {
6372 /* hard coded <SNR>, already translated */
6373 *arg += 3;
6374 return get_id_len(arg) + 3;
6375 }
6376 len = eval_fname_script(*arg);
6377 if (len > 0)
6378 {
6379 /* literal "<SID>", "s:" or "<SNR>" */
6380 *arg += len;
6381 }
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006386 p = find_name_end(*arg, &expr_start, &expr_end,
6387 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (expr_start != NULL)
6389 {
6390 char_u *temp_string;
6391
6392 if (!evaluate)
6393 {
6394 len += (int)(p - *arg);
6395 *arg = skipwhite(p);
6396 return len;
6397 }
6398
6399 /*
6400 * Include any <SID> etc in the expanded string:
6401 * Thus the -len here.
6402 */
6403 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6404 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006405 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 *alias = temp_string;
6407 *arg = skipwhite(p);
6408 return (int)STRLEN(temp_string);
6409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006412 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 EMSG2(_(e_invexpr2), *arg);
6414
6415 return len;
6416}
6417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418/*
6419 * Find the end of a variable or function name, taking care of magic braces.
6420 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6421 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006422 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006423 * Return a pointer to just after the name. Equal to "arg" if there is no
6424 * valid name.
6425 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006426 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006427find_name_end(
6428 char_u *arg,
6429 char_u **expr_start,
6430 char_u **expr_end,
6431 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006433 int mb_nest = 0;
6434 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006436 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006438 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006440 *expr_start = NULL;
6441 *expr_end = NULL;
6442 }
6443
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006444 /* Quick check for valid starting character. */
6445 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6446 return arg;
6447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006448 for (p = arg; *p != NUL
6449 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006450 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006451 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006452 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006453 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006454 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006455 if (*p == '\'')
6456 {
6457 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006458 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006459 ;
6460 if (*p == NUL)
6461 break;
6462 }
6463 else if (*p == '"')
6464 {
6465 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006466 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006467 if (*p == '\\' && p[1] != NUL)
6468 ++p;
6469 if (*p == NUL)
6470 break;
6471 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006472 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6473 {
6474 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006475 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006476 len = (int)(p - arg);
6477 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006478 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006479 break;
6480 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006482 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006484 if (*p == '[')
6485 ++br_nest;
6486 else if (*p == ']')
6487 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 if (*p == '{')
6493 {
6494 mb_nest++;
6495 if (expr_start != NULL && *expr_start == NULL)
6496 *expr_start = p;
6497 }
6498 else if (*p == '}')
6499 {
6500 mb_nest--;
6501 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6502 *expr_end = p;
6503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 }
6506
6507 return p;
6508}
6509
6510/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006511 * Expands out the 'magic' {}'s in a variable/function name.
6512 * Note that this can call itself recursively, to deal with
6513 * constructs like foo{bar}{baz}{bam}
6514 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6515 * "in_start" ^
6516 * "expr_start" ^
6517 * "expr_end" ^
6518 * "in_end" ^
6519 *
6520 * Returns a new allocated string, which the caller must free.
6521 * Returns NULL for failure.
6522 */
6523 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006524make_expanded_name(
6525 char_u *in_start,
6526 char_u *expr_start,
6527 char_u *expr_end,
6528 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006529{
6530 char_u c1;
6531 char_u *retval = NULL;
6532 char_u *temp_result;
6533 char_u *nextcmd = NULL;
6534
6535 if (expr_end == NULL || in_end == NULL)
6536 return NULL;
6537 *expr_start = NUL;
6538 *expr_end = NUL;
6539 c1 = *in_end;
6540 *in_end = NUL;
6541
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006542 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006543 if (temp_result != NULL && nextcmd == NULL)
6544 {
6545 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6546 + (in_end - expr_end) + 1));
6547 if (retval != NULL)
6548 {
6549 STRCPY(retval, in_start);
6550 STRCAT(retval, temp_result);
6551 STRCAT(retval, expr_end + 1);
6552 }
6553 }
6554 vim_free(temp_result);
6555
6556 *in_end = c1; /* put char back for error messages */
6557 *expr_start = '{';
6558 *expr_end = '}';
6559
6560 if (retval != NULL)
6561 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006562 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006563 if (expr_start != NULL)
6564 {
6565 /* Further expansion! */
6566 temp_result = make_expanded_name(retval, expr_start,
6567 expr_end, temp_result);
6568 vim_free(retval);
6569 retval = temp_result;
6570 }
6571 }
6572
6573 return retval;
6574}
6575
6576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006583 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6584}
6585
6586/*
6587 * Return TRUE if character "c" can be used as the first character in a
6588 * variable or function name (excluding '{' and '}').
6589 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006590 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006591eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006592{
6593 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
6596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 * Set number v: variable to "val".
6598 */
6599 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006600set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
6605/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006606 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006608 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612}
6613
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006614/*
6615 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006616 * If the String variable has never been set, return an empty string.
6617 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006618 */
6619 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006621{
6622 return get_tv_string(&vimvars[idx].vv_tv);
6623}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006624
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006626 * Get List v: variable value. Caller must take care of reference count when
6627 * needed.
6628 */
6629 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006630get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006631{
6632 return vimvars[idx].vv_list;
6633}
6634
6635/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006636 * Set v:char to character "c".
6637 */
6638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006639set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006640{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006641 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006642
6643#ifdef FEAT_MBYTE
6644 if (has_mbyte)
6645 buf[(*mb_char2bytes)(c, buf)] = NUL;
6646 else
6647#endif
6648 {
6649 buf[0] = c;
6650 buf[1] = NUL;
6651 }
6652 set_vim_var_string(VV_CHAR, buf, -1);
6653}
6654
6655/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006656 * Set v:count to "count" and v:count1 to "count1".
6657 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 */
6659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006660set_vcount(
6661 long count,
6662 long count1,
6663 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006665 if (set_prevcount)
6666 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 vimvars[VV_COUNT].vv_nr = count;
6668 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669}
6670
6671/*
6672 * Set string v: variable to a copy of "val".
6673 */
6674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675set_vim_var_string(
6676 int idx,
6677 char_u *val,
6678 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
Bram Moolenaara542c682016-01-31 16:28:04 +01006680 clear_tv(&vimvars[idx].vv_di.di_tv);
6681 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006683 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006687 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689
6690/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006691 * Set List v: variable to "val".
6692 */
6693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006695{
Bram Moolenaara542c682016-01-31 16:28:04 +01006696 clear_tv(&vimvars[idx].vv_di.di_tv);
6697 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006698 vimvars[idx].vv_list = val;
6699 if (val != NULL)
6700 ++val->lv_refcount;
6701}
6702
6703/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006704 * Set Dictionary v: variable to "val".
6705 */
6706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006707set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006708{
6709 int todo;
6710 hashitem_T *hi;
6711
Bram Moolenaara542c682016-01-31 16:28:04 +01006712 clear_tv(&vimvars[idx].vv_di.di_tv);
6713 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006714 vimvars[idx].vv_dict = val;
6715 if (val != NULL)
6716 {
6717 ++val->dv_refcount;
6718
6719 /* Set readonly */
6720 todo = (int)val->dv_hashtab.ht_used;
6721 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6722 {
6723 if (HASHITEM_EMPTY(hi))
6724 continue;
6725 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006726 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006727 }
6728 }
6729}
6730
6731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Set v:register if needed.
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 char_u regname;
6738
6739 if (c == 0 || c == ' ')
6740 regname = '"';
6741 else
6742 regname = c;
6743 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006744 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 set_vim_var_string(VV_REG, &regname, 1);
6746}
6747
6748/*
6749 * Get or set v:exception. If "oldval" == NULL, return the current value.
6750 * Otherwise, restore the value to "oldval" and return NULL.
6751 * Must always be called in pairs to save and restore v:exception! Does not
6752 * take care of memory allocations.
6753 */
6754 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 return NULL;
6762}
6763
6764/*
6765 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6766 * Otherwise, restore the value to "oldval" and return NULL.
6767 * Must always be called in pairs to save and restore v:throwpoint! Does not
6768 * take care of memory allocations.
6769 */
6770 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 return NULL;
6778}
6779
6780#if defined(FEAT_AUTOCMD) || defined(PROTO)
6781/*
6782 * Set v:cmdarg.
6783 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6784 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6785 * Must always be called in pairs!
6786 */
6787 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006788set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 char_u *oldval;
6791 char_u *newval;
6792 unsigned len;
6793
Bram Moolenaare9a41262005-01-15 22:18:47 +00006794 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006795 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006797 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006799 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 }
6801
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006802 if (eap->force_bin == FORCE_BIN)
6803 len = 6;
6804 else if (eap->force_bin == FORCE_NOBIN)
6805 len = 8;
6806 else
6807 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006808
6809 if (eap->read_edit)
6810 len += 7;
6811
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006812 if (eap->force_ff != 0)
6813 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6814# ifdef FEAT_MBYTE
6815 if (eap->force_enc != 0)
6816 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006817 if (eap->bad_char != 0)
6818 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006819# endif
6820
6821 newval = alloc(len + 1);
6822 if (newval == NULL)
6823 return NULL;
6824
6825 if (eap->force_bin == FORCE_BIN)
6826 sprintf((char *)newval, " ++bin");
6827 else if (eap->force_bin == FORCE_NOBIN)
6828 sprintf((char *)newval, " ++nobin");
6829 else
6830 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831
6832 if (eap->read_edit)
6833 STRCAT(newval, " ++edit");
6834
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006835 if (eap->force_ff != 0)
6836 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6837 eap->cmd + eap->force_ff);
6838# ifdef FEAT_MBYTE
6839 if (eap->force_enc != 0)
6840 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6841 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006842 if (eap->bad_char == BAD_KEEP)
6843 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6844 else if (eap->bad_char == BAD_DROP)
6845 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6846 else if (eap->bad_char != 0)
6847 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006848# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006850 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851}
6852#endif
6853
6854/*
6855 * Get the value of internal variable "name".
6856 * Return OK or FAIL.
6857 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006858 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006859get_var_tv(
6860 char_u *name,
6861 int len, /* length of "name" */
6862 typval_T *rettv, /* NULL when only checking existence */
6863 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6864 int verbose, /* may give error message */
6865 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006869 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
6872 /* truncate the name, so that we can use strcmp() */
6873 cc = name[len];
6874 name[len] = NUL;
6875
6876 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 * Check for user-defined variables.
6878 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006879 v = find_var(name, NULL, no_autoload);
6880 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006882 tv = &v->di_tv;
6883 if (dip != NULL)
6884 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006889 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006890 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 ret = FAIL;
6892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006894 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 name[len] = cc;
6897
6898 return ret;
6899}
6900
6901/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006902 * Check if variable "name[len]" is a local variable or an argument.
6903 * If so, "*eval_lavars_used" is set to TRUE.
6904 */
6905 static void
6906check_vars(char_u *name, int len)
6907{
6908 int cc;
6909 char_u *varname;
6910 hashtab_T *ht;
6911
6912 if (eval_lavars_used == NULL)
6913 return;
6914
6915 /* truncate the name, so that we can use strcmp() */
6916 cc = name[len];
6917 name[len] = NUL;
6918
6919 ht = find_var_ht(name, &varname);
6920 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6921 {
6922 if (find_var(name, NULL, TRUE) != NULL)
6923 *eval_lavars_used = TRUE;
6924 }
6925
6926 name[len] = cc;
6927}
6928
6929/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006930 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6931 * Also handle function call with Funcref variable: func(expr)
6932 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6933 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006935handle_subscript(
6936 char_u **arg,
6937 typval_T *rettv,
6938 int evaluate, /* do more than finding the end */
6939 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006940{
6941 int ret = OK;
6942 dict_T *selfdict = NULL;
6943 char_u *s;
6944 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006946
6947 while (ret == OK
6948 && (**arg == '['
6949 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006950 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6951 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006952 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006953 {
6954 if (**arg == '(')
6955 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006956 partial_T *pt = NULL;
6957
Bram Moolenaard9fba312005-06-26 22:34:35 +00006958 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006959 if (evaluate)
6960 {
6961 functv = *rettv;
6962 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006963
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006964 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006965 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006966 {
6967 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006968 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006969 }
6970 else
6971 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006972 }
6973 else
6974 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006975 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006976 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006977 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006978
6979 /* Clear the funcref afterwards, so that deleting it while
6980 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006981 if (evaluate)
6982 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006983
6984 /* Stop the expression evaluation when immediately aborting on
6985 * error, or when an interrupt occurred or an exception was thrown
6986 * but not caught. */
6987 if (aborting())
6988 {
6989 if (ret == OK)
6990 clear_tv(rettv);
6991 ret = FAIL;
6992 }
6993 dict_unref(selfdict);
6994 selfdict = NULL;
6995 }
6996 else /* **arg == '[' || **arg == '.' */
6997 {
6998 dict_unref(selfdict);
6999 if (rettv->v_type == VAR_DICT)
7000 {
7001 selfdict = rettv->vval.v_dict;
7002 if (selfdict != NULL)
7003 ++selfdict->dv_refcount;
7004 }
7005 else
7006 selfdict = NULL;
7007 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7008 {
7009 clear_tv(rettv);
7010 ret = FAIL;
7011 }
7012 }
7013 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007014
Bram Moolenaar1d429612016-05-24 15:44:17 +02007015 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7016 * Don't do this when "Func" is already a partial that was bound
7017 * explicitly (pt_auto is FALSE). */
7018 if (selfdict != NULL
7019 && (rettv->v_type == VAR_FUNC
7020 || (rettv->v_type == VAR_PARTIAL
7021 && (rettv->vval.v_partial->pt_auto
7022 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007023 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007024
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007025 dict_unref(selfdict);
7026 return ret;
7027}
7028
7029/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007030 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007031 * value).
7032 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007033 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035{
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037}
7038
7039/*
7040 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 * The string "s" must have been allocated, it is consumed.
7042 * Return NULL for out of memory, the variable otherwise.
7043 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007044 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007049 rettv = alloc_tv();
7050 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007052 rettv->v_type = VAR_STRING;
7053 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055 else
7056 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007057 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058}
7059
7060/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007061 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007064free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 if (varp != NULL)
7067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007068 switch (varp->v_type)
7069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007070 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007071 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007072 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007073 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007074 vim_free(varp->vval.v_string);
7075 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007076 case VAR_PARTIAL:
7077 partial_unref(varp->vval.v_partial);
7078 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007079 case VAR_LIST:
7080 list_unref(varp->vval.v_list);
7081 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007082 case VAR_DICT:
7083 dict_unref(varp->vval.v_dict);
7084 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007085 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007086#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007087 job_unref(varp->vval.v_job);
7088 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007089#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007090 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007091#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007092 channel_unref(varp->vval.v_channel);
7093 break;
7094#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007095 case VAR_NUMBER:
7096 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007097 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007098 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007099 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 vim_free(varp);
7102 }
7103}
7104
7105/*
7106 * Free the memory for a variable value and set the value to NULL or 0.
7107 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
7111 if (varp != NULL)
7112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007116 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007117 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007118 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 vim_free(varp->vval.v_string);
7120 varp->vval.v_string = NULL;
7121 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007122 case VAR_PARTIAL:
7123 partial_unref(varp->vval.v_partial);
7124 varp->vval.v_partial = NULL;
7125 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 case VAR_LIST:
7127 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007129 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 case VAR_DICT:
7131 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007134 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007135 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007136 varp->vval.v_number = 0;
7137 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007138 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007139#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007140 varp->vval.v_float = 0.0;
7141 break;
7142#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007143 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007145 job_unref(varp->vval.v_job);
7146 varp->vval.v_job = NULL;
7147#endif
7148 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007149 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007150#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007151 channel_unref(varp->vval.v_channel);
7152 varp->vval.v_channel = NULL;
7153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 case VAR_UNKNOWN:
7155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007157 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159}
7160
7161/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007162 * Set the value of a variable to NULL without freeing items.
7163 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166{
7167 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007169}
7170
7171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 * Get the number value of a variable.
7173 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 * For incompatible types, return 0.
7175 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7176 * caller of incompatible types: it sets *denote to TRUE if "denote"
7177 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007179 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007182 int error = FALSE;
7183
7184 return get_tv_number_chk(varp, &error); /* return 0L on error */
7185}
7186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007187 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007188get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007189{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007190 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007194 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007195 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007196 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007197#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007198 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007199 break;
7200#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007201 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007202 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007203 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007204 break;
7205 case VAR_STRING:
7206 if (varp->vval.v_string != NULL)
7207 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007208 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007209 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007210 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007211 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007212 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007213 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007214 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007215 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007216 case VAR_SPECIAL:
7217 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7218 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007219 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007220#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007221 EMSG(_("E910: Using a Job as a Number"));
7222 break;
7223#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007224 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007225#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007226 EMSG(_("E913: Using a Channel as a Number"));
7227 break;
7228#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007229 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007230 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007233 if (denote == NULL) /* useful for values that must be unsigned */
7234 n = -1;
7235 else
7236 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007237 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238}
7239
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007240#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007242get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007243{
7244 switch (varp->v_type)
7245 {
7246 case VAR_NUMBER:
7247 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007248 case VAR_FLOAT:
7249 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007250 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007251 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007252 EMSG(_("E891: Using a Funcref as a Float"));
7253 break;
7254 case VAR_STRING:
7255 EMSG(_("E892: Using a String as a Float"));
7256 break;
7257 case VAR_LIST:
7258 EMSG(_("E893: Using a List as a Float"));
7259 break;
7260 case VAR_DICT:
7261 EMSG(_("E894: Using a Dictionary as a Float"));
7262 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007263 case VAR_SPECIAL:
7264 EMSG(_("E907: Using a special value as a Float"));
7265 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007266 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007267# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007268 EMSG(_("E911: Using a Job as a Float"));
7269 break;
7270# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007272# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007273 EMSG(_("E914: Using a Channel as a Float"));
7274 break;
7275# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007276 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007277 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007278 break;
7279 }
7280 return 0;
7281}
7282#endif
7283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 * Get the string value of a variable.
7286 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007287 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7288 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 * If the String variable has never been set, return an empty string.
7290 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007291 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7292 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007295get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297 static char_u mybuf[NUMBUFLEN];
7298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007299 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300}
7301
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007302 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007303get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007305 char_u *res = get_tv_string_buf_chk(varp, buf);
7306
7307 return res != NULL ? res : (char_u *)"";
7308}
7309
Bram Moolenaar7d647822014-04-05 21:28:56 +02007310/*
7311 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7312 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007313 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007314get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315{
7316 static char_u mybuf[NUMBUFLEN];
7317
7318 return get_tv_string_buf_chk(varp, mybuf);
7319}
7320
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007321 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007322get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007323{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007324 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007326 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007327 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7328 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329 return buf;
7330 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007331 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007332 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007333 break;
7334 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007335 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 break;
7337 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007338 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007339 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007340 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007341#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007342 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007343 break;
7344#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007345 case VAR_STRING:
7346 if (varp->vval.v_string != NULL)
7347 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007348 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007349 case VAR_SPECIAL:
7350 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7351 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007352 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007354 {
7355 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007356 char *status;
7357
7358 if (job == NULL)
7359 return (char_u *)"no process";
7360 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007361 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007362 : "run";
7363# ifdef UNIX
7364 vim_snprintf((char *)buf, NUMBUFLEN,
7365 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007366# elif defined(WIN32)
7367 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007368 "process %ld %s",
7369 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007370 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007371# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007372 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007373 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7374# endif
7375 return buf;
7376 }
7377#endif
7378 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007379 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007380#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007381 {
7382 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007383 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007384
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007385 if (channel == NULL)
7386 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7387 else
7388 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007389 "channel %d %s", channel->ch_id, status);
7390 return buf;
7391 }
7392#endif
7393 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007394 case VAR_UNKNOWN:
7395 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007398 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401/*
7402 * Find variable "name" in the list of variables.
7403 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007404 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007405 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007408 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007413 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaara7043832005-01-21 11:56:39 +00007415 ht = find_var_ht(name, &varname);
7416 if (htp != NULL)
7417 *htp = ht;
7418 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007420 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7421 if (ret != NULL)
7422 return ret;
7423
7424 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007425 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426}
7427
7428/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007429 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007430 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007432 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433find_var_in_ht(
7434 hashtab_T *ht,
7435 int htname,
7436 char_u *varname,
7437 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007438{
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 hashitem_T *hi;
7440
7441 if (*varname == NUL)
7442 {
7443 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007444 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007446 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 case 'g': return &globvars_var;
7448 case 'v': return &vimvars_var;
7449 case 'b': return &curbuf->b_bufvar;
7450 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007451 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007452 case 'l': return get_funccal_local_var();
7453 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 }
7455 return NULL;
7456 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007457
7458 hi = hash_find(ht, varname);
7459 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007460 {
7461 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007462 * worked find the variable again. Don't auto-load a script if it was
7463 * loaded already, otherwise it would be loaded every time when
7464 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007465 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007466 {
7467 /* Note: script_autoload() may make "hi" invalid. It must either
7468 * be obtained again or not used. */
7469 if (!script_autoload(varname, FALSE) || aborting())
7470 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007471 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007472 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007473 if (HASHITEM_EMPTY(hi))
7474 return NULL;
7475 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007477}
7478
7479/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007481 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007482 * Set "varname" to the start of name without ':'.
7483 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007484 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007485find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007487 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007488 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007489
Bram Moolenaar73627d02015-08-11 15:46:09 +02007490 if (name[0] == NUL)
7491 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (name[1] != ':')
7493 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007494 /* The name must not start with a colon or #. */
7495 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 return NULL;
7497 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007498
7499 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007500 hi = hash_find(&compat_hashtab, name);
7501 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007502 return &compat_hashtab;
7503
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007504 ht = get_funccal_local_ht();
7505 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007507 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007510 if (*name == 'g') /* global variable */
7511 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007512 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7513 */
7514 if (vim_strchr(name + 2, ':') != NULL
7515 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007516 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007518 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007520 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007521 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007522 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 if (*name == 'v') /* v: variable */
7524 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007525 if (*name == 'a') /* a: function argument */
7526 return get_funccal_args_ht();
7527 if (*name == 'l') /* l: local function variable */
7528 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (*name == 's' /* script variable */
7530 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7531 return &SCRIPT_VARS(current_SID);
7532 return NULL;
7533}
7534
7535/*
7536 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007537 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 * Returns NULL when it doesn't exist.
7539 */
7540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007545 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (v == NULL)
7547 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549}
7550
7551/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 * sourcing this script and when executing functions defined in the script.
7554 */
7555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaara7043832005-01-21 11:56:39 +00007558 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 hashtab_T *ht;
7560 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7563 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007564 /* Re-allocating ga_data means that an ht_array pointing to
7565 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007567 for (i = 1; i <= ga_scripts.ga_len; ++i)
7568 {
7569 ht = &SCRIPT_VARS(i);
7570 if (ht->ht_mask == HT_INIT_SIZE - 1)
7571 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007572 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007574 }
7575
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 while (ga_scripts.ga_len < id)
7577 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007578 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007579 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007580 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 }
7583 }
7584}
7585
7586/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7588 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 */
7590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007591init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007594 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007595 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007596 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007597 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 dict_var->di_tv.vval.v_dict = dict;
7599 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007600 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7602 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
7605/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007606 * Unreference a dictionary initialized by init_var_dict().
7607 */
7608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007609unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007610{
7611 /* Now the dict needs to be freed if no one else is using it, go back to
7612 * normal reference counting. */
7613 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7614 dict_unref(dict);
7615}
7616
7617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 * Frees all allocated variables and the value they contain.
7620 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 */
7622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007623vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007624{
7625 vars_clear_ext(ht, TRUE);
7626}
7627
7628/*
7629 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7630 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaara7043832005-01-21 11:56:39 +00007634 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007635 hashitem_T *hi;
7636 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007639 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007640 for (hi = ht->ht_array; todo > 0; ++hi)
7641 {
7642 if (!HASHITEM_EMPTY(hi))
7643 {
7644 --todo;
7645
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007647 * ht_array might change then. hash_clear() takes care of it
7648 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 v = HI2DI(hi);
7650 if (free_val)
7651 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007652 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007654 }
7655 }
7656 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007657 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
Bram Moolenaara7043832005-01-21 11:56:39 +00007660/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 * Delete a variable from hashtab "ht" at item "hi".
7662 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007668
7669 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 clear_tv(&di->di_tv);
7671 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672}
7673
7674/*
7675 * List the value of one internal variable.
7676 */
7677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 char_u *tofree;
7681 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007682 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007684 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007686 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007691list_one_var_a(
7692 char_u *prefix,
7693 char_u *name,
7694 int type,
7695 char_u *string,
7696 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
Bram Moolenaar31859182007-08-14 20:41:13 +00007698 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7699 msg_start();
7700 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (name != NULL) /* "a:" vars don't have a name stored */
7702 msg_puts(name);
7703 msg_putchar(' ');
7704 msg_advance(22);
7705 if (type == VAR_NUMBER)
7706 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007707 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007708 msg_putchar('*');
7709 else if (type == VAR_LIST)
7710 {
7711 msg_putchar('[');
7712 if (*string == '[')
7713 ++string;
7714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 else if (type == VAR_DICT)
7716 {
7717 msg_putchar('{');
7718 if (*string == '{')
7719 ++string;
7720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 else
7722 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007726 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007727 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007728 if (*first)
7729 {
7730 msg_clr_eos();
7731 *first = FALSE;
7732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733}
7734
7735/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 * If the variable already exists, the value is updated.
7738 * Otherwise the variable is created.
7739 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007741set_var(
7742 char_u *name,
7743 typval_T *tv,
7744 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007750 ht = find_var_ht(name, &varname);
7751 if (ht == NULL || *varname == NUL)
7752 {
7753 EMSG2(_(e_illvar), name);
7754 return;
7755 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007756 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007757
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007758 /* Search in parent scope which is possible to reference from lambda */
7759 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007760 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007761
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007762 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7763 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007764 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007765
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007769 if (var_check_ro(v->di_flags, name, FALSE)
7770 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007772
7773 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007774 * Handle setting internal v: variables separately where needed to
7775 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 */
7777 if (ht == &vimvarht)
7778 {
7779 if (v->di_tv.v_type == VAR_STRING)
7780 {
7781 vim_free(v->di_tv.vval.v_string);
7782 if (copy || tv->v_type != VAR_STRING)
7783 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7784 else
7785 {
7786 /* Take over the string to avoid an extra alloc/free. */
7787 v->di_tv.vval.v_string = tv->vval.v_string;
7788 tv->vval.v_string = NULL;
7789 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007790 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007792 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007793 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795 if (STRCMP(varname, "searchforward") == 0)
7796 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007797#ifdef FEAT_SEARCH_EXTRA
7798 else if (STRCMP(varname, "hlsearch") == 0)
7799 {
7800 no_hlsearch = !v->di_tv.vval.v_number;
7801 redraw_all_later(SOME_VALID);
7802 }
7803#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007804 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007805 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007806 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007807 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 }
7809
7810 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
7812 else /* add a new variable */
7813 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007814 /* Can't add "v:" variable. */
7815 if (ht == &vimvarht)
7816 {
7817 EMSG2(_(e_illvar), name);
7818 return;
7819 }
7820
Bram Moolenaar92124a32005-06-17 22:03:40 +00007821 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007822 if (!valid_varname(varname))
7823 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007825 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7826 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007827 if (v == NULL)
7828 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007830 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007832 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 return;
7834 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007835 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007837
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007840 else
7841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007843 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007849 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 * Also give an error message.
7851 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007854{
7855 if (flags & DI_FLAGS_RO)
7856 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007857 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 return TRUE;
7859 }
7860 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7861 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007862 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 return TRUE;
7864 }
7865 return FALSE;
7866}
7867
7868/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007869 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7870 * Also give an error message.
7871 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007873var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007874{
7875 if (flags & DI_FLAGS_FIX)
7876 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007877 EMSG2(_("E795: Cannot delete variable %s"),
7878 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007879 return TRUE;
7880 }
7881 return FALSE;
7882}
7883
7884/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007885 * Check if a funcref is assigned to a valid variable name.
7886 * Return TRUE and give an error if not.
7887 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889var_check_func_name(
7890 char_u *name, /* points to start of variable name */
7891 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007892{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007893 /* Allow for w: b: s: and t:. */
7894 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007895 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7896 ? name[2] : name[0]))
7897 {
7898 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7899 name);
7900 return TRUE;
7901 }
7902 /* Don't allow hiding a function. When "v" is not NULL we might be
7903 * assigning another function to the same var, the type is checked
7904 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007905 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007906 {
7907 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7908 name);
7909 return TRUE;
7910 }
7911 return FALSE;
7912}
7913
7914/*
7915 * Check if a variable name is valid.
7916 * Return FALSE and give an error if not.
7917 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007919valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007920{
7921 char_u *p;
7922
7923 for (p = varname; *p != NUL; ++p)
7924 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7925 && *p != AUTOLOAD_CHAR)
7926 {
7927 EMSG2(_(e_illvar), varname);
7928 return FALSE;
7929 }
7930 return TRUE;
7931}
7932
7933/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007934 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007935 * Also give an error message, using "name" or _("name") when use_gettext is
7936 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007937 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007939tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007940{
7941 if (lock & VAR_LOCKED)
7942 {
7943 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007944 name == NULL ? (char_u *)_("Unknown")
7945 : use_gettext ? (char_u *)_(name)
7946 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007947 return TRUE;
7948 }
7949 if (lock & VAR_FIXED)
7950 {
7951 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007952 name == NULL ? (char_u *)_("Unknown")
7953 : use_gettext ? (char_u *)_(name)
7954 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007955 return TRUE;
7956 }
7957 return FALSE;
7958}
7959
7960/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007964 * It is OK for "from" and "to" to point to the same item. This is used to
7965 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007966 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007968copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007971 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 switch (from->v_type)
7973 {
7974 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007975 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 to->vval.v_number = from->vval.v_number;
7977 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007978 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007979#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007980 to->vval.v_float = from->vval.v_float;
7981 break;
7982#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007983 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007985 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007986 if (to->vval.v_job != NULL)
7987 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007988 break;
7989#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007990 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007991#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007992 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007993 if (to->vval.v_channel != NULL)
7994 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007995 break;
7996#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 case VAR_STRING:
7998 case VAR_FUNC:
7999 if (from->vval.v_string == NULL)
8000 to->vval.v_string = NULL;
8001 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008004 if (from->v_type == VAR_FUNC)
8005 func_ref(to->vval.v_string);
8006 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008008 case VAR_PARTIAL:
8009 if (from->vval.v_partial == NULL)
8010 to->vval.v_partial = NULL;
8011 else
8012 {
8013 to->vval.v_partial = from->vval.v_partial;
8014 ++to->vval.v_partial->pt_refcount;
8015 }
8016 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 case VAR_LIST:
8018 if (from->vval.v_list == NULL)
8019 to->vval.v_list = NULL;
8020 else
8021 {
8022 to->vval.v_list = from->vval.v_list;
8023 ++to->vval.v_list->lv_refcount;
8024 }
8025 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008026 case VAR_DICT:
8027 if (from->vval.v_dict == NULL)
8028 to->vval.v_dict = NULL;
8029 else
8030 {
8031 to->vval.v_dict = from->vval.v_dict;
8032 ++to->vval.v_dict->dv_refcount;
8033 }
8034 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008035 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008036 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008037 break;
8038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039}
8040
8041/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042 * Make a copy of an item.
8043 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008044 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8045 * reference to an already copied list/dict can be used.
8046 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008049item_copy(
8050 typval_T *from,
8051 typval_T *to,
8052 int deep,
8053 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054{
8055 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008057
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008059 {
8060 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008061 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 }
8063 ++recurse;
8064
8065 switch (from->v_type)
8066 {
8067 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 case VAR_STRING:
8070 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008071 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008072 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008073 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008074 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008075 copy_tv(from, to);
8076 break;
8077 case VAR_LIST:
8078 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008079 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008080 if (from->vval.v_list == NULL)
8081 to->vval.v_list = NULL;
8082 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8083 {
8084 /* use the copy made earlier */
8085 to->vval.v_list = from->vval.v_list->lv_copylist;
8086 ++to->vval.v_list->lv_refcount;
8087 }
8088 else
8089 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8090 if (to->vval.v_list == NULL)
8091 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008092 break;
8093 case VAR_DICT:
8094 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008095 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008096 if (from->vval.v_dict == NULL)
8097 to->vval.v_dict = NULL;
8098 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8099 {
8100 /* use the copy made earlier */
8101 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8102 ++to->vval.v_dict->dv_refcount;
8103 }
8104 else
8105 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8106 if (to->vval.v_dict == NULL)
8107 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008109 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008110 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008111 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008112 }
8113 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008114 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115}
8116
8117/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 * This function is used by f_input() and f_inputdialog() functions. The third
8119 * argument to f_input() specifies the type of completion to use at the
8120 * prompt. The third argument to f_inputdialog() specifies the value to return
8121 * when the user cancels the prompt.
8122 */
8123 void
8124get_user_input(
8125 typval_T *argvars,
8126 typval_T *rettv,
8127 int inputdialog,
8128 int secret)
8129{
8130 char_u *prompt = get_tv_string_chk(&argvars[0]);
8131 char_u *p = NULL;
8132 int c;
8133 char_u buf[NUMBUFLEN];
8134 int cmd_silent_save = cmd_silent;
8135 char_u *defstr = (char_u *)"";
8136 int xp_type = EXPAND_NOTHING;
8137 char_u *xp_arg = NULL;
8138
8139 rettv->v_type = VAR_STRING;
8140 rettv->vval.v_string = NULL;
8141
8142#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008143 /* While starting up, there is no place to enter text. When running tests
8144 * with --not-a-term we assume feedkeys() will be used. */
8145 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 return;
8147#endif
8148
8149 cmd_silent = FALSE; /* Want to see the prompt. */
8150 if (prompt != NULL)
8151 {
8152 /* Only the part of the message after the last NL is considered as
8153 * prompt for the command line */
8154 p = vim_strrchr(prompt, '\n');
8155 if (p == NULL)
8156 p = prompt;
8157 else
8158 {
8159 ++p;
8160 c = *p;
8161 *p = NUL;
8162 msg_start();
8163 msg_clr_eos();
8164 msg_puts_attr(prompt, echo_attr);
8165 msg_didout = FALSE;
8166 msg_starthere();
8167 *p = c;
8168 }
8169 cmdline_row = msg_row;
8170
8171 if (argvars[1].v_type != VAR_UNKNOWN)
8172 {
8173 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8174 if (defstr != NULL)
8175 stuffReadbuffSpec(defstr);
8176
8177 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8178 {
8179 char_u *xp_name;
8180 int xp_namelen;
8181 long argt;
8182
8183 /* input() with a third argument: completion */
8184 rettv->vval.v_string = NULL;
8185
8186 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8187 if (xp_name == NULL)
8188 return;
8189
8190 xp_namelen = (int)STRLEN(xp_name);
8191
8192 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8193 &xp_arg) == FAIL)
8194 return;
8195 }
8196 }
8197
8198 if (defstr != NULL)
8199 {
8200 int save_ex_normal_busy = ex_normal_busy;
8201 ex_normal_busy = 0;
8202 rettv->vval.v_string =
8203 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8204 xp_type, xp_arg);
8205 ex_normal_busy = save_ex_normal_busy;
8206 }
8207 if (inputdialog && rettv->vval.v_string == NULL
8208 && argvars[1].v_type != VAR_UNKNOWN
8209 && argvars[2].v_type != VAR_UNKNOWN)
8210 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8211 &argvars[2], buf));
8212
8213 vim_free(xp_arg);
8214
8215 /* since the user typed this, no need to wait for return */
8216 need_wait_return = FALSE;
8217 msg_didout = FALSE;
8218 }
8219 cmd_silent = cmd_silent_save;
8220}
8221
8222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 * ":echo expr1 ..." print each argument separated with a space, add a
8224 * newline at the end.
8225 * ":echon expr1 ..." print each argument plain.
8226 */
8227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008228ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 char_u *p;
8234 int needclr = TRUE;
8235 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008236 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237
8238 if (eap->skip)
8239 ++emsg_skip;
8240 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8241 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242 /* If eval1() causes an error message the text from the command may
8243 * still need to be cleared. E.g., "echo 22,44". */
8244 need_clr_eos = needclr;
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 /*
8250 * Report the invalid expression unless the expression evaluation
8251 * has been cancelled due to an aborting error, an interrupt, or an
8252 * exception.
8253 */
8254 if (!aborting())
8255 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 break;
8258 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259 need_clr_eos = FALSE;
8260
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 if (!eap->skip)
8262 {
8263 if (atstart)
8264 {
8265 atstart = FALSE;
8266 /* Call msg_start() after eval1(), evaluating the expression
8267 * may cause a message to appear. */
8268 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008269 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008270 /* Mark the saved text as finishing the line, so that what
8271 * follows is displayed on a new line when scrolling back
8272 * at the more prompt. */
8273 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 }
8277 else if (eap->cmdidx == CMD_echo)
8278 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008279 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008280 if (p != NULL)
8281 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008283 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008285 if (*p != TAB && needclr)
8286 {
8287 /* remove any text still there from the command */
8288 msg_clr_eos();
8289 needclr = FALSE;
8290 }
8291 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008294 {
8295#ifdef FEAT_MBYTE
8296 if (has_mbyte)
8297 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008298 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008299
8300 (void)msg_outtrans_len_attr(p, i, echo_attr);
8301 p += i - 1;
8302 }
8303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008305 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 arg = skipwhite(arg);
8312 }
8313 eap->nextcmd = check_nextcmd(arg);
8314
8315 if (eap->skip)
8316 --emsg_skip;
8317 else
8318 {
8319 /* remove text that may still be there from the command */
8320 if (needclr)
8321 msg_clr_eos();
8322 if (eap->cmdidx == CMD_echo)
8323 msg_end();
8324 }
8325}
8326
8327/*
8328 * ":echohl {name}".
8329 */
8330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008331ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008333 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334}
8335
8336/*
8337 * ":execute expr1 ..." execute the result of an expression.
8338 * ":echomsg expr1 ..." Print a message
8339 * ":echoerr expr1 ..." Print an error
8340 * Each gets spaces around each argument and a newline at the end for
8341 * echo commands
8342 */
8343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008344ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 int ret = OK;
8349 char_u *p;
8350 garray_T ga;
8351 int len;
8352 int save_did_emsg;
8353
8354 ga_init2(&ga, 1, 80);
8355
8356 if (eap->skip)
8357 ++emsg_skip;
8358 while (*arg != NUL && *arg != '|' && *arg != '\n')
8359 {
8360 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
8363 /*
8364 * Report the invalid expression unless the expression evaluation
8365 * has been cancelled due to an aborting error, an interrupt, or an
8366 * exception.
8367 */
8368 if (!aborting())
8369 EMSG2(_(e_invexpr2), p);
8370 ret = FAIL;
8371 break;
8372 }
8373
8374 if (!eap->skip)
8375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 len = (int)STRLEN(p);
8378 if (ga_grow(&ga, len + 2) == FAIL)
8379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 ret = FAIL;
8382 break;
8383 }
8384 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 ga.ga_len += len;
8388 }
8389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 arg = skipwhite(arg);
8392 }
8393
8394 if (ret != FAIL && ga.ga_data != NULL)
8395 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008396 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8397 {
8398 /* Mark the already saved text as finishing the line, so that what
8399 * follows is displayed on a new line when scrolling back at the
8400 * more prompt. */
8401 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008402 }
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008407 out_flush();
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 else if (eap->cmdidx == CMD_echoerr)
8410 {
8411 /* We don't want to abort following commands, restore did_emsg. */
8412 save_did_emsg = did_emsg;
8413 EMSG((char_u *)ga.ga_data);
8414 if (!force_abort)
8415 did_emsg = save_did_emsg;
8416 }
8417 else if (eap->cmdidx == CMD_execute)
8418 do_cmdline((char_u *)ga.ga_data,
8419 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8420 }
8421
8422 ga_clear(&ga);
8423
8424 if (eap->skip)
8425 --emsg_skip;
8426
8427 eap->nextcmd = check_nextcmd(arg);
8428}
8429
8430/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431 * Find window specified by "vp" in tabpage "tp".
8432 */
8433 win_T *
8434find_win_by_nr(
8435 typval_T *vp,
8436 tabpage_T *tp UNUSED) /* NULL for current tab page */
8437{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439 int nr;
8440
8441 nr = (int)get_tv_number_chk(vp, NULL);
8442
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 if (nr < 0)
8444 return NULL;
8445 if (nr == 0)
8446 return curwin;
8447
Bram Moolenaar29323592016-07-24 22:04:11 +02008448 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8449 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450 if (nr >= LOWEST_WIN_ID)
8451 {
8452 if (wp->w_id == nr)
8453 return wp;
8454 }
8455 else if (--nr <= 0)
8456 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 if (nr >= LOWEST_WIN_ID)
8459 return NULL;
8460 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461}
8462
8463/*
8464 * Find window specified by "wvp" in tabpage "tvp".
8465 */
8466 win_T *
8467find_tabwin(
8468 typval_T *wvp, /* VAR_UNKNOWN for current window */
8469 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8470{
8471 win_T *wp = NULL;
8472 tabpage_T *tp = NULL;
8473 long n;
8474
8475 if (wvp->v_type != VAR_UNKNOWN)
8476 {
8477 if (tvp->v_type != VAR_UNKNOWN)
8478 {
8479 n = (long)get_tv_number(tvp);
8480 if (n >= 0)
8481 tp = find_tabpage(n);
8482 }
8483 else
8484 tp = curtab;
8485
8486 if (tp != NULL)
8487 wp = find_win_by_nr(wvp, tp);
8488 }
8489 else
8490 wp = curwin;
8491
8492 return wp;
8493}
8494
8495/*
8496 * getwinvar() and gettabwinvar()
8497 */
8498 void
8499getwinvar(
8500 typval_T *argvars,
8501 typval_T *rettv,
8502 int off) /* 1 for gettabwinvar() */
8503{
8504 win_T *win;
8505 char_u *varname;
8506 dictitem_T *v;
8507 tabpage_T *tp = NULL;
8508 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 win_T *oldcurwin;
8510 tabpage_T *oldtabpage;
8511 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 if (off == 1)
8514 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8515 else
8516 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 win = find_win_by_nr(&argvars[off], tp);
8518 varname = get_tv_string_chk(&argvars[off + 1]);
8519 ++emsg_off;
8520
8521 rettv->v_type = VAR_STRING;
8522 rettv->vval.v_string = NULL;
8523
8524 if (win != NULL && varname != NULL)
8525 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 /* Set curwin to be our win, temporarily. Also set the tabpage,
8527 * otherwise the window is not valid. Only do this when needed,
8528 * autocommands get blocked. */
8529 need_switch_win = !(tp == curtab && win == curwin);
8530 if (!need_switch_win
8531 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008533 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008535 if (varname[1] == NUL)
8536 {
8537 /* get all window-local options in a dict */
8538 dict_T *opts = get_winbuf_options(FALSE);
8539
8540 if (opts != NULL)
8541 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008542 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008543 done = TRUE;
8544 }
8545 }
8546 else if (get_option_tv(&varname, rettv, 1) == OK)
8547 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 done = TRUE;
8549 }
8550 else
8551 {
8552 /* Look up the variable. */
8553 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8554 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8555 varname, FALSE);
8556 if (v != NULL)
8557 {
8558 copy_tv(&v->di_tv, rettv);
8559 done = TRUE;
8560 }
8561 }
8562 }
8563
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 if (need_switch_win)
8565 /* restore previous notion of curwin */
8566 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 }
8568
8569 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8570 /* use the default return value */
8571 copy_tv(&argvars[off + 2], rettv);
8572
8573 --emsg_off;
8574}
8575
8576/*
8577 * "setwinvar()" and "settabwinvar()" functions
8578 */
8579 void
8580setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8581{
8582 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 win_T *save_curwin;
8584 tabpage_T *save_curtab;
8585 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 char_u *varname, *winvarname;
8587 typval_T *varp;
8588 char_u nbuf[NUMBUFLEN];
8589 tabpage_T *tp = NULL;
8590
8591 if (check_restricted() || check_secure())
8592 return;
8593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 if (off == 1)
8595 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8596 else
8597 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 win = find_win_by_nr(&argvars[off], tp);
8599 varname = get_tv_string_chk(&argvars[off + 1]);
8600 varp = &argvars[off + 2];
8601
8602 if (win != NULL && varname != NULL && varp != NULL)
8603 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 need_switch_win = !(tp == curtab && win == curwin);
8605 if (!need_switch_win
8606 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 {
8608 if (*varname == '&')
8609 {
8610 long numval;
8611 char_u *strval;
8612 int error = FALSE;
8613
8614 ++varname;
8615 numval = (long)get_tv_number_chk(varp, &error);
8616 strval = get_tv_string_buf_chk(varp, nbuf);
8617 if (!error && strval != NULL)
8618 set_option_value(varname, numval, strval, OPT_LOCAL);
8619 }
8620 else
8621 {
8622 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8623 if (winvarname != NULL)
8624 {
8625 STRCPY(winvarname, "w:");
8626 STRCPY(winvarname + 2, varname);
8627 set_var(winvarname, varp, TRUE);
8628 vim_free(winvarname);
8629 }
8630 }
8631 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (need_switch_win)
8633 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 }
8635}
8636
8637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8639 * "arg" points to the "&" or '+' when called, to "option" when returning.
8640 * Returns NULL when no option name found. Otherwise pointer to the char
8641 * after the option name.
8642 */
8643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008644find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 char_u *p = *arg;
8647
8648 ++p;
8649 if (*p == 'g' && p[1] == ':')
8650 {
8651 *opt_flags = OPT_GLOBAL;
8652 p += 2;
8653 }
8654 else if (*p == 'l' && p[1] == ':')
8655 {
8656 *opt_flags = OPT_LOCAL;
8657 p += 2;
8658 }
8659 else
8660 *opt_flags = 0;
8661
8662 if (!ASCII_ISALPHA(*p))
8663 return NULL;
8664 *arg = p;
8665
8666 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8667 p += 4; /* termcap option */
8668 else
8669 while (ASCII_ISALPHA(*p))
8670 ++p;
8671 return p;
8672}
8673
8674/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008675 * Return the autoload script name for a function or variable name.
8676 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008678 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008679autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008680{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008681 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008682 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008683
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008684 /* Get the script file name: replace '#' with '/', append ".vim". */
8685 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8686 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008687 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008688 STRCPY(scriptname, "autoload/");
8689 STRCAT(scriptname, name);
8690 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8691 STRCAT(scriptname, ".vim");
8692 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8693 *p = '/';
8694 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008695}
8696
8697/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008698 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008699 * Return TRUE if a package was loaded.
8700 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008701 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008702script_autoload(
8703 char_u *name,
8704 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008705{
8706 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008707 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008708 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008709 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008711 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008712 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008713 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 return FALSE;
8715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008716 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008717
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008718 /* Find the name in the list of previously loaded package names. Skip
8719 * "autoload/", it's always the same. */
8720 for (i = 0; i < ga_loaded.ga_len; ++i)
8721 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8722 break;
8723 if (!reload && i < ga_loaded.ga_len)
8724 ret = FALSE; /* was loaded already */
8725 else
8726 {
8727 /* Remember the name if it wasn't loaded already. */
8728 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8729 {
8730 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8731 tofree = NULL;
8732 }
8733
8734 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008735 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008736 ret = TRUE;
8737 }
8738
8739 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 return ret;
8741}
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8744typedef enum
8745{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008746 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8747 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8748 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749} var_flavour_T;
8750
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008751static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008754var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
8756 char_u *p = varname;
8757
8758 if (ASCII_ISUPPER(*p))
8759 {
8760 while (*(++p))
8761 if (ASCII_ISLOWER(*p))
8762 return VAR_FLAVOUR_SESSION;
8763 return VAR_FLAVOUR_VIMINFO;
8764 }
8765 else
8766 return VAR_FLAVOUR_DEFAULT;
8767}
8768#endif
8769
8770#if defined(FEAT_VIMINFO) || defined(PROTO)
8771/*
8772 * Restore global vars that start with a capital from the viminfo file
8773 */
8774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008775read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008778 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008780 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
8782 if (!writing && (find_viminfo_parameter('!') != NULL))
8783 {
8784 tab = vim_strchr(virp->vir_line + 1, '\t');
8785 if (tab != NULL)
8786 {
8787 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008788 switch (*tab)
8789 {
8790 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008792 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008793#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008794 case 'D': type = VAR_DICT; break;
8795 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008796 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
8799 tab = vim_strchr(tab, '\t');
8800 if (tab != NULL)
8801 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008802 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008803 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008804 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008806#ifdef FEAT_FLOAT
8807 else if (type == VAR_FLOAT)
8808 (void)string2float(tab + 1, &tv.vval.v_float);
8809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008811 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008812 if (type == VAR_DICT || type == VAR_LIST)
8813 {
8814 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8815
8816 if (etv == NULL)
8817 /* Failed to parse back the dict or list, use it as a
8818 * string. */
8819 tv.v_type = VAR_STRING;
8820 else
8821 {
8822 vim_free(tv.vval.v_string);
8823 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008824 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008825 }
8826 }
8827
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008828 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008829 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008830 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008831 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008832
8833 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008834 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008835 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8836 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 }
8838 }
8839 }
8840
8841 return viminfo_readline(virp);
8842}
8843
8844/*
8845 * Write global vars that start with a capital to the viminfo file
8846 */
8847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008848write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 hashitem_T *hi;
8851 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008852 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008853 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008854 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008856 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857
8858 if (find_viminfo_parameter('!') == NULL)
8859 return;
8860
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008861 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008862
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 this_var = HI2DI(hi);
8870 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008873 {
8874 case VAR_STRING: s = "STR"; break;
8875 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008876 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008877 case VAR_DICT: s = "DIC"; break;
8878 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008879 case VAR_SPECIAL: s = "XPL"; break;
8880
8881 case VAR_UNKNOWN:
8882 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008883 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008884 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008885 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008886 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008888 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008889 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008890 if (p != NULL)
8891 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008892 vim_free(tofree);
8893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895 }
8896}
8897#endif
8898
8899#if defined(FEAT_SESSION) || defined(PROTO)
8900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008901store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 hashitem_T *hi;
8904 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008905 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 char_u *p, *t;
8907
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008908 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008909 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008911 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008913 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 this_var = HI2DI(hi);
8915 if ((this_var->di_tv.v_type == VAR_NUMBER
8916 || this_var->di_tv.v_type == VAR_STRING)
8917 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008918 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008919 /* Escape special characters with a backslash. Turn a LF and
8920 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008921 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008922 (char_u *)"\\\"\n\r");
8923 if (p == NULL) /* out of memory */
8924 break;
8925 for (t = p; *t != NUL; ++t)
8926 if (*t == '\n')
8927 *t = 'n';
8928 else if (*t == '\r')
8929 *t = 'r';
8930 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008931 this_var->di_key,
8932 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8933 : ' ',
8934 p,
8935 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8936 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008937 || put_eol(fd) == FAIL)
8938 {
8939 vim_free(p);
8940 return FAIL;
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 vim_free(p);
8943 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#ifdef FEAT_FLOAT
8945 else if (this_var->di_tv.v_type == VAR_FLOAT
8946 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8947 {
8948 float_T f = this_var->di_tv.vval.v_float;
8949 int sign = ' ';
8950
8951 if (f < 0)
8952 {
8953 f = -f;
8954 sign = '-';
8955 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008956 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008957 this_var->di_key, sign, f) < 0)
8958 || put_eol(fd) == FAIL)
8959 return FAIL;
8960 }
8961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 }
8964 return OK;
8965}
8966#endif
8967
Bram Moolenaar661b1822005-07-28 22:36:45 +00008968/*
8969 * Display script name where an item was last set.
8970 * Should only be invoked when 'verbose' is non-zero.
8971 */
8972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008974{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008975 char_u *p;
8976
Bram Moolenaar661b1822005-07-28 22:36:45 +00008977 if (scriptID != 0)
8978 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008979 p = home_replace_save(NULL, get_scriptname(scriptID));
8980 if (p != NULL)
8981 {
8982 verbose_enter();
8983 MSG_PUTS(_("\n\tLast set from "));
8984 MSG_PUTS(p);
8985 vim_free(p);
8986 verbose_leave();
8987 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008988 }
8989}
8990
Bram Moolenaar53744302015-07-17 17:38:22 +02008991/* reset v:option_new, v:option_old and v:option_type */
8992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008993reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008994{
8995 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8996 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8997 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8998}
8999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000/*
9001 * Prepare "gap" for an assert error and add the sourcing position.
9002 */
9003 void
9004prepare_assert_error(garray_T *gap)
9005{
9006 char buf[NUMBUFLEN];
9007
9008 ga_init2(gap, 1, 100);
9009 if (sourcing_name != NULL)
9010 {
9011 ga_concat(gap, sourcing_name);
9012 if (sourcing_lnum > 0)
9013 ga_concat(gap, (char_u *)" ");
9014 }
9015 if (sourcing_lnum > 0)
9016 {
9017 sprintf(buf, "line %ld", (long)sourcing_lnum);
9018 ga_concat(gap, (char_u *)buf);
9019 }
9020 if (sourcing_name != NULL || sourcing_lnum > 0)
9021 ga_concat(gap, (char_u *)": ");
9022}
9023
9024/*
9025 * Add an assert error to v:errors.
9026 */
9027 void
9028assert_error(garray_T *gap)
9029{
9030 struct vimvar *vp = &vimvars[VV_ERRORS];
9031
9032 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9033 /* Make sure v:errors is a list. */
9034 set_vim_var_list(VV_ERRORS, list_alloc());
9035 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9036}
9037
9038 void
9039assert_equal_common(typval_T *argvars, assert_type_T atype)
9040{
9041 garray_T ga;
9042
9043 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9044 != (atype == ASSERT_EQUAL))
9045 {
9046 prepare_assert_error(&ga);
9047 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9048 atype);
9049 assert_error(&ga);
9050 ga_clear(&ga);
9051 }
9052}
9053
9054 void
9055assert_match_common(typval_T *argvars, assert_type_T atype)
9056{
9057 garray_T ga;
9058 char_u buf1[NUMBUFLEN];
9059 char_u buf2[NUMBUFLEN];
9060 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9061 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9062
9063 if (pat == NULL || text == NULL)
9064 EMSG(_(e_invarg));
9065 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9066 {
9067 prepare_assert_error(&ga);
9068 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9069 atype);
9070 assert_error(&ga);
9071 ga_clear(&ga);
9072 }
9073}
9074
Bram Moolenaar61c04492016-07-23 15:35:35 +02009075 void
9076assert_inrange(typval_T *argvars)
9077{
9078 garray_T ga;
9079 int error = FALSE;
9080 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9081 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9082 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9083 char_u *tofree;
9084 char msg[200];
9085 char_u numbuf[NUMBUFLEN];
9086
9087 if (error)
9088 return;
9089 if (actual < lower || actual > upper)
9090 {
9091 prepare_assert_error(&ga);
9092 if (argvars[3].v_type != VAR_UNKNOWN)
9093 {
9094 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9095 vim_free(tofree);
9096 }
9097 else
9098 {
9099 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9100 (long)lower, (long)upper, (long)actual);
9101 ga_concat(&ga, (char_u *)msg);
9102 }
9103 assert_error(&ga);
9104 ga_clear(&ga);
9105 }
9106}
9107
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108/*
9109 * Common for assert_true() and assert_false().
9110 */
9111 void
9112assert_bool(typval_T *argvars, int isTrue)
9113{
9114 int error = FALSE;
9115 garray_T ga;
9116
9117 if (argvars[0].v_type == VAR_SPECIAL
9118 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9119 return;
9120 if (argvars[0].v_type != VAR_NUMBER
9121 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9122 || error)
9123 {
9124 prepare_assert_error(&ga);
9125 fill_assert_error(&ga, &argvars[1],
9126 (char_u *)(isTrue ? "True" : "False"),
9127 NULL, &argvars[0], ASSERT_OTHER);
9128 assert_error(&ga);
9129 ga_clear(&ga);
9130 }
9131}
9132
9133 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009134assert_report(typval_T *argvars)
9135{
9136 garray_T ga;
9137
9138 prepare_assert_error(&ga);
9139 ga_concat(&ga, get_tv_string(&argvars[0]));
9140 assert_error(&ga);
9141 ga_clear(&ga);
9142}
9143
9144 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145assert_exception(typval_T *argvars)
9146{
9147 garray_T ga;
9148 char_u *error = get_tv_string_chk(&argvars[0]);
9149
9150 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9151 {
9152 prepare_assert_error(&ga);
9153 ga_concat(&ga, (char_u *)"v:exception is not set");
9154 assert_error(&ga);
9155 ga_clear(&ga);
9156 }
9157 else if (error != NULL
9158 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9159 {
9160 prepare_assert_error(&ga);
9161 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9162 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9163 assert_error(&ga);
9164 ga_clear(&ga);
9165 }
9166}
9167
9168 void
9169assert_fails(typval_T *argvars)
9170{
9171 char_u *cmd = get_tv_string_chk(&argvars[0]);
9172 garray_T ga;
9173
9174 called_emsg = FALSE;
9175 suppress_errthrow = TRUE;
9176 emsg_silent = TRUE;
9177 do_cmdline_cmd(cmd);
9178 if (!called_emsg)
9179 {
9180 prepare_assert_error(&ga);
9181 ga_concat(&ga, (char_u *)"command did not fail: ");
9182 ga_concat(&ga, cmd);
9183 assert_error(&ga);
9184 ga_clear(&ga);
9185 }
9186 else if (argvars[1].v_type != VAR_UNKNOWN)
9187 {
9188 char_u buf[NUMBUFLEN];
9189 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9190
9191 if (error == NULL
9192 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9193 {
9194 prepare_assert_error(&ga);
9195 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9196 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9197 assert_error(&ga);
9198 ga_clear(&ga);
9199 }
9200 }
9201
9202 called_emsg = FALSE;
9203 suppress_errthrow = FALSE;
9204 emsg_silent = FALSE;
9205 emsg_on_display = FALSE;
9206 set_vim_var_string(VV_ERRMSG, NULL, 0);
9207}
9208
9209/*
9210 * Append "str" to "gap", escaping unprintable characters.
9211 * Changes NL to \n, CR to \r, etc.
9212 */
9213 static void
9214ga_concat_esc(garray_T *gap, char_u *str)
9215{
9216 char_u *p;
9217 char_u buf[NUMBUFLEN];
9218
9219 if (str == NULL)
9220 {
9221 ga_concat(gap, (char_u *)"NULL");
9222 return;
9223 }
9224
9225 for (p = str; *p != NUL; ++p)
9226 switch (*p)
9227 {
9228 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9229 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9230 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9231 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9232 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9233 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9234 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9235 default:
9236 if (*p < ' ')
9237 {
9238 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9239 ga_concat(gap, buf);
9240 }
9241 else
9242 ga_append(gap, *p);
9243 break;
9244 }
9245}
9246
9247/*
9248 * Fill "gap" with information about an assert error.
9249 */
9250 void
9251fill_assert_error(
9252 garray_T *gap,
9253 typval_T *opt_msg_tv,
9254 char_u *exp_str,
9255 typval_T *exp_tv,
9256 typval_T *got_tv,
9257 assert_type_T atype)
9258{
9259 char_u numbuf[NUMBUFLEN];
9260 char_u *tofree;
9261
9262 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9263 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009264 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9265 vim_free(tofree);
9266 ga_concat(gap, (char_u *)": ");
9267 }
9268
9269 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9270 ga_concat(gap, (char_u *)"Pattern ");
9271 else if (atype == ASSERT_NOTEQUAL)
9272 ga_concat(gap, (char_u *)"Expected not equal to ");
9273 else
9274 ga_concat(gap, (char_u *)"Expected ");
9275 if (exp_str == NULL)
9276 {
9277 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 vim_free(tofree);
9279 }
9280 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009281 ga_concat_esc(gap, exp_str);
9282 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009284 if (atype == ASSERT_MATCH)
9285 ga_concat(gap, (char_u *)" does not match ");
9286 else if (atype == ASSERT_NOTMATCH)
9287 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009289 ga_concat(gap, (char_u *)" but got ");
9290 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9291 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009292 }
9293}
9294
Bram Moolenaar53744302015-07-17 17:38:22 +02009295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296#endif /* FEAT_EVAL */
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009299#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
9301#ifdef WIN3264
9302/*
9303 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9304 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009305static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9306static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9307static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
9309/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009310 * Get the short path (8.3) for the filename in "fnamep".
9311 * Only works for a valid file name.
9312 * When the path gets longer "fnamep" is changed and the allocated buffer
9313 * is put in "bufp".
9314 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9315 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 */
9317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009320 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 char_u *newbuf;
9322
9323 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009324 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 if (l > len - 1)
9326 {
9327 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009328 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 newbuf = vim_strnsave(*fnamep, l);
9330 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
9333 vim_free(*bufp);
9334 *fnamep = *bufp = newbuf;
9335
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009336 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009337 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 }
9339
9340 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009341 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342}
9343
9344/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009345 * Get the short path (8.3) for the filename in "fname". The converted
9346 * path is returned in "bufp".
9347 *
9348 * Some of the directories specified in "fname" may not exist. This function
9349 * will shorten the existing directories at the beginning of the path and then
9350 * append the remaining non-existing path.
9351 *
9352 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009353 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009354 * bufp - Pointer to an allocated buffer for the filename.
9355 * fnamelen - Length of the filename pointed to by fname
9356 *
9357 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 */
9359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360shortpath_for_invalid_fname(
9361 char_u **fname,
9362 char_u **bufp,
9363 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009365 char_u *short_fname, *save_fname, *pbuf_unused;
9366 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009368 int old_len, len;
9369 int new_len, sfx_len;
9370 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
9372 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009373 old_len = *fnamelen;
9374 save_fname = vim_strnsave(*fname, old_len);
9375 pbuf_unused = NULL;
9376 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009378 endp = save_fname + old_len - 1; /* Find the end of the copy */
9379 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009381 /*
9382 * Try shortening the supplied path till it succeeds by removing one
9383 * directory at a time from the tail of the path.
9384 */
9385 len = 0;
9386 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009388 /* go back one path-separator */
9389 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9390 --endp;
9391 if (endp <= save_fname)
9392 break; /* processed the complete path */
9393
9394 /*
9395 * Replace the path separator with a NUL and try to shorten the
9396 * resulting path.
9397 */
9398 ch = *endp;
9399 *endp = 0;
9400 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009401 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009402 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9403 {
9404 retval = FAIL;
9405 goto theend;
9406 }
9407 *endp = ch; /* preserve the string */
9408
9409 if (len > 0)
9410 break; /* successfully shortened the path */
9411
9412 /* failed to shorten the path. Skip the path separator */
9413 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 }
9415
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009416 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009418 /*
9419 * Succeeded in shortening the path. Now concatenate the shortened
9420 * path with the remaining path at the tail.
9421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009423 /* Compute the length of the new path. */
9424 sfx_len = (int)(save_endp - endp) + 1;
9425 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009427 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009429 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009431 /* There is not enough space in the currently allocated string,
9432 * copy it to a buffer big enough. */
9433 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009435 {
9436 retval = FAIL;
9437 goto theend;
9438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 }
9440 else
9441 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009442 /* Transfer short_fname to the main buffer (it's big enough),
9443 * unless get_short_pathname() did its work in-place. */
9444 *fname = *bufp = save_fname;
9445 if (short_fname != save_fname)
9446 vim_strncpy(save_fname, short_fname, len);
9447 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009449
9450 /* concat the not-shortened part of the path */
9451 vim_strncpy(*fname + len, endp, sfx_len);
9452 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009454
9455theend:
9456 vim_free(pbuf_unused);
9457 vim_free(save_fname);
9458
9459 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462/*
9463 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009464 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 */
9466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467shortpath_for_partial(
9468 char_u **fnamep,
9469 char_u **bufp,
9470 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471{
9472 int sepcount, len, tflen;
9473 char_u *p;
9474 char_u *pbuf, *tfname;
9475 int hasTilde;
9476
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009477 /* Count up the path separators from the RHS.. so we know which part
9478 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009480 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (vim_ispathsep(*p))
9482 ++sepcount;
9483
9484 /* Need full path first (use expand_env() to remove a "~/") */
9485 hasTilde = (**fnamep == '~');
9486 if (hasTilde)
9487 pbuf = tfname = expand_env_save(*fnamep);
9488 else
9489 pbuf = tfname = FullName_save(*fnamep, FALSE);
9490
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009491 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009493 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9494 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
9496 if (len == 0)
9497 {
9498 /* Don't have a valid filename, so shorten the rest of the
9499 * path if we can. This CAN give us invalid 8.3 filenames, but
9500 * there's not a lot of point in guessing what it might be.
9501 */
9502 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009503 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9504 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 }
9506
9507 /* Count the paths backward to find the beginning of the desired string. */
9508 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009509 {
9510#ifdef FEAT_MBYTE
9511 if (has_mbyte)
9512 p -= mb_head_off(tfname, p);
9513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (vim_ispathsep(*p))
9515 {
9516 if (sepcount == 0 || (hasTilde && sepcount == 1))
9517 break;
9518 else
9519 sepcount --;
9520 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (hasTilde)
9523 {
9524 --p;
9525 if (p >= tfname)
9526 *p = '~';
9527 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009528 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 }
9530 else
9531 ++p;
9532
9533 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9534 vim_free(*bufp);
9535 *fnamelen = (int)STRLEN(p);
9536 *bufp = pbuf;
9537 *fnamep = p;
9538
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009539 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541#endif /* WIN3264 */
9542
9543/*
9544 * Adjust a filename, according to a string of modifiers.
9545 * *fnamep must be NUL terminated when called. When returning, the length is
9546 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009547 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 * When there is an error, *fnamep is set to NULL.
9549 */
9550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009551modify_fname(
9552 char_u *src, /* string with modifiers */
9553 int *usedlen, /* characters after src that are used */
9554 char_u **fnamep, /* file name so far */
9555 char_u **bufp, /* buffer for allocated file name or NULL */
9556 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 int valid = 0;
9559 char_u *tail;
9560 char_u *s, *p, *pbuf;
9561 char_u dirname[MAXPATHL];
9562 int c;
9563 int has_fullname = 0;
9564#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009565 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 int has_shortname = 0;
9567#endif
9568
9569repeat:
9570 /* ":p" - full path/file_name */
9571 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9572 {
9573 has_fullname = 1;
9574
9575 valid |= VALID_PATH;
9576 *usedlen += 2;
9577
9578 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9579 if ((*fnamep)[0] == '~'
9580#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9581 && ((*fnamep)[1] == '/'
9582# ifdef BACKSLASH_IN_FILENAME
9583 || (*fnamep)[1] == '\\'
9584# endif
9585 || (*fnamep)[1] == NUL)
9586
9587#endif
9588 )
9589 {
9590 *fnamep = expand_env_save(*fnamep);
9591 vim_free(*bufp); /* free any allocated file name */
9592 *bufp = *fnamep;
9593 if (*fnamep == NULL)
9594 return -1;
9595 }
9596
9597 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009598 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
9600 if (vim_ispathsep(*p)
9601 && p[1] == '.'
9602 && (p[2] == NUL
9603 || vim_ispathsep(p[2])
9604 || (p[2] == '.'
9605 && (p[3] == NUL || vim_ispathsep(p[3])))))
9606 break;
9607 }
9608
9609 /* FullName_save() is slow, don't use it when not needed. */
9610 if (*p != NUL || !vim_isAbsName(*fnamep))
9611 {
9612 *fnamep = FullName_save(*fnamep, *p != NUL);
9613 vim_free(*bufp); /* free any allocated file name */
9614 *bufp = *fnamep;
9615 if (*fnamep == NULL)
9616 return -1;
9617 }
9618
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009619#ifdef WIN3264
9620# if _WIN32_WINNT >= 0x0500
9621 if (vim_strchr(*fnamep, '~') != NULL)
9622 {
9623 /* Expand 8.3 filename to full path. Needed to make sure the same
9624 * file does not have two different names.
9625 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9626 p = alloc(_MAX_PATH + 1);
9627 if (p != NULL)
9628 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009629 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009630 {
9631 vim_free(*bufp);
9632 *bufp = *fnamep = p;
9633 }
9634 else
9635 vim_free(p);
9636 }
9637 }
9638# endif
9639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 /* Append a path separator to a directory. */
9641 if (mch_isdir(*fnamep))
9642 {
9643 /* Make room for one or two extra characters. */
9644 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9645 vim_free(*bufp); /* free any allocated file name */
9646 *bufp = *fnamep;
9647 if (*fnamep == NULL)
9648 return -1;
9649 add_pathsep(*fnamep);
9650 }
9651 }
9652
9653 /* ":." - path relative to the current directory */
9654 /* ":~" - path relative to the home directory */
9655 /* ":8" - shortname path - postponed till after */
9656 while (src[*usedlen] == ':'
9657 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9658 {
9659 *usedlen += 2;
9660 if (c == '8')
9661 {
9662#ifdef WIN3264
9663 has_shortname = 1; /* Postpone this. */
9664#endif
9665 continue;
9666 }
9667 pbuf = NULL;
9668 /* Need full path first (use expand_env() to remove a "~/") */
9669 if (!has_fullname)
9670 {
9671 if (c == '.' && **fnamep == '~')
9672 p = pbuf = expand_env_save(*fnamep);
9673 else
9674 p = pbuf = FullName_save(*fnamep, FALSE);
9675 }
9676 else
9677 p = *fnamep;
9678
9679 has_fullname = 0;
9680
9681 if (p != NULL)
9682 {
9683 if (c == '.')
9684 {
9685 mch_dirname(dirname, MAXPATHL);
9686 s = shorten_fname(p, dirname);
9687 if (s != NULL)
9688 {
9689 *fnamep = s;
9690 if (pbuf != NULL)
9691 {
9692 vim_free(*bufp); /* free any allocated file name */
9693 *bufp = pbuf;
9694 pbuf = NULL;
9695 }
9696 }
9697 }
9698 else
9699 {
9700 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9701 /* Only replace it when it starts with '~' */
9702 if (*dirname == '~')
9703 {
9704 s = vim_strsave(dirname);
9705 if (s != NULL)
9706 {
9707 *fnamep = s;
9708 vim_free(*bufp);
9709 *bufp = s;
9710 }
9711 }
9712 }
9713 vim_free(pbuf);
9714 }
9715 }
9716
9717 tail = gettail(*fnamep);
9718 *fnamelen = (int)STRLEN(*fnamep);
9719
9720 /* ":h" - head, remove "/file_name", can be repeated */
9721 /* Don't remove the first "/" or "c:\" */
9722 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9723 {
9724 valid |= VALID_HEAD;
9725 *usedlen += 2;
9726 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009727 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009728 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 *fnamelen = (int)(tail - *fnamep);
9730#ifdef VMS
9731 if (*fnamelen > 0)
9732 *fnamelen += 1; /* the path separator is part of the path */
9733#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009734 if (*fnamelen == 0)
9735 {
9736 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9737 p = vim_strsave((char_u *)".");
9738 if (p == NULL)
9739 return -1;
9740 vim_free(*bufp);
9741 *bufp = *fnamep = tail = p;
9742 *fnamelen = 1;
9743 }
9744 else
9745 {
9746 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009747 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 }
9750
9751 /* ":8" - shortname */
9752 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9753 {
9754 *usedlen += 2;
9755#ifdef WIN3264
9756 has_shortname = 1;
9757#endif
9758 }
9759
9760#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009761 /*
9762 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 */
9764 if (has_shortname)
9765 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009766 /* Copy the string if it is shortened by :h and when it wasn't copied
9767 * yet, because we are going to change it in place. Avoids changing
9768 * the buffer name for "%:8". */
9769 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 {
9771 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009772 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 return -1;
9774 vim_free(*bufp);
9775 *bufp = *fnamep = p;
9776 }
9777
9778 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009779 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (!has_fullname && !vim_isAbsName(*fnamep))
9781 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009782 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 return -1;
9784 }
9785 else
9786 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009787 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaardc935552011-08-17 15:23:23 +02009789 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009791 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 return -1;
9793
9794 if (l == 0)
9795 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009796 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009798 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 return -1;
9800 }
9801 *fnamelen = l;
9802 }
9803 }
9804#endif /* WIN3264 */
9805
9806 /* ":t" - tail, just the basename */
9807 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9808 {
9809 *usedlen += 2;
9810 *fnamelen -= (int)(tail - *fnamep);
9811 *fnamep = tail;
9812 }
9813
9814 /* ":e" - extension, can be repeated */
9815 /* ":r" - root, without extension, can be repeated */
9816 while (src[*usedlen] == ':'
9817 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9818 {
9819 /* find a '.' in the tail:
9820 * - for second :e: before the current fname
9821 * - otherwise: The last '.'
9822 */
9823 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9824 s = *fnamep - 2;
9825 else
9826 s = *fnamep + *fnamelen - 1;
9827 for ( ; s > tail; --s)
9828 if (s[0] == '.')
9829 break;
9830 if (src[*usedlen + 1] == 'e') /* :e */
9831 {
9832 if (s > tail)
9833 {
9834 *fnamelen += (int)(*fnamep - (s + 1));
9835 *fnamep = s + 1;
9836#ifdef VMS
9837 /* cut version from the extension */
9838 s = *fnamep + *fnamelen - 1;
9839 for ( ; s > *fnamep; --s)
9840 if (s[0] == ';')
9841 break;
9842 if (s > *fnamep)
9843 *fnamelen = s - *fnamep;
9844#endif
9845 }
9846 else if (*fnamep <= tail)
9847 *fnamelen = 0;
9848 }
9849 else /* :r */
9850 {
9851 if (s > tail) /* remove one extension */
9852 *fnamelen = (int)(s - *fnamep);
9853 }
9854 *usedlen += 2;
9855 }
9856
9857 /* ":s?pat?foo?" - substitute */
9858 /* ":gs?pat?foo?" - global substitute */
9859 if (src[*usedlen] == ':'
9860 && (src[*usedlen + 1] == 's'
9861 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9862 {
9863 char_u *str;
9864 char_u *pat;
9865 char_u *sub;
9866 int sep;
9867 char_u *flags;
9868 int didit = FALSE;
9869
9870 flags = (char_u *)"";
9871 s = src + *usedlen + 2;
9872 if (src[*usedlen + 1] == 'g')
9873 {
9874 flags = (char_u *)"g";
9875 ++s;
9876 }
9877
9878 sep = *s++;
9879 if (sep)
9880 {
9881 /* find end of pattern */
9882 p = vim_strchr(s, sep);
9883 if (p != NULL)
9884 {
9885 pat = vim_strnsave(s, (int)(p - s));
9886 if (pat != NULL)
9887 {
9888 s = p + 1;
9889 /* find end of substitution */
9890 p = vim_strchr(s, sep);
9891 if (p != NULL)
9892 {
9893 sub = vim_strnsave(s, (int)(p - s));
9894 str = vim_strnsave(*fnamep, *fnamelen);
9895 if (sub != NULL && str != NULL)
9896 {
9897 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009898 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 if (s != NULL)
9900 {
9901 *fnamep = s;
9902 *fnamelen = (int)STRLEN(s);
9903 vim_free(*bufp);
9904 *bufp = s;
9905 didit = TRUE;
9906 }
9907 }
9908 vim_free(sub);
9909 vim_free(str);
9910 }
9911 vim_free(pat);
9912 }
9913 }
9914 /* after using ":s", repeat all the modifiers */
9915 if (didit)
9916 goto repeat;
9917 }
9918 }
9919
Bram Moolenaar26df0922014-02-23 23:39:13 +01009920 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9921 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009922 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009923 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009924 if (c != NUL)
9925 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009926 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009927 if (c != NUL)
9928 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009929 if (p == NULL)
9930 return -1;
9931 vim_free(*bufp);
9932 *bufp = *fnamep = p;
9933 *fnamelen = (int)STRLEN(p);
9934 *usedlen += 2;
9935 }
9936
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 return valid;
9938}
9939
9940/*
9941 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009942 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 * "flags" can be "g" to do a global substitute.
9944 * Returns an allocated string, NULL for error.
9945 */
9946 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947do_string_sub(
9948 char_u *str,
9949 char_u *pat,
9950 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009951 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954 int sublen;
9955 regmatch_T regmatch;
9956 int i;
9957 int do_all;
9958 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009959 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 garray_T ga;
9961 char_u *ret;
9962 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009963 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
9965 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9966 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009967 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
9969 ga_init2(&ga, 1, 200);
9970
9971 do_all = (flags[0] == 'g');
9972
9973 regmatch.rm_ic = p_ic;
9974 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9975 if (regmatch.regprog != NULL)
9976 {
9977 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009978 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9980 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009981 /* Skip empty match except for first match. */
9982 if (regmatch.startp[0] == regmatch.endp[0])
9983 {
9984 if (zero_width == regmatch.startp[0])
9985 {
9986 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009987 i = MB_PTR2LEN(tail);
9988 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9989 (size_t)i);
9990 ga.ga_len += i;
9991 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009992 continue;
9993 }
9994 zero_width = regmatch.startp[0];
9995 }
9996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 /*
9998 * Get some space for a temporary buffer to do the substitution
9999 * into. It will contain:
10000 * - The text up to where the match is.
10001 * - The substituted text.
10002 * - The text after the match.
10003 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010004 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010005 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10007 {
10008 ga_clear(&ga);
10009 break;
10010 }
10011
10012 /* copy the text up to where the match is */
10013 i = (int)(regmatch.startp[0] - tail);
10014 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10015 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010016 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 + ga.ga_len + i, TRUE, TRUE, FALSE);
10018 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010019 tail = regmatch.endp[0];
10020 if (*tail == NUL)
10021 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 if (!do_all)
10023 break;
10024 }
10025
10026 if (ga.ga_data != NULL)
10027 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10028
Bram Moolenaar473de612013-06-08 18:19:48 +020010029 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031
10032 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10033 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010034 if (p_cpo == empty_option)
10035 p_cpo = save_cpo;
10036 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010037 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010038 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039
10040 return ret;
10041}
10042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 static int
10044filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10045{
10046 typval_T rettv;
10047 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010048 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049
10050 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10051 argv[0] = vimvars[VV_KEY].vv_tv;
10052 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010053 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10054 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010055 if (map)
10056 {
10057 /* map(): replace the list item value */
10058 clear_tv(tv);
10059 rettv.v_lock = 0;
10060 *tv = rettv;
10061 }
10062 else
10063 {
10064 int error = FALSE;
10065
10066 /* filter(): when expr is zero remove the item */
10067 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10068 clear_tv(&rettv);
10069 /* On type error, nothing has been removed; return FAIL to stop the
10070 * loop. The error message was given by get_tv_number_chk(). */
10071 if (error)
10072 goto theend;
10073 }
10074 retval = OK;
10075theend:
10076 clear_tv(&vimvars[VV_VAL].vv_tv);
10077 return retval;
10078}
10079
10080
10081/*
10082 * Implementation of map() and filter().
10083 */
10084 void
10085filter_map(typval_T *argvars, typval_T *rettv, int map)
10086{
10087 typval_T *expr;
10088 listitem_T *li, *nli;
10089 list_T *l = NULL;
10090 dictitem_T *di;
10091 hashtab_T *ht;
10092 hashitem_T *hi;
10093 dict_T *d = NULL;
10094 typval_T save_val;
10095 typval_T save_key;
10096 int rem;
10097 int todo;
10098 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10099 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10100 : N_("filter() argument"));
10101 int save_did_emsg;
10102 int idx = 0;
10103
10104 if (argvars[0].v_type == VAR_LIST)
10105 {
10106 if ((l = argvars[0].vval.v_list) == NULL
10107 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10108 return;
10109 }
10110 else if (argvars[0].v_type == VAR_DICT)
10111 {
10112 if ((d = argvars[0].vval.v_dict) == NULL
10113 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10114 return;
10115 }
10116 else
10117 {
10118 EMSG2(_(e_listdictarg), ermsg);
10119 return;
10120 }
10121
10122 expr = &argvars[1];
10123 /* On type errors, the preceding call has already displayed an error
10124 * message. Avoid a misleading error message for an empty string that
10125 * was not passed as argument. */
10126 if (expr->v_type != VAR_UNKNOWN)
10127 {
10128 prepare_vimvar(VV_VAL, &save_val);
10129
10130 /* We reset "did_emsg" to be able to detect whether an error
10131 * occurred during evaluation of the expression. */
10132 save_did_emsg = did_emsg;
10133 did_emsg = FALSE;
10134
10135 prepare_vimvar(VV_KEY, &save_key);
10136 if (argvars[0].v_type == VAR_DICT)
10137 {
10138 vimvars[VV_KEY].vv_type = VAR_STRING;
10139
10140 ht = &d->dv_hashtab;
10141 hash_lock(ht);
10142 todo = (int)ht->ht_used;
10143 for (hi = ht->ht_array; todo > 0; ++hi)
10144 {
10145 if (!HASHITEM_EMPTY(hi))
10146 {
10147 int r;
10148
10149 --todo;
10150 di = HI2DI(hi);
10151 if (map &&
10152 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10153 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10154 break;
10155 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10156 r = filter_map_one(&di->di_tv, expr, map, &rem);
10157 clear_tv(&vimvars[VV_KEY].vv_tv);
10158 if (r == FAIL || did_emsg)
10159 break;
10160 if (!map && rem)
10161 {
10162 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10163 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10164 break;
10165 dictitem_remove(d, di);
10166 }
10167 }
10168 }
10169 hash_unlock(ht);
10170 }
10171 else
10172 {
10173 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10174
10175 for (li = l->lv_first; li != NULL; li = nli)
10176 {
10177 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10178 break;
10179 nli = li->li_next;
10180 vimvars[VV_KEY].vv_nr = idx;
10181 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10182 || did_emsg)
10183 break;
10184 if (!map && rem)
10185 listitem_remove(l, li);
10186 ++idx;
10187 }
10188 }
10189
10190 restore_vimvar(VV_KEY, &save_key);
10191 restore_vimvar(VV_VAL, &save_val);
10192
10193 did_emsg |= save_did_emsg;
10194 }
10195
10196 copy_tv(&argvars[0], rettv);
10197}
10198
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */