blob: d4fba7fe0d0b2dbf3a642b6b7a4e02f15926c312 [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
699/*
700 * Top level evaluation function, returning a string. If "skip" is TRUE,
701 * only parsing to "nextcmd" is done, without reporting errors. Return
702 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
703 */
704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100705eval_to_string_skip(
706 char_u *arg,
707 char_u **nextcmd,
708 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
Bram Moolenaar33570922005-01-25 22:26:29 +0000710 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 char_u *retval;
712
713 if (skip)
714 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000715 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 retval = NULL;
717 else
718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 retval = vim_strsave(get_tv_string(&tv));
720 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722 if (skip)
723 --emsg_skip;
724
725 return retval;
726}
727
728/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000729 * Skip over an expression at "*pp".
730 * Return FAIL for an error, OK otherwise.
731 */
732 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100733skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000734{
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000736
737 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000739}
740
741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000743 * When "convert" is TRUE convert a List into a sequence of lines and convert
744 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * Return pointer to allocated memory, or NULL for failure.
746 */
747 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100748eval_to_string(
749 char_u *arg,
750 char_u **nextcmd,
751 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000755 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000756#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000757 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000760 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 retval = NULL;
762 else
763 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000764 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000765 {
766 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000767 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200768 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200769 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200770 if (tv.vval.v_list->lv_len > 0)
771 ga_append(&ga, NL);
772 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000773 ga_append(&ga, NUL);
774 retval = (char_u *)ga.ga_data;
775 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000776#ifdef FEAT_FLOAT
777 else if (convert && tv.v_type == VAR_FLOAT)
778 {
779 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
780 retval = vim_strsave(numbuf);
781 }
782#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000783 else
784 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787
788 return retval;
789}
790
791/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 * Call eval_to_string() without using current local variables and using
793 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 */
795 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100796eval_to_string_safe(
797 char_u *arg,
798 char_u **nextcmd,
799 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 char_u *retval;
802 void *save_funccalp;
803
804 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000805 if (use_sandbox)
806 ++sandbox;
807 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000808 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000809 if (use_sandbox)
810 --sandbox;
811 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 restore_funccal(save_funccalp);
813 return retval;
814}
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Top level evaluation function, returning a number.
818 * Evaluates "expr" silently.
819 * Returns -1 for an error.
820 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200821 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100822eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200825 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000826 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 ++emsg_off;
829
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 retval = -1;
832 else
833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000834 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 --emsg_off;
838
839 return retval;
840}
841
Bram Moolenaara40058a2005-07-11 22:42:07 +0000842/*
843 * Prepare v: variable "idx" to be used.
844 * Save the current typeval in "save_tv".
845 * When not used yet add the variable to the v: hashtable.
846 */
847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100848prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000849{
850 *save_tv = vimvars[idx].vv_tv;
851 if (vimvars[idx].vv_type == VAR_UNKNOWN)
852 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
853}
854
855/*
856 * Restore v: variable "idx" to typeval "save_tv".
857 * When no longer defined, remove the variable from the v: hashtable.
858 */
859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100860restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000861{
862 hashitem_T *hi;
863
Bram Moolenaara40058a2005-07-11 22:42:07 +0000864 vimvars[idx].vv_tv = *save_tv;
865 if (vimvars[idx].vv_type == VAR_UNKNOWN)
866 {
867 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
868 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100869 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000870 else
871 hash_remove(&vimvarht, hi);
872 }
873}
874
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000875#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000876/*
877 * Evaluate an expression to a list with suggestions.
878 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000879 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000880 */
881 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100882eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000883{
884 typval_T save_val;
885 typval_T rettv;
886 list_T *list = NULL;
887 char_u *p = skipwhite(expr);
888
889 /* Set "v:val" to the bad word. */
890 prepare_vimvar(VV_VAL, &save_val);
891 vimvars[VV_VAL].vv_type = VAR_STRING;
892 vimvars[VV_VAL].vv_str = badword;
893 if (p_verbose == 0)
894 ++emsg_off;
895
896 if (eval1(&p, &rettv, TRUE) == OK)
897 {
898 if (rettv.v_type != VAR_LIST)
899 clear_tv(&rettv);
900 else
901 list = rettv.vval.v_list;
902 }
903
904 if (p_verbose == 0)
905 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000906 restore_vimvar(VV_VAL, &save_val);
907
908 return list;
909}
910
911/*
912 * "list" is supposed to contain two items: a word and a number. Return the
913 * word in "pp" and the number as the return value.
914 * Return -1 if anything isn't right.
915 * Used to get the good word and score from the eval_spell_expr() result.
916 */
917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000919{
920 listitem_T *li;
921
922 li = list->lv_first;
923 if (li == NULL)
924 return -1;
925 *pp = get_tv_string(&li->li_tv);
926
927 li = li->li_next;
928 if (li == NULL)
929 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200930 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000931}
932#endif
933
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000934/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000935 * Top level evaluation function.
936 * Returns an allocated typval_T with the result.
937 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000938 */
939 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100940eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000941{
942 typval_T *tv;
943
944 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000945 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000946 {
947 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000948 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000949 }
950
951 return tv;
952}
953
954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100956 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000957 * Uses argv[argc] for the function arguments. Only Number and String
958 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000959 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100962call_vim_function(
963 char_u *func,
964 int argc,
965 char_u **argv,
966 int safe, /* use the sandbox */
967 int str_arg_only, /* all arguments are strings */
968 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200971 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 int len;
973 int i;
974 int doesrange;
975 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000976 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000978 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000980 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 for (i = 0; i < argc; i++)
983 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000984 /* Pass a NULL or empty argument as an empty string */
985 if (argv[i] == NULL || *argv[i] == NUL)
986 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000987 argvars[i].v_type = VAR_STRING;
988 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000989 continue;
990 }
991
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200992 if (str_arg_only)
993 len = 0;
994 else
995 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100996 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (len != 0 && len == (int)STRLEN(argv[i]))
998 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 argvars[i].v_type = VAR_NUMBER;
1000 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 argvars[i].v_type = VAR_STRING;
1005 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 }
1008
1009 if (safe)
1010 {
1011 save_funccalp = save_funccal();
1012 ++sandbox;
1013 }
1014
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001015 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001016 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001018 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (safe)
1020 {
1021 --sandbox;
1022 restore_funccal(save_funccalp);
1023 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001024 vim_free(argvars);
1025
1026 if (ret == FAIL)
1027 clear_tv(rettv);
1028
1029 return ret;
1030}
1031
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001032/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001033 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001034 * Returns -1 when calling the function fails.
1035 * Uses argv[argc] for the function arguments.
1036 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001037 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001038call_func_retnr(
1039 char_u *func,
1040 int argc,
1041 char_u **argv,
1042 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043{
1044 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001045 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001046
1047 /* All arguments are passed as strings, no conversion to number. */
1048 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1049 return -1;
1050
1051 retval = get_tv_number_chk(&rettv, NULL);
1052 clear_tv(&rettv);
1053 return retval;
1054}
1055
1056#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1057 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1058
Bram Moolenaar4f688582007-07-24 12:34:30 +00001059# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001060/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001061 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001062 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001063 * Uses argv[argc] for the function arguments.
1064 */
1065 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001066call_func_retstr(
1067 char_u *func,
1068 int argc,
1069 char_u **argv,
1070 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001071{
1072 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001073 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001074
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001075 /* All arguments are passed as strings, no conversion to number. */
1076 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001077 return NULL;
1078
1079 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001080 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 return retval;
1082}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001083# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001085/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001086 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001087 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001088 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 */
1090 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001091call_func_retlist(
1092 char_u *func,
1093 int argc,
1094 char_u **argv,
1095 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001096{
1097 typval_T rettv;
1098
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001099 /* All arguments are passed as strings, no conversion to number. */
1100 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001101 return NULL;
1102
1103 if (rettv.v_type != VAR_LIST)
1104 {
1105 clear_tv(&rettv);
1106 return NULL;
1107 }
1108
1109 return rettv.vval.v_list;
1110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112
Bram Moolenaar05159a02005-02-26 23:04:13 +00001113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#ifdef FEAT_FOLDING
1115/*
1116 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1117 * it in "*cp". Doesn't give error messages.
1118 */
1119 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001120eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001123 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001125 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1126 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001129 if (use_sandbox)
1130 ++sandbox;
1131 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 retval = 0;
1135 else
1136 {
1137 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 if (tv.v_type == VAR_NUMBER)
1139 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001140 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 retval = 0;
1142 else
1143 {
1144 /* If the result is a string, check if there is a non-digit before
1145 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (!VIM_ISDIGIT(*s) && *s != '-')
1148 *cp = *s++;
1149 retval = atol((char *)s);
1150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001154 if (use_sandbox)
1155 --sandbox;
1156 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001158 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159}
1160#endif
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001163 * ":let" list all variable values
1164 * ":let var1 var2" list variable values
1165 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001166 * ":let var += expr" assignment command.
1167 * ":let var -= expr" assignment command.
1168 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001169 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 */
1171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001172ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
1174 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001176 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 int var_count = 0;
1179 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001180 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001181 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001182 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaardb552d602006-03-23 22:59:57 +00001184 argend = skip_var_list(arg, &var_count, &semicolon);
1185 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001186 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001187 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1188 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001189 expr = skipwhite(argend);
1190 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1191 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001193 /*
1194 * ":let" without "=": list variables
1195 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001196 if (*arg == '[')
1197 EMSG(_(e_invarg));
1198 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001200 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001204 list_glob_vars(&first);
1205 list_buf_vars(&first);
1206 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001207 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001208 list_script_vars(&first);
1209 list_func_vars(&first);
1210 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 eap->nextcmd = check_nextcmd(arg);
1213 }
1214 else
1215 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001216 op[0] = '=';
1217 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001218 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001219 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001220 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1221 op[0] = *expr; /* +=, -= or .= */
1222 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001223 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001224 else
1225 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (eap->skip)
1228 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001229 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (eap->skip)
1231 {
1232 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 --emsg_skip;
1235 }
1236 else if (i != FAIL)
1237 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001238 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001239 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242 }
1243}
1244
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001245/*
1246 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1247 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001248 * When "nextchars" is not NULL it points to a string with characters that
1249 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1250 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001251 * Returns OK or FAIL;
1252 */
1253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001254ex_let_vars(
1255 char_u *arg_start,
1256 typval_T *tv,
1257 int copy, /* copy values from "tv", don't move */
1258 int semicolon, /* from skip_var_list() */
1259 int var_count, /* from skip_var_list() */
1260 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001261{
1262 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001263 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001264 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001265 listitem_T *item;
1266 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001267
1268 if (*arg != '[')
1269 {
1270 /*
1271 * ":let var = expr" or ":for var in list"
1272 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001273 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001274 return FAIL;
1275 return OK;
1276 }
1277
1278 /*
1279 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1280 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001281 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001282 {
1283 EMSG(_(e_listreq));
1284 return FAIL;
1285 }
1286
1287 i = list_len(l);
1288 if (semicolon == 0 && var_count < i)
1289 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001290 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001291 return FAIL;
1292 }
1293 if (var_count - semicolon > i)
1294 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001295 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001296 return FAIL;
1297 }
1298
1299 item = l->lv_first;
1300 while (*arg != ']')
1301 {
1302 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001303 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001304 item = item->li_next;
1305 if (arg == NULL)
1306 return FAIL;
1307
1308 arg = skipwhite(arg);
1309 if (*arg == ';')
1310 {
1311 /* Put the rest of the list (may be empty) in the var after ';'.
1312 * Create a new list for this. */
1313 l = list_alloc();
1314 if (l == NULL)
1315 return FAIL;
1316 while (item != NULL)
1317 {
1318 list_append_tv(l, &item->li_tv);
1319 item = item->li_next;
1320 }
1321
1322 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001323 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 ltv.vval.v_list = l;
1325 l->lv_refcount = 1;
1326
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1328 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001329 clear_tv(&ltv);
1330 if (arg == NULL)
1331 return FAIL;
1332 break;
1333 }
1334 else if (*arg != ',' && *arg != ']')
1335 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001336 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 return FAIL;
1338 }
1339 }
1340
1341 return OK;
1342}
1343
1344/*
1345 * Skip over assignable variable "var" or list of variables "[var, var]".
1346 * Used for ":let varvar = expr" and ":for varvar in expr".
1347 * For "[var, var]" increment "*var_count" for each variable.
1348 * for "[var, var; var]" set "semicolon".
1349 * Return NULL for an error.
1350 */
1351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001352skip_var_list(
1353 char_u *arg,
1354 int *var_count,
1355 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356{
1357 char_u *p, *s;
1358
1359 if (*arg == '[')
1360 {
1361 /* "[var, var]": find the matching ']'. */
1362 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001363 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364 {
1365 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1366 s = skip_var_one(p);
1367 if (s == p)
1368 {
1369 EMSG2(_(e_invarg2), p);
1370 return NULL;
1371 }
1372 ++*var_count;
1373
1374 p = skipwhite(s);
1375 if (*p == ']')
1376 break;
1377 else if (*p == ';')
1378 {
1379 if (*semicolon == 1)
1380 {
1381 EMSG(_("Double ; in list of variables"));
1382 return NULL;
1383 }
1384 *semicolon = 1;
1385 }
1386 else if (*p != ',')
1387 {
1388 EMSG2(_(e_invarg2), p);
1389 return NULL;
1390 }
1391 }
1392 return p + 1;
1393 }
1394 else
1395 return skip_var_one(arg);
1396}
1397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001398/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001399 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001400 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001401 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001403skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001404{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001405 if (*arg == '@' && arg[1] != NUL)
1406 return arg + 2;
1407 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1408 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001409}
1410
Bram Moolenaara7043832005-01-21 11:56:39 +00001411/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 * List variables for hashtab "ht" with prefix "prefix".
1413 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001414 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001415 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001416list_hashtable_vars(
1417 hashtab_T *ht,
1418 char_u *prefix,
1419 int empty,
1420 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 hashitem_T *hi;
1423 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001424 int todo;
1425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001426 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001427 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1428 {
1429 if (!HASHITEM_EMPTY(hi))
1430 {
1431 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 di = HI2DI(hi);
1433 if (empty || di->di_tv.v_type != VAR_STRING
1434 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001435 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001436 }
1437 }
1438}
1439
1440/*
1441 * List global variables.
1442 */
1443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001445{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001446 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001447}
1448
1449/*
1450 * List buffer variables.
1451 */
1452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001453list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001454{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001455 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001456 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001457}
1458
1459/*
1460 * List window variables.
1461 */
1462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001463list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001464{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001465 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001466 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001467}
1468
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001469/*
1470 * List tab page variables.
1471 */
1472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001473list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001474{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001475 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001477}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001478
Bram Moolenaara7043832005-01-21 11:56:39 +00001479/*
1480 * List Vim variables.
1481 */
1482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001485 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486}
1487
1488/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001489 * List script-local variables, if there is a script.
1490 */
1491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001493{
1494 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001495 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1496 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001497}
1498
1499/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500 * List variables in "arg".
1501 */
1502 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504{
1505 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001506 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001508 char_u *name_start;
1509 char_u *arg_subsc;
1510 char_u *tofree;
1511 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512
1513 while (!ends_excmd(*arg) && !got_int)
1514 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001515 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001517 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001518 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001519 {
1520 emsg_severe = TRUE;
1521 EMSG(_(e_trailing));
1522 break;
1523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001527 /* get_name_len() takes care of expanding curly braces */
1528 name_start = name = arg;
1529 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1530 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001532 /* This is mainly to keep test 49 working: when expanding
1533 * curly braces fails overrule the exception error message. */
1534 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001536 emsg_severe = TRUE;
1537 EMSG2(_(e_invarg2), arg);
1538 break;
1539 }
1540 error = TRUE;
1541 }
1542 else
1543 {
1544 if (tofree != NULL)
1545 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001546 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 else
1549 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001550 /* handle d.key, l[idx], f(expr) */
1551 arg_subsc = arg;
1552 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001553 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001554 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001555 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001556 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001557 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001558 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001559 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001560 case 'g': list_glob_vars(first); break;
1561 case 'b': list_buf_vars(first); break;
1562 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001563 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001564 case 'v': list_vim_vars(first); break;
1565 case 's': list_script_vars(first); break;
1566 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001567 default:
1568 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001570 }
1571 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 {
1573 char_u numbuf[NUMBUFLEN];
1574 char_u *tf;
1575 int c;
1576 char_u *s;
1577
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001578 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 c = *arg;
1580 *arg = NUL;
1581 list_one_var_a((char_u *)"",
1582 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001583 tv.v_type,
1584 s == NULL ? (char_u *)"" : s,
1585 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001586 *arg = c;
1587 vim_free(tf);
1588 }
1589 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 }
1592 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001593
1594 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596
1597 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 }
1599
1600 return arg;
1601}
1602
1603/*
1604 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1605 * Returns a pointer to the char just after the var name.
1606 * Returns NULL if there is an error.
1607 */
1608 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609ex_let_one(
1610 char_u *arg, /* points to variable name */
1611 typval_T *tv, /* value to assign to variable */
1612 int copy, /* copy value from "tv" */
1613 char_u *endchars, /* valid chars after variable name or NULL */
1614 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615{
1616 int c1;
1617 char_u *name;
1618 char_u *p;
1619 char_u *arg_end = NULL;
1620 int len;
1621 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623
1624 /*
1625 * ":let $VAR = expr": Set environment variable.
1626 */
1627 if (*arg == '$')
1628 {
1629 /* Find the end of the name. */
1630 ++arg;
1631 name = arg;
1632 len = get_env_len(&arg);
1633 if (len == 0)
1634 EMSG2(_(e_invarg2), name - 1);
1635 else
1636 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637 if (op != NULL && (*op == '+' || *op == '-'))
1638 EMSG2(_(e_letwrong), op);
1639 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001641 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001642 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643 {
1644 c1 = name[len];
1645 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001646 p = get_tv_string_chk(tv);
1647 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 {
1649 int mustfree = FALSE;
1650 char_u *s = vim_getenv(name, &mustfree);
1651
1652 if (s != NULL)
1653 {
1654 p = tofree = concat_str(s, p);
1655 if (mustfree)
1656 vim_free(s);
1657 }
1658 }
1659 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001660 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001662 if (STRICMP(name, "HOME") == 0)
1663 init_homedir();
1664 else if (didset_vim && STRICMP(name, "VIM") == 0)
1665 didset_vim = FALSE;
1666 else if (didset_vimruntime
1667 && STRICMP(name, "VIMRUNTIME") == 0)
1668 didset_vimruntime = FALSE;
1669 arg_end = arg;
1670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 }
1674 }
1675 }
1676
1677 /*
1678 * ":let &option = expr": Set option value.
1679 * ":let &l:option = expr": Set local option value.
1680 * ":let &g:option = expr": Set global option value.
1681 */
1682 else if (*arg == '&')
1683 {
1684 /* Find the end of the name. */
1685 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 if (p == NULL || (endchars != NULL
1687 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688 EMSG(_(e_letunexp));
1689 else
1690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 long n;
1692 int opt_type;
1693 long numval;
1694 char_u *stringval = NULL;
1695 char_u *s;
1696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 c1 = *p;
1698 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001700 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001701 s = get_tv_string_chk(tv); /* != NULL if number or string */
1702 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703 {
1704 opt_type = get_option_value(arg, &numval,
1705 &stringval, opt_flags);
1706 if ((opt_type == 1 && *op == '.')
1707 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001709 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001710 s = NULL; /* don't set the value */
1711 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 else
1713 {
1714 if (opt_type == 1) /* number */
1715 {
1716 if (*op == '+')
1717 n = numval + n;
1718 else
1719 n = numval - n;
1720 }
1721 else if (opt_type == 0 && stringval != NULL) /* string */
1722 {
1723 s = concat_str(stringval, s);
1724 vim_free(stringval);
1725 stringval = s;
1726 }
1727 }
1728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 if (s != NULL)
1730 {
1731 set_option_value(arg, n, s, opt_flags);
1732 arg_end = p;
1733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 }
1737 }
1738
1739 /*
1740 * ":let @r = expr": Set register contents.
1741 */
1742 else if (*arg == '@')
1743 {
1744 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 if (op != NULL && (*op == '+' || *op == '-'))
1746 EMSG2(_(e_letwrong), op);
1747 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 EMSG(_(e_letunexp));
1750 else
1751 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 char_u *s;
1754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 p = get_tv_string_chk(tv);
1756 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001758 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 if (s != NULL)
1760 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 vim_free(s);
1763 }
1764 }
1765 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001768 arg_end = arg + 1;
1769 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 }
1772 }
1773
1774 /*
1775 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001776 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001778 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001782 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001785 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1786 EMSG(_(e_letunexp));
1787 else
1788 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 arg_end = p;
1791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001793 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 }
1795
1796 else
1797 EMSG2(_(e_invarg2), arg);
1798
1799 return arg_end;
1800}
1801
1802/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001803 * Get an lval: variable, Dict item or List item that can be assigned a value
1804 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1805 * "name.key", "name.key[expr]" etc.
1806 * Indexing only works if "name" is an existing List or Dictionary.
1807 * "name" points to the start of the name.
1808 * If "rettv" is not NULL it points to the value to be assigned.
1809 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1810 * wrong; must end in space or cmd separator.
1811 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001812 * flags:
1813 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001814 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001815 * GLV_NO_AUTOLOAD: do not use script autoloading
1816 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822get_lval(
1823 char_u *name,
1824 typval_T *rettv,
1825 lval_T *lp,
1826 int unlet,
1827 int skip,
1828 int flags, /* GLV_ values */
1829 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001832 char_u *expr_start, *expr_end;
1833 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001834 dictitem_T *v;
1835 typval_T var1;
1836 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001839 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001840 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001842 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001844 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001846
1847 if (skip)
1848 {
1849 /* When skipping just find the end of the name. */
1850 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001851 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 }
1853
1854 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001855 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 if (expr_start != NULL)
1857 {
1858 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 && *p != '[' && *p != '.')
1861 {
1862 EMSG(_(e_trailing));
1863 return NULL;
1864 }
1865
1866 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1867 if (lp->ll_exp_name == NULL)
1868 {
1869 /* Report an invalid expression in braces, unless the
1870 * expression evaluation has been cancelled due to an
1871 * aborting error, an interrupt, or an exception. */
1872 if (!aborting() && !quiet)
1873 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001874 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001875 EMSG2(_(e_invarg2), name);
1876 return NULL;
1877 }
1878 }
1879 lp->ll_name = lp->ll_exp_name;
1880 }
1881 else
1882 lp->ll_name = name;
1883
1884 /* Without [idx] or .key we are done. */
1885 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1886 return p;
1887
1888 cc = *p;
1889 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001890 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 if (v == NULL && !quiet)
1892 EMSG2(_(e_undefvar), lp->ll_name);
1893 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 if (v == NULL)
1895 return NULL;
1896
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 /*
1898 * Loop until no more [idx] or .key is following.
1899 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001901 var1.v_type = VAR_UNKNOWN;
1902 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001903 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1906 && !(lp->ll_tv->v_type == VAR_DICT
1907 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 if (!quiet)
1910 EMSG(_("E689: Can only index a List or Dictionary"));
1911 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915 if (!quiet)
1916 EMSG(_("E708: [:] must come last"));
1917 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001919
Bram Moolenaar8c711452005-01-14 21:53:12 +00001920 len = -1;
1921 if (*p == '.')
1922 {
1923 key = p + 1;
1924 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1925 ;
1926 if (len == 0)
1927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 if (!quiet)
1929 EMSG(_(e_emptykey));
1930 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 }
1932 p = key + len;
1933 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001934 else
1935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001936 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001937 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001938 if (*p == ':')
1939 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001940 else
1941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001942 empty1 = FALSE;
1943 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001945 if (get_tv_string_chk(&var1) == NULL)
1946 {
1947 /* not a number or string */
1948 clear_tv(&var1);
1949 return NULL;
1950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001951 }
1952
1953 /* Optionally get the second index [ :expr]. */
1954 if (*p == ':')
1955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001960 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001962 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (rettv != NULL && (rettv->v_type != VAR_LIST
1964 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 if (!quiet)
1967 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001968 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 }
1971 p = skipwhite(p + 1);
1972 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 else
1975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1978 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001979 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982 if (get_tv_string_chk(&var2) == NULL)
1983 {
1984 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001985 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 clear_tv(&var2);
1987 return NULL;
1988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001994
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 if (!quiet)
1998 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001999 clear_tv(&var1);
2000 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002002 }
2003
2004 /* Skip to past ']'. */
2005 ++p;
2006 }
2007
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 {
2010 if (len == -1)
2011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002013 key = get_tv_string_chk(&var1); /* is number or string */
2014 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 }
2019 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 lp->ll_list = NULL;
2021 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002022 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002023
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002024 /* When assigning to a scope dictionary check that a function and
2025 * variable name is valid (only variable name unless it is l: or
2026 * g: dictionary). Disallow overwriting a builtin function. */
2027 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002028 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002029 int prevval;
2030 int wrong;
2031
2032 if (len != -1)
2033 {
2034 prevval = key[len];
2035 key[len] = NUL;
2036 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002037 else
2038 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002039 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2040 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002041 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002042 || !valid_varname(key);
2043 if (len != -1)
2044 key[len] = prevval;
2045 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002046 return NULL;
2047 }
2048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002051 /* Can't add "v:" variable. */
2052 if (lp->ll_dict == &vimvardict)
2053 {
2054 EMSG2(_(e_illvar), name);
2055 return NULL;
2056 }
2057
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002058 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002063 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002065 }
2066 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002068 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002070 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002072 p = NULL;
2073 break;
2074 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002076 else if ((flags & GLV_READ_ONLY) == 0
2077 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002078 {
2079 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002081 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002082
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 }
2086 else
2087 {
2088 /*
2089 * Get the number and item for the only or first index of the List.
2090 */
2091 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002093 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002094 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002095 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002096 clear_tv(&var1);
2097
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 lp->ll_list = lp->ll_tv->vval.v_list;
2100 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2101 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002102 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002103 if (lp->ll_n1 < 0)
2104 {
2105 lp->ll_n1 = 0;
2106 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2107 }
2108 }
2109 if (lp->ll_li == NULL)
2110 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002111 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002112 if (!quiet)
2113 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 }
2116
2117 /*
2118 * May need to find the item or absolute index for the second
2119 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 * When no index given: "lp->ll_empty2" is TRUE.
2121 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002125 lp->ll_n2 = (long)get_tv_number(&var2);
2126 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002127 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002132 {
2133 if (!quiet)
2134 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
2139
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2141 if (lp->ll_n1 < 0)
2142 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2143 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002144 {
2145 if (!quiet)
2146 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002148 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002149 }
2150
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 }
2154
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002155 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 return p;
2157}
2158
2159/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164{
2165 vim_free(lp->ll_exp_name);
2166 vim_free(lp->ll_newkey);
2167}
2168
2169/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002170 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175set_var_lval(
2176 lval_T *lp,
2177 char_u *endp,
2178 typval_T *rettv,
2179 int copy,
2180 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181{
2182 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 listitem_T *ri;
2184 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185
2186 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002188 cc = *endp;
2189 *endp = NUL;
2190 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002192 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193
Bram Moolenaar79518e22017-02-17 16:31:35 +01002194 /* handle +=, -= and .= */
2195 di = NULL;
2196 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2197 &tv, &di, TRUE, FALSE) == OK)
2198 {
2199 if ((di == NULL
2200 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2201 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2202 FALSE)))
2203 && tv_op(&tv, rettv, op) == OK)
2204 set_var(lp->ll_name, &tv, FALSE);
2205 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002208 else
2209 set_var(lp->ll_name, rettv, copy);
2210 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002212 else if (tv_check_lock(lp->ll_newkey == NULL
2213 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002214 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002215 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 else if (lp->ll_range)
2217 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002218 listitem_T *ll_li = lp->ll_li;
2219 int ll_n1 = lp->ll_n1;
2220
2221 /*
2222 * Check whether any of the list items is locked
2223 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002224 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002225 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002226 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002227 return;
2228 ri = ri->li_next;
2229 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2230 break;
2231 ll_li = ll_li->li_next;
2232 ++ll_n1;
2233 }
2234
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 /*
2236 * Assign the List values to the list items.
2237 */
2238 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002239 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 if (op != NULL && *op != '=')
2241 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2242 else
2243 {
2244 clear_tv(&lp->ll_li->li_tv);
2245 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 ri = ri->li_next;
2248 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2249 break;
2250 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002253 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 ri = NULL;
2256 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002257 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002258 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 lp->ll_li = lp->ll_li->li_next;
2260 ++lp->ll_n1;
2261 }
2262 if (ri != NULL)
2263 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 else if (lp->ll_empty2
2265 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 : lp->ll_n1 != lp->ll_n2)
2267 EMSG(_("E711: List value has not enough items"));
2268 }
2269 else
2270 {
2271 /*
2272 * Assign to a List or Dictionary item.
2273 */
2274 if (lp->ll_newkey != NULL)
2275 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 if (op != NULL && *op != '=')
2277 {
2278 EMSG2(_(e_letwrong), op);
2279 return;
2280 }
2281
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002283 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (di == NULL)
2285 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002286 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2287 {
2288 vim_free(di);
2289 return;
2290 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002292 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002293 else if (op != NULL && *op != '=')
2294 {
2295 tv_op(lp->ll_tv, rettv, op);
2296 return;
2297 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002300
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 /*
2302 * Assign the value to the variable or list item.
2303 */
2304 if (copy)
2305 copy_tv(rettv, lp->ll_tv);
2306 else
2307 {
2308 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002309 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
2312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313}
2314
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002315/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2317 * Returns OK or FAIL.
2318 */
2319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002320tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002322 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u numbuf[NUMBUFLEN];
2324 char_u *s;
2325
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002326 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2327 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2328 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 {
2330 switch (tv1->v_type)
2331 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002332 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 case VAR_DICT:
2334 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002335 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002336 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002337 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002338 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 break;
2340
2341 case VAR_LIST:
2342 if (*op != '+' || tv2->v_type != VAR_LIST)
2343 break;
2344 /* List += List */
2345 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2346 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2347 return OK;
2348
2349 case VAR_NUMBER:
2350 case VAR_STRING:
2351 if (tv2->v_type == VAR_LIST)
2352 break;
2353 if (*op == '+' || *op == '-')
2354 {
2355 /* nr += nr or nr -= nr*/
2356 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002357#ifdef FEAT_FLOAT
2358 if (tv2->v_type == VAR_FLOAT)
2359 {
2360 float_T f = n;
2361
2362 if (*op == '+')
2363 f += tv2->vval.v_float;
2364 else
2365 f -= tv2->vval.v_float;
2366 clear_tv(tv1);
2367 tv1->v_type = VAR_FLOAT;
2368 tv1->vval.v_float = f;
2369 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002371#endif
2372 {
2373 if (*op == '+')
2374 n += get_tv_number(tv2);
2375 else
2376 n -= get_tv_number(tv2);
2377 clear_tv(tv1);
2378 tv1->v_type = VAR_NUMBER;
2379 tv1->vval.v_number = n;
2380 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 }
2382 else
2383 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002384 if (tv2->v_type == VAR_FLOAT)
2385 break;
2386
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 /* str .= str */
2388 s = get_tv_string(tv1);
2389 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2390 clear_tv(tv1);
2391 tv1->v_type = VAR_STRING;
2392 tv1->vval.v_string = s;
2393 }
2394 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002395
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002396 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002397#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398 {
2399 float_T f;
2400
2401 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2402 && tv2->v_type != VAR_NUMBER
2403 && tv2->v_type != VAR_STRING))
2404 break;
2405 if (tv2->v_type == VAR_FLOAT)
2406 f = tv2->vval.v_float;
2407 else
2408 f = get_tv_number(tv2);
2409 if (*op == '+')
2410 tv1->vval.v_float += f;
2411 else
2412 tv1->vval.v_float -= f;
2413 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002414#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002415 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 }
2417 }
2418
2419 EMSG2(_(e_letwrong), op);
2420 return FAIL;
2421}
2422
2423/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 * Evaluate the expression used in a ":for var in expr" command.
2425 * "arg" points to "var".
2426 * Set "*errp" to TRUE for an error, FALSE otherwise;
2427 * Return a pointer that holds the info. Null when there is an error.
2428 */
2429 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002430eval_for_line(
2431 char_u *arg,
2432 int *errp,
2433 char_u **nextcmdp,
2434 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435{
Bram Moolenaar33570922005-01-25 22:26:29 +00002436 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002438 typval_T tv;
2439 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440
2441 *errp = TRUE; /* default: there is an error */
2442
Bram Moolenaar33570922005-01-25 22:26:29 +00002443 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 if (fi == NULL)
2445 return NULL;
2446
2447 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2448 if (expr == NULL)
2449 return fi;
2450
2451 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002452 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002453 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002454 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 return fi;
2456 }
2457
2458 if (skip)
2459 ++emsg_skip;
2460 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2461 {
2462 *errp = FALSE;
2463 if (!skip)
2464 {
2465 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002466 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002467 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002469 clear_tv(&tv);
2470 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002471 else if (l == NULL)
2472 {
2473 /* a null list is like an empty list: do nothing */
2474 clear_tv(&tv);
2475 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002476 else
2477 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002478 /* No need to increment the refcount, it's already set for the
2479 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480 fi->fi_list = l;
2481 list_add_watch(l, &fi->fi_lw);
2482 fi->fi_lw.lw_item = l->lv_first;
2483 }
2484 }
2485 }
2486 if (skip)
2487 --emsg_skip;
2488
2489 return fi;
2490}
2491
2492/*
2493 * Use the first item in a ":for" list. Advance to the next.
2494 * Assign the values to the variable (list). "arg" points to the first one.
2495 * Return TRUE when a valid item was found, FALSE when at end of list or
2496 * something wrong.
2497 */
2498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002500{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002501 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002502 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504
2505 item = fi->fi_lw.lw_item;
2506 if (item == NULL)
2507 result = FALSE;
2508 else
2509 {
2510 fi->fi_lw.lw_item = item->li_next;
2511 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2512 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2513 }
2514 return result;
2515}
2516
2517/*
2518 * Free the structure used to store info used by ":for".
2519 */
2520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002521free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002522{
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002525 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002526 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002528 list_unref(fi->fi_list);
2529 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002530 vim_free(fi);
2531}
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2534
2535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536set_context_for_expression(
2537 expand_T *xp,
2538 char_u *arg,
2539 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int got_eq = FALSE;
2542 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002543 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545 if (cmdidx == CMD_let)
2546 {
2547 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002548 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 {
2550 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002551 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 {
2553 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002554 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002555 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556 break;
2557 }
2558 return;
2559 }
2560 }
2561 else
2562 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2563 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 while ((xp->xp_pattern = vim_strpbrk(arg,
2565 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2566 {
2567 c = *xp->xp_pattern;
2568 if (c == '&')
2569 {
2570 c = xp->xp_pattern[1];
2571 if (c == '&')
2572 {
2573 ++xp->xp_pattern;
2574 xp->xp_context = cmdidx != CMD_let || got_eq
2575 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2576 }
2577 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002580 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2581 xp->xp_pattern += 2;
2582
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585 else if (c == '$')
2586 {
2587 /* environment variable */
2588 xp->xp_context = EXPAND_ENV_VARS;
2589 }
2590 else if (c == '=')
2591 {
2592 got_eq = TRUE;
2593 xp->xp_context = EXPAND_EXPRESSION;
2594 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002595 else if (c == '#'
2596 && xp->xp_context == EXPAND_EXPRESSION)
2597 {
2598 /* Autoload function/variable contains '#'. */
2599 break;
2600 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002601 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 && xp->xp_context == EXPAND_FUNCTIONS
2603 && vim_strchr(xp->xp_pattern, '(') == NULL)
2604 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002605 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 break;
2607 }
2608 else if (cmdidx != CMD_let || got_eq)
2609 {
2610 if (c == '"') /* string */
2611 {
2612 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2613 if (c == '\\' && xp->xp_pattern[1] != NUL)
2614 ++xp->xp_pattern;
2615 xp->xp_context = EXPAND_NOTHING;
2616 }
2617 else if (c == '\'') /* literal string */
2618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2621 /* skip */ ;
2622 xp->xp_context = EXPAND_NOTHING;
2623 }
2624 else if (c == '|')
2625 {
2626 if (xp->xp_pattern[1] == '|')
2627 {
2628 ++xp->xp_pattern;
2629 xp->xp_context = EXPAND_EXPRESSION;
2630 }
2631 else
2632 xp->xp_context = EXPAND_COMMANDS;
2633 }
2634 else
2635 xp->xp_context = EXPAND_EXPRESSION;
2636 }
2637 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002638 /* Doesn't look like something valid, expand as an expression
2639 * anyway. */
2640 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 arg = xp->xp_pattern;
2642 if (*arg != NUL)
2643 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2644 /* skip */ ;
2645 }
2646 xp->xp_pattern = arg;
2647}
2648
2649#endif /* FEAT_CMDL_COMPL */
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * ":unlet[!] var1 ... " command.
2653 */
2654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002655ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002657 ex_unletlock(eap, eap->arg, 0);
2658}
2659
2660/*
2661 * ":lockvar" and ":unlockvar" commands
2662 */
2663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002665{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002667 int deep = 2;
2668
2669 if (eap->forceit)
2670 deep = -1;
2671 else if (vim_isdigit(*arg))
2672 {
2673 deep = getdigits(&arg);
2674 arg = skipwhite(arg);
2675 }
2676
2677 ex_unletlock(eap, arg, deep);
2678}
2679
2680/*
2681 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2682 */
2683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002684ex_unletlock(
2685 exarg_T *eap,
2686 char_u *argstart,
2687 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688{
2689 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002692 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 do
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002697 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002698 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lv.ll_name == NULL)
2700 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002701 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (name_end != NULL)
2705 {
2706 emsg_severe = TRUE;
2707 EMSG(_(e_trailing));
2708 }
2709 if (!(eap->skip || error))
2710 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712 }
2713
2714 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002715 {
2716 if (eap->cmdidx == CMD_unlet)
2717 {
2718 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2719 error = TRUE;
2720 }
2721 else
2722 {
2723 if (do_lock_var(&lv, name_end, deep,
2724 eap->cmdidx == CMD_lockvar) == FAIL)
2725 error = TRUE;
2726 }
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (!eap->skip)
2730 clear_lval(&lv);
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 arg = skipwhite(name_end);
2733 } while (!ends_excmd(*arg));
2734
2735 eap->nextcmd = check_nextcmd(arg);
2736}
2737
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002739do_unlet_var(
2740 lval_T *lp,
2741 char_u *name_end,
2742 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 int ret = OK;
2745 int cc;
2746
2747 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 cc = *name_end;
2750 *name_end = NUL;
2751
2752 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002753 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002757 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002758 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002759 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002760 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002761 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 else if (lp->ll_range)
2763 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002764 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002765 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002766 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002767
2768 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2769 {
2770 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002771 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002772 return FAIL;
2773 ll_li = li;
2774 ++ll_n1;
2775 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776
2777 /* Delete a range of List items. */
2778 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2779 {
2780 li = lp->ll_li->li_next;
2781 listitem_remove(lp->ll_list, lp->ll_li);
2782 lp->ll_li = li;
2783 ++lp->ll_n1;
2784 }
2785 }
2786 else
2787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 /* unlet a List item. */
2790 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002793 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 }
2795
2796 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797}
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799/*
2800 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002801 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
2803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002804do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 hashtab_T *ht;
2807 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002808 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002809 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002810 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 ht = find_var_ht(name, &varname);
2813 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002816 if (d == NULL)
2817 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 if (ht == &globvarht)
2819 d = &globvardict;
2820 else if (ht == &compat_hashtab)
2821 d = &vimvardict;
2822 else
2823 {
2824 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2825 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2826 }
2827 if (d == NULL)
2828 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002829 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 return FAIL;
2831 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002832 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002834 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002835 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002836 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002837 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002838 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002839 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002840 || var_check_ro(di->di_flags, name, FALSE)
2841 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002842 return FAIL;
2843
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 delete_var(ht, hi);
2845 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 if (forceit)
2849 return OK;
2850 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 return FAIL;
2852}
2853
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002854/*
2855 * Lock or unlock variable indicated by "lp".
2856 * "deep" is the levels to go (-1 for unlimited);
2857 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2858 */
2859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002860do_lock_var(
2861 lval_T *lp,
2862 char_u *name_end,
2863 int deep,
2864 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002865{
2866 int ret = OK;
2867 int cc;
2868 dictitem_T *di;
2869
2870 if (deep == 0) /* nothing to do */
2871 return OK;
2872
2873 if (lp->ll_tv == NULL)
2874 {
2875 cc = *name_end;
2876 *name_end = NUL;
2877
2878 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002879 di = find_var(lp->ll_name, NULL, TRUE);
2880 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002882 else if ((di->di_flags & DI_FLAGS_FIX)
2883 && di->di_tv.v_type != VAR_DICT
2884 && di->di_tv.v_type != VAR_LIST)
2885 /* For historic reasons this error is not given for a list or dict.
2886 * E.g., the b: dict could be locked/unlocked. */
2887 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002888 else
2889 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002890 if (lock)
2891 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002893 di->di_flags &= ~DI_FLAGS_LOCK;
2894 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 }
2896 *name_end = cc;
2897 }
2898 else if (lp->ll_range)
2899 {
2900 listitem_T *li = lp->ll_li;
2901
2902 /* (un)lock a range of List items. */
2903 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2904 {
2905 item_lock(&li->li_tv, deep, lock);
2906 li = li->li_next;
2907 ++lp->ll_n1;
2908 }
2909 }
2910 else if (lp->ll_list != NULL)
2911 /* (un)lock a List item. */
2912 item_lock(&lp->ll_li->li_tv, deep, lock);
2913 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002914 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 item_lock(&lp->ll_di->di_tv, deep, lock);
2916
2917 return ret;
2918}
2919
2920/*
2921 * Lock or unlock an item. "deep" is nr of levels to go.
2922 */
2923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925{
2926 static int recurse = 0;
2927 list_T *l;
2928 listitem_T *li;
2929 dict_T *d;
2930 hashitem_T *hi;
2931 int todo;
2932
2933 if (recurse >= DICT_MAXNEST)
2934 {
2935 EMSG(_("E743: variable nested too deep for (un)lock"));
2936 return;
2937 }
2938 if (deep == 0)
2939 return;
2940 ++recurse;
2941
2942 /* lock/unlock the item itself */
2943 if (lock)
2944 tv->v_lock |= VAR_LOCKED;
2945 else
2946 tv->v_lock &= ~VAR_LOCKED;
2947
2948 switch (tv->v_type)
2949 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002950 case VAR_UNKNOWN:
2951 case VAR_NUMBER:
2952 case VAR_STRING:
2953 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002954 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 case VAR_FLOAT:
2956 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002957 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002958 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002959 break;
2960
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 case VAR_LIST:
2962 if ((l = tv->vval.v_list) != NULL)
2963 {
2964 if (lock)
2965 l->lv_lock |= VAR_LOCKED;
2966 else
2967 l->lv_lock &= ~VAR_LOCKED;
2968 if (deep < 0 || deep > 1)
2969 /* recursive: lock/unlock the items the List contains */
2970 for (li = l->lv_first; li != NULL; li = li->li_next)
2971 item_lock(&li->li_tv, deep - 1, lock);
2972 }
2973 break;
2974 case VAR_DICT:
2975 if ((d = tv->vval.v_dict) != NULL)
2976 {
2977 if (lock)
2978 d->dv_lock |= VAR_LOCKED;
2979 else
2980 d->dv_lock &= ~VAR_LOCKED;
2981 if (deep < 0 || deep > 1)
2982 {
2983 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002984 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002985 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2986 {
2987 if (!HASHITEM_EMPTY(hi))
2988 {
2989 --todo;
2990 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2991 }
2992 }
2993 }
2994 }
2995 }
2996 --recurse;
2997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3000/*
3001 * Delete all "menutrans_" variables.
3002 */
3003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003004del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003007 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003010 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003012 {
3013 if (!HASHITEM_EMPTY(hi))
3014 {
3015 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3017 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003018 }
3019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021}
3022#endif
3023
3024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3025
3026/*
3027 * Local string buffer for the next two functions to store a variable name
3028 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3029 * get_user_var_name().
3030 */
3031
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003032static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034static char_u *varnamebuf = NULL;
3035static int varnamebuflen = 0;
3036
3037/*
3038 * Function to concatenate a prefix and a variable name.
3039 */
3040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003041cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 int len;
3044
3045 len = (int)STRLEN(name) + 3;
3046 if (len > varnamebuflen)
3047 {
3048 vim_free(varnamebuf);
3049 len += 10; /* some additional space */
3050 varnamebuf = alloc(len);
3051 if (varnamebuf == NULL)
3052 {
3053 varnamebuflen = 0;
3054 return NULL;
3055 }
3056 varnamebuflen = len;
3057 }
3058 *varnamebuf = prefix;
3059 varnamebuf[1] = ':';
3060 STRCPY(varnamebuf + 2, name);
3061 return varnamebuf;
3062}
3063
3064/*
3065 * Function given to ExpandGeneric() to obtain the list of user defined
3066 * (global/buffer/window/built-in) variable names.
3067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003069get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003071 static long_u gdone;
3072 static long_u bdone;
3073 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003074 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003075 static int vidx;
3076 static hashitem_T *hi;
3077 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003080 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003081 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003082 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003083 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003084
3085 /* Global variables */
3086 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003088 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003090 else
3091 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003092 while (HASHITEM_EMPTY(hi))
3093 ++hi;
3094 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3095 return cat_prefix_varname('g', hi->hi_key);
3096 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003098
3099 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003100 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003101 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003103 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003104 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003105 else
3106 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003107 while (HASHITEM_EMPTY(hi))
3108 ++hi;
3109 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003111
3112 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003113 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003114 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003116 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003118 else
3119 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003120 while (HASHITEM_EMPTY(hi))
3121 ++hi;
3122 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003124
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003125 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003126 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003127 if (tdone < ht->ht_used)
3128 {
3129 if (tdone++ == 0)
3130 hi = ht->ht_array;
3131 else
3132 ++hi;
3133 while (HASHITEM_EMPTY(hi))
3134 ++hi;
3135 return cat_prefix_varname('t', hi->hi_key);
3136 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003137
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 /* v: variables */
3139 if (vidx < VV_LEN)
3140 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 vim_free(varnamebuf);
3143 varnamebuf = NULL;
3144 varnamebuflen = 0;
3145 return NULL;
3146}
3147
3148#endif /* FEAT_CMDL_COMPL */
3149
3150/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003151 * Return TRUE if "pat" matches "text".
3152 * Does not use 'cpo' and always uses 'magic'.
3153 */
3154 static int
3155pattern_match(char_u *pat, char_u *text, int ic)
3156{
3157 int matches = FALSE;
3158 char_u *save_cpo;
3159 regmatch_T regmatch;
3160
3161 /* avoid 'l' flag in 'cpoptions' */
3162 save_cpo = p_cpo;
3163 p_cpo = (char_u *)"";
3164 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3165 if (regmatch.regprog != NULL)
3166 {
3167 regmatch.rm_ic = ic;
3168 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3169 vim_regfree(regmatch.regprog);
3170 }
3171 p_cpo = save_cpo;
3172 return matches;
3173}
3174
3175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * types for expressions.
3177 */
3178typedef enum
3179{
3180 TYPE_UNKNOWN = 0
3181 , TYPE_EQUAL /* == */
3182 , TYPE_NEQUAL /* != */
3183 , TYPE_GREATER /* > */
3184 , TYPE_GEQUAL /* >= */
3185 , TYPE_SMALLER /* < */
3186 , TYPE_SEQUAL /* <= */
3187 , TYPE_MATCH /* =~ */
3188 , TYPE_NOMATCH /* !~ */
3189} exptype_T;
3190
3191/*
3192 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3195 */
3196
3197/*
3198 * Handle zero level expression.
3199 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003200 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003201 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 * Return OK or FAIL.
3203 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205eval0(
3206 char_u *arg,
3207 typval_T *rettv,
3208 char_u **nextcmd,
3209 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 int ret;
3212 char_u *p;
3213
3214 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003215 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (ret == FAIL || !ends_excmd(*p))
3217 {
3218 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 /*
3221 * Report the invalid expression unless the expression evaluation has
3222 * been cancelled due to an aborting error, an interrupt, or an
3223 * exception.
3224 */
3225 if (!aborting())
3226 EMSG2(_(e_invexpr2), arg);
3227 ret = FAIL;
3228 }
3229 if (nextcmd != NULL)
3230 *nextcmd = check_nextcmd(p);
3231
3232 return ret;
3233}
3234
3235/*
3236 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003237 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 *
3239 * "arg" must point to the first non-white of the expression.
3240 * "arg" is advanced to the next non-white after the recognized expression.
3241 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003242 * Note: "rettv.v_lock" is not set.
3243 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 * Return OK or FAIL.
3245 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003247eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
3249 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /*
3253 * Get the first variable.
3254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003255 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return FAIL;
3257
3258 if ((*arg)[0] == '?')
3259 {
3260 result = FALSE;
3261 if (evaluate)
3262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003263 int error = FALSE;
3264
3265 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003268 if (error)
3269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271
3272 /*
3273 * Get the second variable.
3274 */
3275 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 return FAIL;
3278
3279 /*
3280 * Check for the ":".
3281 */
3282 if ((*arg)[0] != ':')
3283 {
3284 EMSG(_("E109: Missing ':' after '?'"));
3285 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003286 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return FAIL;
3288 }
3289
3290 /*
3291 * Get the third variable.
3292 */
3293 *arg = skipwhite(*arg + 1);
3294 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3295 {
3296 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 return FAIL;
3299 }
3300 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003301 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303
3304 return OK;
3305}
3306
3307/*
3308 * Handle first level expression:
3309 * expr2 || expr2 || expr2 logical OR
3310 *
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 *
3314 * Return OK or FAIL.
3315 */
3316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 long result;
3321 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 /*
3325 * Get the first variable.
3326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 return FAIL;
3329
3330 /*
3331 * Repeat until there is no following "||".
3332 */
3333 first = TRUE;
3334 result = FALSE;
3335 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3336 {
3337 if (evaluate && first)
3338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003339 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 if (error)
3343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 first = FALSE;
3345 }
3346
3347 /*
3348 * Get the second variable.
3349 */
3350 *arg = skipwhite(*arg + 2);
3351 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3352 return FAIL;
3353
3354 /*
3355 * Compute the result.
3356 */
3357 if (evaluate && !result)
3358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003361 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 if (error)
3363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 if (evaluate)
3366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003367 rettv->v_type = VAR_NUMBER;
3368 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 }
3371
3372 return OK;
3373}
3374
3375/*
3376 * Handle second level expression:
3377 * expr3 && expr3 && expr3 logical AND
3378 *
3379 * "arg" must point to the first non-white of the expression.
3380 * "arg" is advanced to the next non-white after the recognized expression.
3381 *
3382 * Return OK or FAIL.
3383 */
3384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003385eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 long result;
3389 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003390 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
3392 /*
3393 * Get the first variable.
3394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return FAIL;
3397
3398 /*
3399 * Repeat until there is no following "&&".
3400 */
3401 first = TRUE;
3402 result = TRUE;
3403 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3404 {
3405 if (evaluate && first)
3406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 if (error)
3411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 first = FALSE;
3413 }
3414
3415 /*
3416 * Get the second variable.
3417 */
3418 *arg = skipwhite(*arg + 2);
3419 if (eval4(arg, &var2, evaluate && result) == FAIL)
3420 return FAIL;
3421
3422 /*
3423 * Compute the result.
3424 */
3425 if (evaluate && result)
3426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003427 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003430 if (error)
3431 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433 if (evaluate)
3434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 rettv->v_type = VAR_NUMBER;
3436 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 }
3439
3440 return OK;
3441}
3442
3443/*
3444 * Handle third level expression:
3445 * var1 == var2
3446 * var1 =~ var2
3447 * var1 != var2
3448 * var1 !~ var2
3449 * var1 > var2
3450 * var1 >= var2
3451 * var1 < var2
3452 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003453 * var1 is var2
3454 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 *
3456 * "arg" must point to the first non-white of the expression.
3457 * "arg" is advanced to the next non-white after the recognized expression.
3458 *
3459 * Return OK or FAIL.
3460 */
3461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003462eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 char_u *p;
3466 int i;
3467 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003468 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003470 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 char_u *s1, *s2;
3472 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 /*
3476 * Get the first variable.
3477 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 return FAIL;
3480
3481 p = *arg;
3482 switch (p[0])
3483 {
3484 case '=': if (p[1] == '=')
3485 type = TYPE_EQUAL;
3486 else if (p[1] == '~')
3487 type = TYPE_MATCH;
3488 break;
3489 case '!': if (p[1] == '=')
3490 type = TYPE_NEQUAL;
3491 else if (p[1] == '~')
3492 type = TYPE_NOMATCH;
3493 break;
3494 case '>': if (p[1] != '=')
3495 {
3496 type = TYPE_GREATER;
3497 len = 1;
3498 }
3499 else
3500 type = TYPE_GEQUAL;
3501 break;
3502 case '<': if (p[1] != '=')
3503 {
3504 type = TYPE_SMALLER;
3505 len = 1;
3506 }
3507 else
3508 type = TYPE_SEQUAL;
3509 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003510 case 'i': if (p[1] == 's')
3511 {
3512 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3513 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003514 i = p[len];
3515 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003516 {
3517 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3518 type_is = TRUE;
3519 }
3520 }
3521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523
3524 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003525 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
3527 if (type != TYPE_UNKNOWN)
3528 {
3529 /* extra question mark appended: ignore case */
3530 if (p[len] == '?')
3531 {
3532 ic = TRUE;
3533 ++len;
3534 }
3535 /* extra '#' appended: match case */
3536 else if (p[len] == '#')
3537 {
3538 ic = FALSE;
3539 ++len;
3540 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else
3543 ic = p_ic;
3544
3545 /*
3546 * Get the second variable.
3547 */
3548 *arg = skipwhite(p + len);
3549 if (eval5(arg, &var2, evaluate) == FAIL)
3550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return FAIL;
3553 }
3554
3555 if (evaluate)
3556 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003557 if (type_is && rettv->v_type != var2.v_type)
3558 {
3559 /* For "is" a different type always means FALSE, for "notis"
3560 * it means TRUE. */
3561 n1 = (type == TYPE_NEQUAL);
3562 }
3563 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3564 {
3565 if (type_is)
3566 {
3567 n1 = (rettv->v_type == var2.v_type
3568 && rettv->vval.v_list == var2.vval.v_list);
3569 if (type == TYPE_NEQUAL)
3570 n1 = !n1;
3571 }
3572 else if (rettv->v_type != var2.v_type
3573 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3574 {
3575 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003576 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003577 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003578 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003579 clear_tv(rettv);
3580 clear_tv(&var2);
3581 return FAIL;
3582 }
3583 else
3584 {
3585 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003586 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3587 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003588 if (type == TYPE_NEQUAL)
3589 n1 = !n1;
3590 }
3591 }
3592
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003593 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3594 {
3595 if (type_is)
3596 {
3597 n1 = (rettv->v_type == var2.v_type
3598 && rettv->vval.v_dict == var2.vval.v_dict);
3599 if (type == TYPE_NEQUAL)
3600 n1 = !n1;
3601 }
3602 else if (rettv->v_type != var2.v_type
3603 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3604 {
3605 if (rettv->v_type != var2.v_type)
3606 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3607 else
3608 EMSG(_("E736: Invalid operation for Dictionary"));
3609 clear_tv(rettv);
3610 clear_tv(&var2);
3611 return FAIL;
3612 }
3613 else
3614 {
3615 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003616 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3617 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003618 if (type == TYPE_NEQUAL)
3619 n1 = !n1;
3620 }
3621 }
3622
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003623 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3624 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003625 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003626 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003627 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003628 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003629 clear_tv(rettv);
3630 clear_tv(&var2);
3631 return FAIL;
3632 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003633 if ((rettv->v_type == VAR_PARTIAL
3634 && rettv->vval.v_partial == NULL)
3635 || (var2.v_type == VAR_PARTIAL
3636 && var2.vval.v_partial == NULL))
3637 /* when a partial is NULL assume not equal */
3638 n1 = FALSE;
3639 else if (type_is)
3640 {
3641 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3642 /* strings are considered the same if their value is
3643 * the same */
3644 n1 = tv_equal(rettv, &var2, ic, FALSE);
3645 else if (rettv->v_type == VAR_PARTIAL
3646 && var2.v_type == VAR_PARTIAL)
3647 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3648 else
3649 n1 = FALSE;
3650 }
3651 else
3652 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003653 if (type == TYPE_NEQUAL)
3654 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003655 }
3656
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003657#ifdef FEAT_FLOAT
3658 /*
3659 * If one of the two variables is a float, compare as a float.
3660 * When using "=~" or "!~", always compare as string.
3661 */
3662 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3663 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3664 {
3665 float_T f1, f2;
3666
3667 if (rettv->v_type == VAR_FLOAT)
3668 f1 = rettv->vval.v_float;
3669 else
3670 f1 = get_tv_number(rettv);
3671 if (var2.v_type == VAR_FLOAT)
3672 f2 = var2.vval.v_float;
3673 else
3674 f2 = get_tv_number(&var2);
3675 n1 = FALSE;
3676 switch (type)
3677 {
3678 case TYPE_EQUAL: n1 = (f1 == f2); break;
3679 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3680 case TYPE_GREATER: n1 = (f1 > f2); break;
3681 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3682 case TYPE_SMALLER: n1 = (f1 < f2); break;
3683 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3684 case TYPE_UNKNOWN:
3685 case TYPE_MATCH:
3686 case TYPE_NOMATCH: break; /* avoid gcc warning */
3687 }
3688 }
3689#endif
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 /*
3692 * If one of the two variables is a number, compare as a number.
3693 * When using "=~" or "!~", always compare as string.
3694 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003695 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003698 n1 = get_tv_number(rettv);
3699 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 switch (type)
3701 {
3702 case TYPE_EQUAL: n1 = (n1 == n2); break;
3703 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3704 case TYPE_GREATER: n1 = (n1 > n2); break;
3705 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3706 case TYPE_SMALLER: n1 = (n1 < n2); break;
3707 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3708 case TYPE_UNKNOWN:
3709 case TYPE_MATCH:
3710 case TYPE_NOMATCH: break; /* avoid gcc warning */
3711 }
3712 }
3713 else
3714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715 s1 = get_tv_string_buf(rettv, buf1);
3716 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3718 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3719 else
3720 i = 0;
3721 n1 = FALSE;
3722 switch (type)
3723 {
3724 case TYPE_EQUAL: n1 = (i == 0); break;
3725 case TYPE_NEQUAL: n1 = (i != 0); break;
3726 case TYPE_GREATER: n1 = (i > 0); break;
3727 case TYPE_GEQUAL: n1 = (i >= 0); break;
3728 case TYPE_SMALLER: n1 = (i < 0); break;
3729 case TYPE_SEQUAL: n1 = (i <= 0); break;
3730
3731 case TYPE_MATCH:
3732 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003733 n1 = pattern_match(s2, s1, ic);
3734 if (type == TYPE_NOMATCH)
3735 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 break;
3737
3738 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3739 }
3740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 clear_tv(rettv);
3742 clear_tv(&var2);
3743 rettv->v_type = VAR_NUMBER;
3744 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 }
3747
3748 return OK;
3749}
3750
3751/*
3752 * Handle fourth level expression:
3753 * + number addition
3754 * - number subtraction
3755 * . string concatenation
3756 *
3757 * "arg" must point to the first non-white of the expression.
3758 * "arg" is advanced to the next non-white after the recognized expression.
3759 *
3760 * Return OK or FAIL.
3761 */
3762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 typval_T var2;
3766 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003768 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003769#ifdef FEAT_FLOAT
3770 float_T f1 = 0, f2 = 0;
3771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 char_u *s1, *s2;
3773 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3774 char_u *p;
3775
3776 /*
3777 * Get the first variable.
3778 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003779 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781
3782 /*
3783 * Repeat computing, until no '+', '-' or '.' is following.
3784 */
3785 for (;;)
3786 {
3787 op = **arg;
3788 if (op != '+' && op != '-' && op != '.')
3789 break;
3790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003791 if ((op != '+' || rettv->v_type != VAR_LIST)
3792#ifdef FEAT_FLOAT
3793 && (op == '.' || rettv->v_type != VAR_FLOAT)
3794#endif
3795 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003796 {
3797 /* For "list + ...", an illegal use of the first operand as
3798 * a number cannot be determined before evaluating the 2nd
3799 * operand: if this is also a list, all is ok.
3800 * For "something . ...", "something - ..." or "non-list + ...",
3801 * we know that the first operand needs to be a string or number
3802 * without evaluating the 2nd operand. So check before to avoid
3803 * side effects after an error. */
3804 if (evaluate && get_tv_string_chk(rettv) == NULL)
3805 {
3806 clear_tv(rettv);
3807 return FAIL;
3808 }
3809 }
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /*
3812 * Get the second variable.
3813 */
3814 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003815 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return FAIL;
3819 }
3820
3821 if (evaluate)
3822 {
3823 /*
3824 * Compute the result.
3825 */
3826 if (op == '.')
3827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3829 s2 = get_tv_string_buf_chk(&var2, buf2);
3830 if (s2 == NULL) /* type error ? */
3831 {
3832 clear_tv(rettv);
3833 clear_tv(&var2);
3834 return FAIL;
3835 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003836 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 clear_tv(rettv);
3838 rettv->v_type = VAR_STRING;
3839 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003841 else if (op == '+' && rettv->v_type == VAR_LIST
3842 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003843 {
3844 /* concatenate Lists */
3845 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3846 &var3) == FAIL)
3847 {
3848 clear_tv(rettv);
3849 clear_tv(&var2);
3850 return FAIL;
3851 }
3852 clear_tv(rettv);
3853 *rettv = var3;
3854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 else
3856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 int error = FALSE;
3858
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003859#ifdef FEAT_FLOAT
3860 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003862 f1 = rettv->vval.v_float;
3863 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003864 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003865 else
3866#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 n1 = get_tv_number_chk(rettv, &error);
3869 if (error)
3870 {
3871 /* This can only happen for "list + non-list". For
3872 * "non-list + ..." or "something - ...", we returned
3873 * before evaluating the 2nd operand. */
3874 clear_tv(rettv);
3875 return FAIL;
3876 }
3877#ifdef FEAT_FLOAT
3878 if (var2.v_type == VAR_FLOAT)
3879 f1 = n1;
3880#endif
3881 }
3882#ifdef FEAT_FLOAT
3883 if (var2.v_type == VAR_FLOAT)
3884 {
3885 f2 = var2.vval.v_float;
3886 n2 = 0;
3887 }
3888 else
3889#endif
3890 {
3891 n2 = get_tv_number_chk(&var2, &error);
3892 if (error)
3893 {
3894 clear_tv(rettv);
3895 clear_tv(&var2);
3896 return FAIL;
3897 }
3898#ifdef FEAT_FLOAT
3899 if (rettv->v_type == VAR_FLOAT)
3900 f2 = n2;
3901#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003904
3905#ifdef FEAT_FLOAT
3906 /* If there is a float on either side the result is a float. */
3907 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3908 {
3909 if (op == '+')
3910 f1 = f1 + f2;
3911 else
3912 f1 = f1 - f2;
3913 rettv->v_type = VAR_FLOAT;
3914 rettv->vval.v_float = f1;
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003917#endif
3918 {
3919 if (op == '+')
3920 n1 = n1 + n2;
3921 else
3922 n1 = n1 - n2;
3923 rettv->v_type = VAR_NUMBER;
3924 rettv->vval.v_number = n1;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003927 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929 }
3930 return OK;
3931}
3932
3933/*
3934 * Handle fifth level expression:
3935 * * number multiplication
3936 * / number division
3937 * % number modulo
3938 *
3939 * "arg" must point to the first non-white of the expression.
3940 * "arg" is advanced to the next non-white after the recognized expression.
3941 *
3942 * Return OK or FAIL.
3943 */
3944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003945eval6(
3946 char_u **arg,
3947 typval_T *rettv,
3948 int evaluate,
3949 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003953 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003954#ifdef FEAT_FLOAT
3955 int use_float = FALSE;
3956 float_T f1 = 0, f2;
3957#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003958 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 /*
3961 * Get the first variable.
3962 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003963 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return FAIL;
3965
3966 /*
3967 * Repeat computing, until no '*', '/' or '%' is following.
3968 */
3969 for (;;)
3970 {
3971 op = **arg;
3972 if (op != '*' && op != '/' && op != '%')
3973 break;
3974
3975 if (evaluate)
3976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003977#ifdef FEAT_FLOAT
3978 if (rettv->v_type == VAR_FLOAT)
3979 {
3980 f1 = rettv->vval.v_float;
3981 use_float = TRUE;
3982 n1 = 0;
3983 }
3984 else
3985#endif
3986 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003987 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003988 if (error)
3989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991 else
3992 n1 = 0;
3993
3994 /*
3995 * Get the second variable.
3996 */
3997 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003998 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return FAIL;
4000
4001 if (evaluate)
4002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004003#ifdef FEAT_FLOAT
4004 if (var2.v_type == VAR_FLOAT)
4005 {
4006 if (!use_float)
4007 {
4008 f1 = n1;
4009 use_float = TRUE;
4010 }
4011 f2 = var2.vval.v_float;
4012 n2 = 0;
4013 }
4014 else
4015#endif
4016 {
4017 n2 = get_tv_number_chk(&var2, &error);
4018 clear_tv(&var2);
4019 if (error)
4020 return FAIL;
4021#ifdef FEAT_FLOAT
4022 if (use_float)
4023 f2 = n2;
4024#endif
4025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 /*
4028 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004029 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004031#ifdef FEAT_FLOAT
4032 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004034 if (op == '*')
4035 f1 = f1 * f2;
4036 else if (op == '/')
4037 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004038# ifdef VMS
4039 /* VMS crashes on divide by zero, work around it */
4040 if (f2 == 0.0)
4041 {
4042 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004043 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004044 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004045 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004047 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048 }
4049 else
4050 f1 = f1 / f2;
4051# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004052 /* We rely on the floating point library to handle divide
4053 * by zero to result in "inf" and not a crash. */
4054 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004055# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004059 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 return FAIL;
4061 }
4062 rettv->v_type = VAR_FLOAT;
4063 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068 if (op == '*')
4069 n1 = n1 * n2;
4070 else if (op == '/')
4071 {
4072 if (n2 == 0) /* give an error message? */
4073 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004074 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004075 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004076 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004077 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004078 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004079 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004080 }
4081 else
4082 n1 = n1 / n2;
4083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004085 {
4086 if (n2 == 0) /* give an error message? */
4087 n1 = 0;
4088 else
4089 n1 = n1 % n2;
4090 }
4091 rettv->v_type = VAR_NUMBER;
4092 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 }
4096
4097 return OK;
4098}
4099
4100/*
4101 * Handle sixth level expression:
4102 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004103 * "string" string constant
4104 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * &option-name option value
4106 * @r register contents
4107 * identifier variable value
4108 * function() function call
4109 * $VAR environment variable
4110 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004111 * [expr, expr] List
4112 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 *
4114 * Also handle:
4115 * ! in front logical NOT
4116 * - in front unary minus
4117 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004118 * trailing [] subscript in String or List
4119 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 *
4121 * "arg" must point to the first non-white of the expression.
4122 * "arg" is advanced to the next non-white after the recognized expression.
4123 *
4124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004127eval7(
4128 char_u **arg,
4129 typval_T *rettv,
4130 int evaluate,
4131 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004133 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 int len;
4135 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 char_u *start_leader, *end_leader;
4137 int ret = OK;
4138 char_u *alias;
4139
4140 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004142 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004147 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
4149 start_leader = *arg;
4150 while (**arg == '!' || **arg == '-' || **arg == '+')
4151 *arg = skipwhite(*arg + 1);
4152 end_leader = *arg;
4153
4154 switch (**arg)
4155 {
4156 /*
4157 * Number constant.
4158 */
4159 case '0':
4160 case '1':
4161 case '2':
4162 case '3':
4163 case '4':
4164 case '5':
4165 case '6':
4166 case '7':
4167 case '8':
4168 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004169 {
4170#ifdef FEAT_FLOAT
4171 char_u *p = skipdigits(*arg + 1);
4172 int get_float = FALSE;
4173
4174 /* We accept a float when the format matches
4175 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004176 * strict to avoid backwards compatibility problems.
4177 * Don't look for a float after the "." operator, so that
4178 * ":let vers = 1.2.3" doesn't fail. */
4179 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004181 get_float = TRUE;
4182 p = skipdigits(p + 2);
4183 if (*p == 'e' || *p == 'E')
4184 {
4185 ++p;
4186 if (*p == '-' || *p == '+')
4187 ++p;
4188 if (!vim_isdigit(*p))
4189 get_float = FALSE;
4190 else
4191 p = skipdigits(p + 1);
4192 }
4193 if (ASCII_ISALPHA(*p) || *p == '.')
4194 get_float = FALSE;
4195 }
4196 if (get_float)
4197 {
4198 float_T f;
4199
4200 *arg += string2float(*arg, &f);
4201 if (evaluate)
4202 {
4203 rettv->v_type = VAR_FLOAT;
4204 rettv->vval.v_float = f;
4205 }
4206 }
4207 else
4208#endif
4209 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004210 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004211 *arg += len;
4212 if (evaluate)
4213 {
4214 rettv->v_type = VAR_NUMBER;
4215 rettv->vval.v_number = n;
4216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 /*
4222 * String constant: "string".
4223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 break;
4226
4227 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004228 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231 break;
4232
4233 /*
4234 * List: [expr, expr]
4235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 break;
4238
4239 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004240 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 * Dictionary: {key: val, key: val}
4242 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004243 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4244 if (ret == NOTDONE)
4245 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004246 break;
4247
4248 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004249 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004251 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 break;
4253
4254 /*
4255 * Environment variable: $VAR.
4256 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 break;
4259
4260 /*
4261 * Register contents: @r.
4262 */
4263 case '@': ++*arg;
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004267 rettv->vval.v_string = get_reg_contents(**arg,
4268 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 if (**arg != NUL)
4271 ++*arg;
4272 break;
4273
4274 /*
4275 * nested expression: (expression).
4276 */
4277 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (**arg == ')')
4280 ++*arg;
4281 else if (ret == OK)
4282 {
4283 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 ret = FAIL;
4286 }
4287 break;
4288
Bram Moolenaar8c711452005-01-14 21:53:12 +00004289 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 break;
4291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004292
4293 if (ret == NOTDONE)
4294 {
4295 /*
4296 * Must be a variable or function name.
4297 * Can also be a curly-braces kind of name: {expr}.
4298 */
4299 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004300 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301 if (alias != NULL)
4302 s = alias;
4303
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004304 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004305 ret = FAIL;
4306 else
4307 {
4308 if (**arg == '(') /* recursive! */
4309 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004310 partial_T *partial;
4311
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004312 if (!evaluate)
4313 check_vars(s, len);
4314
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 /* If "s" is the name of a variable of type VAR_FUNC
4316 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004317 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004319 /* Need to make a copy, in case evaluating the arguments makes
4320 * the name invalid. */
4321 s = vim_strsave(s);
4322 if (s == NULL)
4323 ret = FAIL;
4324 else
4325 /* Invoke the function. */
4326 ret = get_func_tv(s, len, rettv, arg,
4327 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4328 &len, evaluate, partial, NULL);
4329 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004330
4331 /* If evaluate is FALSE rettv->v_type was not set in
4332 * get_func_tv, but it's needed in handle_subscript() to parse
4333 * what follows. So set it here. */
4334 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4335 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004336 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004337 rettv->v_type = VAR_FUNC;
4338 }
4339
Bram Moolenaar8c711452005-01-14 21:53:12 +00004340 /* Stop the expression evaluation when immediately
4341 * aborting on error, or when an interrupt occurred or
4342 * an exception was thrown but not caught. */
4343 if (aborting())
4344 {
4345 if (ret == OK)
4346 clear_tv(rettv);
4347 ret = FAIL;
4348 }
4349 }
4350 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004351 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004352 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004353 {
4354 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004355 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004356 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004357 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004358 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359 }
4360
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 *arg = skipwhite(*arg);
4362
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004363 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4364 * expr(expr). */
4365 if (ret == OK)
4366 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 /*
4369 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4370 */
4371 if (ret == OK && evaluate && end_leader > start_leader)
4372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004374 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004375#ifdef FEAT_FLOAT
4376 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004378 if (rettv->v_type == VAR_FLOAT)
4379 f = rettv->vval.v_float;
4380 else
4381#endif
4382 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385 clear_tv(rettv);
4386 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 else
4389 {
4390 while (end_leader > start_leader)
4391 {
4392 --end_leader;
4393 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394 {
4395#ifdef FEAT_FLOAT
4396 if (rettv->v_type == VAR_FLOAT)
4397 f = !f;
4398 else
4399#endif
4400 val = !val;
4401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004402 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004403 {
4404#ifdef FEAT_FLOAT
4405 if (rettv->v_type == VAR_FLOAT)
4406 f = -f;
4407 else
4408#endif
4409 val = -val;
4410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412#ifdef FEAT_FLOAT
4413 if (rettv->v_type == VAR_FLOAT)
4414 {
4415 clear_tv(rettv);
4416 rettv->vval.v_float = f;
4417 }
4418 else
4419#endif
4420 {
4421 clear_tv(rettv);
4422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = val;
4424 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427
4428 return ret;
4429}
4430
4431/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004432 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4433 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4435 */
4436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004437eval_index(
4438 char_u **arg,
4439 typval_T *rettv,
4440 int evaluate,
4441 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442{
4443 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004444 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004445 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 long len = -1;
4447 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450
Bram Moolenaara03f2332016-02-06 18:09:59 +01004451 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004453 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004454 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004455 if (verbose)
4456 EMSG(_("E695: Cannot index a Funcref"));
4457 return FAIL;
4458 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004459#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004460 if (verbose)
4461 EMSG(_(e_float_as_string));
4462 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004463#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004464 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004465 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004466 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004467 if (verbose)
4468 EMSG(_("E909: Cannot index a special variable"));
4469 return FAIL;
4470 case VAR_UNKNOWN:
4471 if (evaluate)
4472 return FAIL;
4473 /* FALLTHROUGH */
4474
4475 case VAR_STRING:
4476 case VAR_NUMBER:
4477 case VAR_LIST:
4478 case VAR_DICT:
4479 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004482 init_tv(&var1);
4483 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 /*
4487 * dict.name
4488 */
4489 key = *arg + 1;
4490 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4491 ;
4492 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004493 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 }
4496 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 /*
4499 * something[idx]
4500 *
4501 * Get the (first) variable from inside the [].
4502 */
4503 *arg = skipwhite(*arg + 1);
4504 if (**arg == ':')
4505 empty1 = TRUE;
4506 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4507 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4509 {
4510 /* not a number or string */
4511 clear_tv(&var1);
4512 return FAIL;
4513 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514
4515 /*
4516 * Get the second variable from inside the [:].
4517 */
4518 if (**arg == ':')
4519 {
4520 range = TRUE;
4521 *arg = skipwhite(*arg + 1);
4522 if (**arg == ']')
4523 empty2 = TRUE;
4524 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 if (!empty1)
4527 clear_tv(&var1);
4528 return FAIL;
4529 }
4530 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4531 {
4532 /* not a number or string */
4533 if (!empty1)
4534 clear_tv(&var1);
4535 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 return FAIL;
4537 }
4538 }
4539
4540 /* Check for the ']'. */
4541 if (**arg != ']')
4542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 if (verbose)
4544 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 clear_tv(&var1);
4546 if (range)
4547 clear_tv(&var2);
4548 return FAIL;
4549 }
4550 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004551 }
4552
4553 if (evaluate)
4554 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 n1 = 0;
4556 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 n1 = get_tv_number(&var1);
4559 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 }
4561 if (range)
4562 {
4563 if (empty2)
4564 n2 = -1;
4565 else
4566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 n2 = get_tv_number(&var2);
4568 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 }
4570 }
4571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004574 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004575 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004576 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004577 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004578 case VAR_SPECIAL:
4579 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004580 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004581 break; /* not evaluating, skipping over subscript */
4582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 case VAR_NUMBER:
4584 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 len = (long)STRLEN(s);
4587 if (range)
4588 {
4589 /* The resulting variable is a substring. If the indexes
4590 * are out of range the result is empty. */
4591 if (n1 < 0)
4592 {
4593 n1 = len + n1;
4594 if (n1 < 0)
4595 n1 = 0;
4596 }
4597 if (n2 < 0)
4598 n2 = len + n2;
4599 else if (n2 >= len)
4600 n2 = len;
4601 if (n1 >= len || n2 < 0 || n1 > n2)
4602 s = NULL;
4603 else
4604 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4605 }
4606 else
4607 {
4608 /* The resulting variable is a string of a single
4609 * character. If the index is too big or negative the
4610 * result is empty. */
4611 if (n1 >= len || n1 < 0)
4612 s = NULL;
4613 else
4614 s = vim_strnsave(s + n1, 1);
4615 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 clear_tv(rettv);
4617 rettv->v_type = VAR_STRING;
4618 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 break;
4620
4621 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 if (n1 < 0)
4624 n1 = len + n1;
4625 if (!empty1 && (n1 < 0 || n1 >= len))
4626 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004627 /* For a range we allow invalid values and return an empty
4628 * list. A list index out of range is an error. */
4629 if (!range)
4630 {
4631 if (verbose)
4632 EMSGN(_(e_listidx), n1);
4633 return FAIL;
4634 }
4635 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 }
4637 if (range)
4638 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004639 list_T *l;
4640 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641
4642 if (n2 < 0)
4643 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004644 else if (n2 >= len)
4645 n2 = len - 1;
4646 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004647 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 l = list_alloc();
4649 if (l == NULL)
4650 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 n1 <= n2; ++n1)
4653 {
4654 if (list_append_tv(l, &item->li_tv) == FAIL)
4655 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004656 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 return FAIL;
4658 }
4659 item = item->li_next;
4660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004662 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004663 }
4664 else
4665 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004666 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(rettv);
4668 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 }
4670 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671
4672 case VAR_DICT:
4673 if (range)
4674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004675 if (verbose)
4676 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 if (len == -1)
4678 clear_tv(&var1);
4679 return FAIL;
4680 }
4681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004682 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683
4684 if (len == -1)
4685 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004686 key = get_tv_string_chk(&var1);
4687 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 clear_tv(&var1);
4690 return FAIL;
4691 }
4692 }
4693
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004694 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004696 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004697 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 if (len == -1)
4699 clear_tv(&var1);
4700 if (item == NULL)
4701 return FAIL;
4702
4703 copy_tv(&item->di_tv, &var1);
4704 clear_tv(rettv);
4705 *rettv = var1;
4706 }
4707 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 }
4709 }
4710
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 return OK;
4712}
4713
4714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 * Get an option value.
4716 * "arg" points to the '&' or '+' before the option name.
4717 * "arg" is advanced to character after the option name.
4718 * Return OK or FAIL.
4719 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004721get_option_tv(
4722 char_u **arg,
4723 typval_T *rettv, /* when NULL, only check if option exists */
4724 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 char_u *option_end;
4727 long numval;
4728 char_u *stringval;
4729 int opt_type;
4730 int c;
4731 int working = (**arg == '+'); /* has("+option") */
4732 int ret = OK;
4733 int opt_flags;
4734
4735 /*
4736 * Isolate the option name and find its value.
4737 */
4738 option_end = find_option_end(arg, &opt_flags);
4739 if (option_end == NULL)
4740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 EMSG2(_("E112: Option name missing: %s"), *arg);
4743 return FAIL;
4744 }
4745
4746 if (!evaluate)
4747 {
4748 *arg = option_end;
4749 return OK;
4750 }
4751
4752 c = *option_end;
4753 *option_end = NUL;
4754 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
4757 if (opt_type == -3) /* invalid name */
4758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 EMSG2(_("E113: Unknown option: %s"), *arg);
4761 ret = FAIL;
4762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 if (opt_type == -2) /* hidden string option */
4766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 rettv->v_type = VAR_STRING;
4768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 else if (opt_type == -1) /* hidden number option */
4771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 rettv->v_type = VAR_NUMBER;
4773 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775 else if (opt_type == 1) /* number option */
4776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 else /* string option */
4781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 rettv->v_type = VAR_STRING;
4783 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 }
4786 else if (working && (opt_type == -2 || opt_type == -1))
4787 ret = FAIL;
4788
4789 *option_end = c; /* put back for error messages */
4790 *arg = option_end;
4791
4792 return ret;
4793}
4794
4795/*
4796 * Allocate a variable for a string constant.
4797 * Return OK or FAIL.
4798 */
4799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004800get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802 char_u *p;
4803 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 int extra = 0;
4805
4806 /*
4807 * Find the end of the string, skipping backslashed characters.
4808 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004809 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 if (*p == '\\' && p[1] != NUL)
4812 {
4813 ++p;
4814 /* A "\<x>" form occupies at least 4 characters, and produces up
4815 * to 6 characters: reserve space for 2 extra */
4816 if (*p == '<')
4817 extra += 2;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820
4821 if (*p != '"')
4822 {
4823 EMSG2(_("E114: Missing quote: %s"), *arg);
4824 return FAIL;
4825 }
4826
4827 /* If only parsing, set *arg and return here */
4828 if (!evaluate)
4829 {
4830 *arg = p + 1;
4831 return OK;
4832 }
4833
4834 /*
4835 * Copy the string into allocated memory, handling backslashed
4836 * characters.
4837 */
4838 name = alloc((unsigned)(p - *arg + extra));
4839 if (name == NULL)
4840 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
Bram Moolenaar8c711452005-01-14 21:53:12 +00004844 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 if (*p == '\\')
4847 {
4848 switch (*++p)
4849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004850 case 'b': *name++ = BS; ++p; break;
4851 case 'e': *name++ = ESC; ++p; break;
4852 case 'f': *name++ = FF; ++p; break;
4853 case 'n': *name++ = NL; ++p; break;
4854 case 'r': *name++ = CAR; ++p; break;
4855 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857 case 'X': /* hex: "\x1", "\x12" */
4858 case 'x':
4859 case 'u': /* Unicode: "\u0023" */
4860 case 'U':
4861 if (vim_isxdigit(p[1]))
4862 {
4863 int n, nr;
4864 int c = toupper(*p);
4865
4866 if (c == 'X')
4867 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004868 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004870 else
4871 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 nr = 0;
4873 while (--n >= 0 && vim_isxdigit(p[1]))
4874 {
4875 ++p;
4876 nr = (nr << 4) + hex2nr(*p);
4877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004878 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879#ifdef FEAT_MBYTE
4880 /* For "\u" store the number according to
4881 * 'encoding'. */
4882 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else
4885#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 break;
4889
4890 /* octal: "\1", "\12", "\123" */
4891 case '0':
4892 case '1':
4893 case '2':
4894 case '3':
4895 case '4':
4896 case '5':
4897 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 case '7': *name = *p++ - '0';
4899 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 *name = (*name << 3) + *p++ - '0';
4902 if (*p >= '0' && *p <= '7')
4903 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004905 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 break;
4907
4908 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004909 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (extra != 0)
4911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 break;
4914 }
4915 /* FALLTHROUGH */
4916
Bram Moolenaar8c711452005-01-14 21:53:12 +00004917 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919 }
4920 }
4921 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004922 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004926 if (*p != NUL) /* just in case */
4927 ++p;
4928 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return OK;
4931}
4932
4933/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 * Return OK or FAIL.
4936 */
4937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004938get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004941 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004942 int reduce = 0;
4943
4944 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004946 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004947 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004952 break;
4953 ++reduce;
4954 ++p;
4955 }
4956 }
4957
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004959 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004961 return FAIL;
4962 }
4963
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004965 if (!evaluate)
4966 {
4967 *arg = p + 1;
4968 return OK;
4969 }
4970
4971 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004973 */
4974 str = alloc((unsigned)((p - *arg) - reduce));
4975 if (str == NULL)
4976 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 rettv->v_type = VAR_STRING;
4978 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004979
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004985 break;
4986 ++p;
4987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004989 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004991 *arg = p + 1;
4992
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004993 return OK;
4994}
4995
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004996/*
4997 * Return the function name of the partial.
4998 */
4999 char_u *
5000partial_name(partial_T *pt)
5001{
5002 if (pt->pt_name != NULL)
5003 return pt->pt_name;
5004 return pt->pt_func->uf_name;
5005}
5006
Bram Moolenaarddecc252016-04-06 22:59:37 +02005007 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005008partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005009{
5010 int i;
5011
5012 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005013 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005014 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005015 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005016 if (pt->pt_name != NULL)
5017 {
5018 func_unref(pt->pt_name);
5019 vim_free(pt->pt_name);
5020 }
5021 else
5022 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005023 vim_free(pt);
5024}
5025
5026/*
5027 * Unreference a closure: decrement the reference count and free it when it
5028 * becomes zero.
5029 */
5030 void
5031partial_unref(partial_T *pt)
5032{
5033 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005034 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005035}
5036
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005037static int tv_equal_recurse_limit;
5038
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005039 static int
5040func_equal(
5041 typval_T *tv1,
5042 typval_T *tv2,
5043 int ic) /* ignore case */
5044{
5045 char_u *s1, *s2;
5046 dict_T *d1, *d2;
5047 int a1, a2;
5048 int i;
5049
5050 /* empty and NULL function name considered the same */
5051 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005052 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005053 if (s1 != NULL && *s1 == NUL)
5054 s1 = NULL;
5055 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005056 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005057 if (s2 != NULL && *s2 == NUL)
5058 s2 = NULL;
5059 if (s1 == NULL || s2 == NULL)
5060 {
5061 if (s1 != s2)
5062 return FALSE;
5063 }
5064 else if (STRCMP(s1, s2) != 0)
5065 return FALSE;
5066
5067 /* empty dict and NULL dict is different */
5068 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5069 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5070 if (d1 == NULL || d2 == NULL)
5071 {
5072 if (d1 != d2)
5073 return FALSE;
5074 }
5075 else if (!dict_equal(d1, d2, ic, TRUE))
5076 return FALSE;
5077
5078 /* empty list and no list considered the same */
5079 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5080 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5081 if (a1 != a2)
5082 return FALSE;
5083 for (i = 0; i < a1; ++i)
5084 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5085 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5086 return FALSE;
5087
5088 return TRUE;
5089}
5090
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005092 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005093 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005095 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005096 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097tv_equal(
5098 typval_T *tv1,
5099 typval_T *tv2,
5100 int ic, /* ignore case */
5101 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005102{
5103 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005104 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005105 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005106 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005107
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005108 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005109 * recursiveness to a limit. We guess they are equal then.
5110 * A fixed limit has the problem of still taking an awful long time.
5111 * Reduce the limit every time running into it. That should work fine for
5112 * deeply linked structures that are not recursively linked and catch
5113 * recursiveness quickly. */
5114 if (!recursive)
5115 tv_equal_recurse_limit = 1000;
5116 if (recursive_cnt >= tv_equal_recurse_limit)
5117 {
5118 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005119 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005120 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005121
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005122 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5123 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005124 if ((tv1->v_type == VAR_FUNC
5125 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5126 && (tv2->v_type == VAR_FUNC
5127 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5128 {
5129 ++recursive_cnt;
5130 r = func_equal(tv1, tv2, ic);
5131 --recursive_cnt;
5132 return r;
5133 }
5134
5135 if (tv1->v_type != tv2->v_type)
5136 return FALSE;
5137
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005138 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005139 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005140 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005141 ++recursive_cnt;
5142 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5143 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005144 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005145
5146 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005147 ++recursive_cnt;
5148 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5149 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005150 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005151
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005152 case VAR_NUMBER:
5153 return tv1->vval.v_number == tv2->vval.v_number;
5154
5155 case VAR_STRING:
5156 s1 = get_tv_string_buf(tv1, buf1);
5157 s2 = get_tv_string_buf(tv2, buf2);
5158 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005159
5160 case VAR_SPECIAL:
5161 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005162
5163 case VAR_FLOAT:
5164#ifdef FEAT_FLOAT
5165 return tv1->vval.v_float == tv2->vval.v_float;
5166#endif
5167 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005169 return tv1->vval.v_job == tv2->vval.v_job;
5170#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005171 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005172#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005173 return tv1->vval.v_channel == tv2->vval.v_channel;
5174#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005175 case VAR_FUNC:
5176 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005177 case VAR_UNKNOWN:
5178 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005179 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005180
Bram Moolenaara03f2332016-02-06 18:09:59 +01005181 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5182 * does not equal anything, not even itself. */
5183 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005184}
5185
5186/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005187 * Return the next (unique) copy ID.
5188 * Used for serializing nested structures.
5189 */
5190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005191get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005192{
5193 current_copyID += COPYID_INC;
5194 return current_copyID;
5195}
5196
5197/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198 * Garbage collection for lists and dictionaries.
5199 *
5200 * We use reference counts to be able to free most items right away when they
5201 * are no longer used. But for composite items it's possible that it becomes
5202 * unused while the reference count is > 0: When there is a recursive
5203 * reference. Example:
5204 * :let l = [1, 2, 3]
5205 * :let d = {9: l}
5206 * :let l[1] = d
5207 *
5208 * Since this is quite unusual we handle this with garbage collection: every
5209 * once in a while find out which lists and dicts are not referenced from any
5210 * variable.
5211 *
5212 * Here is a good reference text about garbage collection (refers to Python
5213 * but it applies to all reference-counting mechanisms):
5214 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005215 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005216
5217/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005219 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005221 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005223garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005224{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005225 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005226 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227 buf_T *buf;
5228 win_T *wp;
5229 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005230 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005231 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005233 if (!testing)
5234 {
5235 /* Only do this once. */
5236 want_garbage_collect = FALSE;
5237 may_garbage_collect = FALSE;
5238 garbage_collect_at_exit = FALSE;
5239 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005240
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005241 /* We advance by two because we add one for items referenced through
5242 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005243 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005244
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245 /*
5246 * 1. Go through all accessible variables and mark all lists and dicts
5247 * with copyID.
5248 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005249
5250 /* Don't free variables in the previous_funccal list unless they are only
5251 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005252 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005254
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005255 /* script-local variables */
5256 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005258
5259 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005260 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005261 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5262 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005263
5264 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005265 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5267 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005268#ifdef FEAT_AUTOCMD
5269 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5271 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005272#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005273
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005274 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005275 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005276 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5277 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005278 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005279 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005280
5281 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005283
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005284 /* named functions (matters for closures) */
5285 abort = abort || set_ref_in_functions(copyID);
5286
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005287 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005288 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005289
Bram Moolenaard812df62008-11-09 12:46:09 +00005290 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005291 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005292
Bram Moolenaar1dced572012-04-05 16:54:08 +02005293#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005295#endif
5296
Bram Moolenaardb913952012-06-29 12:54:53 +02005297#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005299#endif
5300
5301#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005303#endif
5304
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005305#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005306 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005307 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005308#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005309#ifdef FEAT_NETBEANS_INTG
5310 abort = abort || set_ref_in_nb_channel(copyID);
5311#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005312
Bram Moolenaare3188e22016-05-31 21:13:04 +02005313#ifdef FEAT_TIMERS
5314 abort = abort || set_ref_in_timer(copyID);
5315#endif
5316
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005317#ifdef FEAT_QUICKFIX
5318 abort = abort || set_ref_in_quickfix(copyID);
5319#endif
5320
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005321#ifdef FEAT_TERMINAL
5322 abort = abort || set_ref_in_term(copyID);
5323#endif
5324
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005325 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005326 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005327 /*
5328 * 2. Free lists and dictionaries that are not referenced.
5329 */
5330 did_free = free_unref_items(copyID);
5331
5332 /*
5333 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005334 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005336 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005337 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005338 else if (p_verbose > 0)
5339 {
5340 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5341 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005342
5343 return did_free;
5344}
5345
5346/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005347 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005348 */
5349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005350free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005351{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005352 int did_free = FALSE;
5353
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005354 /* Let all "free" functions know that we are here. This means no
5355 * dictionaries, lists, channels or jobs are to be freed, because we will
5356 * do that here. */
5357 in_free_unref_items = TRUE;
5358
5359 /*
5360 * PASS 1: free the contents of the items. We don't free the items
5361 * themselves yet, so that it is possible to decrement refcount counters
5362 */
5363
Bram Moolenaarcd524592016-07-17 14:57:05 +02005364 /* Go through the list of dicts and free items without the copyID. */
5365 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005366
Bram Moolenaarda861d62016-07-17 15:46:27 +02005367 /* Go through the list of lists and free items without the copyID. */
5368 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369
5370#ifdef FEAT_JOB_CHANNEL
5371 /* Go through the list of jobs and free items without the copyID. This
5372 * must happen before doing channels, because jobs refer to channels, but
5373 * the reference from the channel to the job isn't tracked. */
5374 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5375
5376 /* Go through the list of channels and free items without the copyID. */
5377 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5378#endif
5379
5380 /*
5381 * PASS 2: free the items themselves.
5382 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005383 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005384 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005385
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005386#ifdef FEAT_JOB_CHANNEL
5387 /* Go through the list of jobs and free items without the copyID. This
5388 * must happen before doing channels, because jobs refer to channels, but
5389 * the reference from the channel to the job isn't tracked. */
5390 free_unused_jobs(copyID, COPYID_MASK);
5391
5392 /* Go through the list of channels and free items without the copyID. */
5393 free_unused_channels(copyID, COPYID_MASK);
5394#endif
5395
5396 in_free_unref_items = FALSE;
5397
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398 return did_free;
5399}
5400
5401/*
5402 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005403 * "list_stack" is used to add lists to be marked. Can be NULL.
5404 *
5405 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005408set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005409{
5410 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005411 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005412 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005413 hashtab_T *cur_ht;
5414 ht_stack_T *ht_stack = NULL;
5415 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005416
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005417 cur_ht = ht;
5418 for (;;)
5419 {
5420 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422 /* Mark each item in the hashtab. If the item contains a hashtab
5423 * it is added to ht_stack, if it contains a list it is added to
5424 * list_stack. */
5425 todo = (int)cur_ht->ht_used;
5426 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5427 if (!HASHITEM_EMPTY(hi))
5428 {
5429 --todo;
5430 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5431 &ht_stack, list_stack);
5432 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005434
5435 if (ht_stack == NULL)
5436 break;
5437
5438 /* take an item from the stack */
5439 cur_ht = ht_stack->ht;
5440 tempitem = ht_stack;
5441 ht_stack = ht_stack->prev;
5442 free(tempitem);
5443 }
5444
5445 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005446}
5447
5448/*
5449 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5451 *
5452 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005453 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005455set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005456{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005457 listitem_T *li;
5458 int abort = FALSE;
5459 list_T *cur_l;
5460 list_stack_T *list_stack = NULL;
5461 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005463 cur_l = l;
5464 for (;;)
5465 {
5466 if (!abort)
5467 /* Mark each item in the list. If the item contains a hashtab
5468 * it is added to ht_stack, if it contains a list it is added to
5469 * list_stack. */
5470 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5471 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5472 ht_stack, &list_stack);
5473 if (list_stack == NULL)
5474 break;
5475
5476 /* take an item from the stack */
5477 cur_l = list_stack->list;
5478 tempitem = list_stack;
5479 list_stack = list_stack->prev;
5480 free(tempitem);
5481 }
5482
5483 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005484}
5485
5486/*
5487 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 * "list_stack" is used to add lists to be marked. Can be NULL.
5489 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5490 *
5491 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005492 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005494set_ref_in_item(
5495 typval_T *tv,
5496 int copyID,
5497 ht_stack_T **ht_stack,
5498 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005499{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005500 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005501
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005502 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005503 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005504 dict_T *dd = tv->vval.v_dict;
5505
Bram Moolenaara03f2332016-02-06 18:09:59 +01005506 if (dd != NULL && dd->dv_copyID != copyID)
5507 {
5508 /* Didn't see this dict yet. */
5509 dd->dv_copyID = copyID;
5510 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005511 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005512 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5513 }
5514 else
5515 {
5516 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5517 if (newitem == NULL)
5518 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005519 else
5520 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005521 newitem->ht = &dd->dv_hashtab;
5522 newitem->prev = *ht_stack;
5523 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005524 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005525 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005526 }
5527 }
5528 else if (tv->v_type == VAR_LIST)
5529 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005530 list_T *ll = tv->vval.v_list;
5531
Bram Moolenaara03f2332016-02-06 18:09:59 +01005532 if (ll != NULL && ll->lv_copyID != copyID)
5533 {
5534 /* Didn't see this list yet. */
5535 ll->lv_copyID = copyID;
5536 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005537 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 abort = set_ref_in_list(ll, copyID, ht_stack);
5539 }
5540 else
5541 {
5542 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005543 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005544 if (newitem == NULL)
5545 abort = TRUE;
5546 else
5547 {
5548 newitem->list = ll;
5549 newitem->prev = *list_stack;
5550 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005551 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005552 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005553 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005554 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005555 else if (tv->v_type == VAR_FUNC)
5556 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005557 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005558 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005559 else if (tv->v_type == VAR_PARTIAL)
5560 {
5561 partial_T *pt = tv->vval.v_partial;
5562 int i;
5563
5564 /* A partial does not have a copyID, because it cannot contain itself.
5565 */
5566 if (pt != NULL)
5567 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005568 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005569
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005570 if (pt->pt_dict != NULL)
5571 {
5572 typval_T dtv;
5573
5574 dtv.v_type = VAR_DICT;
5575 dtv.vval.v_dict = pt->pt_dict;
5576 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5577 }
5578
5579 for (i = 0; i < pt->pt_argc; ++i)
5580 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5581 ht_stack, list_stack);
5582 }
5583 }
5584#ifdef FEAT_JOB_CHANNEL
5585 else if (tv->v_type == VAR_JOB)
5586 {
5587 job_T *job = tv->vval.v_job;
5588 typval_T dtv;
5589
5590 if (job != NULL && job->jv_copyID != copyID)
5591 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005592 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005593 if (job->jv_channel != NULL)
5594 {
5595 dtv.v_type = VAR_CHANNEL;
5596 dtv.vval.v_channel = job->jv_channel;
5597 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5598 }
5599 if (job->jv_exit_partial != NULL)
5600 {
5601 dtv.v_type = VAR_PARTIAL;
5602 dtv.vval.v_partial = job->jv_exit_partial;
5603 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5604 }
5605 }
5606 }
5607 else if (tv->v_type == VAR_CHANNEL)
5608 {
5609 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005610 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005611 typval_T dtv;
5612 jsonq_T *jq;
5613 cbq_T *cq;
5614
5615 if (ch != NULL && ch->ch_copyID != copyID)
5616 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005617 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005618 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005619 {
5620 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5621 jq = jq->jq_next)
5622 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5623 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5624 cq = cq->cq_next)
5625 if (cq->cq_partial != NULL)
5626 {
5627 dtv.v_type = VAR_PARTIAL;
5628 dtv.vval.v_partial = cq->cq_partial;
5629 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5630 }
5631 if (ch->ch_part[part].ch_partial != NULL)
5632 {
5633 dtv.v_type = VAR_PARTIAL;
5634 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5635 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5636 }
5637 }
5638 if (ch->ch_partial != NULL)
5639 {
5640 dtv.v_type = VAR_PARTIAL;
5641 dtv.vval.v_partial = ch->ch_partial;
5642 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5643 }
5644 if (ch->ch_close_partial != NULL)
5645 {
5646 dtv.v_type = VAR_PARTIAL;
5647 dtv.vval.v_partial = ch->ch_close_partial;
5648 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5649 }
5650 }
5651 }
5652#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005653 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005654}
5655
Bram Moolenaar17a13432016-01-24 14:22:10 +01005656 static char *
5657get_var_special_name(int nr)
5658{
5659 switch (nr)
5660 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005661 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005662 case VVAL_TRUE: return "v:true";
5663 case VVAL_NONE: return "v:none";
5664 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005665 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005666 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005667 return "42";
5668}
5669
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 * Return a string with the string representation of a variable.
5672 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005674 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005675 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5676 * stings as "string()", otherwise does not put quotes around strings, as
5677 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005678 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5679 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005680 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005682 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005683echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684 typval_T *tv,
5685 char_u **tofree,
5686 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005687 int copyID,
5688 int echo_style,
5689 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005690 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005692 static int recurse = 0;
5693 char_u *r = NULL;
5694
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005696 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005697 if (!did_echo_string_emsg)
5698 {
5699 /* Only give this message once for a recursive call to avoid
5700 * flooding the user with errors. And stop iterating over lists
5701 * and dicts. */
5702 did_echo_string_emsg = TRUE;
5703 EMSG(_("E724: variable nested too deep for displaying"));
5704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005705 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005706 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005707 }
5708 ++recurse;
5709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005710 switch (tv->v_type)
5711 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005712 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005713 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005714 {
5715 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005716 r = tv->vval.v_string;
5717 if (r == NULL)
5718 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005719 }
5720 else
5721 {
5722 *tofree = string_quote(tv->vval.v_string, FALSE);
5723 r = *tofree;
5724 }
5725 break;
5726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005728 if (echo_style)
5729 {
5730 *tofree = NULL;
5731 r = tv->vval.v_string;
5732 }
5733 else
5734 {
5735 *tofree = string_quote(tv->vval.v_string, TRUE);
5736 r = *tofree;
5737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005738 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005739
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005740 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005741 {
5742 partial_T *pt = tv->vval.v_partial;
5743 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005744 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005745 garray_T ga;
5746 int i;
5747 char_u *tf;
5748
5749 ga_init2(&ga, 1, 100);
5750 ga_concat(&ga, (char_u *)"function(");
5751 if (fname != NULL)
5752 {
5753 ga_concat(&ga, fname);
5754 vim_free(fname);
5755 }
5756 if (pt != NULL && pt->pt_argc > 0)
5757 {
5758 ga_concat(&ga, (char_u *)", [");
5759 for (i = 0; i < pt->pt_argc; ++i)
5760 {
5761 if (i > 0)
5762 ga_concat(&ga, (char_u *)", ");
5763 ga_concat(&ga,
5764 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5765 vim_free(tf);
5766 }
5767 ga_concat(&ga, (char_u *)"]");
5768 }
5769 if (pt != NULL && pt->pt_dict != NULL)
5770 {
5771 typval_T dtv;
5772
5773 ga_concat(&ga, (char_u *)", ");
5774 dtv.v_type = VAR_DICT;
5775 dtv.vval.v_dict = pt->pt_dict;
5776 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5777 vim_free(tf);
5778 }
5779 ga_concat(&ga, (char_u *)")");
5780
5781 *tofree = ga.ga_data;
5782 r = *tofree;
5783 break;
5784 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005785
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005787 if (tv->vval.v_list == NULL)
5788 {
5789 *tofree = NULL;
5790 r = NULL;
5791 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005792 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5793 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005794 {
5795 *tofree = NULL;
5796 r = (char_u *)"[...]";
5797 }
5798 else
5799 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005800 int old_copyID = tv->vval.v_list->lv_copyID;
5801
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005802 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005803 *tofree = list2string(tv, copyID, restore_copyID);
5804 if (restore_copyID)
5805 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005806 r = *tofree;
5807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005808 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005809
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005811 if (tv->vval.v_dict == NULL)
5812 {
5813 *tofree = NULL;
5814 r = NULL;
5815 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005816 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5817 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005818 {
5819 *tofree = NULL;
5820 r = (char_u *)"{...}";
5821 }
5822 else
5823 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005824 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005825 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005826 *tofree = dict2string(tv, copyID, restore_copyID);
5827 if (restore_copyID)
5828 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005829 r = *tofree;
5830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005833 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005834 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005835 *tofree = NULL;
5836 r = get_tv_string_buf(tv, numbuf);
5837 break;
5838
Bram Moolenaar835dc632016-02-07 14:27:38 +01005839 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005840 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005841 *tofree = NULL;
5842 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005843 if (composite_val)
5844 {
5845 *tofree = string_quote(r, FALSE);
5846 r = *tofree;
5847 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005849
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005850 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005852 *tofree = NULL;
5853 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5854 r = numbuf;
5855 break;
5856#endif
5857
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005858 case VAR_SPECIAL:
5859 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005860 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005861 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863
Bram Moolenaar8502c702014-06-17 12:51:16 +02005864 if (--recurse == 0)
5865 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005866 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867}
5868
5869/*
5870 * Return a string with the string representation of a variable.
5871 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5872 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005873 * Does not put quotes around strings, as ":echo" displays values.
5874 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5875 * May return NULL.
5876 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005877 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005878echo_string(
5879 typval_T *tv,
5880 char_u **tofree,
5881 char_u *numbuf,
5882 int copyID)
5883{
5884 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5885}
5886
5887/*
5888 * Return a string with the string representation of a variable.
5889 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5890 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005892 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005894 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005895tv2string(
5896 typval_T *tv,
5897 char_u **tofree,
5898 char_u *numbuf,
5899 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005901 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902}
5903
5904/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 * Return string "str" in ' quotes, doubling ' characters.
5906 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911{
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 char_u *p, *r, *s;
5914
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 len = (function ? 13 : 3);
5916 if (str != NULL)
5917 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005918 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005919 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 if (*p == '\'')
5921 ++len;
5922 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 s = r = alloc(len);
5924 if (r != NULL)
5925 {
5926 if (function)
5927 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 r += 10;
5930 }
5931 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 if (str != NULL)
5934 for (p = str; *p != NUL; )
5935 {
5936 if (*p == '\'')
5937 *r++ = '\'';
5938 MB_COPY_CHAR(p, r);
5939 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 if (function)
5942 *r++ = ')';
5943 *r++ = NUL;
5944 }
5945 return s;
5946}
5947
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005948#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005949/*
5950 * Convert the string "text" to a floating point number.
5951 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5952 * this always uses a decimal point.
5953 * Returns the length of the text that was consumed.
5954 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005955 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005956string2float(
5957 char_u *text,
5958 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005959{
5960 char *s = (char *)text;
5961 float_T f;
5962
Bram Moolenaar62473612017-01-08 19:25:40 +01005963 /* MS-Windows does not deal with "inf" and "nan" properly. */
5964 if (STRNICMP(text, "inf", 3) == 0)
5965 {
5966 *value = INFINITY;
5967 return 3;
5968 }
5969 if (STRNICMP(text, "-inf", 3) == 0)
5970 {
5971 *value = -INFINITY;
5972 return 4;
5973 }
5974 if (STRNICMP(text, "nan", 3) == 0)
5975 {
5976 *value = NAN;
5977 return 3;
5978 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005979 f = strtod(s, &s);
5980 *value = f;
5981 return (int)((char_u *)s - text);
5982}
5983#endif
5984
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 * Get the value of an environment variable.
5987 * "arg" is pointing to the '$'. It is advanced to after the name.
5988 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005989 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005992get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 char_u *string = NULL;
5995 int len;
5996 int cc;
5997 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005998 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
6000 ++*arg;
6001 name = *arg;
6002 len = get_env_len(arg);
6003 if (evaluate)
6004 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006005 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006006 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006007
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006008 cc = name[len];
6009 name[len] = NUL;
6010 /* first try vim_getenv(), fast for normal environment vars */
6011 string = vim_getenv(name, &mustfree);
6012 if (string != NULL && *string != NUL)
6013 {
6014 if (!mustfree)
6015 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006017 else
6018 {
6019 if (mustfree)
6020 vim_free(string);
6021
6022 /* next try expanding things like $VIM and ${HOME} */
6023 string = expand_env_save(name - 1);
6024 if (string != NULL && *string == '$')
6025 {
6026 vim_free(string);
6027 string = NULL;
6028 }
6029 }
6030 name[len] = cc;
6031
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006032 rettv->v_type = VAR_STRING;
6033 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035
6036 return OK;
6037}
6038
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006039
6040
6041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006043 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006045 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046var2fpos(
6047 typval_T *varp,
6048 int dollar_lnum, /* TRUE when $ is last line */
6049 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006051 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006053 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
Bram Moolenaara5525202006-03-02 22:52:09 +00006055 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006056 if (varp->v_type == VAR_LIST)
6057 {
6058 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006059 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006060 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006061 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006062
6063 l = varp->vval.v_list;
6064 if (l == NULL)
6065 return NULL;
6066
6067 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006068 pos.lnum = list_find_nr(l, 0L, &error);
6069 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006070 return NULL; /* invalid line number */
6071
6072 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006073 pos.col = list_find_nr(l, 1L, &error);
6074 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006075 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006076 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006077
6078 /* We accept "$" for the column number: last column. */
6079 li = list_find(l, 1L);
6080 if (li != NULL && li->li_tv.v_type == VAR_STRING
6081 && li->li_tv.vval.v_string != NULL
6082 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6083 pos.col = len + 1;
6084
Bram Moolenaara5525202006-03-02 22:52:09 +00006085 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006086 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006087 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006088 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006089
Bram Moolenaara5525202006-03-02 22:52:09 +00006090#ifdef FEAT_VIRTUALEDIT
6091 /* Get the virtual offset. Defaults to zero. */
6092 pos.coladd = list_find_nr(l, 2L, &error);
6093 if (error)
6094 pos.coladd = 0;
6095#endif
6096
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006097 return &pos;
6098 }
6099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006100 name = get_tv_string_chk(varp);
6101 if (name == NULL)
6102 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006103 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006105 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6106 {
6107 if (VIsual_active)
6108 return &VIsual;
6109 return &curwin->w_cursor;
6110 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006111 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006113 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6115 return NULL;
6116 return pp;
6117 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006118
6119#ifdef FEAT_VIRTUALEDIT
6120 pos.coladd = 0;
6121#endif
6122
Bram Moolenaar477933c2007-07-17 14:32:23 +00006123 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006124 {
6125 pos.col = 0;
6126 if (name[1] == '0') /* "w0": first visible line */
6127 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006128 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006129 /* In silent Ex mode topline is zero, but that's not a valid line
6130 * number; use one instead. */
6131 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006132 return &pos;
6133 }
6134 else if (name[1] == '$') /* "w$": last visible line */
6135 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006136 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006137 /* In silent Ex mode botline is zero, return zero then. */
6138 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006139 return &pos;
6140 }
6141 }
6142 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006144 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 pos.lnum = curbuf->b_ml.ml_line_count;
6147 pos.col = 0;
6148 }
6149 else
6150 {
6151 pos.lnum = curwin->w_cursor.lnum;
6152 pos.col = (colnr_T)STRLEN(ml_get_curline());
6153 }
6154 return &pos;
6155 }
6156 return NULL;
6157}
6158
6159/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006160 * Convert list in "arg" into a position and optional file number.
6161 * When "fnump" is NULL there is no file number, only 3 items.
6162 * Note that the column is passed on as-is, the caller may want to decrement
6163 * it to use 1 for the first column.
6164 * Return FAIL when conversion is not possible, doesn't check the position for
6165 * validity.
6166 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168list2fpos(
6169 typval_T *arg,
6170 pos_T *posp,
6171 int *fnump,
6172 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006173{
6174 list_T *l = arg->vval.v_list;
6175 long i = 0;
6176 long n;
6177
Bram Moolenaar493c1782014-05-28 14:34:46 +02006178 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6179 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006180 if (arg->v_type != VAR_LIST
6181 || l == NULL
6182 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006183 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006184 return FAIL;
6185
6186 if (fnump != NULL)
6187 {
6188 n = list_find_nr(l, i++, NULL); /* fnum */
6189 if (n < 0)
6190 return FAIL;
6191 if (n == 0)
6192 n = curbuf->b_fnum; /* current buffer */
6193 *fnump = n;
6194 }
6195
6196 n = list_find_nr(l, i++, NULL); /* lnum */
6197 if (n < 0)
6198 return FAIL;
6199 posp->lnum = n;
6200
6201 n = list_find_nr(l, i++, NULL); /* col */
6202 if (n < 0)
6203 return FAIL;
6204 posp->col = n;
6205
6206#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006207 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006208 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006209 posp->coladd = 0;
6210 else
6211 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006212#endif
6213
Bram Moolenaar493c1782014-05-28 14:34:46 +02006214 if (curswantp != NULL)
6215 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6216
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006217 return OK;
6218}
6219
6220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 * Get the length of an environment variable name.
6222 * Advance "arg" to the first character after the name.
6223 * Return 0 for error.
6224 */
6225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006226get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 char_u *p;
6229 int len;
6230
6231 for (p = *arg; vim_isIDc(*p); ++p)
6232 ;
6233 if (p == *arg) /* no name found */
6234 return 0;
6235
6236 len = (int)(p - *arg);
6237 *arg = p;
6238 return len;
6239}
6240
6241/*
6242 * Get the length of the name of a function or internal variable.
6243 * "arg" is advanced to the first non-white character after the name.
6244 * Return 0 if something is wrong.
6245 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006247get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 char_u *p;
6250 int len;
6251
6252 /* Find the end of the name. */
6253 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006254 {
6255 if (*p == ':')
6256 {
6257 /* "s:" is start of "s:var", but "n:" is not and can be used in
6258 * slice "[n:]". Also "xx:" is not a namespace. */
6259 len = (int)(p - *arg);
6260 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6261 || len > 1)
6262 break;
6263 }
6264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (p == *arg) /* no name found */
6266 return 0;
6267
6268 len = (int)(p - *arg);
6269 *arg = skipwhite(p);
6270
6271 return len;
6272}
6273
6274/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006275 * Get the length of the name of a variable or function.
6276 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006278 * Return -1 if curly braces expansion failed.
6279 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 * If the name contains 'magic' {}'s, expand them and return the
6281 * expanded name in an allocated string via 'alias' - caller must free.
6282 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006284get_name_len(
6285 char_u **arg,
6286 char_u **alias,
6287 int evaluate,
6288 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 char_u *p;
6292 char_u *expr_start;
6293 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 *alias = NULL; /* default to no alias */
6296
6297 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6298 && (*arg)[2] == (int)KE_SNR)
6299 {
6300 /* hard coded <SNR>, already translated */
6301 *arg += 3;
6302 return get_id_len(arg) + 3;
6303 }
6304 len = eval_fname_script(*arg);
6305 if (len > 0)
6306 {
6307 /* literal "<SID>", "s:" or "<SNR>" */
6308 *arg += len;
6309 }
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006312 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006314 p = find_name_end(*arg, &expr_start, &expr_end,
6315 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 if (expr_start != NULL)
6317 {
6318 char_u *temp_string;
6319
6320 if (!evaluate)
6321 {
6322 len += (int)(p - *arg);
6323 *arg = skipwhite(p);
6324 return len;
6325 }
6326
6327 /*
6328 * Include any <SID> etc in the expanded string:
6329 * Thus the -len here.
6330 */
6331 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6332 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006333 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 *alias = temp_string;
6335 *arg = skipwhite(p);
6336 return (int)STRLEN(temp_string);
6337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006340 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 EMSG2(_(e_invexpr2), *arg);
6342
6343 return len;
6344}
6345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006346/*
6347 * Find the end of a variable or function name, taking care of magic braces.
6348 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6349 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006350 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006351 * Return a pointer to just after the name. Equal to "arg" if there is no
6352 * valid name.
6353 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355find_name_end(
6356 char_u *arg,
6357 char_u **expr_start,
6358 char_u **expr_end,
6359 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006361 int mb_nest = 0;
6362 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006364 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006366 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006368 *expr_start = NULL;
6369 *expr_end = NULL;
6370 }
6371
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006372 /* Quick check for valid starting character. */
6373 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6374 return arg;
6375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 for (p = arg; *p != NUL
6377 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006379 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006380 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006381 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006382 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006383 if (*p == '\'')
6384 {
6385 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006386 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006387 ;
6388 if (*p == NUL)
6389 break;
6390 }
6391 else if (*p == '"')
6392 {
6393 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006394 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006395 if (*p == '\\' && p[1] != NUL)
6396 ++p;
6397 if (*p == NUL)
6398 break;
6399 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006400 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6401 {
6402 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006403 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006404 len = (int)(p - arg);
6405 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006406 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006407 break;
6408 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006409
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006412 if (*p == '[')
6413 ++br_nest;
6414 else if (*p == ']')
6415 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 if (*p == '{')
6421 {
6422 mb_nest++;
6423 if (expr_start != NULL && *expr_start == NULL)
6424 *expr_start = p;
6425 }
6426 else if (*p == '}')
6427 {
6428 mb_nest--;
6429 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6430 *expr_end = p;
6431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 }
6434
6435 return p;
6436}
6437
6438/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006439 * Expands out the 'magic' {}'s in a variable/function name.
6440 * Note that this can call itself recursively, to deal with
6441 * constructs like foo{bar}{baz}{bam}
6442 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6443 * "in_start" ^
6444 * "expr_start" ^
6445 * "expr_end" ^
6446 * "in_end" ^
6447 *
6448 * Returns a new allocated string, which the caller must free.
6449 * Returns NULL for failure.
6450 */
6451 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006452make_expanded_name(
6453 char_u *in_start,
6454 char_u *expr_start,
6455 char_u *expr_end,
6456 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006457{
6458 char_u c1;
6459 char_u *retval = NULL;
6460 char_u *temp_result;
6461 char_u *nextcmd = NULL;
6462
6463 if (expr_end == NULL || in_end == NULL)
6464 return NULL;
6465 *expr_start = NUL;
6466 *expr_end = NUL;
6467 c1 = *in_end;
6468 *in_end = NUL;
6469
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006470 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006471 if (temp_result != NULL && nextcmd == NULL)
6472 {
6473 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6474 + (in_end - expr_end) + 1));
6475 if (retval != NULL)
6476 {
6477 STRCPY(retval, in_start);
6478 STRCAT(retval, temp_result);
6479 STRCAT(retval, expr_end + 1);
6480 }
6481 }
6482 vim_free(temp_result);
6483
6484 *in_end = c1; /* put char back for error messages */
6485 *expr_start = '{';
6486 *expr_end = '}';
6487
6488 if (retval != NULL)
6489 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006490 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006491 if (expr_start != NULL)
6492 {
6493 /* Further expansion! */
6494 temp_result = make_expanded_name(retval, expr_start,
6495 expr_end, temp_result);
6496 vim_free(retval);
6497 retval = temp_result;
6498 }
6499 }
6500
6501 return retval;
6502}
6503
6504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006509eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006511 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6512}
6513
6514/*
6515 * Return TRUE if character "c" can be used as the first character in a
6516 * variable or function name (excluding '{' and '}').
6517 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006520{
6521 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522}
6523
6524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 * Set number v: variable to "val".
6526 */
6527 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006528set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531}
6532
6533/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006534 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006536 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
6541
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006542/*
6543 * Get string v: variable value. Uses a static buffer, can only be used once.
6544 */
6545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006547{
6548 return get_tv_string(&vimvars[idx].vv_tv);
6549}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006552 * Get List v: variable value. Caller must take care of reference count when
6553 * needed.
6554 */
6555 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006556get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006557{
6558 return vimvars[idx].vv_list;
6559}
6560
6561/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006562 * Set v:char to character "c".
6563 */
6564 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006566{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006567 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006568
6569#ifdef FEAT_MBYTE
6570 if (has_mbyte)
6571 buf[(*mb_char2bytes)(c, buf)] = NUL;
6572 else
6573#endif
6574 {
6575 buf[0] = c;
6576 buf[1] = NUL;
6577 }
6578 set_vim_var_string(VV_CHAR, buf, -1);
6579}
6580
6581/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006582 * Set v:count to "count" and v:count1 to "count1".
6583 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 */
6585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006586set_vcount(
6587 long count,
6588 long count1,
6589 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006591 if (set_prevcount)
6592 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 vimvars[VV_COUNT].vv_nr = count;
6594 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595}
6596
6597/*
6598 * Set string v: variable to a copy of "val".
6599 */
6600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601set_vim_var_string(
6602 int idx,
6603 char_u *val,
6604 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
Bram Moolenaara542c682016-01-31 16:28:04 +01006606 clear_tv(&vimvars[idx].vv_di.di_tv);
6607 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006613 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
6615
6616/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006617 * Set List v: variable to "val".
6618 */
6619 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006621{
Bram Moolenaara542c682016-01-31 16:28:04 +01006622 clear_tv(&vimvars[idx].vv_di.di_tv);
6623 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006624 vimvars[idx].vv_list = val;
6625 if (val != NULL)
6626 ++val->lv_refcount;
6627}
6628
6629/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006630 * Set Dictionary v: variable to "val".
6631 */
6632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006633set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006634{
6635 int todo;
6636 hashitem_T *hi;
6637
Bram Moolenaara542c682016-01-31 16:28:04 +01006638 clear_tv(&vimvars[idx].vv_di.di_tv);
6639 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006640 vimvars[idx].vv_dict = val;
6641 if (val != NULL)
6642 {
6643 ++val->dv_refcount;
6644
6645 /* Set readonly */
6646 todo = (int)val->dv_hashtab.ht_used;
6647 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6648 {
6649 if (HASHITEM_EMPTY(hi))
6650 continue;
6651 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006652 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006653 }
6654 }
6655}
6656
6657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 * Set v:register if needed.
6659 */
6660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 char_u regname;
6664
6665 if (c == 0 || c == ' ')
6666 regname = '"';
6667 else
6668 regname = c;
6669 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006670 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 set_vim_var_string(VV_REG, &regname, 1);
6672}
6673
6674/*
6675 * Get or set v:exception. If "oldval" == NULL, return the current value.
6676 * Otherwise, restore the value to "oldval" and return NULL.
6677 * Must always be called in pairs to save and restore v:exception! Does not
6678 * take care of memory allocations.
6679 */
6680 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006681v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006684 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
Bram Moolenaare9a41262005-01-15 22:18:47 +00006686 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 return NULL;
6688}
6689
6690/*
6691 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6692 * Otherwise, restore the value to "oldval" and return NULL.
6693 * Must always be called in pairs to save and restore v:throwpoint! Does not
6694 * take care of memory allocations.
6695 */
6696 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006697v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 return NULL;
6704}
6705
6706#if defined(FEAT_AUTOCMD) || defined(PROTO)
6707/*
6708 * Set v:cmdarg.
6709 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6710 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6711 * Must always be called in pairs!
6712 */
6713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 char_u *oldval;
6717 char_u *newval;
6718 unsigned len;
6719
Bram Moolenaare9a41262005-01-15 22:18:47 +00006720 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006721 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006723 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006724 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006725 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006728 if (eap->force_bin == FORCE_BIN)
6729 len = 6;
6730 else if (eap->force_bin == FORCE_NOBIN)
6731 len = 8;
6732 else
6733 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006734
6735 if (eap->read_edit)
6736 len += 7;
6737
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006738 if (eap->force_ff != 0)
6739 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6740# ifdef FEAT_MBYTE
6741 if (eap->force_enc != 0)
6742 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006743 if (eap->bad_char != 0)
6744 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006745# endif
6746
6747 newval = alloc(len + 1);
6748 if (newval == NULL)
6749 return NULL;
6750
6751 if (eap->force_bin == FORCE_BIN)
6752 sprintf((char *)newval, " ++bin");
6753 else if (eap->force_bin == FORCE_NOBIN)
6754 sprintf((char *)newval, " ++nobin");
6755 else
6756 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006757
6758 if (eap->read_edit)
6759 STRCAT(newval, " ++edit");
6760
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006761 if (eap->force_ff != 0)
6762 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6763 eap->cmd + eap->force_ff);
6764# ifdef FEAT_MBYTE
6765 if (eap->force_enc != 0)
6766 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6767 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006768 if (eap->bad_char == BAD_KEEP)
6769 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6770 else if (eap->bad_char == BAD_DROP)
6771 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6772 else if (eap->bad_char != 0)
6773 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006774# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006775 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006776 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777}
6778#endif
6779
6780/*
6781 * Get the value of internal variable "name".
6782 * Return OK or FAIL.
6783 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006785get_var_tv(
6786 char_u *name,
6787 int len, /* length of "name" */
6788 typval_T *rettv, /* NULL when only checking existence */
6789 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6790 int verbose, /* may give error message */
6791 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006795 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798 /* truncate the name, so that we can use strcmp() */
6799 cc = name[len];
6800 name[len] = NUL;
6801
6802 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 * Check for user-defined variables.
6804 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006805 v = find_var(name, NULL, no_autoload);
6806 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006808 tv = &v->di_tv;
6809 if (dip != NULL)
6810 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006815 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006816 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 ret = FAIL;
6818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006819 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006820 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 name[len] = cc;
6823
6824 return ret;
6825}
6826
6827/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006828 * Check if variable "name[len]" is a local variable or an argument.
6829 * If so, "*eval_lavars_used" is set to TRUE.
6830 */
6831 static void
6832check_vars(char_u *name, int len)
6833{
6834 int cc;
6835 char_u *varname;
6836 hashtab_T *ht;
6837
6838 if (eval_lavars_used == NULL)
6839 return;
6840
6841 /* truncate the name, so that we can use strcmp() */
6842 cc = name[len];
6843 name[len] = NUL;
6844
6845 ht = find_var_ht(name, &varname);
6846 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6847 {
6848 if (find_var(name, NULL, TRUE) != NULL)
6849 *eval_lavars_used = TRUE;
6850 }
6851
6852 name[len] = cc;
6853}
6854
6855/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006856 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6857 * Also handle function call with Funcref variable: func(expr)
6858 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6859 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006861handle_subscript(
6862 char_u **arg,
6863 typval_T *rettv,
6864 int evaluate, /* do more than finding the end */
6865 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006866{
6867 int ret = OK;
6868 dict_T *selfdict = NULL;
6869 char_u *s;
6870 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006871 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006872
6873 while (ret == OK
6874 && (**arg == '['
6875 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006876 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6877 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006878 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006879 {
6880 if (**arg == '(')
6881 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006882 partial_T *pt = NULL;
6883
Bram Moolenaard9fba312005-06-26 22:34:35 +00006884 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006885 if (evaluate)
6886 {
6887 functv = *rettv;
6888 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006889
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006890 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006891 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006892 {
6893 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006894 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006895 }
6896 else
6897 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006898 }
6899 else
6900 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006901 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006902 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006903 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006904
6905 /* Clear the funcref afterwards, so that deleting it while
6906 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006907 if (evaluate)
6908 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006909
6910 /* Stop the expression evaluation when immediately aborting on
6911 * error, or when an interrupt occurred or an exception was thrown
6912 * but not caught. */
6913 if (aborting())
6914 {
6915 if (ret == OK)
6916 clear_tv(rettv);
6917 ret = FAIL;
6918 }
6919 dict_unref(selfdict);
6920 selfdict = NULL;
6921 }
6922 else /* **arg == '[' || **arg == '.' */
6923 {
6924 dict_unref(selfdict);
6925 if (rettv->v_type == VAR_DICT)
6926 {
6927 selfdict = rettv->vval.v_dict;
6928 if (selfdict != NULL)
6929 ++selfdict->dv_refcount;
6930 }
6931 else
6932 selfdict = NULL;
6933 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6934 {
6935 clear_tv(rettv);
6936 ret = FAIL;
6937 }
6938 }
6939 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006940
Bram Moolenaar1d429612016-05-24 15:44:17 +02006941 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6942 * Don't do this when "Func" is already a partial that was bound
6943 * explicitly (pt_auto is FALSE). */
6944 if (selfdict != NULL
6945 && (rettv->v_type == VAR_FUNC
6946 || (rettv->v_type == VAR_PARTIAL
6947 && (rettv->vval.v_partial->pt_auto
6948 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006949 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006951 dict_unref(selfdict);
6952 return ret;
6953}
6954
6955/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006956 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006957 * value).
6958 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006959 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006961{
Bram Moolenaar33570922005-01-25 22:26:29 +00006962 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006963}
6964
6965/*
6966 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 * The string "s" must have been allocated, it is consumed.
6968 * Return NULL for out of memory, the variable otherwise.
6969 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006975 rettv = alloc_tv();
6976 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006978 rettv->v_type = VAR_STRING;
6979 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981 else
6982 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006983 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984}
6985
6986/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006987 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006990free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 if (varp != NULL)
6993 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006994 switch (varp->v_type)
6995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006996 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006997 func_unref(varp->vval.v_string);
6998 /*FALLTHROUGH*/
6999 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007000 vim_free(varp->vval.v_string);
7001 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007002 case VAR_PARTIAL:
7003 partial_unref(varp->vval.v_partial);
7004 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007005 case VAR_LIST:
7006 list_unref(varp->vval.v_list);
7007 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007008 case VAR_DICT:
7009 dict_unref(varp->vval.v_dict);
7010 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007011 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007012#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007013 job_unref(varp->vval.v_job);
7014 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007015#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007016 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007017#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007018 channel_unref(varp->vval.v_channel);
7019 break;
7020#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007021 case VAR_NUMBER:
7022 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007023 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007024 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007025 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 vim_free(varp);
7028 }
7029}
7030
7031/*
7032 * Free the memory for a variable value and set the value to NULL or 0.
7033 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007035clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 if (varp != NULL)
7038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007039 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007042 func_unref(varp->vval.v_string);
7043 /*FALLTHROUGH*/
7044 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007045 vim_free(varp->vval.v_string);
7046 varp->vval.v_string = NULL;
7047 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007048 case VAR_PARTIAL:
7049 partial_unref(varp->vval.v_partial);
7050 varp->vval.v_partial = NULL;
7051 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007052 case VAR_LIST:
7053 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007055 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056 case VAR_DICT:
7057 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007060 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007061 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007062 varp->vval.v_number = 0;
7063 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007064 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007065#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007066 varp->vval.v_float = 0.0;
7067 break;
7068#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007069 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007070#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007071 job_unref(varp->vval.v_job);
7072 varp->vval.v_job = NULL;
7073#endif
7074 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007075 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007076#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007077 channel_unref(varp->vval.v_channel);
7078 varp->vval.v_channel = NULL;
7079#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007080 case VAR_UNKNOWN:
7081 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085}
7086
7087/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007088 * Set the value of a variable to NULL without freeing items.
7089 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007091init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007092{
7093 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007094 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007095}
7096
7097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 * Get the number value of a variable.
7099 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007100 * For incompatible types, return 0.
7101 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7102 * caller of incompatible types: it sets *denote to TRUE if "denote"
7103 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007105 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007108 int error = FALSE;
7109
7110 return get_tv_number_chk(varp, &error); /* return 0L on error */
7111}
7112
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007113 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007114get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007115{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007116 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007120 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007121 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007122 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007123#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007124 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007125 break;
7126#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007128 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007129 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007130 break;
7131 case VAR_STRING:
7132 if (varp->vval.v_string != NULL)
7133 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007134 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007135 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007136 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007137 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007138 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007139 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007140 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007141 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007142 case VAR_SPECIAL:
7143 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7144 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007145 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007147 EMSG(_("E910: Using a Job as a Number"));
7148 break;
7149#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007150 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007151#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007152 EMSG(_("E913: Using a Channel as a Number"));
7153 break;
7154#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007156 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007157 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007159 if (denote == NULL) /* useful for values that must be unsigned */
7160 n = -1;
7161 else
7162 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164}
7165
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007166#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007168get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007169{
7170 switch (varp->v_type)
7171 {
7172 case VAR_NUMBER:
7173 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007174 case VAR_FLOAT:
7175 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007176 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007177 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007178 EMSG(_("E891: Using a Funcref as a Float"));
7179 break;
7180 case VAR_STRING:
7181 EMSG(_("E892: Using a String as a Float"));
7182 break;
7183 case VAR_LIST:
7184 EMSG(_("E893: Using a List as a Float"));
7185 break;
7186 case VAR_DICT:
7187 EMSG(_("E894: Using a Dictionary as a Float"));
7188 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007189 case VAR_SPECIAL:
7190 EMSG(_("E907: Using a special value as a Float"));
7191 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007192 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007193# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007194 EMSG(_("E911: Using a Job as a Float"));
7195 break;
7196# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007197 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007198# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007199 EMSG(_("E914: Using a Channel as a Float"));
7200 break;
7201# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007202 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007203 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007204 break;
7205 }
7206 return 0;
7207}
7208#endif
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 * Get the string value of a variable.
7212 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007213 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7214 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 * If the String variable has never been set, return an empty string.
7216 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007217 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7218 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007220 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 static char_u mybuf[NUMBUFLEN];
7224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226}
7227
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007228 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007229get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007231 char_u *res = get_tv_string_buf_chk(varp, buf);
7232
7233 return res != NULL ? res : (char_u *)"";
7234}
7235
Bram Moolenaar7d647822014-04-05 21:28:56 +02007236/*
7237 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7238 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007239 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007240get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007241{
7242 static char_u mybuf[NUMBUFLEN];
7243
7244 return get_tv_string_buf_chk(varp, mybuf);
7245}
7246
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007247 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007249{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007250 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007252 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007253 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7254 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007255 return buf;
7256 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007257 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007258 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007259 break;
7260 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007261 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 break;
7263 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007264 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007265 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007266 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007267#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007268 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007269 break;
7270#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007271 case VAR_STRING:
7272 if (varp->vval.v_string != NULL)
7273 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007274 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007275 case VAR_SPECIAL:
7276 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7277 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007278 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007280 {
7281 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007282 char *status;
7283
7284 if (job == NULL)
7285 return (char_u *)"no process";
7286 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007287 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007288 : "run";
7289# ifdef UNIX
7290 vim_snprintf((char *)buf, NUMBUFLEN,
7291 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007292# elif defined(WIN32)
7293 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007294 "process %ld %s",
7295 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007296 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007297# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007298 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007299 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7300# endif
7301 return buf;
7302 }
7303#endif
7304 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007305 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007306#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007307 {
7308 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007309 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007310
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007311 if (channel == NULL)
7312 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7313 else
7314 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007315 "channel %d %s", channel->ch_id, status);
7316 return buf;
7317 }
7318#endif
7319 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007320 case VAR_UNKNOWN:
7321 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
7327/*
7328 * Find variable "name" in the list of variables.
7329 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007330 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007331 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007334 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007339 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
Bram Moolenaara7043832005-01-21 11:56:39 +00007341 ht = find_var_ht(name, &varname);
7342 if (htp != NULL)
7343 *htp = ht;
7344 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007346 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7347 if (ret != NULL)
7348 return ret;
7349
7350 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007351 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352}
7353
7354/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007355 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007356 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007359find_var_in_ht(
7360 hashtab_T *ht,
7361 int htname,
7362 char_u *varname,
7363 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007364{
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 hashitem_T *hi;
7366
7367 if (*varname == NUL)
7368 {
7369 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007370 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007372 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 case 'g': return &globvars_var;
7374 case 'v': return &vimvars_var;
7375 case 'b': return &curbuf->b_bufvar;
7376 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007377 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007378 case 'l': return get_funccal_local_var();
7379 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 }
7381 return NULL;
7382 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007383
7384 hi = hash_find(ht, varname);
7385 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007386 {
7387 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007388 * worked find the variable again. Don't auto-load a script if it was
7389 * loaded already, otherwise it would be loaded every time when
7390 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007391 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007392 {
7393 /* Note: script_autoload() may make "hi" invalid. It must either
7394 * be obtained again or not used. */
7395 if (!script_autoload(varname, FALSE) || aborting())
7396 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007397 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007398 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007399 if (HASHITEM_EMPTY(hi))
7400 return NULL;
7401 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007403}
7404
7405/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007407 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007408 * Set "varname" to the start of name without ':'.
7409 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007410 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007413 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007414 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007415
Bram Moolenaar73627d02015-08-11 15:46:09 +02007416 if (name[0] == NUL)
7417 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 if (name[1] != ':')
7419 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007420 /* The name must not start with a colon or #. */
7421 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 return NULL;
7423 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007424
7425 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007426 hi = hash_find(&compat_hashtab, name);
7427 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007428 return &compat_hashtab;
7429
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007430 ht = get_funccal_local_ht();
7431 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007433 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 }
7435 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007436 if (*name == 'g') /* global variable */
7437 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007438 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7439 */
7440 if (vim_strchr(name + 2, ':') != NULL
7441 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007442 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007444 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007446 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007447 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007448 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 if (*name == 'v') /* v: variable */
7450 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007451 if (*name == 'a') /* a: function argument */
7452 return get_funccal_args_ht();
7453 if (*name == 'l') /* l: local function variable */
7454 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (*name == 's' /* script variable */
7456 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7457 return &SCRIPT_VARS(current_SID);
7458 return NULL;
7459}
7460
7461/*
7462 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007463 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 * Returns NULL when it doesn't exist.
7465 */
7466 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007467get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
Bram Moolenaar33570922005-01-25 22:26:29 +00007469 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007471 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 if (v == NULL)
7473 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475}
7476
7477/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 * sourcing this script and when executing functions defined in the script.
7480 */
7481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007482new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
Bram Moolenaara7043832005-01-21 11:56:39 +00007484 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 hashtab_T *ht;
7486 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7489 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007490 /* Re-allocating ga_data means that an ht_array pointing to
7491 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007493 for (i = 1; i <= ga_scripts.ga_len; ++i)
7494 {
7495 ht = &SCRIPT_VARS(i);
7496 if (ht->ht_mask == HT_INIT_SIZE - 1)
7497 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007498 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007500 }
7501
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 while (ga_scripts.ga_len < id)
7503 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007504 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007505 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007506 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 }
7510}
7511
7512/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7514 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 */
7516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007517init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007520 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007521 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007522 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007523 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 dict_var->di_tv.vval.v_dict = dict;
7525 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007526 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7528 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529}
7530
7531/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007532 * Unreference a dictionary initialized by init_var_dict().
7533 */
7534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007536{
7537 /* Now the dict needs to be freed if no one else is using it, go back to
7538 * normal reference counting. */
7539 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7540 dict_unref(dict);
7541}
7542
7543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 * Frees all allocated variables and the value they contain.
7546 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 */
7548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007550{
7551 vars_clear_ext(ht, TRUE);
7552}
7553
7554/*
7555 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7556 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007557 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007558vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaara7043832005-01-21 11:56:39 +00007560 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 hashitem_T *hi;
7562 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007565 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007566 for (hi = ht->ht_array; todo > 0; ++hi)
7567 {
7568 if (!HASHITEM_EMPTY(hi))
7569 {
7570 --todo;
7571
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007573 * ht_array might change then. hash_clear() takes care of it
7574 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 v = HI2DI(hi);
7576 if (free_val)
7577 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007578 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007580 }
7581 }
7582 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007583 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584}
7585
Bram Moolenaara7043832005-01-21 11:56:39 +00007586/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 * Delete a variable from hashtab "ht" at item "hi".
7588 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007591delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007594
7595 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 clear_tv(&di->di_tv);
7597 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600/*
7601 * List the value of one internal variable.
7602 */
7603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007606 char_u *tofree;
7607 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007608 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007610 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007612 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007613 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614}
7615
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617list_one_var_a(
7618 char_u *prefix,
7619 char_u *name,
7620 int type,
7621 char_u *string,
7622 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
Bram Moolenaar31859182007-08-14 20:41:13 +00007624 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7625 msg_start();
7626 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (name != NULL) /* "a:" vars don't have a name stored */
7628 msg_puts(name);
7629 msg_putchar(' ');
7630 msg_advance(22);
7631 if (type == VAR_NUMBER)
7632 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007633 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 msg_putchar('*');
7635 else if (type == VAR_LIST)
7636 {
7637 msg_putchar('[');
7638 if (*string == '[')
7639 ++string;
7640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 else if (type == VAR_DICT)
7642 {
7643 msg_putchar('{');
7644 if (*string == '{')
7645 ++string;
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 else
7648 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007652 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007653 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007654 if (*first)
7655 {
7656 msg_clr_eos();
7657 *first = FALSE;
7658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659}
7660
7661/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * If the variable already exists, the value is updated.
7664 * Otherwise the variable is created.
7665 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667set_var(
7668 char_u *name,
7669 typval_T *tv,
7670 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007676 ht = find_var_ht(name, &varname);
7677 if (ht == NULL || *varname == NUL)
7678 {
7679 EMSG2(_(e_illvar), name);
7680 return;
7681 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007682 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007683
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007684 /* Search in parent scope which is possible to reference from lambda */
7685 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007686 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007687
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007688 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7689 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007690 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007691
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007695 if (var_check_ro(v->di_flags, name, FALSE)
7696 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007698
7699 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007700 * Handle setting internal v: variables separately where needed to
7701 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 */
7703 if (ht == &vimvarht)
7704 {
7705 if (v->di_tv.v_type == VAR_STRING)
7706 {
7707 vim_free(v->di_tv.vval.v_string);
7708 if (copy || tv->v_type != VAR_STRING)
7709 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7710 else
7711 {
7712 /* Take over the string to avoid an extra alloc/free. */
7713 v->di_tv.vval.v_string = tv->vval.v_string;
7714 tv->vval.v_string = NULL;
7715 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007716 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007718 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007719 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007720 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007721 if (STRCMP(varname, "searchforward") == 0)
7722 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007723#ifdef FEAT_SEARCH_EXTRA
7724 else if (STRCMP(varname, "hlsearch") == 0)
7725 {
7726 no_hlsearch = !v->di_tv.vval.v_number;
7727 redraw_all_later(SOME_VALID);
7728 }
7729#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007730 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007731 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007732 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007733 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 }
7735
7736 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
7738 else /* add a new variable */
7739 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007740 /* Can't add "v:" variable. */
7741 if (ht == &vimvarht)
7742 {
7743 EMSG2(_(e_illvar), name);
7744 return;
7745 }
7746
Bram Moolenaar92124a32005-06-17 22:03:40 +00007747 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007748 if (!valid_varname(varname))
7749 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007750
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007751 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7752 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007753 if (v == NULL)
7754 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007758 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 return;
7760 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007761 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007763
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007764 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007765 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007766 else
7767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007769 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772}
7773
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007775 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 * Also give an error message.
7777 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007779var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780{
7781 if (flags & DI_FLAGS_RO)
7782 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007783 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 return TRUE;
7785 }
7786 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7787 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007788 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 return TRUE;
7790 }
7791 return FALSE;
7792}
7793
7794/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007795 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7796 * Also give an error message.
7797 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007799var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007800{
7801 if (flags & DI_FLAGS_FIX)
7802 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007803 EMSG2(_("E795: Cannot delete variable %s"),
7804 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007805 return TRUE;
7806 }
7807 return FALSE;
7808}
7809
7810/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007811 * Check if a funcref is assigned to a valid variable name.
7812 * Return TRUE and give an error if not.
7813 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007815var_check_func_name(
7816 char_u *name, /* points to start of variable name */
7817 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007818{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007819 /* Allow for w: b: s: and t:. */
7820 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007821 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7822 ? name[2] : name[0]))
7823 {
7824 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7825 name);
7826 return TRUE;
7827 }
7828 /* Don't allow hiding a function. When "v" is not NULL we might be
7829 * assigning another function to the same var, the type is checked
7830 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007831 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007832 {
7833 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7834 name);
7835 return TRUE;
7836 }
7837 return FALSE;
7838}
7839
7840/*
7841 * Check if a variable name is valid.
7842 * Return FALSE and give an error if not.
7843 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007845valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007846{
7847 char_u *p;
7848
7849 for (p = varname; *p != NUL; ++p)
7850 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7851 && *p != AUTOLOAD_CHAR)
7852 {
7853 EMSG2(_(e_illvar), varname);
7854 return FALSE;
7855 }
7856 return TRUE;
7857}
7858
7859/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007860 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007861 * Also give an error message, using "name" or _("name") when use_gettext is
7862 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007863 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007864 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007865tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007866{
7867 if (lock & VAR_LOCKED)
7868 {
7869 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007870 name == NULL ? (char_u *)_("Unknown")
7871 : use_gettext ? (char_u *)_(name)
7872 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007873 return TRUE;
7874 }
7875 if (lock & VAR_FIXED)
7876 {
7877 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007878 name == NULL ? (char_u *)_("Unknown")
7879 : use_gettext ? (char_u *)_(name)
7880 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007881 return TRUE;
7882 }
7883 return FALSE;
7884}
7885
7886/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007890 * It is OK for "from" and "to" to point to the same item. This is used to
7891 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007894copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007897 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898 switch (from->v_type)
7899 {
7900 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007901 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007902 to->vval.v_number = from->vval.v_number;
7903 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007905#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906 to->vval.v_float = from->vval.v_float;
7907 break;
7908#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007909 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007910#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007911 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007912 if (to->vval.v_job != NULL)
7913 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007914 break;
7915#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007916 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007917#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007918 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007919 if (to->vval.v_channel != NULL)
7920 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007921 break;
7922#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007923 case VAR_STRING:
7924 case VAR_FUNC:
7925 if (from->vval.v_string == NULL)
7926 to->vval.v_string = NULL;
7927 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007928 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007930 if (from->v_type == VAR_FUNC)
7931 func_ref(to->vval.v_string);
7932 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007934 case VAR_PARTIAL:
7935 if (from->vval.v_partial == NULL)
7936 to->vval.v_partial = NULL;
7937 else
7938 {
7939 to->vval.v_partial = from->vval.v_partial;
7940 ++to->vval.v_partial->pt_refcount;
7941 }
7942 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007943 case VAR_LIST:
7944 if (from->vval.v_list == NULL)
7945 to->vval.v_list = NULL;
7946 else
7947 {
7948 to->vval.v_list = from->vval.v_list;
7949 ++to->vval.v_list->lv_refcount;
7950 }
7951 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007952 case VAR_DICT:
7953 if (from->vval.v_dict == NULL)
7954 to->vval.v_dict = NULL;
7955 else
7956 {
7957 to->vval.v_dict = from->vval.v_dict;
7958 ++to->vval.v_dict->dv_refcount;
7959 }
7960 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007961 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007962 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007963 break;
7964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965}
7966
7967/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007968 * Make a copy of an item.
7969 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007970 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7971 * reference to an already copied list/dict can be used.
7972 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007975item_copy(
7976 typval_T *from,
7977 typval_T *to,
7978 int deep,
7979 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007980{
7981 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007982 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007985 {
7986 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007987 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988 }
7989 ++recurse;
7990
7991 switch (from->v_type)
7992 {
7993 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007994 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007995 case VAR_STRING:
7996 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007997 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007998 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007999 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008000 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 copy_tv(from, to);
8002 break;
8003 case VAR_LIST:
8004 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008005 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008006 if (from->vval.v_list == NULL)
8007 to->vval.v_list = NULL;
8008 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8009 {
8010 /* use the copy made earlier */
8011 to->vval.v_list = from->vval.v_list->lv_copylist;
8012 ++to->vval.v_list->lv_refcount;
8013 }
8014 else
8015 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8016 if (to->vval.v_list == NULL)
8017 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 break;
8019 case VAR_DICT:
8020 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008021 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008022 if (from->vval.v_dict == NULL)
8023 to->vval.v_dict = NULL;
8024 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8025 {
8026 /* use the copy made earlier */
8027 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8028 ++to->vval.v_dict->dv_refcount;
8029 }
8030 else
8031 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8032 if (to->vval.v_dict == NULL)
8033 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008034 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008035 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008036 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008037 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008038 }
8039 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008040 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041}
8042
8043/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 * This function is used by f_input() and f_inputdialog() functions. The third
8045 * argument to f_input() specifies the type of completion to use at the
8046 * prompt. The third argument to f_inputdialog() specifies the value to return
8047 * when the user cancels the prompt.
8048 */
8049 void
8050get_user_input(
8051 typval_T *argvars,
8052 typval_T *rettv,
8053 int inputdialog,
8054 int secret)
8055{
8056 char_u *prompt = get_tv_string_chk(&argvars[0]);
8057 char_u *p = NULL;
8058 int c;
8059 char_u buf[NUMBUFLEN];
8060 int cmd_silent_save = cmd_silent;
8061 char_u *defstr = (char_u *)"";
8062 int xp_type = EXPAND_NOTHING;
8063 char_u *xp_arg = NULL;
8064
8065 rettv->v_type = VAR_STRING;
8066 rettv->vval.v_string = NULL;
8067
8068#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008069 /* While starting up, there is no place to enter text. When running tests
8070 * with --not-a-term we assume feedkeys() will be used. */
8071 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 return;
8073#endif
8074
8075 cmd_silent = FALSE; /* Want to see the prompt. */
8076 if (prompt != NULL)
8077 {
8078 /* Only the part of the message after the last NL is considered as
8079 * prompt for the command line */
8080 p = vim_strrchr(prompt, '\n');
8081 if (p == NULL)
8082 p = prompt;
8083 else
8084 {
8085 ++p;
8086 c = *p;
8087 *p = NUL;
8088 msg_start();
8089 msg_clr_eos();
8090 msg_puts_attr(prompt, echo_attr);
8091 msg_didout = FALSE;
8092 msg_starthere();
8093 *p = c;
8094 }
8095 cmdline_row = msg_row;
8096
8097 if (argvars[1].v_type != VAR_UNKNOWN)
8098 {
8099 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8100 if (defstr != NULL)
8101 stuffReadbuffSpec(defstr);
8102
8103 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8104 {
8105 char_u *xp_name;
8106 int xp_namelen;
8107 long argt;
8108
8109 /* input() with a third argument: completion */
8110 rettv->vval.v_string = NULL;
8111
8112 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8113 if (xp_name == NULL)
8114 return;
8115
8116 xp_namelen = (int)STRLEN(xp_name);
8117
8118 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8119 &xp_arg) == FAIL)
8120 return;
8121 }
8122 }
8123
8124 if (defstr != NULL)
8125 {
8126 int save_ex_normal_busy = ex_normal_busy;
8127 ex_normal_busy = 0;
8128 rettv->vval.v_string =
8129 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8130 xp_type, xp_arg);
8131 ex_normal_busy = save_ex_normal_busy;
8132 }
8133 if (inputdialog && rettv->vval.v_string == NULL
8134 && argvars[1].v_type != VAR_UNKNOWN
8135 && argvars[2].v_type != VAR_UNKNOWN)
8136 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8137 &argvars[2], buf));
8138
8139 vim_free(xp_arg);
8140
8141 /* since the user typed this, no need to wait for return */
8142 need_wait_return = FALSE;
8143 msg_didout = FALSE;
8144 }
8145 cmd_silent = cmd_silent_save;
8146}
8147
8148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 * ":echo expr1 ..." print each argument separated with a space, add a
8150 * newline at the end.
8151 * ":echon expr1 ..." print each argument plain.
8152 */
8153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008154ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
8156 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 char_u *p;
8160 int needclr = TRUE;
8161 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008162 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
8164 if (eap->skip)
8165 ++emsg_skip;
8166 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8167 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168 /* If eval1() causes an error message the text from the command may
8169 * still need to be cleared. E.g., "echo 22,44". */
8170 need_clr_eos = needclr;
8171
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 /*
8176 * Report the invalid expression unless the expression evaluation
8177 * has been cancelled due to an aborting error, an interrupt, or an
8178 * exception.
8179 */
8180 if (!aborting())
8181 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 break;
8184 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185 need_clr_eos = FALSE;
8186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (!eap->skip)
8188 {
8189 if (atstart)
8190 {
8191 atstart = FALSE;
8192 /* Call msg_start() after eval1(), evaluating the expression
8193 * may cause a message to appear. */
8194 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008195 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008196 /* Mark the saved text as finishing the line, so that what
8197 * follows is displayed on a new line when scrolling back
8198 * at the more prompt. */
8199 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 }
8203 else if (eap->cmdidx == CMD_echo)
8204 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008205 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 if (p != NULL)
8207 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008209 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008211 if (*p != TAB && needclr)
8212 {
8213 /* remove any text still there from the command */
8214 msg_clr_eos();
8215 needclr = FALSE;
8216 }
8217 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 }
8219 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008220 {
8221#ifdef FEAT_MBYTE
8222 if (has_mbyte)
8223 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008224 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008225
8226 (void)msg_outtrans_len_attr(p, i, echo_attr);
8227 p += i - 1;
8228 }
8229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008231 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008236 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 arg = skipwhite(arg);
8238 }
8239 eap->nextcmd = check_nextcmd(arg);
8240
8241 if (eap->skip)
8242 --emsg_skip;
8243 else
8244 {
8245 /* remove text that may still be there from the command */
8246 if (needclr)
8247 msg_clr_eos();
8248 if (eap->cmdidx == CMD_echo)
8249 msg_end();
8250 }
8251}
8252
8253/*
8254 * ":echohl {name}".
8255 */
8256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008257ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008259 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260}
8261
8262/*
8263 * ":execute expr1 ..." execute the result of an expression.
8264 * ":echomsg expr1 ..." Print a message
8265 * ":echoerr expr1 ..." Print an error
8266 * Each gets spaces around each argument and a newline at the end for
8267 * echo commands
8268 */
8269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008270ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
8272 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008273 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 int ret = OK;
8275 char_u *p;
8276 garray_T ga;
8277 int len;
8278 int save_did_emsg;
8279
8280 ga_init2(&ga, 1, 80);
8281
8282 if (eap->skip)
8283 ++emsg_skip;
8284 while (*arg != NUL && *arg != '|' && *arg != '\n')
8285 {
8286 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {
8289 /*
8290 * Report the invalid expression unless the expression evaluation
8291 * has been cancelled due to an aborting error, an interrupt, or an
8292 * exception.
8293 */
8294 if (!aborting())
8295 EMSG2(_(e_invexpr2), p);
8296 ret = FAIL;
8297 break;
8298 }
8299
8300 if (!eap->skip)
8301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 len = (int)STRLEN(p);
8304 if (ga_grow(&ga, len + 2) == FAIL)
8305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 ret = FAIL;
8308 break;
8309 }
8310 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 ga.ga_len += len;
8314 }
8315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 arg = skipwhite(arg);
8318 }
8319
8320 if (ret != FAIL && ga.ga_data != NULL)
8321 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008322 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8323 {
8324 /* Mark the already saved text as finishing the line, so that what
8325 * follows is displayed on a new line when scrolling back at the
8326 * more prompt. */
8327 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008328 }
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008333 out_flush();
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 else if (eap->cmdidx == CMD_echoerr)
8336 {
8337 /* We don't want to abort following commands, restore did_emsg. */
8338 save_did_emsg = did_emsg;
8339 EMSG((char_u *)ga.ga_data);
8340 if (!force_abort)
8341 did_emsg = save_did_emsg;
8342 }
8343 else if (eap->cmdidx == CMD_execute)
8344 do_cmdline((char_u *)ga.ga_data,
8345 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8346 }
8347
8348 ga_clear(&ga);
8349
8350 if (eap->skip)
8351 --emsg_skip;
8352
8353 eap->nextcmd = check_nextcmd(arg);
8354}
8355
8356/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 * Find window specified by "vp" in tabpage "tp".
8358 */
8359 win_T *
8360find_win_by_nr(
8361 typval_T *vp,
8362 tabpage_T *tp UNUSED) /* NULL for current tab page */
8363{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 int nr;
8366
8367 nr = (int)get_tv_number_chk(vp, NULL);
8368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 if (nr < 0)
8370 return NULL;
8371 if (nr == 0)
8372 return curwin;
8373
Bram Moolenaar29323592016-07-24 22:04:11 +02008374 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8375 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 if (nr >= LOWEST_WIN_ID)
8377 {
8378 if (wp->w_id == nr)
8379 return wp;
8380 }
8381 else if (--nr <= 0)
8382 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008383 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 if (nr >= LOWEST_WIN_ID)
8385 return NULL;
8386 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387}
8388
8389/*
8390 * Find window specified by "wvp" in tabpage "tvp".
8391 */
8392 win_T *
8393find_tabwin(
8394 typval_T *wvp, /* VAR_UNKNOWN for current window */
8395 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8396{
8397 win_T *wp = NULL;
8398 tabpage_T *tp = NULL;
8399 long n;
8400
8401 if (wvp->v_type != VAR_UNKNOWN)
8402 {
8403 if (tvp->v_type != VAR_UNKNOWN)
8404 {
8405 n = (long)get_tv_number(tvp);
8406 if (n >= 0)
8407 tp = find_tabpage(n);
8408 }
8409 else
8410 tp = curtab;
8411
8412 if (tp != NULL)
8413 wp = find_win_by_nr(wvp, tp);
8414 }
8415 else
8416 wp = curwin;
8417
8418 return wp;
8419}
8420
8421/*
8422 * getwinvar() and gettabwinvar()
8423 */
8424 void
8425getwinvar(
8426 typval_T *argvars,
8427 typval_T *rettv,
8428 int off) /* 1 for gettabwinvar() */
8429{
8430 win_T *win;
8431 char_u *varname;
8432 dictitem_T *v;
8433 tabpage_T *tp = NULL;
8434 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 win_T *oldcurwin;
8436 tabpage_T *oldtabpage;
8437 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439 if (off == 1)
8440 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8441 else
8442 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 win = find_win_by_nr(&argvars[off], tp);
8444 varname = get_tv_string_chk(&argvars[off + 1]);
8445 ++emsg_off;
8446
8447 rettv->v_type = VAR_STRING;
8448 rettv->vval.v_string = NULL;
8449
8450 if (win != NULL && varname != NULL)
8451 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 /* Set curwin to be our win, temporarily. Also set the tabpage,
8453 * otherwise the window is not valid. Only do this when needed,
8454 * autocommands get blocked. */
8455 need_switch_win = !(tp == curtab && win == curwin);
8456 if (!need_switch_win
8457 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008459 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008461 if (varname[1] == NUL)
8462 {
8463 /* get all window-local options in a dict */
8464 dict_T *opts = get_winbuf_options(FALSE);
8465
8466 if (opts != NULL)
8467 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008468 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008469 done = TRUE;
8470 }
8471 }
8472 else if (get_option_tv(&varname, rettv, 1) == OK)
8473 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 done = TRUE;
8475 }
8476 else
8477 {
8478 /* Look up the variable. */
8479 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8480 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8481 varname, FALSE);
8482 if (v != NULL)
8483 {
8484 copy_tv(&v->di_tv, rettv);
8485 done = TRUE;
8486 }
8487 }
8488 }
8489
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008490 if (need_switch_win)
8491 /* restore previous notion of curwin */
8492 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 }
8494
8495 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8496 /* use the default return value */
8497 copy_tv(&argvars[off + 2], rettv);
8498
8499 --emsg_off;
8500}
8501
8502/*
8503 * "setwinvar()" and "settabwinvar()" functions
8504 */
8505 void
8506setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8507{
8508 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 win_T *save_curwin;
8510 tabpage_T *save_curtab;
8511 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 char_u *varname, *winvarname;
8513 typval_T *varp;
8514 char_u nbuf[NUMBUFLEN];
8515 tabpage_T *tp = NULL;
8516
8517 if (check_restricted() || check_secure())
8518 return;
8519
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 if (off == 1)
8521 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8522 else
8523 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 win = find_win_by_nr(&argvars[off], tp);
8525 varname = get_tv_string_chk(&argvars[off + 1]);
8526 varp = &argvars[off + 2];
8527
8528 if (win != NULL && varname != NULL && varp != NULL)
8529 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 need_switch_win = !(tp == curtab && win == curwin);
8531 if (!need_switch_win
8532 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 {
8534 if (*varname == '&')
8535 {
8536 long numval;
8537 char_u *strval;
8538 int error = FALSE;
8539
8540 ++varname;
8541 numval = (long)get_tv_number_chk(varp, &error);
8542 strval = get_tv_string_buf_chk(varp, nbuf);
8543 if (!error && strval != NULL)
8544 set_option_value(varname, numval, strval, OPT_LOCAL);
8545 }
8546 else
8547 {
8548 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8549 if (winvarname != NULL)
8550 {
8551 STRCPY(winvarname, "w:");
8552 STRCPY(winvarname + 2, varname);
8553 set_var(winvarname, varp, TRUE);
8554 vim_free(winvarname);
8555 }
8556 }
8557 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 if (need_switch_win)
8559 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 }
8561}
8562
8563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8565 * "arg" points to the "&" or '+' when called, to "option" when returning.
8566 * Returns NULL when no option name found. Otherwise pointer to the char
8567 * after the option name.
8568 */
8569 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008570find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 char_u *p = *arg;
8573
8574 ++p;
8575 if (*p == 'g' && p[1] == ':')
8576 {
8577 *opt_flags = OPT_GLOBAL;
8578 p += 2;
8579 }
8580 else if (*p == 'l' && p[1] == ':')
8581 {
8582 *opt_flags = OPT_LOCAL;
8583 p += 2;
8584 }
8585 else
8586 *opt_flags = 0;
8587
8588 if (!ASCII_ISALPHA(*p))
8589 return NULL;
8590 *arg = p;
8591
8592 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8593 p += 4; /* termcap option */
8594 else
8595 while (ASCII_ISALPHA(*p))
8596 ++p;
8597 return p;
8598}
8599
8600/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008601 * Return the autoload script name for a function or variable name.
8602 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008604 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008605autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008606{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008607 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008608 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008609
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008610 /* Get the script file name: replace '#' with '/', append ".vim". */
8611 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8612 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008613 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008614 STRCPY(scriptname, "autoload/");
8615 STRCAT(scriptname, name);
8616 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8617 STRCAT(scriptname, ".vim");
8618 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8619 *p = '/';
8620 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008621}
8622
8623/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008624 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008625 * Return TRUE if a package was loaded.
8626 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008628script_autoload(
8629 char_u *name,
8630 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008631{
8632 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008633 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008634 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008635 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008636
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008637 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008638 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008639 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008640 return FALSE;
8641
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008642 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008643
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008644 /* Find the name in the list of previously loaded package names. Skip
8645 * "autoload/", it's always the same. */
8646 for (i = 0; i < ga_loaded.ga_len; ++i)
8647 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8648 break;
8649 if (!reload && i < ga_loaded.ga_len)
8650 ret = FALSE; /* was loaded already */
8651 else
8652 {
8653 /* Remember the name if it wasn't loaded already. */
8654 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8655 {
8656 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8657 tofree = NULL;
8658 }
8659
8660 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008661 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008662 ret = TRUE;
8663 }
8664
8665 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008666 return ret;
8667}
8668
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8670typedef enum
8671{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008672 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8673 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8674 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675} var_flavour_T;
8676
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008677static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008680var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 char_u *p = varname;
8683
8684 if (ASCII_ISUPPER(*p))
8685 {
8686 while (*(++p))
8687 if (ASCII_ISLOWER(*p))
8688 return VAR_FLAVOUR_SESSION;
8689 return VAR_FLAVOUR_VIMINFO;
8690 }
8691 else
8692 return VAR_FLAVOUR_DEFAULT;
8693}
8694#endif
8695
8696#if defined(FEAT_VIMINFO) || defined(PROTO)
8697/*
8698 * Restore global vars that start with a capital from the viminfo file
8699 */
8700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008701read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008704 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008706 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707
8708 if (!writing && (find_viminfo_parameter('!') != NULL))
8709 {
8710 tab = vim_strchr(virp->vir_line + 1, '\t');
8711 if (tab != NULL)
8712 {
8713 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008714 switch (*tab)
8715 {
8716 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008717#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008718 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008720 case 'D': type = VAR_DICT; break;
8721 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008722 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 tab = vim_strchr(tab, '\t');
8726 if (tab != NULL)
8727 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008728 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008729 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008730 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008732#ifdef FEAT_FLOAT
8733 else if (type == VAR_FLOAT)
8734 (void)string2float(tab + 1, &tv.vval.v_float);
8735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008737 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008738 if (type == VAR_DICT || type == VAR_LIST)
8739 {
8740 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8741
8742 if (etv == NULL)
8743 /* Failed to parse back the dict or list, use it as a
8744 * string. */
8745 tv.v_type = VAR_STRING;
8746 else
8747 {
8748 vim_free(tv.vval.v_string);
8749 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008750 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008751 }
8752 }
8753
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008754 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008755 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008756 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008757 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008758
8759 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008760 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008761 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8762 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 }
8764 }
8765 }
8766
8767 return viminfo_readline(virp);
8768}
8769
8770/*
8771 * Write global vars that start with a capital to the viminfo file
8772 */
8773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008774write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 hashitem_T *hi;
8777 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008778 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008779 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008780 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008782 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 if (find_viminfo_parameter('!') == NULL)
8785 return;
8786
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008787 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008788
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008789 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008792 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008794 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008795 this_var = HI2DI(hi);
8796 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008798 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008799 {
8800 case VAR_STRING: s = "STR"; break;
8801 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008802 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008803 case VAR_DICT: s = "DIC"; break;
8804 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008805 case VAR_SPECIAL: s = "XPL"; break;
8806
8807 case VAR_UNKNOWN:
8808 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008809 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008810 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008811 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008812 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008813 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008815 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008816 if (p != NULL)
8817 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008818 vim_free(tofree);
8819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 }
8821 }
8822}
8823#endif
8824
8825#if defined(FEAT_SESSION) || defined(PROTO)
8826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008827store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 hashitem_T *hi;
8830 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008831 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 char_u *p, *t;
8833
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008834 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008837 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008839 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 this_var = HI2DI(hi);
8841 if ((this_var->di_tv.v_type == VAR_NUMBER
8842 || this_var->di_tv.v_type == VAR_STRING)
8843 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008844 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008845 /* Escape special characters with a backslash. Turn a LF and
8846 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008848 (char_u *)"\\\"\n\r");
8849 if (p == NULL) /* out of memory */
8850 break;
8851 for (t = p; *t != NUL; ++t)
8852 if (*t == '\n')
8853 *t = 'n';
8854 else if (*t == '\r')
8855 *t = 'r';
8856 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 this_var->di_key,
8858 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8859 : ' ',
8860 p,
8861 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8862 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008863 || put_eol(fd) == FAIL)
8864 {
8865 vim_free(p);
8866 return FAIL;
8867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 vim_free(p);
8869 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008870#ifdef FEAT_FLOAT
8871 else if (this_var->di_tv.v_type == VAR_FLOAT
8872 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8873 {
8874 float_T f = this_var->di_tv.vval.v_float;
8875 int sign = ' ';
8876
8877 if (f < 0)
8878 {
8879 f = -f;
8880 sign = '-';
8881 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008882 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008883 this_var->di_key, sign, f) < 0)
8884 || put_eol(fd) == FAIL)
8885 return FAIL;
8886 }
8887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 }
8889 }
8890 return OK;
8891}
8892#endif
8893
Bram Moolenaar661b1822005-07-28 22:36:45 +00008894/*
8895 * Display script name where an item was last set.
8896 * Should only be invoked when 'verbose' is non-zero.
8897 */
8898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008899last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008900{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008901 char_u *p;
8902
Bram Moolenaar661b1822005-07-28 22:36:45 +00008903 if (scriptID != 0)
8904 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008905 p = home_replace_save(NULL, get_scriptname(scriptID));
8906 if (p != NULL)
8907 {
8908 verbose_enter();
8909 MSG_PUTS(_("\n\tLast set from "));
8910 MSG_PUTS(p);
8911 vim_free(p);
8912 verbose_leave();
8913 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008914 }
8915}
8916
Bram Moolenaar53744302015-07-17 17:38:22 +02008917/* reset v:option_new, v:option_old and v:option_type */
8918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008919reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008920{
8921 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8922 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8923 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8924}
8925
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926/*
8927 * Prepare "gap" for an assert error and add the sourcing position.
8928 */
8929 void
8930prepare_assert_error(garray_T *gap)
8931{
8932 char buf[NUMBUFLEN];
8933
8934 ga_init2(gap, 1, 100);
8935 if (sourcing_name != NULL)
8936 {
8937 ga_concat(gap, sourcing_name);
8938 if (sourcing_lnum > 0)
8939 ga_concat(gap, (char_u *)" ");
8940 }
8941 if (sourcing_lnum > 0)
8942 {
8943 sprintf(buf, "line %ld", (long)sourcing_lnum);
8944 ga_concat(gap, (char_u *)buf);
8945 }
8946 if (sourcing_name != NULL || sourcing_lnum > 0)
8947 ga_concat(gap, (char_u *)": ");
8948}
8949
8950/*
8951 * Add an assert error to v:errors.
8952 */
8953 void
8954assert_error(garray_T *gap)
8955{
8956 struct vimvar *vp = &vimvars[VV_ERRORS];
8957
8958 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8959 /* Make sure v:errors is a list. */
8960 set_vim_var_list(VV_ERRORS, list_alloc());
8961 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8962}
8963
8964 void
8965assert_equal_common(typval_T *argvars, assert_type_T atype)
8966{
8967 garray_T ga;
8968
8969 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8970 != (atype == ASSERT_EQUAL))
8971 {
8972 prepare_assert_error(&ga);
8973 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8974 atype);
8975 assert_error(&ga);
8976 ga_clear(&ga);
8977 }
8978}
8979
8980 void
8981assert_match_common(typval_T *argvars, assert_type_T atype)
8982{
8983 garray_T ga;
8984 char_u buf1[NUMBUFLEN];
8985 char_u buf2[NUMBUFLEN];
8986 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8987 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8988
8989 if (pat == NULL || text == NULL)
8990 EMSG(_(e_invarg));
8991 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8992 {
8993 prepare_assert_error(&ga);
8994 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8995 atype);
8996 assert_error(&ga);
8997 ga_clear(&ga);
8998 }
8999}
9000
Bram Moolenaar61c04492016-07-23 15:35:35 +02009001 void
9002assert_inrange(typval_T *argvars)
9003{
9004 garray_T ga;
9005 int error = FALSE;
9006 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9007 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9008 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9009 char_u *tofree;
9010 char msg[200];
9011 char_u numbuf[NUMBUFLEN];
9012
9013 if (error)
9014 return;
9015 if (actual < lower || actual > upper)
9016 {
9017 prepare_assert_error(&ga);
9018 if (argvars[3].v_type != VAR_UNKNOWN)
9019 {
9020 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9021 vim_free(tofree);
9022 }
9023 else
9024 {
9025 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9026 (long)lower, (long)upper, (long)actual);
9027 ga_concat(&ga, (char_u *)msg);
9028 }
9029 assert_error(&ga);
9030 ga_clear(&ga);
9031 }
9032}
9033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034/*
9035 * Common for assert_true() and assert_false().
9036 */
9037 void
9038assert_bool(typval_T *argvars, int isTrue)
9039{
9040 int error = FALSE;
9041 garray_T ga;
9042
9043 if (argvars[0].v_type == VAR_SPECIAL
9044 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9045 return;
9046 if (argvars[0].v_type != VAR_NUMBER
9047 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9048 || error)
9049 {
9050 prepare_assert_error(&ga);
9051 fill_assert_error(&ga, &argvars[1],
9052 (char_u *)(isTrue ? "True" : "False"),
9053 NULL, &argvars[0], ASSERT_OTHER);
9054 assert_error(&ga);
9055 ga_clear(&ga);
9056 }
9057}
9058
9059 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009060assert_report(typval_T *argvars)
9061{
9062 garray_T ga;
9063
9064 prepare_assert_error(&ga);
9065 ga_concat(&ga, get_tv_string(&argvars[0]));
9066 assert_error(&ga);
9067 ga_clear(&ga);
9068}
9069
9070 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071assert_exception(typval_T *argvars)
9072{
9073 garray_T ga;
9074 char_u *error = get_tv_string_chk(&argvars[0]);
9075
9076 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9077 {
9078 prepare_assert_error(&ga);
9079 ga_concat(&ga, (char_u *)"v:exception is not set");
9080 assert_error(&ga);
9081 ga_clear(&ga);
9082 }
9083 else if (error != NULL
9084 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9085 {
9086 prepare_assert_error(&ga);
9087 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9088 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9089 assert_error(&ga);
9090 ga_clear(&ga);
9091 }
9092}
9093
9094 void
9095assert_fails(typval_T *argvars)
9096{
9097 char_u *cmd = get_tv_string_chk(&argvars[0]);
9098 garray_T ga;
9099
9100 called_emsg = FALSE;
9101 suppress_errthrow = TRUE;
9102 emsg_silent = TRUE;
9103 do_cmdline_cmd(cmd);
9104 if (!called_emsg)
9105 {
9106 prepare_assert_error(&ga);
9107 ga_concat(&ga, (char_u *)"command did not fail: ");
9108 ga_concat(&ga, cmd);
9109 assert_error(&ga);
9110 ga_clear(&ga);
9111 }
9112 else if (argvars[1].v_type != VAR_UNKNOWN)
9113 {
9114 char_u buf[NUMBUFLEN];
9115 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9116
9117 if (error == NULL
9118 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9119 {
9120 prepare_assert_error(&ga);
9121 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9122 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9123 assert_error(&ga);
9124 ga_clear(&ga);
9125 }
9126 }
9127
9128 called_emsg = FALSE;
9129 suppress_errthrow = FALSE;
9130 emsg_silent = FALSE;
9131 emsg_on_display = FALSE;
9132 set_vim_var_string(VV_ERRMSG, NULL, 0);
9133}
9134
9135/*
9136 * Append "str" to "gap", escaping unprintable characters.
9137 * Changes NL to \n, CR to \r, etc.
9138 */
9139 static void
9140ga_concat_esc(garray_T *gap, char_u *str)
9141{
9142 char_u *p;
9143 char_u buf[NUMBUFLEN];
9144
9145 if (str == NULL)
9146 {
9147 ga_concat(gap, (char_u *)"NULL");
9148 return;
9149 }
9150
9151 for (p = str; *p != NUL; ++p)
9152 switch (*p)
9153 {
9154 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9155 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9156 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9157 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9158 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9159 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9160 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9161 default:
9162 if (*p < ' ')
9163 {
9164 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9165 ga_concat(gap, buf);
9166 }
9167 else
9168 ga_append(gap, *p);
9169 break;
9170 }
9171}
9172
9173/*
9174 * Fill "gap" with information about an assert error.
9175 */
9176 void
9177fill_assert_error(
9178 garray_T *gap,
9179 typval_T *opt_msg_tv,
9180 char_u *exp_str,
9181 typval_T *exp_tv,
9182 typval_T *got_tv,
9183 assert_type_T atype)
9184{
9185 char_u numbuf[NUMBUFLEN];
9186 char_u *tofree;
9187
9188 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9189 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009190 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9191 vim_free(tofree);
9192 ga_concat(gap, (char_u *)": ");
9193 }
9194
9195 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9196 ga_concat(gap, (char_u *)"Pattern ");
9197 else if (atype == ASSERT_NOTEQUAL)
9198 ga_concat(gap, (char_u *)"Expected not equal to ");
9199 else
9200 ga_concat(gap, (char_u *)"Expected ");
9201 if (exp_str == NULL)
9202 {
9203 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 vim_free(tofree);
9205 }
9206 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009207 ga_concat_esc(gap, exp_str);
9208 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009209 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009210 if (atype == ASSERT_MATCH)
9211 ga_concat(gap, (char_u *)" does not match ");
9212 else if (atype == ASSERT_NOTMATCH)
9213 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009215 ga_concat(gap, (char_u *)" but got ");
9216 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9217 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218 }
9219}
9220
Bram Moolenaar53744302015-07-17 17:38:22 +02009221
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222#endif /* FEAT_EVAL */
9223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009225#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
9227#ifdef WIN3264
9228/*
9229 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9230 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009231static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9232static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9233static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009236 * Get the short path (8.3) for the filename in "fnamep".
9237 * Only works for a valid file name.
9238 * When the path gets longer "fnamep" is changed and the allocated buffer
9239 * is put in "bufp".
9240 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9241 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 */
9243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009244get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009246 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 char_u *newbuf;
9248
9249 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009250 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 if (l > len - 1)
9252 {
9253 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009254 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 newbuf = vim_strnsave(*fnamep, l);
9256 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258
9259 vim_free(*bufp);
9260 *fnamep = *bufp = newbuf;
9261
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009262 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009263 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
9265
9266 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009267 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268}
9269
9270/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009271 * Get the short path (8.3) for the filename in "fname". The converted
9272 * path is returned in "bufp".
9273 *
9274 * Some of the directories specified in "fname" may not exist. This function
9275 * will shorten the existing directories at the beginning of the path and then
9276 * append the remaining non-existing path.
9277 *
9278 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009279 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009280 * bufp - Pointer to an allocated buffer for the filename.
9281 * fnamelen - Length of the filename pointed to by fname
9282 *
9283 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 */
9285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009286shortpath_for_invalid_fname(
9287 char_u **fname,
9288 char_u **bufp,
9289 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009291 char_u *short_fname, *save_fname, *pbuf_unused;
9292 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009294 int old_len, len;
9295 int new_len, sfx_len;
9296 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297
9298 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009299 old_len = *fnamelen;
9300 save_fname = vim_strnsave(*fname, old_len);
9301 pbuf_unused = NULL;
9302 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009304 endp = save_fname + old_len - 1; /* Find the end of the copy */
9305 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009307 /*
9308 * Try shortening the supplied path till it succeeds by removing one
9309 * directory at a time from the tail of the path.
9310 */
9311 len = 0;
9312 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009314 /* go back one path-separator */
9315 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9316 --endp;
9317 if (endp <= save_fname)
9318 break; /* processed the complete path */
9319
9320 /*
9321 * Replace the path separator with a NUL and try to shorten the
9322 * resulting path.
9323 */
9324 ch = *endp;
9325 *endp = 0;
9326 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009327 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009328 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9329 {
9330 retval = FAIL;
9331 goto theend;
9332 }
9333 *endp = ch; /* preserve the string */
9334
9335 if (len > 0)
9336 break; /* successfully shortened the path */
9337
9338 /* failed to shorten the path. Skip the path separator */
9339 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 }
9341
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009342 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009344 /*
9345 * Succeeded in shortening the path. Now concatenate the shortened
9346 * path with the remaining path at the tail.
9347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009349 /* Compute the length of the new path. */
9350 sfx_len = (int)(save_endp - endp) + 1;
9351 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009353 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009355 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009357 /* There is not enough space in the currently allocated string,
9358 * copy it to a buffer big enough. */
9359 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009361 {
9362 retval = FAIL;
9363 goto theend;
9364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 }
9366 else
9367 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009368 /* Transfer short_fname to the main buffer (it's big enough),
9369 * unless get_short_pathname() did its work in-place. */
9370 *fname = *bufp = save_fname;
9371 if (short_fname != save_fname)
9372 vim_strncpy(save_fname, short_fname, len);
9373 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009375
9376 /* concat the not-shortened part of the path */
9377 vim_strncpy(*fname + len, endp, sfx_len);
9378 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009380
9381theend:
9382 vim_free(pbuf_unused);
9383 vim_free(save_fname);
9384
9385 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386}
9387
9388/*
9389 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009390 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 */
9392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009393shortpath_for_partial(
9394 char_u **fnamep,
9395 char_u **bufp,
9396 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 int sepcount, len, tflen;
9399 char_u *p;
9400 char_u *pbuf, *tfname;
9401 int hasTilde;
9402
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009403 /* Count up the path separators from the RHS.. so we know which part
9404 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009406 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 if (vim_ispathsep(*p))
9408 ++sepcount;
9409
9410 /* Need full path first (use expand_env() to remove a "~/") */
9411 hasTilde = (**fnamep == '~');
9412 if (hasTilde)
9413 pbuf = tfname = expand_env_save(*fnamep);
9414 else
9415 pbuf = tfname = FullName_save(*fnamep, FALSE);
9416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009417 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009419 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421
9422 if (len == 0)
9423 {
9424 /* Don't have a valid filename, so shorten the rest of the
9425 * path if we can. This CAN give us invalid 8.3 filenames, but
9426 * there's not a lot of point in guessing what it might be.
9427 */
9428 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009429 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9430 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 }
9432
9433 /* Count the paths backward to find the beginning of the desired string. */
9434 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009435 {
9436#ifdef FEAT_MBYTE
9437 if (has_mbyte)
9438 p -= mb_head_off(tfname, p);
9439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 if (vim_ispathsep(*p))
9441 {
9442 if (sepcount == 0 || (hasTilde && sepcount == 1))
9443 break;
9444 else
9445 sepcount --;
9446 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 if (hasTilde)
9449 {
9450 --p;
9451 if (p >= tfname)
9452 *p = '~';
9453 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009454 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 }
9456 else
9457 ++p;
9458
9459 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9460 vim_free(*bufp);
9461 *fnamelen = (int)STRLEN(p);
9462 *bufp = pbuf;
9463 *fnamep = p;
9464
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009465 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467#endif /* WIN3264 */
9468
9469/*
9470 * Adjust a filename, according to a string of modifiers.
9471 * *fnamep must be NUL terminated when called. When returning, the length is
9472 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009473 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 * When there is an error, *fnamep is set to NULL.
9475 */
9476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009477modify_fname(
9478 char_u *src, /* string with modifiers */
9479 int *usedlen, /* characters after src that are used */
9480 char_u **fnamep, /* file name so far */
9481 char_u **bufp, /* buffer for allocated file name or NULL */
9482 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484 int valid = 0;
9485 char_u *tail;
9486 char_u *s, *p, *pbuf;
9487 char_u dirname[MAXPATHL];
9488 int c;
9489 int has_fullname = 0;
9490#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009491 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 int has_shortname = 0;
9493#endif
9494
9495repeat:
9496 /* ":p" - full path/file_name */
9497 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9498 {
9499 has_fullname = 1;
9500
9501 valid |= VALID_PATH;
9502 *usedlen += 2;
9503
9504 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9505 if ((*fnamep)[0] == '~'
9506#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9507 && ((*fnamep)[1] == '/'
9508# ifdef BACKSLASH_IN_FILENAME
9509 || (*fnamep)[1] == '\\'
9510# endif
9511 || (*fnamep)[1] == NUL)
9512
9513#endif
9514 )
9515 {
9516 *fnamep = expand_env_save(*fnamep);
9517 vim_free(*bufp); /* free any allocated file name */
9518 *bufp = *fnamep;
9519 if (*fnamep == NULL)
9520 return -1;
9521 }
9522
9523 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009524 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 {
9526 if (vim_ispathsep(*p)
9527 && p[1] == '.'
9528 && (p[2] == NUL
9529 || vim_ispathsep(p[2])
9530 || (p[2] == '.'
9531 && (p[3] == NUL || vim_ispathsep(p[3])))))
9532 break;
9533 }
9534
9535 /* FullName_save() is slow, don't use it when not needed. */
9536 if (*p != NUL || !vim_isAbsName(*fnamep))
9537 {
9538 *fnamep = FullName_save(*fnamep, *p != NUL);
9539 vim_free(*bufp); /* free any allocated file name */
9540 *bufp = *fnamep;
9541 if (*fnamep == NULL)
9542 return -1;
9543 }
9544
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009545#ifdef WIN3264
9546# if _WIN32_WINNT >= 0x0500
9547 if (vim_strchr(*fnamep, '~') != NULL)
9548 {
9549 /* Expand 8.3 filename to full path. Needed to make sure the same
9550 * file does not have two different names.
9551 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9552 p = alloc(_MAX_PATH + 1);
9553 if (p != NULL)
9554 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009555 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009556 {
9557 vim_free(*bufp);
9558 *bufp = *fnamep = p;
9559 }
9560 else
9561 vim_free(p);
9562 }
9563 }
9564# endif
9565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 /* Append a path separator to a directory. */
9567 if (mch_isdir(*fnamep))
9568 {
9569 /* Make room for one or two extra characters. */
9570 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9571 vim_free(*bufp); /* free any allocated file name */
9572 *bufp = *fnamep;
9573 if (*fnamep == NULL)
9574 return -1;
9575 add_pathsep(*fnamep);
9576 }
9577 }
9578
9579 /* ":." - path relative to the current directory */
9580 /* ":~" - path relative to the home directory */
9581 /* ":8" - shortname path - postponed till after */
9582 while (src[*usedlen] == ':'
9583 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9584 {
9585 *usedlen += 2;
9586 if (c == '8')
9587 {
9588#ifdef WIN3264
9589 has_shortname = 1; /* Postpone this. */
9590#endif
9591 continue;
9592 }
9593 pbuf = NULL;
9594 /* Need full path first (use expand_env() to remove a "~/") */
9595 if (!has_fullname)
9596 {
9597 if (c == '.' && **fnamep == '~')
9598 p = pbuf = expand_env_save(*fnamep);
9599 else
9600 p = pbuf = FullName_save(*fnamep, FALSE);
9601 }
9602 else
9603 p = *fnamep;
9604
9605 has_fullname = 0;
9606
9607 if (p != NULL)
9608 {
9609 if (c == '.')
9610 {
9611 mch_dirname(dirname, MAXPATHL);
9612 s = shorten_fname(p, dirname);
9613 if (s != NULL)
9614 {
9615 *fnamep = s;
9616 if (pbuf != NULL)
9617 {
9618 vim_free(*bufp); /* free any allocated file name */
9619 *bufp = pbuf;
9620 pbuf = NULL;
9621 }
9622 }
9623 }
9624 else
9625 {
9626 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9627 /* Only replace it when it starts with '~' */
9628 if (*dirname == '~')
9629 {
9630 s = vim_strsave(dirname);
9631 if (s != NULL)
9632 {
9633 *fnamep = s;
9634 vim_free(*bufp);
9635 *bufp = s;
9636 }
9637 }
9638 }
9639 vim_free(pbuf);
9640 }
9641 }
9642
9643 tail = gettail(*fnamep);
9644 *fnamelen = (int)STRLEN(*fnamep);
9645
9646 /* ":h" - head, remove "/file_name", can be repeated */
9647 /* Don't remove the first "/" or "c:\" */
9648 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9649 {
9650 valid |= VALID_HEAD;
9651 *usedlen += 2;
9652 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009653 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009654 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 *fnamelen = (int)(tail - *fnamep);
9656#ifdef VMS
9657 if (*fnamelen > 0)
9658 *fnamelen += 1; /* the path separator is part of the path */
9659#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009660 if (*fnamelen == 0)
9661 {
9662 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9663 p = vim_strsave((char_u *)".");
9664 if (p == NULL)
9665 return -1;
9666 vim_free(*bufp);
9667 *bufp = *fnamep = tail = p;
9668 *fnamelen = 1;
9669 }
9670 else
9671 {
9672 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009673 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
9676
9677 /* ":8" - shortname */
9678 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9679 {
9680 *usedlen += 2;
9681#ifdef WIN3264
9682 has_shortname = 1;
9683#endif
9684 }
9685
9686#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009687 /*
9688 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 */
9690 if (has_shortname)
9691 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009692 /* Copy the string if it is shortened by :h and when it wasn't copied
9693 * yet, because we are going to change it in place. Avoids changing
9694 * the buffer name for "%:8". */
9695 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 {
9697 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009698 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 return -1;
9700 vim_free(*bufp);
9701 *bufp = *fnamep = p;
9702 }
9703
9704 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009705 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (!has_fullname && !vim_isAbsName(*fnamep))
9707 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009708 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 return -1;
9710 }
9711 else
9712 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009713 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
Bram Moolenaardc935552011-08-17 15:23:23 +02009715 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009717 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 return -1;
9719
9720 if (l == 0)
9721 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009722 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009724 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 return -1;
9726 }
9727 *fnamelen = l;
9728 }
9729 }
9730#endif /* WIN3264 */
9731
9732 /* ":t" - tail, just the basename */
9733 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9734 {
9735 *usedlen += 2;
9736 *fnamelen -= (int)(tail - *fnamep);
9737 *fnamep = tail;
9738 }
9739
9740 /* ":e" - extension, can be repeated */
9741 /* ":r" - root, without extension, can be repeated */
9742 while (src[*usedlen] == ':'
9743 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9744 {
9745 /* find a '.' in the tail:
9746 * - for second :e: before the current fname
9747 * - otherwise: The last '.'
9748 */
9749 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9750 s = *fnamep - 2;
9751 else
9752 s = *fnamep + *fnamelen - 1;
9753 for ( ; s > tail; --s)
9754 if (s[0] == '.')
9755 break;
9756 if (src[*usedlen + 1] == 'e') /* :e */
9757 {
9758 if (s > tail)
9759 {
9760 *fnamelen += (int)(*fnamep - (s + 1));
9761 *fnamep = s + 1;
9762#ifdef VMS
9763 /* cut version from the extension */
9764 s = *fnamep + *fnamelen - 1;
9765 for ( ; s > *fnamep; --s)
9766 if (s[0] == ';')
9767 break;
9768 if (s > *fnamep)
9769 *fnamelen = s - *fnamep;
9770#endif
9771 }
9772 else if (*fnamep <= tail)
9773 *fnamelen = 0;
9774 }
9775 else /* :r */
9776 {
9777 if (s > tail) /* remove one extension */
9778 *fnamelen = (int)(s - *fnamep);
9779 }
9780 *usedlen += 2;
9781 }
9782
9783 /* ":s?pat?foo?" - substitute */
9784 /* ":gs?pat?foo?" - global substitute */
9785 if (src[*usedlen] == ':'
9786 && (src[*usedlen + 1] == 's'
9787 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9788 {
9789 char_u *str;
9790 char_u *pat;
9791 char_u *sub;
9792 int sep;
9793 char_u *flags;
9794 int didit = FALSE;
9795
9796 flags = (char_u *)"";
9797 s = src + *usedlen + 2;
9798 if (src[*usedlen + 1] == 'g')
9799 {
9800 flags = (char_u *)"g";
9801 ++s;
9802 }
9803
9804 sep = *s++;
9805 if (sep)
9806 {
9807 /* find end of pattern */
9808 p = vim_strchr(s, sep);
9809 if (p != NULL)
9810 {
9811 pat = vim_strnsave(s, (int)(p - s));
9812 if (pat != NULL)
9813 {
9814 s = p + 1;
9815 /* find end of substitution */
9816 p = vim_strchr(s, sep);
9817 if (p != NULL)
9818 {
9819 sub = vim_strnsave(s, (int)(p - s));
9820 str = vim_strnsave(*fnamep, *fnamelen);
9821 if (sub != NULL && str != NULL)
9822 {
9823 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009824 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 if (s != NULL)
9826 {
9827 *fnamep = s;
9828 *fnamelen = (int)STRLEN(s);
9829 vim_free(*bufp);
9830 *bufp = s;
9831 didit = TRUE;
9832 }
9833 }
9834 vim_free(sub);
9835 vim_free(str);
9836 }
9837 vim_free(pat);
9838 }
9839 }
9840 /* after using ":s", repeat all the modifiers */
9841 if (didit)
9842 goto repeat;
9843 }
9844 }
9845
Bram Moolenaar26df0922014-02-23 23:39:13 +01009846 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9847 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009848 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009849 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009850 if (c != NUL)
9851 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009852 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009853 if (c != NUL)
9854 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009855 if (p == NULL)
9856 return -1;
9857 vim_free(*bufp);
9858 *bufp = *fnamep = p;
9859 *fnamelen = (int)STRLEN(p);
9860 *usedlen += 2;
9861 }
9862
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 return valid;
9864}
9865
9866/*
9867 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009868 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 * "flags" can be "g" to do a global substitute.
9870 * Returns an allocated string, NULL for error.
9871 */
9872 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009873do_string_sub(
9874 char_u *str,
9875 char_u *pat,
9876 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009877 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009878 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 int sublen;
9881 regmatch_T regmatch;
9882 int i;
9883 int do_all;
9884 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009885 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 garray_T ga;
9887 char_u *ret;
9888 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009889 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9892 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009893 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
9895 ga_init2(&ga, 1, 200);
9896
9897 do_all = (flags[0] == 'g');
9898
9899 regmatch.rm_ic = p_ic;
9900 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9901 if (regmatch.regprog != NULL)
9902 {
9903 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009904 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9906 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009907 /* Skip empty match except for first match. */
9908 if (regmatch.startp[0] == regmatch.endp[0])
9909 {
9910 if (zero_width == regmatch.startp[0])
9911 {
9912 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009913 i = MB_PTR2LEN(tail);
9914 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9915 (size_t)i);
9916 ga.ga_len += i;
9917 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009918 continue;
9919 }
9920 zero_width = regmatch.startp[0];
9921 }
9922
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 /*
9924 * Get some space for a temporary buffer to do the substitution
9925 * into. It will contain:
9926 * - The text up to where the match is.
9927 * - The substituted text.
9928 * - The text after the match.
9929 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009930 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009931 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9933 {
9934 ga_clear(&ga);
9935 break;
9936 }
9937
9938 /* copy the text up to where the match is */
9939 i = (int)(regmatch.startp[0] - tail);
9940 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9941 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009942 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 + ga.ga_len + i, TRUE, TRUE, FALSE);
9944 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009945 tail = regmatch.endp[0];
9946 if (*tail == NUL)
9947 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 if (!do_all)
9949 break;
9950 }
9951
9952 if (ga.ga_data != NULL)
9953 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9954
Bram Moolenaar473de612013-06-08 18:19:48 +02009955 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 }
9957
9958 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9959 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009960 if (p_cpo == empty_option)
9961 p_cpo = save_cpo;
9962 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009963 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009964 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 return ret;
9967}
9968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009969 static int
9970filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
9971{
9972 typval_T rettv;
9973 typval_T argv[3];
9974 char_u buf[NUMBUFLEN];
9975 char_u *s;
9976 int retval = FAIL;
9977 int dummy;
9978
9979 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
9980 argv[0] = vimvars[VV_KEY].vv_tv;
9981 argv[1] = vimvars[VV_VAL].vv_tv;
9982 if (expr->v_type == VAR_FUNC)
9983 {
9984 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009985 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9986 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009987 goto theend;
9988 }
9989 else if (expr->v_type == VAR_PARTIAL)
9990 {
9991 partial_T *partial = expr->vval.v_partial;
9992
Bram Moolenaar437bafe2016-08-01 15:40:54 +02009993 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02009994 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
9995 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996 goto theend;
9997 }
9998 else
9999 {
10000 s = get_tv_string_buf_chk(expr, buf);
10001 if (s == NULL)
10002 goto theend;
10003 s = skipwhite(s);
10004 if (eval1(&s, &rettv, TRUE) == FAIL)
10005 goto theend;
10006 if (*s != NUL) /* check for trailing chars after expr */
10007 {
10008 EMSG2(_(e_invexpr2), s);
10009 goto theend;
10010 }
10011 }
10012 if (map)
10013 {
10014 /* map(): replace the list item value */
10015 clear_tv(tv);
10016 rettv.v_lock = 0;
10017 *tv = rettv;
10018 }
10019 else
10020 {
10021 int error = FALSE;
10022
10023 /* filter(): when expr is zero remove the item */
10024 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10025 clear_tv(&rettv);
10026 /* On type error, nothing has been removed; return FAIL to stop the
10027 * loop. The error message was given by get_tv_number_chk(). */
10028 if (error)
10029 goto theend;
10030 }
10031 retval = OK;
10032theend:
10033 clear_tv(&vimvars[VV_VAL].vv_tv);
10034 return retval;
10035}
10036
10037
10038/*
10039 * Implementation of map() and filter().
10040 */
10041 void
10042filter_map(typval_T *argvars, typval_T *rettv, int map)
10043{
10044 typval_T *expr;
10045 listitem_T *li, *nli;
10046 list_T *l = NULL;
10047 dictitem_T *di;
10048 hashtab_T *ht;
10049 hashitem_T *hi;
10050 dict_T *d = NULL;
10051 typval_T save_val;
10052 typval_T save_key;
10053 int rem;
10054 int todo;
10055 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10056 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10057 : N_("filter() argument"));
10058 int save_did_emsg;
10059 int idx = 0;
10060
10061 if (argvars[0].v_type == VAR_LIST)
10062 {
10063 if ((l = argvars[0].vval.v_list) == NULL
10064 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10065 return;
10066 }
10067 else if (argvars[0].v_type == VAR_DICT)
10068 {
10069 if ((d = argvars[0].vval.v_dict) == NULL
10070 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10071 return;
10072 }
10073 else
10074 {
10075 EMSG2(_(e_listdictarg), ermsg);
10076 return;
10077 }
10078
10079 expr = &argvars[1];
10080 /* On type errors, the preceding call has already displayed an error
10081 * message. Avoid a misleading error message for an empty string that
10082 * was not passed as argument. */
10083 if (expr->v_type != VAR_UNKNOWN)
10084 {
10085 prepare_vimvar(VV_VAL, &save_val);
10086
10087 /* We reset "did_emsg" to be able to detect whether an error
10088 * occurred during evaluation of the expression. */
10089 save_did_emsg = did_emsg;
10090 did_emsg = FALSE;
10091
10092 prepare_vimvar(VV_KEY, &save_key);
10093 if (argvars[0].v_type == VAR_DICT)
10094 {
10095 vimvars[VV_KEY].vv_type = VAR_STRING;
10096
10097 ht = &d->dv_hashtab;
10098 hash_lock(ht);
10099 todo = (int)ht->ht_used;
10100 for (hi = ht->ht_array; todo > 0; ++hi)
10101 {
10102 if (!HASHITEM_EMPTY(hi))
10103 {
10104 int r;
10105
10106 --todo;
10107 di = HI2DI(hi);
10108 if (map &&
10109 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10110 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10111 break;
10112 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10113 r = filter_map_one(&di->di_tv, expr, map, &rem);
10114 clear_tv(&vimvars[VV_KEY].vv_tv);
10115 if (r == FAIL || did_emsg)
10116 break;
10117 if (!map && rem)
10118 {
10119 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10120 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10121 break;
10122 dictitem_remove(d, di);
10123 }
10124 }
10125 }
10126 hash_unlock(ht);
10127 }
10128 else
10129 {
10130 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10131
10132 for (li = l->lv_first; li != NULL; li = nli)
10133 {
10134 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10135 break;
10136 nli = li->li_next;
10137 vimvars[VV_KEY].vv_nr = idx;
10138 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10139 || did_emsg)
10140 break;
10141 if (!map && rem)
10142 listitem_remove(l, li);
10143 ++idx;
10144 }
10145 }
10146
10147 restore_vimvar(VV_KEY, &save_key);
10148 restore_vimvar(VV_VAL, &save_val);
10149
10150 did_emsg |= save_did_emsg;
10151 }
10152
10153 copy_tv(&argvars[0], rettv);
10154}
10155
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */