blob: e8a1b0aa35425dfce1c47f558de6d14cc2c75e8a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200190 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
191 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200192 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
193 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
194 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000195};
196
197/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000198#define vv_type vv_di.di_tv.v_type
199#define vv_nr vv_di.di_tv.vval.v_number
200#define vv_float vv_di.di_tv.vval.v_float
201#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000202#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200203#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000204#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000205
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200206static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207#define vimvarht vimvardict.dv_hashtab
208
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100209static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
210static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
211static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100212static void list_glob_vars(int *first);
213static void list_buf_vars(int *first);
214static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100216static void list_vim_vars(int *first);
217static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100218static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
219static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
221static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
223static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
224static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
225static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000226
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static int eval2(char_u **arg, typval_T *rettv, int evaluate);
228static int eval3(char_u **arg, typval_T *rettv, int evaluate);
229static int eval4(char_u **arg, typval_T *rettv, int evaluate);
230static int eval5(char_u **arg, typval_T *rettv, int evaluate);
231static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
232static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000233
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
236static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200241static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static void delete_var(hashtab_T *ht, hashitem_T *hi);
244static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
245static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248/* for VIM_VERSION_ defines */
249#include "version.h"
250
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100251
252#if defined(EBCDIC) || defined(PROTO)
253/*
254 * Compare struct fst by function name.
255 */
256 static int
257compare_func_name(const void *s1, const void *s2)
258{
259 struct fst *p1 = (struct fst *)s1;
260 struct fst *p2 = (struct fst *)s2;
261
262 return STRCMP(p1->f_name, p2->f_name);
263}
264
265/*
266 * Sort the function table by function name.
267 * The sorting of the table above is ASCII dependant.
268 * On machines using EBCDIC we have to sort it.
269 */
270 static void
271sortFunctions(void)
272{
273 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
274
275 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
276}
277#endif
278
279
Bram Moolenaar33570922005-01-25 22:26:29 +0000280/*
281 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000282 */
283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100284eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000285{
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 int i;
287 struct vimvar *p;
288
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200289 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
290 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200291 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000292 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200293 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295 for (i = 0; i < VV_LEN; ++i)
296 {
297 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 if (STRLEN(p->vv_name) > 16)
299 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100300 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200301 getout(1);
302 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000303 STRCPY(p->vv_di.di_key, p->vv_name);
304 if (p->vv_flags & VV_RO)
305 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
306 else if (p->vv_flags & VV_RO_SBX)
307 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
308 else
309 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000310
311 /* add to v: scope dict, unless the value is not always available */
312 if (p->vv_type != VAR_UNKNOWN)
313 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000315 /* add to compat scope dict */
316 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000317 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100318 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
319
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100321 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200322 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100323 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100324
325 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
326 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
327 set_vim_var_nr(VV_NONE, VVAL_NONE);
328 set_vim_var_nr(VV_NULL, VVAL_NULL);
329
Bram Moolenaarf562e722016-07-19 17:25:25 +0200330 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
331 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
332 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
333 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
334 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
335 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
336 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
337 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
338 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
339 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
340
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200341 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200342
343#ifdef EBCDIC
344 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100345 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200346 */
347 sortFunctions();
348#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000349}
350
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000351#if defined(EXITFREE) || defined(PROTO)
352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000354{
355 int i;
356 struct vimvar *p;
357
358 for (i = 0; i < VV_LEN; ++i)
359 {
360 p = &vimvars[i];
361 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000362 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000363 vim_free(p->vv_str);
364 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 }
366 else if (p->vv_di.di_tv.v_type == VAR_LIST)
367 {
368 list_unref(p->vv_list);
369 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000370 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000371 }
372 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000373 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 hash_clear(&compat_hashtab);
375
Bram Moolenaard9fba312005-06-26 22:34:35 +0000376 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100377# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200378 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100379# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000380
381 /* global variables */
382 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000383
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000384 /* autoloaded script names */
385 ga_clear_strings(&ga_loaded);
386
Bram Moolenaarcca74132013-09-25 21:00:28 +0200387 /* Script-local variables. First clear all the variables and in a second
388 * loop free the scriptvar_T, because a variable in one script might hold
389 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200392 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 ga_clear(&ga_scripts);
395
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000396 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200397 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000398
399 /* functions */
400 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000401}
402#endif
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 * Set an internal variable to a string value. Creates the variable if it does
407 * not already exist.
408 */
409 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100410set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000412 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000413 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415 val = vim_strsave(value);
416 if (val != NULL)
417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000418 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000419 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000421 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000422 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 }
424 }
425}
426
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200428#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000429static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static char_u *redir_endp = NULL;
431static char_u *redir_varname = NULL;
432
433/*
434 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100435 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000436 * Returns OK if successfully completed the setup. FAIL otherwise.
437 */
438 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100439var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000440{
441 int save_emsg;
442 int err;
443 typval_T tv;
444
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000445 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000446 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000447 {
448 EMSG(_(e_invarg));
449 return FAIL;
450 }
451
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000452 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000453 redir_varname = vim_strsave(name);
454 if (redir_varname == NULL)
455 return FAIL;
456
457 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
458 if (redir_lval == NULL)
459 {
460 var_redir_stop();
461 return FAIL;
462 }
463
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000464 /* The output is stored in growarray "redir_ga" until redirection ends. */
465 ga_init2(&redir_ga, (int)sizeof(char), 500);
466
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100468 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000469 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
471 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200472 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp != NULL && *redir_endp != NUL)
474 /* Trailing characters are present after the variable name */
475 EMSG(_(e_trailing));
476 else
477 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000478 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 var_redir_stop();
480 return FAIL;
481 }
482
483 /* check if we can write to the variable: set it to or append an empty
484 * string */
485 save_emsg = did_emsg;
486 did_emsg = FALSE;
487 tv.v_type = VAR_STRING;
488 tv.vval.v_string = (char_u *)"";
489 if (append)
490 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
491 else
492 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200493 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000495 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 if (err)
497 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000498 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 var_redir_stop();
500 return FAIL;
501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502
503 return OK;
504}
505
506/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000507 * Append "value[value_len]" to the variable set by var_redir_start().
508 * The actual appending is postponed until redirection ends, because the value
509 * appended may in fact be the string we write to, changing it may cause freed
510 * memory to be used:
511 * :redir => foo
512 * :let foo
513 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 */
515 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100516var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000518 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000519
520 if (redir_lval == NULL)
521 return;
522
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000523 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000524 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000526 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000529 {
530 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 }
533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535}
536
537/*
538 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000539 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 */
541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100542var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000544 typval_T tv;
545
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200546 if (EVALCMD_BUSY)
547 {
548 redir_lval = NULL;
549 return;
550 }
551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000552 if (redir_lval != NULL)
553 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000554 /* If there was no error: assign the text to the variable. */
555 if (redir_endp != NULL)
556 {
557 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
558 tv.v_type = VAR_STRING;
559 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200560 /* Call get_lval() again, if it's inside a Dict or List it may
561 * have changed. */
562 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100563 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200564 if (redir_endp != NULL && redir_lval->ll_name != NULL)
565 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
566 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000567 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000569 /* free the collected output */
570 vim_free(redir_ga.ga_data);
571 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000572
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573 vim_free(redir_lval);
574 redir_lval = NULL;
575 }
576 vim_free(redir_varname);
577 redir_varname = NULL;
578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaar48570482017-10-30 21:48:41 +0100699 static int
700eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
701{
702 char_u *s;
703 int dummy;
704 char_u buf[NUMBUFLEN];
705
706 if (expr->v_type == VAR_FUNC)
707 {
708 s = expr->vval.v_string;
709 if (s == NULL || *s == NUL)
710 return FAIL;
711 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
712 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
713 return FAIL;
714 }
715 else if (expr->v_type == VAR_PARTIAL)
716 {
717 partial_T *partial = expr->vval.v_partial;
718
719 s = partial_name(partial);
720 if (s == NULL || *s == NUL)
721 return FAIL;
722 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
723 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
724 return FAIL;
725 }
726 else
727 {
728 s = get_tv_string_buf_chk(expr, buf);
729 if (s == NULL)
730 return FAIL;
731 s = skipwhite(s);
732 if (eval1(&s, rettv, TRUE) == FAIL)
733 return FAIL;
734 if (*s != NUL) /* check for trailing chars after expr */
735 {
736 EMSG2(_(e_invexpr2), s);
737 return FAIL;
738 }
739 }
740 return OK;
741}
742
743/*
744 * Like eval_to_bool() but using a typval_T instead of a string.
745 * Works for string, funcref and partial.
746 */
747 int
748eval_expr_to_bool(typval_T *expr, int *error)
749{
750 typval_T rettv;
751 int res;
752
753 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
754 {
755 *error = TRUE;
756 return FALSE;
757 }
758 res = (get_tv_number_chk(&rettv, error) != 0);
759 clear_tv(&rettv);
760 return res;
761}
762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763/*
764 * Top level evaluation function, returning a string. If "skip" is TRUE,
765 * only parsing to "nextcmd" is done, without reporting errors. Return
766 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
767 */
768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100769eval_to_string_skip(
770 char_u *arg,
771 char_u **nextcmd,
772 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 char_u *retval;
776
777 if (skip)
778 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 retval = NULL;
781 else
782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783 retval = vim_strsave(get_tv_string(&tv));
784 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 if (skip)
787 --emsg_skip;
788
789 return retval;
790}
791
792/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000793 * Skip over an expression at "*pp".
794 * Return FAIL for an error, OK otherwise.
795 */
796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800
801 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000803}
804
805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000807 * When "convert" is TRUE convert a List into a sequence of lines and convert
808 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 * Return pointer to allocated memory, or NULL for failure.
810 */
811 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100812eval_to_string(
813 char_u *arg,
814 char_u **nextcmd,
815 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000819 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000820#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 retval = NULL;
826 else
827 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000828 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829 {
830 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000831 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200832 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200833 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200834 if (tv.vval.v_list->lv_len > 0)
835 ga_append(&ga, NL);
836 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837 ga_append(&ga, NUL);
838 retval = (char_u *)ga.ga_data;
839 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000840#ifdef FEAT_FLOAT
841 else if (convert && tv.v_type == VAR_FLOAT)
842 {
843 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
844 retval = vim_strsave(numbuf);
845 }
846#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000847 else
848 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
852 return retval;
853}
854
855/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000856 * Call eval_to_string() without using current local variables and using
857 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 */
859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100860eval_to_string_safe(
861 char_u *arg,
862 char_u **nextcmd,
863 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 char_u *retval;
866 void *save_funccalp;
867
868 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000869 if (use_sandbox)
870 ++sandbox;
871 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000872 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000873 if (use_sandbox)
874 --sandbox;
875 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 restore_funccal(save_funccalp);
877 return retval;
878}
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880/*
881 * Top level evaluation function, returning a number.
882 * Evaluates "expr" silently.
883 * Returns -1 for an error.
884 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200885 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200889 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000890 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 ++emsg_off;
893
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 retval = -1;
896 else
897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000898 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901 --emsg_off;
902
903 return retval;
904}
905
Bram Moolenaara40058a2005-07-11 22:42:07 +0000906/*
907 * Prepare v: variable "idx" to be used.
908 * Save the current typeval in "save_tv".
909 * When not used yet add the variable to the v: hashtable.
910 */
911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000913{
914 *save_tv = vimvars[idx].vv_tv;
915 if (vimvars[idx].vv_type == VAR_UNKNOWN)
916 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
917}
918
919/*
920 * Restore v: variable "idx" to typeval "save_tv".
921 * When no longer defined, remove the variable from the v: hashtable.
922 */
923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100924restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000925{
926 hashitem_T *hi;
927
Bram Moolenaara40058a2005-07-11 22:42:07 +0000928 vimvars[idx].vv_tv = *save_tv;
929 if (vimvars[idx].vv_type == VAR_UNKNOWN)
930 {
931 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
932 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100933 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000934 else
935 hash_remove(&vimvarht, hi);
936 }
937}
938
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000939#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000940/*
941 * Evaluate an expression to a list with suggestions.
942 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000943 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000944 */
945 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000947{
948 typval_T save_val;
949 typval_T rettv;
950 list_T *list = NULL;
951 char_u *p = skipwhite(expr);
952
953 /* Set "v:val" to the bad word. */
954 prepare_vimvar(VV_VAL, &save_val);
955 vimvars[VV_VAL].vv_type = VAR_STRING;
956 vimvars[VV_VAL].vv_str = badword;
957 if (p_verbose == 0)
958 ++emsg_off;
959
960 if (eval1(&p, &rettv, TRUE) == OK)
961 {
962 if (rettv.v_type != VAR_LIST)
963 clear_tv(&rettv);
964 else
965 list = rettv.vval.v_list;
966 }
967
968 if (p_verbose == 0)
969 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000970 restore_vimvar(VV_VAL, &save_val);
971
972 return list;
973}
974
975/*
976 * "list" is supposed to contain two items: a word and a number. Return the
977 * word in "pp" and the number as the return value.
978 * Return -1 if anything isn't right.
979 * Used to get the good word and score from the eval_spell_expr() result.
980 */
981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000983{
984 listitem_T *li;
985
986 li = list->lv_first;
987 if (li == NULL)
988 return -1;
989 *pp = get_tv_string(&li->li_tv);
990
991 li = li->li_next;
992 if (li == NULL)
993 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200994 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000995}
996#endif
997
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000999 * Top level evaluation function.
1000 * Returns an allocated typval_T with the result.
1001 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001002 */
1003 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001005{
1006 typval_T *tv;
1007
1008 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001009 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001010 {
1011 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001013 }
1014
1015 return tv;
1016}
1017
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001020 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001021 * Uses argv[argc] for the function arguments. Only Number and String
1022 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001023 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026call_vim_function(
1027 char_u *func,
1028 int argc,
1029 char_u **argv,
1030 int safe, /* use the sandbox */
1031 int str_arg_only, /* all arguments are strings */
1032 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001035 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int len;
1037 int i;
1038 int doesrange;
1039 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001040 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001042 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001044 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 for (i = 0; i < argc; i++)
1047 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001048 /* Pass a NULL or empty argument as an empty string */
1049 if (argv[i] == NULL || *argv[i] == NUL)
1050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001051 argvars[i].v_type = VAR_STRING;
1052 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001053 continue;
1054 }
1055
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001056 if (str_arg_only)
1057 len = 0;
1058 else
1059 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001060 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (len != 0 && len == (int)STRLEN(argv[i]))
1062 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001063 argvars[i].v_type = VAR_NUMBER;
1064 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else
1067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001068 argvars[i].v_type = VAR_STRING;
1069 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
1071 }
1072
1073 if (safe)
1074 {
1075 save_funccalp = save_funccal();
1076 ++sandbox;
1077 }
1078
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001079 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001080 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001082 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (safe)
1084 {
1085 --sandbox;
1086 restore_funccal(save_funccalp);
1087 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088 vim_free(argvars);
1089
1090 if (ret == FAIL)
1091 clear_tv(rettv);
1092
1093 return ret;
1094}
1095
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001096/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001097 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001098 * Returns -1 when calling the function fails.
1099 * Uses argv[argc] for the function arguments.
1100 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001101 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102call_func_retnr(
1103 char_u *func,
1104 int argc,
1105 char_u **argv,
1106 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001107{
1108 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001109 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001110
1111 /* All arguments are passed as strings, no conversion to number. */
1112 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1113 return -1;
1114
1115 retval = get_tv_number_chk(&rettv, NULL);
1116 clear_tv(&rettv);
1117 return retval;
1118}
1119
1120#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1121 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1122
Bram Moolenaar4f688582007-07-24 12:34:30 +00001123# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001124/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001125 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001126 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001127 * Uses argv[argc] for the function arguments.
1128 */
1129 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001130call_func_retstr(
1131 char_u *func,
1132 int argc,
1133 char_u **argv,
1134 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001135{
1136 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001137 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001138
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001139 /* All arguments are passed as strings, no conversion to number. */
1140 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141 return NULL;
1142
1143 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001144 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 return retval;
1146}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001147# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001148
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001149/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001150 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001151 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001152 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001153 */
1154 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001155call_func_retlist(
1156 char_u *func,
1157 int argc,
1158 char_u **argv,
1159 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001160{
1161 typval_T rettv;
1162
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001163 /* All arguments are passed as strings, no conversion to number. */
1164 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165 return NULL;
1166
1167 if (rettv.v_type != VAR_LIST)
1168 {
1169 clear_tv(&rettv);
1170 return NULL;
1171 }
1172
1173 return rettv.vval.v_list;
1174}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176
Bram Moolenaar05159a02005-02-26 23:04:13 +00001177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#ifdef FEAT_FOLDING
1179/*
1180 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1181 * it in "*cp". Doesn't give error messages.
1182 */
1183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001184eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
Bram Moolenaar33570922005-01-25 22:26:29 +00001186 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001187 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001189 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1190 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
1192 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001193 if (use_sandbox)
1194 ++sandbox;
1195 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 retval = 0;
1199 else
1200 {
1201 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 if (tv.v_type == VAR_NUMBER)
1203 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001204 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 retval = 0;
1206 else
1207 {
1208 /* If the result is a string, check if there is a non-digit before
1209 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (!VIM_ISDIGIT(*s) && *s != '-')
1212 *cp = *s++;
1213 retval = atol((char *)s);
1214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001218 if (use_sandbox)
1219 --sandbox;
1220 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001222 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223}
1224#endif
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001227 * ":let" list all variable values
1228 * ":let var1 var2" list variable values
1229 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001230 * ":let var += expr" assignment command.
1231 * ":let var -= expr" assignment command.
1232 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 */
1235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001236ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001239 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001240 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 int var_count = 0;
1243 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001245 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001246 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaardb552d602006-03-23 22:59:57 +00001248 argend = skip_var_list(arg, &var_count, &semicolon);
1249 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001250 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001251 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1252 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001253 expr = skipwhite(argend);
1254 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1255 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001257 /*
1258 * ":let" without "=": list variables
1259 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260 if (*arg == '[')
1261 EMSG(_(e_invarg));
1262 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001264 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001268 list_glob_vars(&first);
1269 list_buf_vars(&first);
1270 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001271 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001272 list_script_vars(&first);
1273 list_func_vars(&first);
1274 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 eap->nextcmd = check_nextcmd(arg);
1277 }
1278 else
1279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 op[0] = '=';
1281 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001282 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001284 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1285 op[0] = *expr; /* +=, -= or .= */
1286 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001288 else
1289 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (eap->skip)
1292 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001293 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (eap->skip)
1295 {
1296 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 --emsg_skip;
1299 }
1300 else if (i != FAIL)
1301 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001302 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001303 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 }
1307}
1308
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001309/*
1310 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1311 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001312 * When "nextchars" is not NULL it points to a string with characters that
1313 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1314 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001315 * Returns OK or FAIL;
1316 */
1317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318ex_let_vars(
1319 char_u *arg_start,
1320 typval_T *tv,
1321 int copy, /* copy values from "tv", don't move */
1322 int semicolon, /* from skip_var_list() */
1323 int var_count, /* from skip_var_list() */
1324 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001325{
1326 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001329 listitem_T *item;
1330 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001331
1332 if (*arg != '[')
1333 {
1334 /*
1335 * ":let var = expr" or ":for var in list"
1336 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001337 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001338 return FAIL;
1339 return OK;
1340 }
1341
1342 /*
1343 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1344 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001345 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001346 {
1347 EMSG(_(e_listreq));
1348 return FAIL;
1349 }
1350
1351 i = list_len(l);
1352 if (semicolon == 0 && var_count < i)
1353 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001354 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355 return FAIL;
1356 }
1357 if (var_count - semicolon > i)
1358 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001359 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001360 return FAIL;
1361 }
1362
1363 item = l->lv_first;
1364 while (*arg != ']')
1365 {
1366 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001367 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001368 item = item->li_next;
1369 if (arg == NULL)
1370 return FAIL;
1371
1372 arg = skipwhite(arg);
1373 if (*arg == ';')
1374 {
1375 /* Put the rest of the list (may be empty) in the var after ';'.
1376 * Create a new list for this. */
1377 l = list_alloc();
1378 if (l == NULL)
1379 return FAIL;
1380 while (item != NULL)
1381 {
1382 list_append_tv(l, &item->li_tv);
1383 item = item->li_next;
1384 }
1385
1386 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001387 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001388 ltv.vval.v_list = l;
1389 l->lv_refcount = 1;
1390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1392 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 clear_tv(&ltv);
1394 if (arg == NULL)
1395 return FAIL;
1396 break;
1397 }
1398 else if (*arg != ',' && *arg != ']')
1399 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001400 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 return FAIL;
1402 }
1403 }
1404
1405 return OK;
1406}
1407
1408/*
1409 * Skip over assignable variable "var" or list of variables "[var, var]".
1410 * Used for ":let varvar = expr" and ":for varvar in expr".
1411 * For "[var, var]" increment "*var_count" for each variable.
1412 * for "[var, var; var]" set "semicolon".
1413 * Return NULL for an error.
1414 */
1415 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001416skip_var_list(
1417 char_u *arg,
1418 int *var_count,
1419 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420{
1421 char_u *p, *s;
1422
1423 if (*arg == '[')
1424 {
1425 /* "[var, var]": find the matching ']'. */
1426 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001427 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001428 {
1429 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1430 s = skip_var_one(p);
1431 if (s == p)
1432 {
1433 EMSG2(_(e_invarg2), p);
1434 return NULL;
1435 }
1436 ++*var_count;
1437
1438 p = skipwhite(s);
1439 if (*p == ']')
1440 break;
1441 else if (*p == ';')
1442 {
1443 if (*semicolon == 1)
1444 {
1445 EMSG(_("Double ; in list of variables"));
1446 return NULL;
1447 }
1448 *semicolon = 1;
1449 }
1450 else if (*p != ',')
1451 {
1452 EMSG2(_(e_invarg2), p);
1453 return NULL;
1454 }
1455 }
1456 return p + 1;
1457 }
1458 else
1459 return skip_var_one(arg);
1460}
1461
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001462/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001463 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001464 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001465 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001467skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001468{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001469 if (*arg == '@' && arg[1] != NUL)
1470 return arg + 2;
1471 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1472 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001473}
1474
Bram Moolenaara7043832005-01-21 11:56:39 +00001475/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001476 * List variables for hashtab "ht" with prefix "prefix".
1477 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001478 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001480list_hashtable_vars(
1481 hashtab_T *ht,
1482 char_u *prefix,
1483 int empty,
1484 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001485{
Bram Moolenaar33570922005-01-25 22:26:29 +00001486 hashitem_T *hi;
1487 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001488 int todo;
1489
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001490 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001491 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1492 {
1493 if (!HASHITEM_EMPTY(hi))
1494 {
1495 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001496 di = HI2DI(hi);
1497 if (empty || di->di_tv.v_type != VAR_STRING
1498 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001499 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001500 }
1501 }
1502}
1503
1504/*
1505 * List global variables.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001509{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001510 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001511}
1512
1513/*
1514 * List buffer variables.
1515 */
1516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001517list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001518{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001519 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001520 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001521}
1522
1523/*
1524 * List window variables.
1525 */
1526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001527list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001528{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001529 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001530 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001531}
1532
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001533/*
1534 * List tab page variables.
1535 */
1536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001538{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001539 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001540 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001541}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001542
Bram Moolenaara7043832005-01-21 11:56:39 +00001543/*
1544 * List Vim variables.
1545 */
1546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001549 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001550}
1551
1552/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001553 * List script-local variables, if there is a script.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001557{
1558 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001559 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1560 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001561}
1562
1563/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 * List variables in "arg".
1565 */
1566 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001567list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001568{
1569 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001570 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 char_u *name_start;
1573 char_u *arg_subsc;
1574 char_u *tofree;
1575 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576
1577 while (!ends_excmd(*arg) && !got_int)
1578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001581 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001582 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583 {
1584 emsg_severe = TRUE;
1585 EMSG(_(e_trailing));
1586 break;
1587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001591 /* get_name_len() takes care of expanding curly braces */
1592 name_start = name = arg;
1593 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1594 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596 /* This is mainly to keep test 49 working: when expanding
1597 * curly braces fails overrule the exception error message. */
1598 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600 emsg_severe = TRUE;
1601 EMSG2(_(e_invarg2), arg);
1602 break;
1603 }
1604 error = TRUE;
1605 }
1606 else
1607 {
1608 if (tofree != NULL)
1609 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001610 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 else
1613 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001614 /* handle d.key, l[idx], f(expr) */
1615 arg_subsc = arg;
1616 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001617 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001620 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001621 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001622 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001623 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001624 case 'g': list_glob_vars(first); break;
1625 case 'b': list_buf_vars(first); break;
1626 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001627 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001628 case 'v': list_vim_vars(first); break;
1629 case 's': list_script_vars(first); break;
1630 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001631 default:
1632 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001633 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001634 }
1635 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636 {
1637 char_u numbuf[NUMBUFLEN];
1638 char_u *tf;
1639 int c;
1640 char_u *s;
1641
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001642 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001643 c = *arg;
1644 *arg = NUL;
1645 list_one_var_a((char_u *)"",
1646 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001647 tv.v_type,
1648 s == NULL ? (char_u *)"" : s,
1649 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001650 *arg = c;
1651 vim_free(tf);
1652 }
1653 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 }
1656 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001657
1658 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001660
1661 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 }
1663
1664 return arg;
1665}
1666
1667/*
1668 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1669 * Returns a pointer to the char just after the var name.
1670 * Returns NULL if there is an error.
1671 */
1672 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001673ex_let_one(
1674 char_u *arg, /* points to variable name */
1675 typval_T *tv, /* value to assign to variable */
1676 int copy, /* copy value from "tv" */
1677 char_u *endchars, /* valid chars after variable name or NULL */
1678 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679{
1680 int c1;
1681 char_u *name;
1682 char_u *p;
1683 char_u *arg_end = NULL;
1684 int len;
1685 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687
1688 /*
1689 * ":let $VAR = expr": Set environment variable.
1690 */
1691 if (*arg == '$')
1692 {
1693 /* Find the end of the name. */
1694 ++arg;
1695 name = arg;
1696 len = get_env_len(&arg);
1697 if (len == 0)
1698 EMSG2(_(e_invarg2), name - 1);
1699 else
1700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 if (op != NULL && (*op == '+' || *op == '-'))
1702 EMSG2(_(e_letwrong), op);
1703 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001705 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001706 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001707 {
1708 c1 = name[len];
1709 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001710 p = get_tv_string_chk(tv);
1711 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 {
1713 int mustfree = FALSE;
1714 char_u *s = vim_getenv(name, &mustfree);
1715
1716 if (s != NULL)
1717 {
1718 p = tofree = concat_str(s, p);
1719 if (mustfree)
1720 vim_free(s);
1721 }
1722 }
1723 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001726 if (STRICMP(name, "HOME") == 0)
1727 init_homedir();
1728 else if (didset_vim && STRICMP(name, "VIM") == 0)
1729 didset_vim = FALSE;
1730 else if (didset_vimruntime
1731 && STRICMP(name, "VIMRUNTIME") == 0)
1732 didset_vimruntime = FALSE;
1733 arg_end = arg;
1734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001737 }
1738 }
1739 }
1740
1741 /*
1742 * ":let &option = expr": Set option value.
1743 * ":let &l:option = expr": Set local option value.
1744 * ":let &g:option = expr": Set global option value.
1745 */
1746 else if (*arg == '&')
1747 {
1748 /* Find the end of the name. */
1749 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 if (p == NULL || (endchars != NULL
1751 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 EMSG(_(e_letunexp));
1753 else
1754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 long n;
1756 int opt_type;
1757 long numval;
1758 char_u *stringval = NULL;
1759 char_u *s;
1760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 c1 = *p;
1762 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001764 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001765 s = get_tv_string_chk(tv); /* != NULL if number or string */
1766 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767 {
1768 opt_type = get_option_value(arg, &numval,
1769 &stringval, opt_flags);
1770 if ((opt_type == 1 && *op == '.')
1771 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001774 s = NULL; /* don't set the value */
1775 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 else
1777 {
1778 if (opt_type == 1) /* number */
1779 {
1780 if (*op == '+')
1781 n = numval + n;
1782 else
1783 n = numval - n;
1784 }
1785 else if (opt_type == 0 && stringval != NULL) /* string */
1786 {
1787 s = concat_str(stringval, s);
1788 vim_free(stringval);
1789 stringval = s;
1790 }
1791 }
1792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001793 if (s != NULL)
1794 {
1795 set_option_value(arg, n, s, opt_flags);
1796 arg_end = p;
1797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 }
1801 }
1802
1803 /*
1804 * ":let @r = expr": Set register contents.
1805 */
1806 else if (*arg == '@')
1807 {
1808 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001809 if (op != NULL && (*op == '+' || *op == '-'))
1810 EMSG2(_(e_letwrong), op);
1811 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 EMSG(_(e_letunexp));
1814 else
1815 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001816 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 char_u *s;
1818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001819 p = get_tv_string_chk(tv);
1820 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001822 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 if (s != NULL)
1824 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001825 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 vim_free(s);
1827 }
1828 }
1829 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001830 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001832 arg_end = arg + 1;
1833 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001834 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 }
1836 }
1837
1838 /*
1839 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001842 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001846 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001849 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1850 EMSG(_(e_letunexp));
1851 else
1852 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001853 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 arg_end = p;
1855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001857 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 }
1859
1860 else
1861 EMSG2(_(e_invarg2), arg);
1862
1863 return arg_end;
1864}
1865
1866/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 * Get an lval: variable, Dict item or List item that can be assigned a value
1868 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1869 * "name.key", "name.key[expr]" etc.
1870 * Indexing only works if "name" is an existing List or Dictionary.
1871 * "name" points to the start of the name.
1872 * If "rettv" is not NULL it points to the value to be assigned.
1873 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1874 * wrong; must end in space or cmd separator.
1875 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001876 * flags:
1877 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001878 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001879 * GLV_NO_AUTOLOAD: do not use script autoloading
1880 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001882 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001883 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001886get_lval(
1887 char_u *name,
1888 typval_T *rettv,
1889 lval_T *lp,
1890 int unlet,
1891 int skip,
1892 int flags, /* GLV_ values */
1893 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 char_u *expr_start, *expr_end;
1897 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 dictitem_T *v;
1899 typval_T var1;
1900 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001903 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001904 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001906 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910
1911 if (skip)
1912 {
1913 /* When skipping just find the end of the name. */
1914 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001915 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 }
1917
1918 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001919 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 if (expr_start != NULL)
1921 {
1922 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001923 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 && *p != '[' && *p != '.')
1925 {
1926 EMSG(_(e_trailing));
1927 return NULL;
1928 }
1929
1930 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1931 if (lp->ll_exp_name == NULL)
1932 {
1933 /* Report an invalid expression in braces, unless the
1934 * expression evaluation has been cancelled due to an
1935 * aborting error, an interrupt, or an exception. */
1936 if (!aborting() && !quiet)
1937 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001938 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 EMSG2(_(e_invarg2), name);
1940 return NULL;
1941 }
1942 }
1943 lp->ll_name = lp->ll_exp_name;
1944 }
1945 else
1946 lp->ll_name = name;
1947
1948 /* Without [idx] or .key we are done. */
1949 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1950 return p;
1951
1952 cc = *p;
1953 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001954 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (v == NULL && !quiet)
1956 EMSG2(_(e_undefvar), lp->ll_name);
1957 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 if (v == NULL)
1959 return NULL;
1960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 /*
1962 * Loop until no more [idx] or .key is following.
1963 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001965 var1.v_type = VAR_UNKNOWN;
1966 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1970 && !(lp->ll_tv->v_type == VAR_DICT
1971 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 if (!quiet)
1974 EMSG(_("E689: Can only index a List or Dictionary"));
1975 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!quiet)
1980 EMSG(_("E708: [:] must come last"));
1981 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 len = -1;
1985 if (*p == '.')
1986 {
1987 key = p + 1;
1988 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1989 ;
1990 if (len == 0)
1991 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001992 if (!quiet)
1993 EMSG(_(e_emptykey));
1994 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001995 }
1996 p = key + len;
1997 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001998 else
1999 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002000 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002001 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002002 if (*p == ':')
2003 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002004 else
2005 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 empty1 = FALSE;
2007 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002008 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002009 if (get_tv_string_chk(&var1) == NULL)
2010 {
2011 /* not a number or string */
2012 clear_tv(&var1);
2013 return NULL;
2014 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 }
2016
2017 /* Optionally get the second index [ :expr]. */
2018 if (*p == ':')
2019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002024 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 if (rettv != NULL && (rettv->v_type != VAR_LIST
2028 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (!quiet)
2031 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034 }
2035 p = skipwhite(p + 1);
2036 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002038 else
2039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2042 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002043 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002044 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002046 if (get_tv_string_chk(&var2) == NULL)
2047 {
2048 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002049 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002050 clear_tv(&var2);
2051 return NULL;
2052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002056 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002058
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 if (!quiet)
2062 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002063 clear_tv(&var1);
2064 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 }
2067
2068 /* Skip to past ']'. */
2069 ++p;
2070 }
2071
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 {
2074 if (len == -1)
2075 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002077 key = get_tv_string_chk(&var1); /* is number or string */
2078 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002080 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 }
2083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084 lp->ll_list = NULL;
2085 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002086 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002087
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002088 /* When assigning to a scope dictionary check that a function and
2089 * variable name is valid (only variable name unless it is l: or
2090 * g: dictionary). Disallow overwriting a builtin function. */
2091 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002092 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002093 int prevval;
2094 int wrong;
2095
2096 if (len != -1)
2097 {
2098 prevval = key[len];
2099 key[len] = NUL;
2100 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002101 else
2102 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002103 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2104 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002105 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002106 || !valid_varname(key);
2107 if (len != -1)
2108 key[len] = prevval;
2109 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002110 return NULL;
2111 }
2112
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002115 /* Can't add "v:" variable. */
2116 if (lp->ll_dict == &vimvardict)
2117 {
2118 EMSG2(_(e_illvar), name);
2119 return NULL;
2120 }
2121
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002122 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002127 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 }
2130 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002132 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002134 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002136 p = NULL;
2137 break;
2138 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002139 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002140 else if ((flags & GLV_READ_ONLY) == 0
2141 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002142 {
2143 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002144 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002145 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002146
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002148 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 }
2150 else
2151 {
2152 /*
2153 * Get the number and item for the only or first index of the List.
2154 */
2155 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002157 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002158 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002159 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002160 clear_tv(&var1);
2161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 lp->ll_list = lp->ll_tv->vval.v_list;
2164 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2165 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002166 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002167 if (lp->ll_n1 < 0)
2168 {
2169 lp->ll_n1 = 0;
2170 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2171 }
2172 }
2173 if (lp->ll_li == NULL)
2174 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002175 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002176 if (!quiet)
2177 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002179 }
2180
2181 /*
2182 * May need to find the item or absolute index for the second
2183 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 * When no index given: "lp->ll_empty2" is TRUE.
2185 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002188 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002189 lp->ll_n2 = (long)get_tv_number(&var2);
2190 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002191 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002195 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002196 {
2197 if (!quiet)
2198 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002200 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002202 }
2203
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2205 if (lp->ll_n1 < 0)
2206 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2207 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002208 {
2209 if (!quiet)
2210 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002212 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002213 }
2214
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 }
2218
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002219 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 return p;
2221}
2222
2223/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002224 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228{
2229 vim_free(lp->ll_exp_name);
2230 vim_free(lp->ll_newkey);
2231}
2232
2233/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002234 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239set_var_lval(
2240 lval_T *lp,
2241 char_u *endp,
2242 typval_T *rettv,
2243 int copy,
2244 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245{
2246 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002247 listitem_T *ri;
2248 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249
2250 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002252 cc = *endp;
2253 *endp = NUL;
2254 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002256 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257
Bram Moolenaar79518e22017-02-17 16:31:35 +01002258 /* handle +=, -= and .= */
2259 di = NULL;
2260 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2261 &tv, &di, TRUE, FALSE) == OK)
2262 {
2263 if ((di == NULL
2264 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2265 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2266 FALSE)))
2267 && tv_op(&tv, rettv, op) == OK)
2268 set_var(lp->ll_name, &tv, FALSE);
2269 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002272 else
2273 set_var(lp->ll_name, rettv, copy);
2274 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002276 else if (tv_check_lock(lp->ll_newkey == NULL
2277 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002278 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002279 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 else if (lp->ll_range)
2281 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002282 listitem_T *ll_li = lp->ll_li;
2283 int ll_n1 = lp->ll_n1;
2284
2285 /*
2286 * Check whether any of the list items is locked
2287 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002288 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002289 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002290 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002291 return;
2292 ri = ri->li_next;
2293 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2294 break;
2295 ll_li = ll_li->li_next;
2296 ++ll_n1;
2297 }
2298
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 /*
2300 * Assign the List values to the list items.
2301 */
2302 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002303 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 if (op != NULL && *op != '=')
2305 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2306 else
2307 {
2308 clear_tv(&lp->ll_li->li_tv);
2309 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2310 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311 ri = ri->li_next;
2312 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2313 break;
2314 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002317 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 ri = NULL;
2320 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002321 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002322 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 lp->ll_li = lp->ll_li->li_next;
2324 ++lp->ll_n1;
2325 }
2326 if (ri != NULL)
2327 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 else if (lp->ll_empty2
2329 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 : lp->ll_n1 != lp->ll_n2)
2331 EMSG(_("E711: List value has not enough items"));
2332 }
2333 else
2334 {
2335 /*
2336 * Assign to a List or Dictionary item.
2337 */
2338 if (lp->ll_newkey != NULL)
2339 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 if (op != NULL && *op != '=')
2341 {
2342 EMSG2(_(e_letwrong), op);
2343 return;
2344 }
2345
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002347 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 if (di == NULL)
2349 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002350 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2351 {
2352 vim_free(di);
2353 return;
2354 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002356 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 else if (op != NULL && *op != '=')
2358 {
2359 tv_op(lp->ll_tv, rettv, op);
2360 return;
2361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 /*
2366 * Assign the value to the variable or list item.
2367 */
2368 if (copy)
2369 copy_tv(rettv, lp->ll_tv);
2370 else
2371 {
2372 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002373 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 }
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377}
2378
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2381 * Returns OK or FAIL.
2382 */
2383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002384tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002386 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 char_u numbuf[NUMBUFLEN];
2388 char_u *s;
2389
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002390 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2391 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2392 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 {
2394 switch (tv1->v_type)
2395 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002396 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 case VAR_DICT:
2398 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002399 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002400 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002401 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002402 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 break;
2404
2405 case VAR_LIST:
2406 if (*op != '+' || tv2->v_type != VAR_LIST)
2407 break;
2408 /* List += List */
2409 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2410 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2411 return OK;
2412
2413 case VAR_NUMBER:
2414 case VAR_STRING:
2415 if (tv2->v_type == VAR_LIST)
2416 break;
2417 if (*op == '+' || *op == '-')
2418 {
2419 /* nr += nr or nr -= nr*/
2420 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002421#ifdef FEAT_FLOAT
2422 if (tv2->v_type == VAR_FLOAT)
2423 {
2424 float_T f = n;
2425
2426 if (*op == '+')
2427 f += tv2->vval.v_float;
2428 else
2429 f -= tv2->vval.v_float;
2430 clear_tv(tv1);
2431 tv1->v_type = VAR_FLOAT;
2432 tv1->vval.v_float = f;
2433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002435#endif
2436 {
2437 if (*op == '+')
2438 n += get_tv_number(tv2);
2439 else
2440 n -= get_tv_number(tv2);
2441 clear_tv(tv1);
2442 tv1->v_type = VAR_NUMBER;
2443 tv1->vval.v_number = n;
2444 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 }
2446 else
2447 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002448 if (tv2->v_type == VAR_FLOAT)
2449 break;
2450
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 /* str .= str */
2452 s = get_tv_string(tv1);
2453 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2454 clear_tv(tv1);
2455 tv1->v_type = VAR_STRING;
2456 tv1->vval.v_string = s;
2457 }
2458 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002459
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002460 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002461#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002462 {
2463 float_T f;
2464
2465 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2466 && tv2->v_type != VAR_NUMBER
2467 && tv2->v_type != VAR_STRING))
2468 break;
2469 if (tv2->v_type == VAR_FLOAT)
2470 f = tv2->vval.v_float;
2471 else
2472 f = get_tv_number(tv2);
2473 if (*op == '+')
2474 tv1->vval.v_float += f;
2475 else
2476 tv1->vval.v_float -= f;
2477 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002478#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002479 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 }
2481 }
2482
2483 EMSG2(_(e_letwrong), op);
2484 return FAIL;
2485}
2486
2487/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488 * Evaluate the expression used in a ":for var in expr" command.
2489 * "arg" points to "var".
2490 * Set "*errp" to TRUE for an error, FALSE otherwise;
2491 * Return a pointer that holds the info. Null when there is an error.
2492 */
2493 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494eval_for_line(
2495 char_u *arg,
2496 int *errp,
2497 char_u **nextcmdp,
2498 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002499{
Bram Moolenaar33570922005-01-25 22:26:29 +00002500 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002501 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002502 typval_T tv;
2503 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504
2505 *errp = TRUE; /* default: there is an error */
2506
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002508 if (fi == NULL)
2509 return NULL;
2510
2511 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2512 if (expr == NULL)
2513 return fi;
2514
2515 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002516 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002517 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002518 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002519 return fi;
2520 }
2521
2522 if (skip)
2523 ++emsg_skip;
2524 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2525 {
2526 *errp = FALSE;
2527 if (!skip)
2528 {
2529 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002530 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002531 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002532 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002533 clear_tv(&tv);
2534 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002535 else if (l == NULL)
2536 {
2537 /* a null list is like an empty list: do nothing */
2538 clear_tv(&tv);
2539 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002540 else
2541 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002542 /* No need to increment the refcount, it's already set for the
2543 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002544 fi->fi_list = l;
2545 list_add_watch(l, &fi->fi_lw);
2546 fi->fi_lw.lw_item = l->lv_first;
2547 }
2548 }
2549 }
2550 if (skip)
2551 --emsg_skip;
2552
2553 return fi;
2554}
2555
2556/*
2557 * Use the first item in a ":for" list. Advance to the next.
2558 * Assign the values to the variable (list). "arg" points to the first one.
2559 * Return TRUE when a valid item was found, FALSE when at end of list or
2560 * something wrong.
2561 */
2562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002563next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002564{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002565 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002566 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002568
2569 item = fi->fi_lw.lw_item;
2570 if (item == NULL)
2571 result = FALSE;
2572 else
2573 {
2574 fi->fi_lw.lw_item = item->li_next;
2575 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2576 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2577 }
2578 return result;
2579}
2580
2581/*
2582 * Free the structure used to store info used by ":for".
2583 */
2584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002585free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002586{
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002588
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002589 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002590 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002591 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002592 list_unref(fi->fi_list);
2593 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594 vim_free(fi);
2595}
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2598
2599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002600set_context_for_expression(
2601 expand_T *xp,
2602 char_u *arg,
2603 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 int got_eq = FALSE;
2606 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002607 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002609 if (cmdidx == CMD_let)
2610 {
2611 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002612 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002613 {
2614 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002615 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002616 {
2617 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002618 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002619 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002620 break;
2621 }
2622 return;
2623 }
2624 }
2625 else
2626 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2627 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 while ((xp->xp_pattern = vim_strpbrk(arg,
2629 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2630 {
2631 c = *xp->xp_pattern;
2632 if (c == '&')
2633 {
2634 c = xp->xp_pattern[1];
2635 if (c == '&')
2636 {
2637 ++xp->xp_pattern;
2638 xp->xp_context = cmdidx != CMD_let || got_eq
2639 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2640 }
2641 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002644 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2645 xp->xp_pattern += 2;
2646
2647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 else if (c == '$')
2650 {
2651 /* environment variable */
2652 xp->xp_context = EXPAND_ENV_VARS;
2653 }
2654 else if (c == '=')
2655 {
2656 got_eq = TRUE;
2657 xp->xp_context = EXPAND_EXPRESSION;
2658 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002659 else if (c == '#'
2660 && xp->xp_context == EXPAND_EXPRESSION)
2661 {
2662 /* Autoload function/variable contains '#'. */
2663 break;
2664 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002665 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 && xp->xp_context == EXPAND_FUNCTIONS
2667 && vim_strchr(xp->xp_pattern, '(') == NULL)
2668 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002669 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 break;
2671 }
2672 else if (cmdidx != CMD_let || got_eq)
2673 {
2674 if (c == '"') /* string */
2675 {
2676 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2677 if (c == '\\' && xp->xp_pattern[1] != NUL)
2678 ++xp->xp_pattern;
2679 xp->xp_context = EXPAND_NOTHING;
2680 }
2681 else if (c == '\'') /* literal string */
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2685 /* skip */ ;
2686 xp->xp_context = EXPAND_NOTHING;
2687 }
2688 else if (c == '|')
2689 {
2690 if (xp->xp_pattern[1] == '|')
2691 {
2692 ++xp->xp_pattern;
2693 xp->xp_context = EXPAND_EXPRESSION;
2694 }
2695 else
2696 xp->xp_context = EXPAND_COMMANDS;
2697 }
2698 else
2699 xp->xp_context = EXPAND_EXPRESSION;
2700 }
2701 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002702 /* Doesn't look like something valid, expand as an expression
2703 * anyway. */
2704 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 arg = xp->xp_pattern;
2706 if (*arg != NUL)
2707 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2708 /* skip */ ;
2709 }
2710 xp->xp_pattern = arg;
2711}
2712
2713#endif /* FEAT_CMDL_COMPL */
2714
2715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 * ":unlet[!] var1 ... " command.
2717 */
2718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002719ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002721 ex_unletlock(eap, eap->arg, 0);
2722}
2723
2724/*
2725 * ":lockvar" and ":unlockvar" commands
2726 */
2727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002728ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002729{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002731 int deep = 2;
2732
2733 if (eap->forceit)
2734 deep = -1;
2735 else if (vim_isdigit(*arg))
2736 {
2737 deep = getdigits(&arg);
2738 arg = skipwhite(arg);
2739 }
2740
2741 ex_unletlock(eap, arg, deep);
2742}
2743
2744/*
2745 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2746 */
2747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002748ex_unletlock(
2749 exarg_T *eap,
2750 char_u *argstart,
2751 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002752{
2753 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002756 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
2758 do
2759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002761 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002762 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lv.ll_name == NULL)
2764 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002765 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (name_end != NULL)
2769 {
2770 emsg_severe = TRUE;
2771 EMSG(_(e_trailing));
2772 }
2773 if (!(eap->skip || error))
2774 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 break;
2776 }
2777
2778 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002779 {
2780 if (eap->cmdidx == CMD_unlet)
2781 {
2782 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2783 error = TRUE;
2784 }
2785 else
2786 {
2787 if (do_lock_var(&lv, name_end, deep,
2788 eap->cmdidx == CMD_lockvar) == FAIL)
2789 error = TRUE;
2790 }
2791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!eap->skip)
2794 clear_lval(&lv);
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 arg = skipwhite(name_end);
2797 } while (!ends_excmd(*arg));
2798
2799 eap->nextcmd = check_nextcmd(arg);
2800}
2801
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002803do_unlet_var(
2804 lval_T *lp,
2805 char_u *name_end,
2806 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 int ret = OK;
2809 int cc;
2810
2811 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 cc = *name_end;
2814 *name_end = NUL;
2815
2816 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002817 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002821 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002822 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002823 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002824 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002825 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 else if (lp->ll_range)
2827 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002829 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002830 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002831
2832 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2833 {
2834 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002835 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002836 return FAIL;
2837 ll_li = li;
2838 ++ll_n1;
2839 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840
2841 /* Delete a range of List items. */
2842 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2843 {
2844 li = lp->ll_li->li_next;
2845 listitem_remove(lp->ll_list, lp->ll_li);
2846 lp->ll_li = li;
2847 ++lp->ll_n1;
2848 }
2849 }
2850 else
2851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 /* unlet a List item. */
2854 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002857 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 }
2859
2860 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861}
2862
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863/*
2864 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002865 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 */
2867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 hashtab_T *ht;
2871 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002872 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002873 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002874 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 ht = find_var_ht(name, &varname);
2877 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002880 if (d == NULL)
2881 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 if (ht == &globvarht)
2883 d = &globvardict;
2884 else if (ht == &compat_hashtab)
2885 d = &vimvardict;
2886 else
2887 {
2888 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2889 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2890 }
2891 if (d == NULL)
2892 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002893 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002894 return FAIL;
2895 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002896 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002898 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002899 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002900 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002901 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002902 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002903 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002904 || var_check_ro(di->di_flags, name, FALSE)
2905 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002906 return FAIL;
2907
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002908 delete_var(ht, hi);
2909 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 if (forceit)
2913 return OK;
2914 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 return FAIL;
2916}
2917
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918/*
2919 * Lock or unlock variable indicated by "lp".
2920 * "deep" is the levels to go (-1 for unlimited);
2921 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2922 */
2923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924do_lock_var(
2925 lval_T *lp,
2926 char_u *name_end,
2927 int deep,
2928 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002929{
2930 int ret = OK;
2931 int cc;
2932 dictitem_T *di;
2933
2934 if (deep == 0) /* nothing to do */
2935 return OK;
2936
2937 if (lp->ll_tv == NULL)
2938 {
2939 cc = *name_end;
2940 *name_end = NUL;
2941
2942 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002943 di = find_var(lp->ll_name, NULL, TRUE);
2944 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002946 else if ((di->di_flags & DI_FLAGS_FIX)
2947 && di->di_tv.v_type != VAR_DICT
2948 && di->di_tv.v_type != VAR_LIST)
2949 /* For historic reasons this error is not given for a list or dict.
2950 * E.g., the b: dict could be locked/unlocked. */
2951 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 else
2953 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002954 if (lock)
2955 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002957 di->di_flags &= ~DI_FLAGS_LOCK;
2958 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 }
2960 *name_end = cc;
2961 }
2962 else if (lp->ll_range)
2963 {
2964 listitem_T *li = lp->ll_li;
2965
2966 /* (un)lock a range of List items. */
2967 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2968 {
2969 item_lock(&li->li_tv, deep, lock);
2970 li = li->li_next;
2971 ++lp->ll_n1;
2972 }
2973 }
2974 else if (lp->ll_list != NULL)
2975 /* (un)lock a List item. */
2976 item_lock(&lp->ll_li->li_tv, deep, lock);
2977 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002978 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 item_lock(&lp->ll_di->di_tv, deep, lock);
2980
2981 return ret;
2982}
2983
2984/*
2985 * Lock or unlock an item. "deep" is nr of levels to go.
2986 */
2987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002988item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002989{
2990 static int recurse = 0;
2991 list_T *l;
2992 listitem_T *li;
2993 dict_T *d;
2994 hashitem_T *hi;
2995 int todo;
2996
2997 if (recurse >= DICT_MAXNEST)
2998 {
2999 EMSG(_("E743: variable nested too deep for (un)lock"));
3000 return;
3001 }
3002 if (deep == 0)
3003 return;
3004 ++recurse;
3005
3006 /* lock/unlock the item itself */
3007 if (lock)
3008 tv->v_lock |= VAR_LOCKED;
3009 else
3010 tv->v_lock &= ~VAR_LOCKED;
3011
3012 switch (tv->v_type)
3013 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003014 case VAR_UNKNOWN:
3015 case VAR_NUMBER:
3016 case VAR_STRING:
3017 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003018 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003019 case VAR_FLOAT:
3020 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003021 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003022 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003023 break;
3024
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003025 case VAR_LIST:
3026 if ((l = tv->vval.v_list) != NULL)
3027 {
3028 if (lock)
3029 l->lv_lock |= VAR_LOCKED;
3030 else
3031 l->lv_lock &= ~VAR_LOCKED;
3032 if (deep < 0 || deep > 1)
3033 /* recursive: lock/unlock the items the List contains */
3034 for (li = l->lv_first; li != NULL; li = li->li_next)
3035 item_lock(&li->li_tv, deep - 1, lock);
3036 }
3037 break;
3038 case VAR_DICT:
3039 if ((d = tv->vval.v_dict) != NULL)
3040 {
3041 if (lock)
3042 d->dv_lock |= VAR_LOCKED;
3043 else
3044 d->dv_lock &= ~VAR_LOCKED;
3045 if (deep < 0 || deep > 1)
3046 {
3047 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003048 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003049 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3050 {
3051 if (!HASHITEM_EMPTY(hi))
3052 {
3053 --todo;
3054 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3055 }
3056 }
3057 }
3058 }
3059 }
3060 --recurse;
3061}
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3064/*
3065 * Delete all "menutrans_" variables.
3066 */
3067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003071 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaar33570922005-01-25 22:26:29 +00003073 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003074 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003075 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003076 {
3077 if (!HASHITEM_EMPTY(hi))
3078 {
3079 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3081 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003082 }
3083 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003084 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085}
3086#endif
3087
3088#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3089
3090/*
3091 * Local string buffer for the next two functions to store a variable name
3092 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3093 * get_user_var_name().
3094 */
3095
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003096static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098static char_u *varnamebuf = NULL;
3099static int varnamebuflen = 0;
3100
3101/*
3102 * Function to concatenate a prefix and a variable name.
3103 */
3104 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 int len;
3108
3109 len = (int)STRLEN(name) + 3;
3110 if (len > varnamebuflen)
3111 {
3112 vim_free(varnamebuf);
3113 len += 10; /* some additional space */
3114 varnamebuf = alloc(len);
3115 if (varnamebuf == NULL)
3116 {
3117 varnamebuflen = 0;
3118 return NULL;
3119 }
3120 varnamebuflen = len;
3121 }
3122 *varnamebuf = prefix;
3123 varnamebuf[1] = ':';
3124 STRCPY(varnamebuf + 2, name);
3125 return varnamebuf;
3126}
3127
3128/*
3129 * Function given to ExpandGeneric() to obtain the list of user defined
3130 * (global/buffer/window/built-in) variable names.
3131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003133get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003135 static long_u gdone;
3136 static long_u bdone;
3137 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003138 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003139 static int vidx;
3140 static hashitem_T *hi;
3141 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003144 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003145 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003146 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003147 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003148
3149 /* Global variables */
3150 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003152 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003153 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003154 else
3155 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003156 while (HASHITEM_EMPTY(hi))
3157 ++hi;
3158 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3159 return cat_prefix_varname('g', hi->hi_key);
3160 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003162
3163 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003164 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003167 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003169 else
3170 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003171 while (HASHITEM_EMPTY(hi))
3172 ++hi;
3173 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003175
3176 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003177 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003180 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003182 else
3183 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003184 while (HASHITEM_EMPTY(hi))
3185 ++hi;
3186 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003188
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003189 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003190 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003191 if (tdone < ht->ht_used)
3192 {
3193 if (tdone++ == 0)
3194 hi = ht->ht_array;
3195 else
3196 ++hi;
3197 while (HASHITEM_EMPTY(hi))
3198 ++hi;
3199 return cat_prefix_varname('t', hi->hi_key);
3200 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003201
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 /* v: variables */
3203 if (vidx < VV_LEN)
3204 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 vim_free(varnamebuf);
3207 varnamebuf = NULL;
3208 varnamebuflen = 0;
3209 return NULL;
3210}
3211
3212#endif /* FEAT_CMDL_COMPL */
3213
3214/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003215 * Return TRUE if "pat" matches "text".
3216 * Does not use 'cpo' and always uses 'magic'.
3217 */
3218 static int
3219pattern_match(char_u *pat, char_u *text, int ic)
3220{
3221 int matches = FALSE;
3222 char_u *save_cpo;
3223 regmatch_T regmatch;
3224
3225 /* avoid 'l' flag in 'cpoptions' */
3226 save_cpo = p_cpo;
3227 p_cpo = (char_u *)"";
3228 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3229 if (regmatch.regprog != NULL)
3230 {
3231 regmatch.rm_ic = ic;
3232 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3233 vim_regfree(regmatch.regprog);
3234 }
3235 p_cpo = save_cpo;
3236 return matches;
3237}
3238
3239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * types for expressions.
3241 */
3242typedef enum
3243{
3244 TYPE_UNKNOWN = 0
3245 , TYPE_EQUAL /* == */
3246 , TYPE_NEQUAL /* != */
3247 , TYPE_GREATER /* > */
3248 , TYPE_GEQUAL /* >= */
3249 , TYPE_SMALLER /* < */
3250 , TYPE_SEQUAL /* <= */
3251 , TYPE_MATCH /* =~ */
3252 , TYPE_NOMATCH /* !~ */
3253} exptype_T;
3254
3255/*
3256 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003257 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3259 */
3260
3261/*
3262 * Handle zero level expression.
3263 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003264 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003265 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * Return OK or FAIL.
3267 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269eval0(
3270 char_u *arg,
3271 typval_T *rettv,
3272 char_u **nextcmd,
3273 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 int ret;
3276 char_u *p;
3277
3278 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (ret == FAIL || !ends_excmd(*p))
3281 {
3282 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 /*
3285 * Report the invalid expression unless the expression evaluation has
3286 * been cancelled due to an aborting error, an interrupt, or an
3287 * exception.
3288 */
3289 if (!aborting())
3290 EMSG2(_(e_invexpr2), arg);
3291 ret = FAIL;
3292 }
3293 if (nextcmd != NULL)
3294 *nextcmd = check_nextcmd(p);
3295
3296 return ret;
3297}
3298
3299/*
3300 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003301 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 *
3303 * "arg" must point to the first non-white of the expression.
3304 * "arg" is advanced to the next non-white after the recognized expression.
3305 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003306 * Note: "rettv.v_lock" is not set.
3307 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 * Return OK or FAIL.
3309 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003311eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003314 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 /*
3317 * Get the first variable.
3318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003319 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 return FAIL;
3321
3322 if ((*arg)[0] == '?')
3323 {
3324 result = FALSE;
3325 if (evaluate)
3326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003327 int error = FALSE;
3328
3329 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 if (error)
3333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335
3336 /*
3337 * Get the second variable.
3338 */
3339 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003340 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 return FAIL;
3342
3343 /*
3344 * Check for the ":".
3345 */
3346 if ((*arg)[0] != ':')
3347 {
3348 EMSG(_("E109: Missing ':' after '?'"));
3349 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 return FAIL;
3352 }
3353
3354 /*
3355 * Get the third variable.
3356 */
3357 *arg = skipwhite(*arg + 1);
3358 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3359 {
3360 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003361 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 return FAIL;
3363 }
3364 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003365 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367
3368 return OK;
3369}
3370
3371/*
3372 * Handle first level expression:
3373 * expr2 || expr2 || expr2 logical OR
3374 *
3375 * "arg" must point to the first non-white of the expression.
3376 * "arg" is advanced to the next non-white after the recognized expression.
3377 *
3378 * Return OK or FAIL.
3379 */
3380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 long result;
3385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * Get the first variable.
3390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return FAIL;
3393
3394 /*
3395 * Repeat until there is no following "||".
3396 */
3397 first = TRUE;
3398 result = FALSE;
3399 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3400 {
3401 if (evaluate && first)
3402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 if (error)
3407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 first = FALSE;
3409 }
3410
3411 /*
3412 * Get the second variable.
3413 */
3414 *arg = skipwhite(*arg + 2);
3415 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3416 return FAIL;
3417
3418 /*
3419 * Compute the result.
3420 */
3421 if (evaluate && !result)
3422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003423 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 if (error)
3427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429 if (evaluate)
3430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 rettv->v_type = VAR_NUMBER;
3432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 }
3435
3436 return OK;
3437}
3438
3439/*
3440 * Handle second level expression:
3441 * expr3 && expr3 && expr3 logical AND
3442 *
3443 * "arg" must point to the first non-white of the expression.
3444 * "arg" is advanced to the next non-white after the recognized expression.
3445 *
3446 * Return OK or FAIL.
3447 */
3448 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 long result;
3453 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003454 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 /*
3457 * Get the first variable.
3458 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 return FAIL;
3461
3462 /*
3463 * Repeat until there is no following "&&".
3464 */
3465 first = TRUE;
3466 result = TRUE;
3467 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3468 {
3469 if (evaluate && first)
3470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003474 if (error)
3475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 first = FALSE;
3477 }
3478
3479 /*
3480 * Get the second variable.
3481 */
3482 *arg = skipwhite(*arg + 2);
3483 if (eval4(arg, &var2, evaluate && result) == FAIL)
3484 return FAIL;
3485
3486 /*
3487 * Compute the result.
3488 */
3489 if (evaluate && result)
3490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003491 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003494 if (error)
3495 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 if (evaluate)
3498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 rettv->v_type = VAR_NUMBER;
3500 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 }
3503
3504 return OK;
3505}
3506
3507/*
3508 * Handle third level expression:
3509 * var1 == var2
3510 * var1 =~ var2
3511 * var1 != var2
3512 * var1 !~ var2
3513 * var1 > var2
3514 * var1 >= var2
3515 * var1 < var2
3516 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003517 * var1 is var2
3518 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 *
3520 * "arg" must point to the first non-white of the expression.
3521 * "arg" is advanced to the next non-white after the recognized expression.
3522 *
3523 * Return OK or FAIL.
3524 */
3525 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003526eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
Bram Moolenaar33570922005-01-25 22:26:29 +00003528 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 char_u *p;
3530 int i;
3531 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003532 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003534 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *s1, *s2;
3536 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539 /*
3540 * Get the first variable.
3541 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 return FAIL;
3544
3545 p = *arg;
3546 switch (p[0])
3547 {
3548 case '=': if (p[1] == '=')
3549 type = TYPE_EQUAL;
3550 else if (p[1] == '~')
3551 type = TYPE_MATCH;
3552 break;
3553 case '!': if (p[1] == '=')
3554 type = TYPE_NEQUAL;
3555 else if (p[1] == '~')
3556 type = TYPE_NOMATCH;
3557 break;
3558 case '>': if (p[1] != '=')
3559 {
3560 type = TYPE_GREATER;
3561 len = 1;
3562 }
3563 else
3564 type = TYPE_GEQUAL;
3565 break;
3566 case '<': if (p[1] != '=')
3567 {
3568 type = TYPE_SMALLER;
3569 len = 1;
3570 }
3571 else
3572 type = TYPE_SEQUAL;
3573 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003574 case 'i': if (p[1] == 's')
3575 {
3576 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3577 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003578 i = p[len];
3579 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003580 {
3581 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3582 type_is = TRUE;
3583 }
3584 }
3585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587
3588 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003589 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 */
3591 if (type != TYPE_UNKNOWN)
3592 {
3593 /* extra question mark appended: ignore case */
3594 if (p[len] == '?')
3595 {
3596 ic = TRUE;
3597 ++len;
3598 }
3599 /* extra '#' appended: match case */
3600 else if (p[len] == '#')
3601 {
3602 ic = FALSE;
3603 ++len;
3604 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003605 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 else
3607 ic = p_ic;
3608
3609 /*
3610 * Get the second variable.
3611 */
3612 *arg = skipwhite(p + len);
3613 if (eval5(arg, &var2, evaluate) == FAIL)
3614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return FAIL;
3617 }
3618
3619 if (evaluate)
3620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003621 if (type_is && rettv->v_type != var2.v_type)
3622 {
3623 /* For "is" a different type always means FALSE, for "notis"
3624 * it means TRUE. */
3625 n1 = (type == TYPE_NEQUAL);
3626 }
3627 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3628 {
3629 if (type_is)
3630 {
3631 n1 = (rettv->v_type == var2.v_type
3632 && rettv->vval.v_list == var2.vval.v_list);
3633 if (type == TYPE_NEQUAL)
3634 n1 = !n1;
3635 }
3636 else if (rettv->v_type != var2.v_type
3637 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3638 {
3639 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003640 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003641 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003642 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003643 clear_tv(rettv);
3644 clear_tv(&var2);
3645 return FAIL;
3646 }
3647 else
3648 {
3649 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003650 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3651 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003652 if (type == TYPE_NEQUAL)
3653 n1 = !n1;
3654 }
3655 }
3656
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003657 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3658 {
3659 if (type_is)
3660 {
3661 n1 = (rettv->v_type == var2.v_type
3662 && rettv->vval.v_dict == var2.vval.v_dict);
3663 if (type == TYPE_NEQUAL)
3664 n1 = !n1;
3665 }
3666 else if (rettv->v_type != var2.v_type
3667 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3668 {
3669 if (rettv->v_type != var2.v_type)
3670 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3671 else
3672 EMSG(_("E736: Invalid operation for Dictionary"));
3673 clear_tv(rettv);
3674 clear_tv(&var2);
3675 return FAIL;
3676 }
3677 else
3678 {
3679 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003680 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3681 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003682 if (type == TYPE_NEQUAL)
3683 n1 = !n1;
3684 }
3685 }
3686
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003687 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3688 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003689 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003690 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003691 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003692 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003693 clear_tv(rettv);
3694 clear_tv(&var2);
3695 return FAIL;
3696 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003697 if ((rettv->v_type == VAR_PARTIAL
3698 && rettv->vval.v_partial == NULL)
3699 || (var2.v_type == VAR_PARTIAL
3700 && var2.vval.v_partial == NULL))
3701 /* when a partial is NULL assume not equal */
3702 n1 = FALSE;
3703 else if (type_is)
3704 {
3705 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3706 /* strings are considered the same if their value is
3707 * the same */
3708 n1 = tv_equal(rettv, &var2, ic, FALSE);
3709 else if (rettv->v_type == VAR_PARTIAL
3710 && var2.v_type == VAR_PARTIAL)
3711 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3712 else
3713 n1 = FALSE;
3714 }
3715 else
3716 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003717 if (type == TYPE_NEQUAL)
3718 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003719 }
3720
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003721#ifdef FEAT_FLOAT
3722 /*
3723 * If one of the two variables is a float, compare as a float.
3724 * When using "=~" or "!~", always compare as string.
3725 */
3726 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3727 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3728 {
3729 float_T f1, f2;
3730
3731 if (rettv->v_type == VAR_FLOAT)
3732 f1 = rettv->vval.v_float;
3733 else
3734 f1 = get_tv_number(rettv);
3735 if (var2.v_type == VAR_FLOAT)
3736 f2 = var2.vval.v_float;
3737 else
3738 f2 = get_tv_number(&var2);
3739 n1 = FALSE;
3740 switch (type)
3741 {
3742 case TYPE_EQUAL: n1 = (f1 == f2); break;
3743 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3744 case TYPE_GREATER: n1 = (f1 > f2); break;
3745 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3746 case TYPE_SMALLER: n1 = (f1 < f2); break;
3747 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3748 case TYPE_UNKNOWN:
3749 case TYPE_MATCH:
3750 case TYPE_NOMATCH: break; /* avoid gcc warning */
3751 }
3752 }
3753#endif
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /*
3756 * If one of the two variables is a number, compare as a number.
3757 * When using "=~" or "!~", always compare as string.
3758 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003759 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 n1 = get_tv_number(rettv);
3763 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 switch (type)
3765 {
3766 case TYPE_EQUAL: n1 = (n1 == n2); break;
3767 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3768 case TYPE_GREATER: n1 = (n1 > n2); break;
3769 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3770 case TYPE_SMALLER: n1 = (n1 < n2); break;
3771 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3772 case TYPE_UNKNOWN:
3773 case TYPE_MATCH:
3774 case TYPE_NOMATCH: break; /* avoid gcc warning */
3775 }
3776 }
3777 else
3778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 s1 = get_tv_string_buf(rettv, buf1);
3780 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3782 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3783 else
3784 i = 0;
3785 n1 = FALSE;
3786 switch (type)
3787 {
3788 case TYPE_EQUAL: n1 = (i == 0); break;
3789 case TYPE_NEQUAL: n1 = (i != 0); break;
3790 case TYPE_GREATER: n1 = (i > 0); break;
3791 case TYPE_GEQUAL: n1 = (i >= 0); break;
3792 case TYPE_SMALLER: n1 = (i < 0); break;
3793 case TYPE_SEQUAL: n1 = (i <= 0); break;
3794
3795 case TYPE_MATCH:
3796 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003797 n1 = pattern_match(s2, s1, ic);
3798 if (type == TYPE_NOMATCH)
3799 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 break;
3801
3802 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3803 }
3804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 clear_tv(rettv);
3806 clear_tv(&var2);
3807 rettv->v_type = VAR_NUMBER;
3808 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 }
3811
3812 return OK;
3813}
3814
3815/*
3816 * Handle fourth level expression:
3817 * + number addition
3818 * - number subtraction
3819 * . string concatenation
3820 *
3821 * "arg" must point to the first non-white of the expression.
3822 * "arg" is advanced to the next non-white after the recognized expression.
3823 *
3824 * Return OK or FAIL.
3825 */
3826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003827eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 typval_T var2;
3830 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003832 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003833#ifdef FEAT_FLOAT
3834 float_T f1 = 0, f2 = 0;
3835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 char_u *s1, *s2;
3837 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3838 char_u *p;
3839
3840 /*
3841 * Get the first variable.
3842 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003843 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 return FAIL;
3845
3846 /*
3847 * Repeat computing, until no '+', '-' or '.' is following.
3848 */
3849 for (;;)
3850 {
3851 op = **arg;
3852 if (op != '+' && op != '-' && op != '.')
3853 break;
3854
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003855 if ((op != '+' || rettv->v_type != VAR_LIST)
3856#ifdef FEAT_FLOAT
3857 && (op == '.' || rettv->v_type != VAR_FLOAT)
3858#endif
3859 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 {
3861 /* For "list + ...", an illegal use of the first operand as
3862 * a number cannot be determined before evaluating the 2nd
3863 * operand: if this is also a list, all is ok.
3864 * For "something . ...", "something - ..." or "non-list + ...",
3865 * we know that the first operand needs to be a string or number
3866 * without evaluating the 2nd operand. So check before to avoid
3867 * side effects after an error. */
3868 if (evaluate && get_tv_string_chk(rettv) == NULL)
3869 {
3870 clear_tv(rettv);
3871 return FAIL;
3872 }
3873 }
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 /*
3876 * Get the second variable.
3877 */
3878 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003879 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 return FAIL;
3883 }
3884
3885 if (evaluate)
3886 {
3887 /*
3888 * Compute the result.
3889 */
3890 if (op == '.')
3891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003892 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3893 s2 = get_tv_string_buf_chk(&var2, buf2);
3894 if (s2 == NULL) /* type error ? */
3895 {
3896 clear_tv(rettv);
3897 clear_tv(&var2);
3898 return FAIL;
3899 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003900 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(rettv);
3902 rettv->v_type = VAR_STRING;
3903 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003905 else if (op == '+' && rettv->v_type == VAR_LIST
3906 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003907 {
3908 /* concatenate Lists */
3909 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3910 &var3) == FAIL)
3911 {
3912 clear_tv(rettv);
3913 clear_tv(&var2);
3914 return FAIL;
3915 }
3916 clear_tv(rettv);
3917 *rettv = var3;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003921 int error = FALSE;
3922
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003923#ifdef FEAT_FLOAT
3924 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003926 f1 = rettv->vval.v_float;
3927 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003929 else
3930#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003932 n1 = get_tv_number_chk(rettv, &error);
3933 if (error)
3934 {
3935 /* This can only happen for "list + non-list". For
3936 * "non-list + ..." or "something - ...", we returned
3937 * before evaluating the 2nd operand. */
3938 clear_tv(rettv);
3939 return FAIL;
3940 }
3941#ifdef FEAT_FLOAT
3942 if (var2.v_type == VAR_FLOAT)
3943 f1 = n1;
3944#endif
3945 }
3946#ifdef FEAT_FLOAT
3947 if (var2.v_type == VAR_FLOAT)
3948 {
3949 f2 = var2.vval.v_float;
3950 n2 = 0;
3951 }
3952 else
3953#endif
3954 {
3955 n2 = get_tv_number_chk(&var2, &error);
3956 if (error)
3957 {
3958 clear_tv(rettv);
3959 clear_tv(&var2);
3960 return FAIL;
3961 }
3962#ifdef FEAT_FLOAT
3963 if (rettv->v_type == VAR_FLOAT)
3964 f2 = n2;
3965#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003968
3969#ifdef FEAT_FLOAT
3970 /* If there is a float on either side the result is a float. */
3971 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3972 {
3973 if (op == '+')
3974 f1 = f1 + f2;
3975 else
3976 f1 = f1 - f2;
3977 rettv->v_type = VAR_FLOAT;
3978 rettv->vval.v_float = f1;
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003981#endif
3982 {
3983 if (op == '+')
3984 n1 = n1 + n2;
3985 else
3986 n1 = n1 - n2;
3987 rettv->v_type = VAR_NUMBER;
3988 rettv->vval.v_number = n1;
3989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 }
3994 return OK;
3995}
3996
3997/*
3998 * Handle fifth level expression:
3999 * * number multiplication
4000 * / number division
4001 * % number modulo
4002 *
4003 * "arg" must point to the first non-white of the expression.
4004 * "arg" is advanced to the next non-white after the recognized expression.
4005 *
4006 * Return OK or FAIL.
4007 */
4008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004009eval6(
4010 char_u **arg,
4011 typval_T *rettv,
4012 int evaluate,
4013 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004017 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004018#ifdef FEAT_FLOAT
4019 int use_float = FALSE;
4020 float_T f1 = 0, f2;
4021#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004022 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 /*
4025 * Get the first variable.
4026 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004027 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return FAIL;
4029
4030 /*
4031 * Repeat computing, until no '*', '/' or '%' is following.
4032 */
4033 for (;;)
4034 {
4035 op = **arg;
4036 if (op != '*' && op != '/' && op != '%')
4037 break;
4038
4039 if (evaluate)
4040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004041#ifdef FEAT_FLOAT
4042 if (rettv->v_type == VAR_FLOAT)
4043 {
4044 f1 = rettv->vval.v_float;
4045 use_float = TRUE;
4046 n1 = 0;
4047 }
4048 else
4049#endif
4050 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004052 if (error)
4053 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055 else
4056 n1 = 0;
4057
4058 /*
4059 * Get the second variable.
4060 */
4061 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004062 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return FAIL;
4064
4065 if (evaluate)
4066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004067#ifdef FEAT_FLOAT
4068 if (var2.v_type == VAR_FLOAT)
4069 {
4070 if (!use_float)
4071 {
4072 f1 = n1;
4073 use_float = TRUE;
4074 }
4075 f2 = var2.vval.v_float;
4076 n2 = 0;
4077 }
4078 else
4079#endif
4080 {
4081 n2 = get_tv_number_chk(&var2, &error);
4082 clear_tv(&var2);
4083 if (error)
4084 return FAIL;
4085#ifdef FEAT_FLOAT
4086 if (use_float)
4087 f2 = n2;
4088#endif
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091 /*
4092 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004093 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004095#ifdef FEAT_FLOAT
4096 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004098 if (op == '*')
4099 f1 = f1 * f2;
4100 else if (op == '/')
4101 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004102# ifdef VMS
4103 /* VMS crashes on divide by zero, work around it */
4104 if (f2 == 0.0)
4105 {
4106 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004107 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004108 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004109 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004110 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004111 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004112 }
4113 else
4114 f1 = f1 / f2;
4115# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004116 /* We rely on the floating point library to handle divide
4117 * by zero to result in "inf" and not a crash. */
4118 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004119# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004122 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004123 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004124 return FAIL;
4125 }
4126 rettv->v_type = VAR_FLOAT;
4127 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004132 if (op == '*')
4133 n1 = n1 * n2;
4134 else if (op == '/')
4135 {
4136 if (n2 == 0) /* give an error message? */
4137 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004138 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004139 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004140 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004141 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004142 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004143 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004144 }
4145 else
4146 n1 = n1 / n2;
4147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004149 {
4150 if (n2 == 0) /* give an error message? */
4151 n1 = 0;
4152 else
4153 n1 = n1 % n2;
4154 }
4155 rettv->v_type = VAR_NUMBER;
4156 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 }
4160
4161 return OK;
4162}
4163
4164/*
4165 * Handle sixth level expression:
4166 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004167 * "string" string constant
4168 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * &option-name option value
4170 * @r register contents
4171 * identifier variable value
4172 * function() function call
4173 * $VAR environment variable
4174 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004175 * [expr, expr] List
4176 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 *
4178 * Also handle:
4179 * ! in front logical NOT
4180 * - in front unary minus
4181 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004182 * trailing [] subscript in String or List
4183 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 *
4185 * "arg" must point to the first non-white of the expression.
4186 * "arg" is advanced to the next non-white after the recognized expression.
4187 *
4188 * Return OK or FAIL.
4189 */
4190 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004191eval7(
4192 char_u **arg,
4193 typval_T *rettv,
4194 int evaluate,
4195 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004197 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 int len;
4199 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 char_u *start_leader, *end_leader;
4201 int ret = OK;
4202 char_u *alias;
4203
4204 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004211 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 */
4213 start_leader = *arg;
4214 while (**arg == '!' || **arg == '-' || **arg == '+')
4215 *arg = skipwhite(*arg + 1);
4216 end_leader = *arg;
4217
4218 switch (**arg)
4219 {
4220 /*
4221 * Number constant.
4222 */
4223 case '0':
4224 case '1':
4225 case '2':
4226 case '3':
4227 case '4':
4228 case '5':
4229 case '6':
4230 case '7':
4231 case '8':
4232 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004233 {
4234#ifdef FEAT_FLOAT
4235 char_u *p = skipdigits(*arg + 1);
4236 int get_float = FALSE;
4237
4238 /* We accept a float when the format matches
4239 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004240 * strict to avoid backwards compatibility problems.
4241 * Don't look for a float after the "." operator, so that
4242 * ":let vers = 1.2.3" doesn't fail. */
4243 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 get_float = TRUE;
4246 p = skipdigits(p + 2);
4247 if (*p == 'e' || *p == 'E')
4248 {
4249 ++p;
4250 if (*p == '-' || *p == '+')
4251 ++p;
4252 if (!vim_isdigit(*p))
4253 get_float = FALSE;
4254 else
4255 p = skipdigits(p + 1);
4256 }
4257 if (ASCII_ISALPHA(*p) || *p == '.')
4258 get_float = FALSE;
4259 }
4260 if (get_float)
4261 {
4262 float_T f;
4263
4264 *arg += string2float(*arg, &f);
4265 if (evaluate)
4266 {
4267 rettv->v_type = VAR_FLOAT;
4268 rettv->vval.v_float = f;
4269 }
4270 }
4271 else
4272#endif
4273 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004274 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004275 *arg += len;
4276 if (evaluate)
4277 {
4278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = n;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
4285 /*
4286 * String constant: "string".
4287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 break;
4290
4291 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004292 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 break;
4296
4297 /*
4298 * List: [expr, expr]
4299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 break;
4302
4303 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004304 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004305 * Dictionary: {key: val, key: val}
4306 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004307 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4308 if (ret == NOTDONE)
4309 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 break;
4311
4312 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004313 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004315 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 break;
4317
4318 /*
4319 * Environment variable: $VAR.
4320 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 break;
4323
4324 /*
4325 * Register contents: @r.
4326 */
4327 case '@': ++*arg;
4328 if (evaluate)
4329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004331 rettv->vval.v_string = get_reg_contents(**arg,
4332 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 if (**arg != NUL)
4335 ++*arg;
4336 break;
4337
4338 /*
4339 * nested expression: (expression).
4340 */
4341 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (**arg == ')')
4344 ++*arg;
4345 else if (ret == OK)
4346 {
4347 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 ret = FAIL;
4350 }
4351 break;
4352
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 break;
4355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004356
4357 if (ret == NOTDONE)
4358 {
4359 /*
4360 * Must be a variable or function name.
4361 * Can also be a curly-braces kind of name: {expr}.
4362 */
4363 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004364 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004365 if (alias != NULL)
4366 s = alias;
4367
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004368 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 ret = FAIL;
4370 else
4371 {
4372 if (**arg == '(') /* recursive! */
4373 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004374 partial_T *partial;
4375
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004376 if (!evaluate)
4377 check_vars(s, len);
4378
Bram Moolenaar8c711452005-01-14 21:53:12 +00004379 /* If "s" is the name of a variable of type VAR_FUNC
4380 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004381 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004382
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004383 /* Need to make a copy, in case evaluating the arguments makes
4384 * the name invalid. */
4385 s = vim_strsave(s);
4386 if (s == NULL)
4387 ret = FAIL;
4388 else
4389 /* Invoke the function. */
4390 ret = get_func_tv(s, len, rettv, arg,
4391 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4392 &len, evaluate, partial, NULL);
4393 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004394
4395 /* If evaluate is FALSE rettv->v_type was not set in
4396 * get_func_tv, but it's needed in handle_subscript() to parse
4397 * what follows. So set it here. */
4398 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4399 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004400 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004401 rettv->v_type = VAR_FUNC;
4402 }
4403
Bram Moolenaar8c711452005-01-14 21:53:12 +00004404 /* Stop the expression evaluation when immediately
4405 * aborting on error, or when an interrupt occurred or
4406 * an exception was thrown but not caught. */
4407 if (aborting())
4408 {
4409 if (ret == OK)
4410 clear_tv(rettv);
4411 ret = FAIL;
4412 }
4413 }
4414 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004415 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004416 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004417 {
4418 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004419 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004421 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004422 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004423 }
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 *arg = skipwhite(*arg);
4426
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004427 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4428 * expr(expr). */
4429 if (ret == OK)
4430 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /*
4433 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4434 */
4435 if (ret == OK && evaluate && end_leader > start_leader)
4436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004437 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004438 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004439#ifdef FEAT_FLOAT
4440 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004442 if (rettv->v_type == VAR_FLOAT)
4443 f = rettv->vval.v_float;
4444 else
4445#endif
4446 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004447 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 clear_tv(rettv);
4450 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 else
4453 {
4454 while (end_leader > start_leader)
4455 {
4456 --end_leader;
4457 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 {
4459#ifdef FEAT_FLOAT
4460 if (rettv->v_type == VAR_FLOAT)
4461 f = !f;
4462 else
4463#endif
4464 val = !val;
4465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004466 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004467 {
4468#ifdef FEAT_FLOAT
4469 if (rettv->v_type == VAR_FLOAT)
4470 f = -f;
4471 else
4472#endif
4473 val = -val;
4474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004476#ifdef FEAT_FLOAT
4477 if (rettv->v_type == VAR_FLOAT)
4478 {
4479 clear_tv(rettv);
4480 rettv->vval.v_float = f;
4481 }
4482 else
4483#endif
4484 {
4485 clear_tv(rettv);
4486 rettv->v_type = VAR_NUMBER;
4487 rettv->vval.v_number = val;
4488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491
4492 return ret;
4493}
4494
4495/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004496 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4497 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004498 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4499 */
4500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004501eval_index(
4502 char_u **arg,
4503 typval_T *rettv,
4504 int evaluate,
4505 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506{
4507 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004508 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004510 long len = -1;
4511 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004512 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004513 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004517 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004518 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004519 if (verbose)
4520 EMSG(_("E695: Cannot index a Funcref"));
4521 return FAIL;
4522 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004523#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004524 if (verbose)
4525 EMSG(_(e_float_as_string));
4526 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004527#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004528 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004529 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004530 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004531 if (verbose)
4532 EMSG(_("E909: Cannot index a special variable"));
4533 return FAIL;
4534 case VAR_UNKNOWN:
4535 if (evaluate)
4536 return FAIL;
4537 /* FALLTHROUGH */
4538
4539 case VAR_STRING:
4540 case VAR_NUMBER:
4541 case VAR_LIST:
4542 case VAR_DICT:
4543 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004544 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004546 init_tv(&var1);
4547 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 /*
4551 * dict.name
4552 */
4553 key = *arg + 1;
4554 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4555 ;
4556 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 }
4560 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 /*
4563 * something[idx]
4564 *
4565 * Get the (first) variable from inside the [].
4566 */
4567 *arg = skipwhite(*arg + 1);
4568 if (**arg == ':')
4569 empty1 = TRUE;
4570 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4571 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004572 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4573 {
4574 /* not a number or string */
4575 clear_tv(&var1);
4576 return FAIL;
4577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004578
4579 /*
4580 * Get the second variable from inside the [:].
4581 */
4582 if (**arg == ':')
4583 {
4584 range = TRUE;
4585 *arg = skipwhite(*arg + 1);
4586 if (**arg == ']')
4587 empty2 = TRUE;
4588 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 if (!empty1)
4591 clear_tv(&var1);
4592 return FAIL;
4593 }
4594 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4595 {
4596 /* not a number or string */
4597 if (!empty1)
4598 clear_tv(&var1);
4599 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004600 return FAIL;
4601 }
4602 }
4603
4604 /* Check for the ']'. */
4605 if (**arg != ']')
4606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004607 if (verbose)
4608 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 clear_tv(&var1);
4610 if (range)
4611 clear_tv(&var2);
4612 return FAIL;
4613 }
4614 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 }
4616
4617 if (evaluate)
4618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 n1 = 0;
4620 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 n1 = get_tv_number(&var1);
4623 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 }
4625 if (range)
4626 {
4627 if (empty2)
4628 n2 = -1;
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 n2 = get_tv_number(&var2);
4632 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 }
4634 }
4635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004638 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004639 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004640 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004641 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004642 case VAR_SPECIAL:
4643 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004644 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004645 break; /* not evaluating, skipping over subscript */
4646
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 case VAR_NUMBER:
4648 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 len = (long)STRLEN(s);
4651 if (range)
4652 {
4653 /* The resulting variable is a substring. If the indexes
4654 * are out of range the result is empty. */
4655 if (n1 < 0)
4656 {
4657 n1 = len + n1;
4658 if (n1 < 0)
4659 n1 = 0;
4660 }
4661 if (n2 < 0)
4662 n2 = len + n2;
4663 else if (n2 >= len)
4664 n2 = len;
4665 if (n1 >= len || n2 < 0 || n1 > n2)
4666 s = NULL;
4667 else
4668 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4669 }
4670 else
4671 {
4672 /* The resulting variable is a string of a single
4673 * character. If the index is too big or negative the
4674 * result is empty. */
4675 if (n1 >= len || n1 < 0)
4676 s = NULL;
4677 else
4678 s = vim_strnsave(s + n1, 1);
4679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
4681 rettv->v_type = VAR_STRING;
4682 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 break;
4684
4685 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 if (n1 < 0)
4688 n1 = len + n1;
4689 if (!empty1 && (n1 < 0 || n1 >= len))
4690 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004691 /* For a range we allow invalid values and return an empty
4692 * list. A list index out of range is an error. */
4693 if (!range)
4694 {
4695 if (verbose)
4696 EMSGN(_(e_listidx), n1);
4697 return FAIL;
4698 }
4699 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 }
4701 if (range)
4702 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004703 list_T *l;
4704 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705
4706 if (n2 < 0)
4707 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004708 else if (n2 >= len)
4709 n2 = len - 1;
4710 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004711 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 l = list_alloc();
4713 if (l == NULL)
4714 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 n1 <= n2; ++n1)
4717 {
4718 if (list_append_tv(l, &item->li_tv) == FAIL)
4719 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 return FAIL;
4722 }
4723 item = item->li_next;
4724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004725 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004726 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 }
4728 else
4729 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004730 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
4732 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 }
4734 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735
4736 case VAR_DICT:
4737 if (range)
4738 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004739 if (verbose)
4740 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 if (len == -1)
4742 clear_tv(&var1);
4743 return FAIL;
4744 }
4745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004746 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747
4748 if (len == -1)
4749 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004750 key = get_tv_string_chk(&var1);
4751 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 clear_tv(&var1);
4754 return FAIL;
4755 }
4756 }
4757
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004758 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004760 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004761 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 if (len == -1)
4763 clear_tv(&var1);
4764 if (item == NULL)
4765 return FAIL;
4766
4767 copy_tv(&item->di_tv, &var1);
4768 clear_tv(rettv);
4769 *rettv = var1;
4770 }
4771 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 }
4773 }
4774
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 return OK;
4776}
4777
4778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * Get an option value.
4780 * "arg" points to the '&' or '+' before the option name.
4781 * "arg" is advanced to character after the option name.
4782 * Return OK or FAIL.
4783 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004785get_option_tv(
4786 char_u **arg,
4787 typval_T *rettv, /* when NULL, only check if option exists */
4788 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 char_u *option_end;
4791 long numval;
4792 char_u *stringval;
4793 int opt_type;
4794 int c;
4795 int working = (**arg == '+'); /* has("+option") */
4796 int ret = OK;
4797 int opt_flags;
4798
4799 /*
4800 * Isolate the option name and find its value.
4801 */
4802 option_end = find_option_end(arg, &opt_flags);
4803 if (option_end == NULL)
4804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004805 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 EMSG2(_("E112: Option name missing: %s"), *arg);
4807 return FAIL;
4808 }
4809
4810 if (!evaluate)
4811 {
4812 *arg = option_end;
4813 return OK;
4814 }
4815
4816 c = *option_end;
4817 *option_end = NUL;
4818 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 if (opt_type == -3) /* invalid name */
4822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 EMSG2(_("E113: Unknown option: %s"), *arg);
4825 ret = FAIL;
4826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 if (opt_type == -2) /* hidden string option */
4830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 rettv->v_type = VAR_STRING;
4832 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 else if (opt_type == -1) /* hidden number option */
4835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 rettv->v_type = VAR_NUMBER;
4837 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839 else if (opt_type == 1) /* number option */
4840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 rettv->v_type = VAR_NUMBER;
4842 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else /* string option */
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 rettv->v_type = VAR_STRING;
4847 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 }
4850 else if (working && (opt_type == -2 || opt_type == -1))
4851 ret = FAIL;
4852
4853 *option_end = c; /* put back for error messages */
4854 *arg = option_end;
4855
4856 return ret;
4857}
4858
4859/*
4860 * Allocate a variable for a string constant.
4861 * Return OK or FAIL.
4862 */
4863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004864get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865{
4866 char_u *p;
4867 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 int extra = 0;
4869
4870 /*
4871 * Find the end of the string, skipping backslashed characters.
4872 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004873 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
4875 if (*p == '\\' && p[1] != NUL)
4876 {
4877 ++p;
4878 /* A "\<x>" form occupies at least 4 characters, and produces up
4879 * to 6 characters: reserve space for 2 extra */
4880 if (*p == '<')
4881 extra += 2;
4882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884
4885 if (*p != '"')
4886 {
4887 EMSG2(_("E114: Missing quote: %s"), *arg);
4888 return FAIL;
4889 }
4890
4891 /* If only parsing, set *arg and return here */
4892 if (!evaluate)
4893 {
4894 *arg = p + 1;
4895 return OK;
4896 }
4897
4898 /*
4899 * Copy the string into allocated memory, handling backslashed
4900 * characters.
4901 */
4902 name = alloc((unsigned)(p - *arg + extra));
4903 if (name == NULL)
4904 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004905 rettv->v_type = VAR_STRING;
4906 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 if (*p == '\\')
4911 {
4912 switch (*++p)
4913 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004914 case 'b': *name++ = BS; ++p; break;
4915 case 'e': *name++ = ESC; ++p; break;
4916 case 'f': *name++ = FF; ++p; break;
4917 case 'n': *name++ = NL; ++p; break;
4918 case 'r': *name++ = CAR; ++p; break;
4919 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 case 'X': /* hex: "\x1", "\x12" */
4922 case 'x':
4923 case 'u': /* Unicode: "\u0023" */
4924 case 'U':
4925 if (vim_isxdigit(p[1]))
4926 {
4927 int n, nr;
4928 int c = toupper(*p);
4929
4930 if (c == 'X')
4931 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004932 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004934 else
4935 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 nr = 0;
4937 while (--n >= 0 && vim_isxdigit(p[1]))
4938 {
4939 ++p;
4940 nr = (nr << 4) + hex2nr(*p);
4941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#ifdef FEAT_MBYTE
4944 /* For "\u" store the number according to
4945 * 'encoding'. */
4946 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 else
4949#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 break;
4953
4954 /* octal: "\1", "\12", "\123" */
4955 case '0':
4956 case '1':
4957 case '2':
4958 case '3':
4959 case '4':
4960 case '5':
4961 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 case '7': *name = *p++ - '0';
4963 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 *name = (*name << 3) + *p++ - '0';
4966 if (*p >= '0' && *p <= '7')
4967 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 break;
4971
4972 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004973 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (extra != 0)
4975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978 }
4979 /* FALLTHROUGH */
4980
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 break;
4983 }
4984 }
4985 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004990 if (*p != NUL) /* just in case */
4991 ++p;
4992 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 return OK;
4995}
4996
4997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 * Return OK or FAIL.
5000 */
5001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005002get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
5004 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005005 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005006 int reduce = 0;
5007
5008 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005011 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005014 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005016 break;
5017 ++reduce;
5018 ++p;
5019 }
5020 }
5021
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005025 return FAIL;
5026 }
5027
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005029 if (!evaluate)
5030 {
5031 *arg = p + 1;
5032 return OK;
5033 }
5034
5035 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037 */
5038 str = alloc((unsigned)((p - *arg) - reduce));
5039 if (str == NULL)
5040 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 rettv->v_type = VAR_STRING;
5042 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 break;
5050 ++p;
5051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 *arg = p + 1;
5056
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057 return OK;
5058}
5059
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005060/*
5061 * Return the function name of the partial.
5062 */
5063 char_u *
5064partial_name(partial_T *pt)
5065{
5066 if (pt->pt_name != NULL)
5067 return pt->pt_name;
5068 return pt->pt_func->uf_name;
5069}
5070
Bram Moolenaarddecc252016-04-06 22:59:37 +02005071 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005072partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005073{
5074 int i;
5075
5076 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005077 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005078 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005079 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005080 if (pt->pt_name != NULL)
5081 {
5082 func_unref(pt->pt_name);
5083 vim_free(pt->pt_name);
5084 }
5085 else
5086 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005087 vim_free(pt);
5088}
5089
5090/*
5091 * Unreference a closure: decrement the reference count and free it when it
5092 * becomes zero.
5093 */
5094 void
5095partial_unref(partial_T *pt)
5096{
5097 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005098 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005099}
5100
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005101static int tv_equal_recurse_limit;
5102
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005103 static int
5104func_equal(
5105 typval_T *tv1,
5106 typval_T *tv2,
5107 int ic) /* ignore case */
5108{
5109 char_u *s1, *s2;
5110 dict_T *d1, *d2;
5111 int a1, a2;
5112 int i;
5113
5114 /* empty and NULL function name considered the same */
5115 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005116 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005117 if (s1 != NULL && *s1 == NUL)
5118 s1 = NULL;
5119 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005120 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005121 if (s2 != NULL && *s2 == NUL)
5122 s2 = NULL;
5123 if (s1 == NULL || s2 == NULL)
5124 {
5125 if (s1 != s2)
5126 return FALSE;
5127 }
5128 else if (STRCMP(s1, s2) != 0)
5129 return FALSE;
5130
5131 /* empty dict and NULL dict is different */
5132 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5133 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5134 if (d1 == NULL || d2 == NULL)
5135 {
5136 if (d1 != d2)
5137 return FALSE;
5138 }
5139 else if (!dict_equal(d1, d2, ic, TRUE))
5140 return FALSE;
5141
5142 /* empty list and no list considered the same */
5143 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5144 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5145 if (a1 != a2)
5146 return FALSE;
5147 for (i = 0; i < a1; ++i)
5148 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5149 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5150 return FALSE;
5151
5152 return TRUE;
5153}
5154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005155/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005156 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005157 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005159 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005161tv_equal(
5162 typval_T *tv1,
5163 typval_T *tv2,
5164 int ic, /* ignore case */
5165 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005166{
5167 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005168 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005169 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005170 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005171
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005172 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005173 * recursiveness to a limit. We guess they are equal then.
5174 * A fixed limit has the problem of still taking an awful long time.
5175 * Reduce the limit every time running into it. That should work fine for
5176 * deeply linked structures that are not recursively linked and catch
5177 * recursiveness quickly. */
5178 if (!recursive)
5179 tv_equal_recurse_limit = 1000;
5180 if (recursive_cnt >= tv_equal_recurse_limit)
5181 {
5182 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005183 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005184 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005185
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005186 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5187 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005188 if ((tv1->v_type == VAR_FUNC
5189 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5190 && (tv2->v_type == VAR_FUNC
5191 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5192 {
5193 ++recursive_cnt;
5194 r = func_equal(tv1, tv2, ic);
5195 --recursive_cnt;
5196 return r;
5197 }
5198
5199 if (tv1->v_type != tv2->v_type)
5200 return FALSE;
5201
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005202 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005203 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005204 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005205 ++recursive_cnt;
5206 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005209
5210 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005211 ++recursive_cnt;
5212 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5213 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005214 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005215
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005216 case VAR_NUMBER:
5217 return tv1->vval.v_number == tv2->vval.v_number;
5218
5219 case VAR_STRING:
5220 s1 = get_tv_string_buf(tv1, buf1);
5221 s2 = get_tv_string_buf(tv2, buf2);
5222 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005223
5224 case VAR_SPECIAL:
5225 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005226
5227 case VAR_FLOAT:
5228#ifdef FEAT_FLOAT
5229 return tv1->vval.v_float == tv2->vval.v_float;
5230#endif
5231 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005233 return tv1->vval.v_job == tv2->vval.v_job;
5234#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005235 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005236#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005237 return tv1->vval.v_channel == tv2->vval.v_channel;
5238#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005239 case VAR_FUNC:
5240 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005241 case VAR_UNKNOWN:
5242 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005243 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005244
Bram Moolenaara03f2332016-02-06 18:09:59 +01005245 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5246 * does not equal anything, not even itself. */
5247 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005248}
5249
5250/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005251 * Return the next (unique) copy ID.
5252 * Used for serializing nested structures.
5253 */
5254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005255get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005256{
5257 current_copyID += COPYID_INC;
5258 return current_copyID;
5259}
5260
5261/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262 * Garbage collection for lists and dictionaries.
5263 *
5264 * We use reference counts to be able to free most items right away when they
5265 * are no longer used. But for composite items it's possible that it becomes
5266 * unused while the reference count is > 0: When there is a recursive
5267 * reference. Example:
5268 * :let l = [1, 2, 3]
5269 * :let d = {9: l}
5270 * :let l[1] = d
5271 *
5272 * Since this is quite unusual we handle this with garbage collection: every
5273 * once in a while find out which lists and dicts are not referenced from any
5274 * variable.
5275 *
5276 * Here is a good reference text about garbage collection (refers to Python
5277 * but it applies to all reference-counting mechanisms):
5278 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005279 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005280
5281/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005282 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005283 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005284 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005285 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005286 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005287garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005288{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005289 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005291 buf_T *buf;
5292 win_T *wp;
5293 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005294 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005295 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005296
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005297 if (!testing)
5298 {
5299 /* Only do this once. */
5300 want_garbage_collect = FALSE;
5301 may_garbage_collect = FALSE;
5302 garbage_collect_at_exit = FALSE;
5303 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005304
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005305 /* We advance by two because we add one for items referenced through
5306 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005307 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005308
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005309 /*
5310 * 1. Go through all accessible variables and mark all lists and dicts
5311 * with copyID.
5312 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005313
5314 /* Don't free variables in the previous_funccal list unless they are only
5315 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005316 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005317 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005319 /* script-local variables */
5320 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005321 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322
5323 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005324 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005325 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5326 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327
5328 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005329 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005330 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5331 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005332#ifdef FEAT_AUTOCMD
5333 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005334 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5335 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005336#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005337
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005338 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005339 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005340 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5341 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005342 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005343 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005344
5345 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005346 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005348 /* named functions (matters for closures) */
5349 abort = abort || set_ref_in_functions(copyID);
5350
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005351 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005353
Bram Moolenaard812df62008-11-09 12:46:09 +00005354 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005355 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005356
Bram Moolenaar1dced572012-04-05 16:54:08 +02005357#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005358 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005359#endif
5360
Bram Moolenaardb913952012-06-29 12:54:53 +02005361#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005363#endif
5364
5365#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005367#endif
5368
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005370 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005371 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005372#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005373#ifdef FEAT_NETBEANS_INTG
5374 abort = abort || set_ref_in_nb_channel(copyID);
5375#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005376
Bram Moolenaare3188e22016-05-31 21:13:04 +02005377#ifdef FEAT_TIMERS
5378 abort = abort || set_ref_in_timer(copyID);
5379#endif
5380
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005381#ifdef FEAT_QUICKFIX
5382 abort = abort || set_ref_in_quickfix(copyID);
5383#endif
5384
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005385#ifdef FEAT_TERMINAL
5386 abort = abort || set_ref_in_term(copyID);
5387#endif
5388
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005390 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005391 /*
5392 * 2. Free lists and dictionaries that are not referenced.
5393 */
5394 did_free = free_unref_items(copyID);
5395
5396 /*
5397 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005400 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005401 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 else if (p_verbose > 0)
5403 {
5404 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5405 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005406
5407 return did_free;
5408}
5409
5410/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005411 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005412 */
5413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005414free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005415{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005416 int did_free = FALSE;
5417
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005418 /* Let all "free" functions know that we are here. This means no
5419 * dictionaries, lists, channels or jobs are to be freed, because we will
5420 * do that here. */
5421 in_free_unref_items = TRUE;
5422
5423 /*
5424 * PASS 1: free the contents of the items. We don't free the items
5425 * themselves yet, so that it is possible to decrement refcount counters
5426 */
5427
Bram Moolenaarcd524592016-07-17 14:57:05 +02005428 /* Go through the list of dicts and free items without the copyID. */
5429 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430
Bram Moolenaarda861d62016-07-17 15:46:27 +02005431 /* Go through the list of lists and free items without the copyID. */
5432 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005433
5434#ifdef FEAT_JOB_CHANNEL
5435 /* Go through the list of jobs and free items without the copyID. This
5436 * must happen before doing channels, because jobs refer to channels, but
5437 * the reference from the channel to the job isn't tracked. */
5438 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5439
5440 /* Go through the list of channels and free items without the copyID. */
5441 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5442#endif
5443
5444 /*
5445 * PASS 2: free the items themselves.
5446 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005447 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005448 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005449
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005450#ifdef FEAT_JOB_CHANNEL
5451 /* Go through the list of jobs and free items without the copyID. This
5452 * must happen before doing channels, because jobs refer to channels, but
5453 * the reference from the channel to the job isn't tracked. */
5454 free_unused_jobs(copyID, COPYID_MASK);
5455
5456 /* Go through the list of channels and free items without the copyID. */
5457 free_unused_channels(copyID, COPYID_MASK);
5458#endif
5459
5460 in_free_unref_items = FALSE;
5461
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005462 return did_free;
5463}
5464
5465/*
5466 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005467 * "list_stack" is used to add lists to be marked. Can be NULL.
5468 *
5469 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005473{
5474 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005475 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005476 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005477 hashtab_T *cur_ht;
5478 ht_stack_T *ht_stack = NULL;
5479 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 cur_ht = ht;
5482 for (;;)
5483 {
5484 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005485 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 /* Mark each item in the hashtab. If the item contains a hashtab
5487 * it is added to ht_stack, if it contains a list it is added to
5488 * list_stack. */
5489 todo = (int)cur_ht->ht_used;
5490 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5491 if (!HASHITEM_EMPTY(hi))
5492 {
5493 --todo;
5494 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5495 &ht_stack, list_stack);
5496 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005497 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005498
5499 if (ht_stack == NULL)
5500 break;
5501
5502 /* take an item from the stack */
5503 cur_ht = ht_stack->ht;
5504 tempitem = ht_stack;
5505 ht_stack = ht_stack->prev;
5506 free(tempitem);
5507 }
5508
5509 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510}
5511
5512/*
5513 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005514 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5515 *
5516 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005517 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005519set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005520{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005521 listitem_T *li;
5522 int abort = FALSE;
5523 list_T *cur_l;
5524 list_stack_T *list_stack = NULL;
5525 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005526
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005527 cur_l = l;
5528 for (;;)
5529 {
5530 if (!abort)
5531 /* Mark each item in the list. If the item contains a hashtab
5532 * it is added to ht_stack, if it contains a list it is added to
5533 * list_stack. */
5534 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5535 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5536 ht_stack, &list_stack);
5537 if (list_stack == NULL)
5538 break;
5539
5540 /* take an item from the stack */
5541 cur_l = list_stack->list;
5542 tempitem = list_stack;
5543 list_stack = list_stack->prev;
5544 free(tempitem);
5545 }
5546
5547 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005548}
5549
5550/*
5551 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005552 * "list_stack" is used to add lists to be marked. Can be NULL.
5553 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5554 *
5555 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005556 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005557 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005558set_ref_in_item(
5559 typval_T *tv,
5560 int copyID,
5561 ht_stack_T **ht_stack,
5562 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005563{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005564 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005565
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005566 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005567 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005568 dict_T *dd = tv->vval.v_dict;
5569
Bram Moolenaara03f2332016-02-06 18:09:59 +01005570 if (dd != NULL && dd->dv_copyID != copyID)
5571 {
5572 /* Didn't see this dict yet. */
5573 dd->dv_copyID = copyID;
5574 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005575 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005576 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5577 }
5578 else
5579 {
5580 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5581 if (newitem == NULL)
5582 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005583 else
5584 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 newitem->ht = &dd->dv_hashtab;
5586 newitem->prev = *ht_stack;
5587 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005588 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005589 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005590 }
5591 }
5592 else if (tv->v_type == VAR_LIST)
5593 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005594 list_T *ll = tv->vval.v_list;
5595
Bram Moolenaara03f2332016-02-06 18:09:59 +01005596 if (ll != NULL && ll->lv_copyID != copyID)
5597 {
5598 /* Didn't see this list yet. */
5599 ll->lv_copyID = copyID;
5600 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005601 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005602 abort = set_ref_in_list(ll, copyID, ht_stack);
5603 }
5604 else
5605 {
5606 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005607 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005608 if (newitem == NULL)
5609 abort = TRUE;
5610 else
5611 {
5612 newitem->list = ll;
5613 newitem->prev = *list_stack;
5614 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005616 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005617 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005618 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005619 else if (tv->v_type == VAR_FUNC)
5620 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005621 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005622 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005623 else if (tv->v_type == VAR_PARTIAL)
5624 {
5625 partial_T *pt = tv->vval.v_partial;
5626 int i;
5627
5628 /* A partial does not have a copyID, because it cannot contain itself.
5629 */
5630 if (pt != NULL)
5631 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005632 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005633
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005634 if (pt->pt_dict != NULL)
5635 {
5636 typval_T dtv;
5637
5638 dtv.v_type = VAR_DICT;
5639 dtv.vval.v_dict = pt->pt_dict;
5640 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5641 }
5642
5643 for (i = 0; i < pt->pt_argc; ++i)
5644 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5645 ht_stack, list_stack);
5646 }
5647 }
5648#ifdef FEAT_JOB_CHANNEL
5649 else if (tv->v_type == VAR_JOB)
5650 {
5651 job_T *job = tv->vval.v_job;
5652 typval_T dtv;
5653
5654 if (job != NULL && job->jv_copyID != copyID)
5655 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005656 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005657 if (job->jv_channel != NULL)
5658 {
5659 dtv.v_type = VAR_CHANNEL;
5660 dtv.vval.v_channel = job->jv_channel;
5661 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5662 }
5663 if (job->jv_exit_partial != NULL)
5664 {
5665 dtv.v_type = VAR_PARTIAL;
5666 dtv.vval.v_partial = job->jv_exit_partial;
5667 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5668 }
5669 }
5670 }
5671 else if (tv->v_type == VAR_CHANNEL)
5672 {
5673 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005674 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005675 typval_T dtv;
5676 jsonq_T *jq;
5677 cbq_T *cq;
5678
5679 if (ch != NULL && ch->ch_copyID != copyID)
5680 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005681 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005682 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005683 {
5684 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5685 jq = jq->jq_next)
5686 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5687 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5688 cq = cq->cq_next)
5689 if (cq->cq_partial != NULL)
5690 {
5691 dtv.v_type = VAR_PARTIAL;
5692 dtv.vval.v_partial = cq->cq_partial;
5693 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5694 }
5695 if (ch->ch_part[part].ch_partial != NULL)
5696 {
5697 dtv.v_type = VAR_PARTIAL;
5698 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5699 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5700 }
5701 }
5702 if (ch->ch_partial != NULL)
5703 {
5704 dtv.v_type = VAR_PARTIAL;
5705 dtv.vval.v_partial = ch->ch_partial;
5706 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5707 }
5708 if (ch->ch_close_partial != NULL)
5709 {
5710 dtv.v_type = VAR_PARTIAL;
5711 dtv.vval.v_partial = ch->ch_close_partial;
5712 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5713 }
5714 }
5715 }
5716#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005717 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005718}
5719
Bram Moolenaar17a13432016-01-24 14:22:10 +01005720 static char *
5721get_var_special_name(int nr)
5722{
5723 switch (nr)
5724 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005725 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005726 case VVAL_TRUE: return "v:true";
5727 case VVAL_NONE: return "v:none";
5728 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005729 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005730 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005731 return "42";
5732}
5733
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735 * Return a string with the string representation of a variable.
5736 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005737 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005738 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005739 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5740 * stings as "string()", otherwise does not put quotes around strings, as
5741 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005742 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5743 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005744 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005746 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005747echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005748 typval_T *tv,
5749 char_u **tofree,
5750 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005751 int copyID,
5752 int echo_style,
5753 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005754 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005756 static int recurse = 0;
5757 char_u *r = NULL;
5758
Bram Moolenaar33570922005-01-25 22:26:29 +00005759 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005760 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005761 if (!did_echo_string_emsg)
5762 {
5763 /* Only give this message once for a recursive call to avoid
5764 * flooding the user with errors. And stop iterating over lists
5765 * and dicts. */
5766 did_echo_string_emsg = TRUE;
5767 EMSG(_("E724: variable nested too deep for displaying"));
5768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005769 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005770 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005771 }
5772 ++recurse;
5773
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 switch (tv->v_type)
5775 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005776 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005777 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005778 {
5779 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005780 r = tv->vval.v_string;
5781 if (r == NULL)
5782 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005783 }
5784 else
5785 {
5786 *tofree = string_quote(tv->vval.v_string, FALSE);
5787 r = *tofree;
5788 }
5789 break;
5790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005792 if (echo_style)
5793 {
5794 *tofree = NULL;
5795 r = tv->vval.v_string;
5796 }
5797 else
5798 {
5799 *tofree = string_quote(tv->vval.v_string, TRUE);
5800 r = *tofree;
5801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005802 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005803
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005804 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005805 {
5806 partial_T *pt = tv->vval.v_partial;
5807 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005808 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005809 garray_T ga;
5810 int i;
5811 char_u *tf;
5812
5813 ga_init2(&ga, 1, 100);
5814 ga_concat(&ga, (char_u *)"function(");
5815 if (fname != NULL)
5816 {
5817 ga_concat(&ga, fname);
5818 vim_free(fname);
5819 }
5820 if (pt != NULL && pt->pt_argc > 0)
5821 {
5822 ga_concat(&ga, (char_u *)", [");
5823 for (i = 0; i < pt->pt_argc; ++i)
5824 {
5825 if (i > 0)
5826 ga_concat(&ga, (char_u *)", ");
5827 ga_concat(&ga,
5828 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5829 vim_free(tf);
5830 }
5831 ga_concat(&ga, (char_u *)"]");
5832 }
5833 if (pt != NULL && pt->pt_dict != NULL)
5834 {
5835 typval_T dtv;
5836
5837 ga_concat(&ga, (char_u *)", ");
5838 dtv.v_type = VAR_DICT;
5839 dtv.vval.v_dict = pt->pt_dict;
5840 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5841 vim_free(tf);
5842 }
5843 ga_concat(&ga, (char_u *)")");
5844
5845 *tofree = ga.ga_data;
5846 r = *tofree;
5847 break;
5848 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005851 if (tv->vval.v_list == NULL)
5852 {
5853 *tofree = NULL;
5854 r = NULL;
5855 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005856 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5857 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005858 {
5859 *tofree = NULL;
5860 r = (char_u *)"[...]";
5861 }
5862 else
5863 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005864 int old_copyID = tv->vval.v_list->lv_copyID;
5865
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005866 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005867 *tofree = list2string(tv, copyID, restore_copyID);
5868 if (restore_copyID)
5869 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005870 r = *tofree;
5871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005873
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005875 if (tv->vval.v_dict == NULL)
5876 {
5877 *tofree = NULL;
5878 r = NULL;
5879 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005880 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5881 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005882 {
5883 *tofree = NULL;
5884 r = (char_u *)"{...}";
5885 }
5886 else
5887 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005888 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005889 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005890 *tofree = dict2string(tv, copyID, restore_copyID);
5891 if (restore_copyID)
5892 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005893 r = *tofree;
5894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005895 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005897 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005898 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005899 *tofree = NULL;
5900 r = get_tv_string_buf(tv, numbuf);
5901 break;
5902
Bram Moolenaar835dc632016-02-07 14:27:38 +01005903 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005904 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 *tofree = NULL;
5906 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005907 if (composite_val)
5908 {
5909 *tofree = string_quote(r, FALSE);
5910 r = *tofree;
5911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005913
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005914 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005915#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005916 *tofree = NULL;
5917 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5918 r = numbuf;
5919 break;
5920#endif
5921
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005922 case VAR_SPECIAL:
5923 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005924 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005925 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005927
Bram Moolenaar8502c702014-06-17 12:51:16 +02005928 if (--recurse == 0)
5929 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005930 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931}
5932
5933/*
5934 * Return a string with the string representation of a variable.
5935 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5936 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005937 * Does not put quotes around strings, as ":echo" displays values.
5938 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5939 * May return NULL.
5940 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005941 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005942echo_string(
5943 typval_T *tv,
5944 char_u **tofree,
5945 char_u *numbuf,
5946 int copyID)
5947{
5948 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5949}
5950
5951/*
5952 * Return a string with the string representation of a variable.
5953 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5954 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005956 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005957 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005958 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005959tv2string(
5960 typval_T *tv,
5961 char_u **tofree,
5962 char_u *numbuf,
5963 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005965 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966}
5967
5968/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 * Return string "str" in ' quotes, doubling ' characters.
5970 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005973 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005974string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005975{
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 char_u *p, *r, *s;
5978
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 len = (function ? 13 : 3);
5980 if (str != NULL)
5981 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005982 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005983 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 if (*p == '\'')
5985 ++len;
5986 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 s = r = alloc(len);
5988 if (r != NULL)
5989 {
5990 if (function)
5991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 r += 10;
5994 }
5995 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005997 if (str != NULL)
5998 for (p = str; *p != NUL; )
5999 {
6000 if (*p == '\'')
6001 *r++ = '\'';
6002 MB_COPY_CHAR(p, r);
6003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005 if (function)
6006 *r++ = ')';
6007 *r++ = NUL;
6008 }
6009 return s;
6010}
6011
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006012#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006013/*
6014 * Convert the string "text" to a floating point number.
6015 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6016 * this always uses a decimal point.
6017 * Returns the length of the text that was consumed.
6018 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020string2float(
6021 char_u *text,
6022 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006023{
6024 char *s = (char *)text;
6025 float_T f;
6026
Bram Moolenaar62473612017-01-08 19:25:40 +01006027 /* MS-Windows does not deal with "inf" and "nan" properly. */
6028 if (STRNICMP(text, "inf", 3) == 0)
6029 {
6030 *value = INFINITY;
6031 return 3;
6032 }
6033 if (STRNICMP(text, "-inf", 3) == 0)
6034 {
6035 *value = -INFINITY;
6036 return 4;
6037 }
6038 if (STRNICMP(text, "nan", 3) == 0)
6039 {
6040 *value = NAN;
6041 return 3;
6042 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006043 f = strtod(s, &s);
6044 *value = f;
6045 return (int)((char_u *)s - text);
6046}
6047#endif
6048
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 * Get the value of an environment variable.
6051 * "arg" is pointing to the '$'. It is advanced to after the name.
6052 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006053 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
6055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 char_u *string = NULL;
6059 int len;
6060 int cc;
6061 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006062 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 ++*arg;
6065 name = *arg;
6066 len = get_env_len(arg);
6067 if (evaluate)
6068 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006069 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006070 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006071
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006072 cc = name[len];
6073 name[len] = NUL;
6074 /* first try vim_getenv(), fast for normal environment vars */
6075 string = vim_getenv(name, &mustfree);
6076 if (string != NULL && *string != NUL)
6077 {
6078 if (!mustfree)
6079 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006081 else
6082 {
6083 if (mustfree)
6084 vim_free(string);
6085
6086 /* next try expanding things like $VIM and ${HOME} */
6087 string = expand_env_save(name - 1);
6088 if (string != NULL && *string == '$')
6089 {
6090 vim_free(string);
6091 string = NULL;
6092 }
6093 }
6094 name[len] = cc;
6095
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006096 rettv->v_type = VAR_STRING;
6097 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 }
6099
6100 return OK;
6101}
6102
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006103
6104
6105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006107 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006110var2fpos(
6111 typval_T *varp,
6112 int dollar_lnum, /* TRUE when $ is last line */
6113 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006115 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006117 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
Bram Moolenaara5525202006-03-02 22:52:09 +00006119 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006120 if (varp->v_type == VAR_LIST)
6121 {
6122 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006123 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006124 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006125 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006126
6127 l = varp->vval.v_list;
6128 if (l == NULL)
6129 return NULL;
6130
6131 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006132 pos.lnum = list_find_nr(l, 0L, &error);
6133 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006134 return NULL; /* invalid line number */
6135
6136 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006137 pos.col = list_find_nr(l, 1L, &error);
6138 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006139 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006140 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006141
6142 /* We accept "$" for the column number: last column. */
6143 li = list_find(l, 1L);
6144 if (li != NULL && li->li_tv.v_type == VAR_STRING
6145 && li->li_tv.vval.v_string != NULL
6146 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6147 pos.col = len + 1;
6148
Bram Moolenaara5525202006-03-02 22:52:09 +00006149 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006150 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006151 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006152 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006153
Bram Moolenaara5525202006-03-02 22:52:09 +00006154#ifdef FEAT_VIRTUALEDIT
6155 /* Get the virtual offset. Defaults to zero. */
6156 pos.coladd = list_find_nr(l, 2L, &error);
6157 if (error)
6158 pos.coladd = 0;
6159#endif
6160
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006161 return &pos;
6162 }
6163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006164 name = get_tv_string_chk(varp);
6165 if (name == NULL)
6166 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006167 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006169 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6170 {
6171 if (VIsual_active)
6172 return &VIsual;
6173 return &curwin->w_cursor;
6174 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006175 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006177 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6179 return NULL;
6180 return pp;
6181 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006182
6183#ifdef FEAT_VIRTUALEDIT
6184 pos.coladd = 0;
6185#endif
6186
Bram Moolenaar477933c2007-07-17 14:32:23 +00006187 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006188 {
6189 pos.col = 0;
6190 if (name[1] == '0') /* "w0": first visible line */
6191 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006192 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006193 /* In silent Ex mode topline is zero, but that's not a valid line
6194 * number; use one instead. */
6195 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006196 return &pos;
6197 }
6198 else if (name[1] == '$') /* "w$": last visible line */
6199 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006200 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006201 /* In silent Ex mode botline is zero, return zero then. */
6202 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006203 return &pos;
6204 }
6205 }
6206 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006208 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
6210 pos.lnum = curbuf->b_ml.ml_line_count;
6211 pos.col = 0;
6212 }
6213 else
6214 {
6215 pos.lnum = curwin->w_cursor.lnum;
6216 pos.col = (colnr_T)STRLEN(ml_get_curline());
6217 }
6218 return &pos;
6219 }
6220 return NULL;
6221}
6222
6223/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006224 * Convert list in "arg" into a position and optional file number.
6225 * When "fnump" is NULL there is no file number, only 3 items.
6226 * Note that the column is passed on as-is, the caller may want to decrement
6227 * it to use 1 for the first column.
6228 * Return FAIL when conversion is not possible, doesn't check the position for
6229 * validity.
6230 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006232list2fpos(
6233 typval_T *arg,
6234 pos_T *posp,
6235 int *fnump,
6236 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006237{
6238 list_T *l = arg->vval.v_list;
6239 long i = 0;
6240 long n;
6241
Bram Moolenaar493c1782014-05-28 14:34:46 +02006242 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6243 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006244 if (arg->v_type != VAR_LIST
6245 || l == NULL
6246 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006247 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006248 return FAIL;
6249
6250 if (fnump != NULL)
6251 {
6252 n = list_find_nr(l, i++, NULL); /* fnum */
6253 if (n < 0)
6254 return FAIL;
6255 if (n == 0)
6256 n = curbuf->b_fnum; /* current buffer */
6257 *fnump = n;
6258 }
6259
6260 n = list_find_nr(l, i++, NULL); /* lnum */
6261 if (n < 0)
6262 return FAIL;
6263 posp->lnum = n;
6264
6265 n = list_find_nr(l, i++, NULL); /* col */
6266 if (n < 0)
6267 return FAIL;
6268 posp->col = n;
6269
6270#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006271 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006272 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006273 posp->coladd = 0;
6274 else
6275 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006276#endif
6277
Bram Moolenaar493c1782014-05-28 14:34:46 +02006278 if (curswantp != NULL)
6279 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6280
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006281 return OK;
6282}
6283
6284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 * Get the length of an environment variable name.
6286 * Advance "arg" to the first character after the name.
6287 * Return 0 for error.
6288 */
6289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006290get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 char_u *p;
6293 int len;
6294
6295 for (p = *arg; vim_isIDc(*p); ++p)
6296 ;
6297 if (p == *arg) /* no name found */
6298 return 0;
6299
6300 len = (int)(p - *arg);
6301 *arg = p;
6302 return len;
6303}
6304
6305/*
6306 * Get the length of the name of a function or internal variable.
6307 * "arg" is advanced to the first non-white character after the name.
6308 * Return 0 if something is wrong.
6309 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006311get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 char_u *p;
6314 int len;
6315
6316 /* Find the end of the name. */
6317 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006318 {
6319 if (*p == ':')
6320 {
6321 /* "s:" is start of "s:var", but "n:" is not and can be used in
6322 * slice "[n:]". Also "xx:" is not a namespace. */
6323 len = (int)(p - *arg);
6324 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6325 || len > 1)
6326 break;
6327 }
6328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (p == *arg) /* no name found */
6330 return 0;
6331
6332 len = (int)(p - *arg);
6333 *arg = skipwhite(p);
6334
6335 return len;
6336}
6337
6338/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006339 * Get the length of the name of a variable or function.
6340 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006342 * Return -1 if curly braces expansion failed.
6343 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 * If the name contains 'magic' {}'s, expand them and return the
6345 * expanded name in an allocated string via 'alias' - caller must free.
6346 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348get_name_len(
6349 char_u **arg,
6350 char_u **alias,
6351 int evaluate,
6352 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 char_u *p;
6356 char_u *expr_start;
6357 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
6359 *alias = NULL; /* default to no alias */
6360
6361 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6362 && (*arg)[2] == (int)KE_SNR)
6363 {
6364 /* hard coded <SNR>, already translated */
6365 *arg += 3;
6366 return get_id_len(arg) + 3;
6367 }
6368 len = eval_fname_script(*arg);
6369 if (len > 0)
6370 {
6371 /* literal "<SID>", "s:" or "<SNR>" */
6372 *arg += len;
6373 }
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006378 p = find_name_end(*arg, &expr_start, &expr_end,
6379 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (expr_start != NULL)
6381 {
6382 char_u *temp_string;
6383
6384 if (!evaluate)
6385 {
6386 len += (int)(p - *arg);
6387 *arg = skipwhite(p);
6388 return len;
6389 }
6390
6391 /*
6392 * Include any <SID> etc in the expanded string:
6393 * Thus the -len here.
6394 */
6395 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6396 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006397 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 *alias = temp_string;
6399 *arg = skipwhite(p);
6400 return (int)STRLEN(temp_string);
6401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402
6403 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006404 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 EMSG2(_(e_invexpr2), *arg);
6406
6407 return len;
6408}
6409
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410/*
6411 * Find the end of a variable or function name, taking care of magic braces.
6412 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6413 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006414 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006415 * Return a pointer to just after the name. Equal to "arg" if there is no
6416 * valid name.
6417 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006419find_name_end(
6420 char_u *arg,
6421 char_u **expr_start,
6422 char_u **expr_end,
6423 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 int mb_nest = 0;
6426 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006428 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006430 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006432 *expr_start = NULL;
6433 *expr_end = NULL;
6434 }
6435
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006436 /* Quick check for valid starting character. */
6437 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6438 return arg;
6439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006440 for (p = arg; *p != NUL
6441 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006443 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006444 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006445 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006446 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006447 if (*p == '\'')
6448 {
6449 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006450 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006451 ;
6452 if (*p == NUL)
6453 break;
6454 }
6455 else if (*p == '"')
6456 {
6457 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006458 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006459 if (*p == '\\' && p[1] != NUL)
6460 ++p;
6461 if (*p == NUL)
6462 break;
6463 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006464 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6465 {
6466 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006467 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006468 len = (int)(p - arg);
6469 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006470 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006471 break;
6472 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 if (*p == '[')
6477 ++br_nest;
6478 else if (*p == ']')
6479 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006482 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006484 if (*p == '{')
6485 {
6486 mb_nest++;
6487 if (expr_start != NULL && *expr_start == NULL)
6488 *expr_start = p;
6489 }
6490 else if (*p == '}')
6491 {
6492 mb_nest--;
6493 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6494 *expr_end = p;
6495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 }
6498
6499 return p;
6500}
6501
6502/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006503 * Expands out the 'magic' {}'s in a variable/function name.
6504 * Note that this can call itself recursively, to deal with
6505 * constructs like foo{bar}{baz}{bam}
6506 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6507 * "in_start" ^
6508 * "expr_start" ^
6509 * "expr_end" ^
6510 * "in_end" ^
6511 *
6512 * Returns a new allocated string, which the caller must free.
6513 * Returns NULL for failure.
6514 */
6515 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516make_expanded_name(
6517 char_u *in_start,
6518 char_u *expr_start,
6519 char_u *expr_end,
6520 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006521{
6522 char_u c1;
6523 char_u *retval = NULL;
6524 char_u *temp_result;
6525 char_u *nextcmd = NULL;
6526
6527 if (expr_end == NULL || in_end == NULL)
6528 return NULL;
6529 *expr_start = NUL;
6530 *expr_end = NUL;
6531 c1 = *in_end;
6532 *in_end = NUL;
6533
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006534 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006535 if (temp_result != NULL && nextcmd == NULL)
6536 {
6537 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6538 + (in_end - expr_end) + 1));
6539 if (retval != NULL)
6540 {
6541 STRCPY(retval, in_start);
6542 STRCAT(retval, temp_result);
6543 STRCAT(retval, expr_end + 1);
6544 }
6545 }
6546 vim_free(temp_result);
6547
6548 *in_end = c1; /* put char back for error messages */
6549 *expr_start = '{';
6550 *expr_end = '}';
6551
6552 if (retval != NULL)
6553 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006554 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006555 if (expr_start != NULL)
6556 {
6557 /* Further expansion! */
6558 temp_result = make_expanded_name(retval, expr_start,
6559 expr_end, temp_result);
6560 vim_free(retval);
6561 retval = temp_result;
6562 }
6563 }
6564
6565 return retval;
6566}
6567
6568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006570 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006575 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6576}
6577
6578/*
6579 * Return TRUE if character "c" can be used as the first character in a
6580 * variable or function name (excluding '{' and '}').
6581 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006584{
6585 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586}
6587
6588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 * Set number v: variable to "val".
6590 */
6591 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006592set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006594 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595}
6596
6597/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006598 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006600 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006603 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604}
6605
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006606/*
6607 * Get string v: variable value. Uses a static buffer, can only be used once.
6608 */
6609 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006611{
6612 return get_tv_string(&vimvars[idx].vv_tv);
6613}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006616 * Get List v: variable value. Caller must take care of reference count when
6617 * needed.
6618 */
6619 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006621{
6622 return vimvars[idx].vv_list;
6623}
6624
6625/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006626 * Set v:char to character "c".
6627 */
6628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006629set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006630{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006631 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006632
6633#ifdef FEAT_MBYTE
6634 if (has_mbyte)
6635 buf[(*mb_char2bytes)(c, buf)] = NUL;
6636 else
6637#endif
6638 {
6639 buf[0] = c;
6640 buf[1] = NUL;
6641 }
6642 set_vim_var_string(VV_CHAR, buf, -1);
6643}
6644
6645/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006646 * Set v:count to "count" and v:count1 to "count1".
6647 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 */
6649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650set_vcount(
6651 long count,
6652 long count1,
6653 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006655 if (set_prevcount)
6656 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657 vimvars[VV_COUNT].vv_nr = count;
6658 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659}
6660
6661/*
6662 * Set string v: variable to a copy of "val".
6663 */
6664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665set_vim_var_string(
6666 int idx,
6667 char_u *val,
6668 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
Bram Moolenaara542c682016-01-31 16:28:04 +01006670 clear_tv(&vimvars[idx].vv_di.di_tv);
6671 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006675 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006677 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
6680/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006681 * Set List v: variable to "val".
6682 */
6683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006684set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006685{
Bram Moolenaara542c682016-01-31 16:28:04 +01006686 clear_tv(&vimvars[idx].vv_di.di_tv);
6687 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006688 vimvars[idx].vv_list = val;
6689 if (val != NULL)
6690 ++val->lv_refcount;
6691}
6692
6693/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006694 * Set Dictionary v: variable to "val".
6695 */
6696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006697set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006698{
6699 int todo;
6700 hashitem_T *hi;
6701
Bram Moolenaara542c682016-01-31 16:28:04 +01006702 clear_tv(&vimvars[idx].vv_di.di_tv);
6703 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006704 vimvars[idx].vv_dict = val;
6705 if (val != NULL)
6706 {
6707 ++val->dv_refcount;
6708
6709 /* Set readonly */
6710 todo = (int)val->dv_hashtab.ht_used;
6711 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6712 {
6713 if (HASHITEM_EMPTY(hi))
6714 continue;
6715 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006716 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006717 }
6718 }
6719}
6720
6721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 * Set v:register if needed.
6723 */
6724 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006725set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 char_u regname;
6728
6729 if (c == 0 || c == ' ')
6730 regname = '"';
6731 else
6732 regname = c;
6733 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006734 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 set_vim_var_string(VV_REG, &regname, 1);
6736}
6737
6738/*
6739 * Get or set v:exception. If "oldval" == NULL, return the current value.
6740 * Otherwise, restore the value to "oldval" and return NULL.
6741 * Must always be called in pairs to save and restore v:exception! Does not
6742 * take care of memory allocations.
6743 */
6744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006745v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
Bram Moolenaare9a41262005-01-15 22:18:47 +00006750 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 return NULL;
6752}
6753
6754/*
6755 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6756 * Otherwise, restore the value to "oldval" and return NULL.
6757 * Must always be called in pairs to save and restore v:throwpoint! Does not
6758 * take care of memory allocations.
6759 */
6760 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006761v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006764 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 return NULL;
6768}
6769
6770#if defined(FEAT_AUTOCMD) || defined(PROTO)
6771/*
6772 * Set v:cmdarg.
6773 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6774 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6775 * Must always be called in pairs!
6776 */
6777 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006778set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 char_u *oldval;
6781 char_u *newval;
6782 unsigned len;
6783
Bram Moolenaare9a41262005-01-15 22:18:47 +00006784 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006785 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006787 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006788 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006789 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
6791
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006792 if (eap->force_bin == FORCE_BIN)
6793 len = 6;
6794 else if (eap->force_bin == FORCE_NOBIN)
6795 len = 8;
6796 else
6797 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006798
6799 if (eap->read_edit)
6800 len += 7;
6801
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006802 if (eap->force_ff != 0)
6803 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6804# ifdef FEAT_MBYTE
6805 if (eap->force_enc != 0)
6806 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006807 if (eap->bad_char != 0)
6808 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006809# endif
6810
6811 newval = alloc(len + 1);
6812 if (newval == NULL)
6813 return NULL;
6814
6815 if (eap->force_bin == FORCE_BIN)
6816 sprintf((char *)newval, " ++bin");
6817 else if (eap->force_bin == FORCE_NOBIN)
6818 sprintf((char *)newval, " ++nobin");
6819 else
6820 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006821
6822 if (eap->read_edit)
6823 STRCAT(newval, " ++edit");
6824
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006825 if (eap->force_ff != 0)
6826 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6827 eap->cmd + eap->force_ff);
6828# ifdef FEAT_MBYTE
6829 if (eap->force_enc != 0)
6830 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6831 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006832 if (eap->bad_char == BAD_KEEP)
6833 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6834 else if (eap->bad_char == BAD_DROP)
6835 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6836 else if (eap->bad_char != 0)
6837 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006838# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006840 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841}
6842#endif
6843
6844/*
6845 * Get the value of internal variable "name".
6846 * Return OK or FAIL.
6847 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849get_var_tv(
6850 char_u *name,
6851 int len, /* length of "name" */
6852 typval_T *rettv, /* NULL when only checking existence */
6853 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6854 int verbose, /* may give error message */
6855 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006859 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
6862 /* truncate the name, so that we can use strcmp() */
6863 cc = name[len];
6864 name[len] = NUL;
6865
6866 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 * Check for user-defined variables.
6868 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006869 v = find_var(name, NULL, no_autoload);
6870 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006872 tv = &v->di_tv;
6873 if (dip != NULL)
6874 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876
Bram Moolenaare9a41262005-01-15 22:18:47 +00006877 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006879 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006880 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 ret = FAIL;
6882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006884 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
6886 name[len] = cc;
6887
6888 return ret;
6889}
6890
6891/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006892 * Check if variable "name[len]" is a local variable or an argument.
6893 * If so, "*eval_lavars_used" is set to TRUE.
6894 */
6895 static void
6896check_vars(char_u *name, int len)
6897{
6898 int cc;
6899 char_u *varname;
6900 hashtab_T *ht;
6901
6902 if (eval_lavars_used == NULL)
6903 return;
6904
6905 /* truncate the name, so that we can use strcmp() */
6906 cc = name[len];
6907 name[len] = NUL;
6908
6909 ht = find_var_ht(name, &varname);
6910 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6911 {
6912 if (find_var(name, NULL, TRUE) != NULL)
6913 *eval_lavars_used = TRUE;
6914 }
6915
6916 name[len] = cc;
6917}
6918
6919/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006920 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6921 * Also handle function call with Funcref variable: func(expr)
6922 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6923 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006925handle_subscript(
6926 char_u **arg,
6927 typval_T *rettv,
6928 int evaluate, /* do more than finding the end */
6929 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006930{
6931 int ret = OK;
6932 dict_T *selfdict = NULL;
6933 char_u *s;
6934 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006936
6937 while (ret == OK
6938 && (**arg == '['
6939 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006940 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6941 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006942 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006943 {
6944 if (**arg == '(')
6945 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006946 partial_T *pt = NULL;
6947
Bram Moolenaard9fba312005-06-26 22:34:35 +00006948 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006949 if (evaluate)
6950 {
6951 functv = *rettv;
6952 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006953
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006954 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006955 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006956 {
6957 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006958 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006959 }
6960 else
6961 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006962 }
6963 else
6964 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006965 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006967 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006968
6969 /* Clear the funcref afterwards, so that deleting it while
6970 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006971 if (evaluate)
6972 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006973
6974 /* Stop the expression evaluation when immediately aborting on
6975 * error, or when an interrupt occurred or an exception was thrown
6976 * but not caught. */
6977 if (aborting())
6978 {
6979 if (ret == OK)
6980 clear_tv(rettv);
6981 ret = FAIL;
6982 }
6983 dict_unref(selfdict);
6984 selfdict = NULL;
6985 }
6986 else /* **arg == '[' || **arg == '.' */
6987 {
6988 dict_unref(selfdict);
6989 if (rettv->v_type == VAR_DICT)
6990 {
6991 selfdict = rettv->vval.v_dict;
6992 if (selfdict != NULL)
6993 ++selfdict->dv_refcount;
6994 }
6995 else
6996 selfdict = NULL;
6997 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6998 {
6999 clear_tv(rettv);
7000 ret = FAIL;
7001 }
7002 }
7003 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007004
Bram Moolenaar1d429612016-05-24 15:44:17 +02007005 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7006 * Don't do this when "Func" is already a partial that was bound
7007 * explicitly (pt_auto is FALSE). */
7008 if (selfdict != NULL
7009 && (rettv->v_type == VAR_FUNC
7010 || (rettv->v_type == VAR_PARTIAL
7011 && (rettv->vval.v_partial->pt_auto
7012 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007013 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007014
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007015 dict_unref(selfdict);
7016 return ret;
7017}
7018
7019/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007020 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007021 * value).
7022 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007023 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007024alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007025{
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007027}
7028
7029/*
7030 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 * The string "s" must have been allocated, it is consumed.
7032 * Return NULL for out of memory, the variable otherwise.
7033 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007035alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007039 rettv = alloc_tv();
7040 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007042 rettv->v_type = VAR_STRING;
7043 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 else
7046 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007047 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048}
7049
7050/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007051 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007054free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 if (varp != NULL)
7057 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007058 switch (varp->v_type)
7059 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007060 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007061 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007062 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007063 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007064 vim_free(varp->vval.v_string);
7065 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007066 case VAR_PARTIAL:
7067 partial_unref(varp->vval.v_partial);
7068 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007069 case VAR_LIST:
7070 list_unref(varp->vval.v_list);
7071 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007072 case VAR_DICT:
7073 dict_unref(varp->vval.v_dict);
7074 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007075 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007076#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007077 job_unref(varp->vval.v_job);
7078 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007079#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007080 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007081#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007082 channel_unref(varp->vval.v_channel);
7083 break;
7084#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007085 case VAR_NUMBER:
7086 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007087 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007088 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007089 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 vim_free(varp);
7092 }
7093}
7094
7095/*
7096 * Free the memory for a variable value and set the value to NULL or 0.
7097 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007099clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 if (varp != NULL)
7102 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007103 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007106 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007107 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007108 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 vim_free(varp->vval.v_string);
7110 varp->vval.v_string = NULL;
7111 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007112 case VAR_PARTIAL:
7113 partial_unref(varp->vval.v_partial);
7114 varp->vval.v_partial = NULL;
7115 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 case VAR_LIST:
7117 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120 case VAR_DICT:
7121 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007124 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007125 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 varp->vval.v_number = 0;
7127 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007128 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007129#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007130 varp->vval.v_float = 0.0;
7131 break;
7132#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007133 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007134#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007135 job_unref(varp->vval.v_job);
7136 varp->vval.v_job = NULL;
7137#endif
7138 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007139 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007141 channel_unref(varp->vval.v_channel);
7142 varp->vval.v_channel = NULL;
7143#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007144 case VAR_UNKNOWN:
7145 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007147 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
7149}
7150
7151/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007152 * Set the value of a variable to NULL without freeing items.
7153 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007155init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007156{
7157 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007159}
7160
7161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 * Get the number value of a variable.
7163 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007164 * For incompatible types, return 0.
7165 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7166 * caller of incompatible types: it sets *denote to TRUE if "denote"
7167 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007169 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007172 int error = FALSE;
7173
7174 return get_tv_number_chk(varp, &error); /* return 0L on error */
7175}
7176
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007177 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007179{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007180 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007184 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007185 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007186 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007187#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007188 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007189 break;
7190#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007191 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007192 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007193 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007194 break;
7195 case VAR_STRING:
7196 if (varp->vval.v_string != NULL)
7197 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007198 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007199 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007200 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007201 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007202 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007203 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007204 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007205 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007206 case VAR_SPECIAL:
7207 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7208 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007209 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007210#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007211 EMSG(_("E910: Using a Job as a Number"));
7212 break;
7213#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007214 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007215#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007216 EMSG(_("E913: Using a Channel as a Number"));
7217 break;
7218#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007219 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007220 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007223 if (denote == NULL) /* useful for values that must be unsigned */
7224 n = -1;
7225 else
7226 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007227 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007230#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007233{
7234 switch (varp->v_type)
7235 {
7236 case VAR_NUMBER:
7237 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007238 case VAR_FLOAT:
7239 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007240 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007241 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007242 EMSG(_("E891: Using a Funcref as a Float"));
7243 break;
7244 case VAR_STRING:
7245 EMSG(_("E892: Using a String as a Float"));
7246 break;
7247 case VAR_LIST:
7248 EMSG(_("E893: Using a List as a Float"));
7249 break;
7250 case VAR_DICT:
7251 EMSG(_("E894: Using a Dictionary as a Float"));
7252 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007253 case VAR_SPECIAL:
7254 EMSG(_("E907: Using a special value as a Float"));
7255 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007256 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007257# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007258 EMSG(_("E911: Using a Job as a Float"));
7259 break;
7260# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007261 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007262# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007263 EMSG(_("E914: Using a Channel as a Float"));
7264 break;
7265# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007266 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007267 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007268 break;
7269 }
7270 return 0;
7271}
7272#endif
7273
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 * Get the string value of a variable.
7276 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007277 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7278 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 * If the String variable has never been set, return an empty string.
7280 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007281 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7282 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007284 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007285get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 static char_u mybuf[NUMBUFLEN];
7288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007289 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290}
7291
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007292 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295 char_u *res = get_tv_string_buf_chk(varp, buf);
7296
7297 return res != NULL ? res : (char_u *)"";
7298}
7299
Bram Moolenaar7d647822014-04-05 21:28:56 +02007300/*
7301 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7302 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007303 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007304get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007305{
7306 static char_u mybuf[NUMBUFLEN];
7307
7308 return get_tv_string_buf_chk(varp, mybuf);
7309}
7310
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007311 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007313{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007317 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7318 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319 return buf;
7320 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007321 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007322 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323 break;
7324 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007325 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 break;
7327 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007328 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007330 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007331#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007332 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007333 break;
7334#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007335 case VAR_STRING:
7336 if (varp->vval.v_string != NULL)
7337 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007338 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007339 case VAR_SPECIAL:
7340 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7341 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007342 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007343#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007344 {
7345 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007346 char *status;
7347
7348 if (job == NULL)
7349 return (char_u *)"no process";
7350 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007351 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007352 : "run";
7353# ifdef UNIX
7354 vim_snprintf((char *)buf, NUMBUFLEN,
7355 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007356# elif defined(WIN32)
7357 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007358 "process %ld %s",
7359 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007360 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007361# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007362 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007363 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7364# endif
7365 return buf;
7366 }
7367#endif
7368 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007369 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007370#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007371 {
7372 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007373 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007374
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007375 if (channel == NULL)
7376 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7377 else
7378 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007379 "channel %d %s", channel->ch_id, status);
7380 return buf;
7381 }
7382#endif
7383 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007384 case VAR_UNKNOWN:
7385 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007388 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389}
7390
7391/*
7392 * Find variable "name" in the list of variables.
7393 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007395 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007398 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007399find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007403 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
Bram Moolenaara7043832005-01-21 11:56:39 +00007405 ht = find_var_ht(name, &varname);
7406 if (htp != NULL)
7407 *htp = ht;
7408 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007410 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7411 if (ret != NULL)
7412 return ret;
7413
7414 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007415 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416}
7417
7418/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007419 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007420 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007422 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423find_var_in_ht(
7424 hashtab_T *ht,
7425 int htname,
7426 char_u *varname,
7427 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007428{
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 hashitem_T *hi;
7430
7431 if (*varname == NUL)
7432 {
7433 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007434 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007436 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 case 'g': return &globvars_var;
7438 case 'v': return &vimvars_var;
7439 case 'b': return &curbuf->b_bufvar;
7440 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007441 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007442 case 'l': return get_funccal_local_var();
7443 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 }
7445 return NULL;
7446 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007447
7448 hi = hash_find(ht, varname);
7449 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007450 {
7451 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007452 * worked find the variable again. Don't auto-load a script if it was
7453 * loaded already, otherwise it would be loaded every time when
7454 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007455 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007456 {
7457 /* Note: script_autoload() may make "hi" invalid. It must either
7458 * be obtained again or not used. */
7459 if (!script_autoload(varname, FALSE) || aborting())
7460 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007461 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007462 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007463 if (HASHITEM_EMPTY(hi))
7464 return NULL;
7465 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007466 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007467}
7468
7469/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007471 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007472 * Set "varname" to the start of name without ':'.
7473 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007474 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007477 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007478 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007479
Bram Moolenaar73627d02015-08-11 15:46:09 +02007480 if (name[0] == NUL)
7481 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 if (name[1] != ':')
7483 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007484 /* The name must not start with a colon or #. */
7485 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 return NULL;
7487 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007488
7489 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007490 hi = hash_find(&compat_hashtab, name);
7491 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007492 return &compat_hashtab;
7493
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007494 ht = get_funccal_local_ht();
7495 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007497 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
7499 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007500 if (*name == 'g') /* global variable */
7501 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007502 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7503 */
7504 if (vim_strchr(name + 2, ':') != NULL
7505 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007506 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007508 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007510 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007511 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007512 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 if (*name == 'v') /* v: variable */
7514 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007515 if (*name == 'a') /* a: function argument */
7516 return get_funccal_args_ht();
7517 if (*name == 'l') /* l: local function variable */
7518 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (*name == 's' /* script variable */
7520 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7521 return &SCRIPT_VARS(current_SID);
7522 return NULL;
7523}
7524
7525/*
7526 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007527 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 * Returns NULL when it doesn't exist.
7529 */
7530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007531get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007535 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (v == NULL)
7537 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540
7541/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 * sourcing this script and when executing functions defined in the script.
7544 */
7545 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
Bram Moolenaara7043832005-01-21 11:56:39 +00007548 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 hashtab_T *ht;
7550 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007551
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7553 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007554 /* Re-allocating ga_data means that an ht_array pointing to
7555 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007556 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007557 for (i = 1; i <= ga_scripts.ga_len; ++i)
7558 {
7559 ht = &SCRIPT_VARS(i);
7560 if (ht->ht_mask == HT_INIT_SIZE - 1)
7561 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007562 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007564 }
7565
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 while (ga_scripts.ga_len < id)
7567 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007568 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007569 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007570 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 }
7573 }
7574}
7575
7576/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7578 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 */
7580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007584 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007585 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007586 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007587 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 dict_var->di_tv.vval.v_dict = dict;
7589 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007590 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7592 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593}
7594
7595/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007596 * Unreference a dictionary initialized by init_var_dict().
7597 */
7598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007600{
7601 /* Now the dict needs to be freed if no one else is using it, go back to
7602 * normal reference counting. */
7603 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7604 dict_unref(dict);
7605}
7606
7607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 * Frees all allocated variables and the value they contain.
7610 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 */
7612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007613vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007614{
7615 vars_clear_ext(ht, TRUE);
7616}
7617
7618/*
7619 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7620 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007622vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
Bram Moolenaara7043832005-01-21 11:56:39 +00007624 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
7626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007629 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007630 for (hi = ht->ht_array; todo > 0; ++hi)
7631 {
7632 if (!HASHITEM_EMPTY(hi))
7633 {
7634 --todo;
7635
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007637 * ht_array might change then. hash_clear() takes care of it
7638 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 v = HI2DI(hi);
7640 if (free_val)
7641 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007642 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007643 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007644 }
7645 }
7646 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007647 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
Bram Moolenaara7043832005-01-21 11:56:39 +00007650/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 * Delete a variable from hashtab "ht" at item "hi".
7652 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007658
7659 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 clear_tv(&di->di_tv);
7661 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664/*
7665 * List the value of one internal variable.
7666 */
7667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007668list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 char_u *tofree;
7671 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007672 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007674 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007676 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007677 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678}
7679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007681list_one_var_a(
7682 char_u *prefix,
7683 char_u *name,
7684 int type,
7685 char_u *string,
7686 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
Bram Moolenaar31859182007-08-14 20:41:13 +00007688 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7689 msg_start();
7690 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 if (name != NULL) /* "a:" vars don't have a name stored */
7692 msg_puts(name);
7693 msg_putchar(' ');
7694 msg_advance(22);
7695 if (type == VAR_NUMBER)
7696 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007697 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 msg_putchar('*');
7699 else if (type == VAR_LIST)
7700 {
7701 msg_putchar('[');
7702 if (*string == '[')
7703 ++string;
7704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 else if (type == VAR_DICT)
7706 {
7707 msg_putchar('{');
7708 if (*string == '{')
7709 ++string;
7710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
7712 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007716 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007718 if (*first)
7719 {
7720 msg_clr_eos();
7721 *first = FALSE;
7722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
7725/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 * If the variable already exists, the value is updated.
7728 * Otherwise the variable is created.
7729 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731set_var(
7732 char_u *name,
7733 typval_T *tv,
7734 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007740 ht = find_var_ht(name, &varname);
7741 if (ht == NULL || *varname == NUL)
7742 {
7743 EMSG2(_(e_illvar), name);
7744 return;
7745 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007746 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007747
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007748 /* Search in parent scope which is possible to reference from lambda */
7749 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007750 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007751
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007752 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7753 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007754 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007759 if (var_check_ro(v->di_flags, name, FALSE)
7760 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007762
7763 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007764 * Handle setting internal v: variables separately where needed to
7765 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 */
7767 if (ht == &vimvarht)
7768 {
7769 if (v->di_tv.v_type == VAR_STRING)
7770 {
7771 vim_free(v->di_tv.vval.v_string);
7772 if (copy || tv->v_type != VAR_STRING)
7773 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7774 else
7775 {
7776 /* Take over the string to avoid an extra alloc/free. */
7777 v->di_tv.vval.v_string = tv->vval.v_string;
7778 tv->vval.v_string = NULL;
7779 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007780 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007782 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007785 if (STRCMP(varname, "searchforward") == 0)
7786 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007787#ifdef FEAT_SEARCH_EXTRA
7788 else if (STRCMP(varname, "hlsearch") == 0)
7789 {
7790 no_hlsearch = !v->di_tv.vval.v_number;
7791 redraw_all_later(SOME_VALID);
7792 }
7793#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007794 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007796 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007797 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 }
7799
7800 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 }
7802 else /* add a new variable */
7803 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007804 /* Can't add "v:" variable. */
7805 if (ht == &vimvarht)
7806 {
7807 EMSG2(_(e_illvar), name);
7808 return;
7809 }
7810
Bram Moolenaar92124a32005-06-17 22:03:40 +00007811 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007812 if (!valid_varname(varname))
7813 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007815 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7816 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007817 if (v == NULL)
7818 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007819 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007822 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 return;
7824 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007825 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007827
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007828 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007830 else
7831 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007833 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836}
7837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007839 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 * Also give an error message.
7841 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007843var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007844{
7845 if (flags & DI_FLAGS_RO)
7846 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007847 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007848 return TRUE;
7849 }
7850 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7851 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007852 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 return TRUE;
7854 }
7855 return FALSE;
7856}
7857
7858/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007859 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7860 * Also give an error message.
7861 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007863var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007864{
7865 if (flags & DI_FLAGS_FIX)
7866 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007867 EMSG2(_("E795: Cannot delete variable %s"),
7868 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007869 return TRUE;
7870 }
7871 return FALSE;
7872}
7873
7874/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007875 * Check if a funcref is assigned to a valid variable name.
7876 * Return TRUE and give an error if not.
7877 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007878 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879var_check_func_name(
7880 char_u *name, /* points to start of variable name */
7881 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007882{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007883 /* Allow for w: b: s: and t:. */
7884 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007885 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7886 ? name[2] : name[0]))
7887 {
7888 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7889 name);
7890 return TRUE;
7891 }
7892 /* Don't allow hiding a function. When "v" is not NULL we might be
7893 * assigning another function to the same var, the type is checked
7894 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007895 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007896 {
7897 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7898 name);
7899 return TRUE;
7900 }
7901 return FALSE;
7902}
7903
7904/*
7905 * Check if a variable name is valid.
7906 * Return FALSE and give an error if not.
7907 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007909valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007910{
7911 char_u *p;
7912
7913 for (p = varname; *p != NUL; ++p)
7914 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7915 && *p != AUTOLOAD_CHAR)
7916 {
7917 EMSG2(_(e_illvar), varname);
7918 return FALSE;
7919 }
7920 return TRUE;
7921}
7922
7923/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007924 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007925 * Also give an error message, using "name" or _("name") when use_gettext is
7926 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007927 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007929tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007930{
7931 if (lock & VAR_LOCKED)
7932 {
7933 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007934 name == NULL ? (char_u *)_("Unknown")
7935 : use_gettext ? (char_u *)_(name)
7936 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007937 return TRUE;
7938 }
7939 if (lock & VAR_FIXED)
7940 {
7941 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007942 name == NULL ? (char_u *)_("Unknown")
7943 : use_gettext ? (char_u *)_(name)
7944 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007945 return TRUE;
7946 }
7947 return FALSE;
7948}
7949
7950/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007952 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007953 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007954 * It is OK for "from" and "to" to point to the same item. This is used to
7955 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007956 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007958copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007960 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007961 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 switch (from->v_type)
7963 {
7964 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007965 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007966 to->vval.v_number = from->vval.v_number;
7967 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007968 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007969#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007970 to->vval.v_float = from->vval.v_float;
7971 break;
7972#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007973 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007974#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007975 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007976 if (to->vval.v_job != NULL)
7977 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007978 break;
7979#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007980 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007981#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007982 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007983 if (to->vval.v_channel != NULL)
7984 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007985 break;
7986#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007987 case VAR_STRING:
7988 case VAR_FUNC:
7989 if (from->vval.v_string == NULL)
7990 to->vval.v_string = NULL;
7991 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007992 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007993 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007994 if (from->v_type == VAR_FUNC)
7995 func_ref(to->vval.v_string);
7996 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007998 case VAR_PARTIAL:
7999 if (from->vval.v_partial == NULL)
8000 to->vval.v_partial = NULL;
8001 else
8002 {
8003 to->vval.v_partial = from->vval.v_partial;
8004 ++to->vval.v_partial->pt_refcount;
8005 }
8006 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 case VAR_LIST:
8008 if (from->vval.v_list == NULL)
8009 to->vval.v_list = NULL;
8010 else
8011 {
8012 to->vval.v_list = from->vval.v_list;
8013 ++to->vval.v_list->lv_refcount;
8014 }
8015 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008016 case VAR_DICT:
8017 if (from->vval.v_dict == NULL)
8018 to->vval.v_dict = NULL;
8019 else
8020 {
8021 to->vval.v_dict = from->vval.v_dict;
8022 ++to->vval.v_dict->dv_refcount;
8023 }
8024 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008025 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008026 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008027 break;
8028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029}
8030
8031/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 * Make a copy of an item.
8033 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008034 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8035 * reference to an already copied list/dict can be used.
8036 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039item_copy(
8040 typval_T *from,
8041 typval_T *to,
8042 int deep,
8043 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008044{
8045 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008046 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008049 {
8050 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008051 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008052 }
8053 ++recurse;
8054
8055 switch (from->v_type)
8056 {
8057 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008059 case VAR_STRING:
8060 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008061 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008062 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008063 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008064 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008065 copy_tv(from, to);
8066 break;
8067 case VAR_LIST:
8068 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008069 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008070 if (from->vval.v_list == NULL)
8071 to->vval.v_list = NULL;
8072 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8073 {
8074 /* use the copy made earlier */
8075 to->vval.v_list = from->vval.v_list->lv_copylist;
8076 ++to->vval.v_list->lv_refcount;
8077 }
8078 else
8079 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8080 if (to->vval.v_list == NULL)
8081 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008082 break;
8083 case VAR_DICT:
8084 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008085 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008086 if (from->vval.v_dict == NULL)
8087 to->vval.v_dict = NULL;
8088 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8089 {
8090 /* use the copy made earlier */
8091 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8092 ++to->vval.v_dict->dv_refcount;
8093 }
8094 else
8095 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8096 if (to->vval.v_dict == NULL)
8097 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008099 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008100 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008101 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008102 }
8103 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008104 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105}
8106
8107/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 * This function is used by f_input() and f_inputdialog() functions. The third
8109 * argument to f_input() specifies the type of completion to use at the
8110 * prompt. The third argument to f_inputdialog() specifies the value to return
8111 * when the user cancels the prompt.
8112 */
8113 void
8114get_user_input(
8115 typval_T *argvars,
8116 typval_T *rettv,
8117 int inputdialog,
8118 int secret)
8119{
8120 char_u *prompt = get_tv_string_chk(&argvars[0]);
8121 char_u *p = NULL;
8122 int c;
8123 char_u buf[NUMBUFLEN];
8124 int cmd_silent_save = cmd_silent;
8125 char_u *defstr = (char_u *)"";
8126 int xp_type = EXPAND_NOTHING;
8127 char_u *xp_arg = NULL;
8128
8129 rettv->v_type = VAR_STRING;
8130 rettv->vval.v_string = NULL;
8131
8132#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008133 /* While starting up, there is no place to enter text. When running tests
8134 * with --not-a-term we assume feedkeys() will be used. */
8135 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 return;
8137#endif
8138
8139 cmd_silent = FALSE; /* Want to see the prompt. */
8140 if (prompt != NULL)
8141 {
8142 /* Only the part of the message after the last NL is considered as
8143 * prompt for the command line */
8144 p = vim_strrchr(prompt, '\n');
8145 if (p == NULL)
8146 p = prompt;
8147 else
8148 {
8149 ++p;
8150 c = *p;
8151 *p = NUL;
8152 msg_start();
8153 msg_clr_eos();
8154 msg_puts_attr(prompt, echo_attr);
8155 msg_didout = FALSE;
8156 msg_starthere();
8157 *p = c;
8158 }
8159 cmdline_row = msg_row;
8160
8161 if (argvars[1].v_type != VAR_UNKNOWN)
8162 {
8163 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8164 if (defstr != NULL)
8165 stuffReadbuffSpec(defstr);
8166
8167 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8168 {
8169 char_u *xp_name;
8170 int xp_namelen;
8171 long argt;
8172
8173 /* input() with a third argument: completion */
8174 rettv->vval.v_string = NULL;
8175
8176 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8177 if (xp_name == NULL)
8178 return;
8179
8180 xp_namelen = (int)STRLEN(xp_name);
8181
8182 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8183 &xp_arg) == FAIL)
8184 return;
8185 }
8186 }
8187
8188 if (defstr != NULL)
8189 {
8190 int save_ex_normal_busy = ex_normal_busy;
8191 ex_normal_busy = 0;
8192 rettv->vval.v_string =
8193 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8194 xp_type, xp_arg);
8195 ex_normal_busy = save_ex_normal_busy;
8196 }
8197 if (inputdialog && rettv->vval.v_string == NULL
8198 && argvars[1].v_type != VAR_UNKNOWN
8199 && argvars[2].v_type != VAR_UNKNOWN)
8200 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8201 &argvars[2], buf));
8202
8203 vim_free(xp_arg);
8204
8205 /* since the user typed this, no need to wait for return */
8206 need_wait_return = FALSE;
8207 msg_didout = FALSE;
8208 }
8209 cmd_silent = cmd_silent_save;
8210}
8211
8212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 * ":echo expr1 ..." print each argument separated with a space, add a
8214 * newline at the end.
8215 * ":echon expr1 ..." print each argument plain.
8216 */
8217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008218ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008222 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 char_u *p;
8224 int needclr = TRUE;
8225 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008226 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227
8228 if (eap->skip)
8229 ++emsg_skip;
8230 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8231 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232 /* If eval1() causes an error message the text from the command may
8233 * still need to be cleared. E.g., "echo 22,44". */
8234 need_clr_eos = needclr;
8235
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
8239 /*
8240 * Report the invalid expression unless the expression evaluation
8241 * has been cancelled due to an aborting error, an interrupt, or an
8242 * exception.
8243 */
8244 if (!aborting())
8245 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 break;
8248 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008249 need_clr_eos = FALSE;
8250
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (!eap->skip)
8252 {
8253 if (atstart)
8254 {
8255 atstart = FALSE;
8256 /* Call msg_start() after eval1(), evaluating the expression
8257 * may cause a message to appear. */
8258 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008259 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008260 /* Mark the saved text as finishing the line, so that what
8261 * follows is displayed on a new line when scrolling back
8262 * at the more prompt. */
8263 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 }
8267 else if (eap->cmdidx == CMD_echo)
8268 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008269 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008270 if (p != NULL)
8271 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008273 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008275 if (*p != TAB && needclr)
8276 {
8277 /* remove any text still there from the command */
8278 msg_clr_eos();
8279 needclr = FALSE;
8280 }
8281 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 }
8283 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008284 {
8285#ifdef FEAT_MBYTE
8286 if (has_mbyte)
8287 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008288 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008289
8290 (void)msg_outtrans_len_attr(p, i, echo_attr);
8291 p += i - 1;
8292 }
8293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008295 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 arg = skipwhite(arg);
8302 }
8303 eap->nextcmd = check_nextcmd(arg);
8304
8305 if (eap->skip)
8306 --emsg_skip;
8307 else
8308 {
8309 /* remove text that may still be there from the command */
8310 if (needclr)
8311 msg_clr_eos();
8312 if (eap->cmdidx == CMD_echo)
8313 msg_end();
8314 }
8315}
8316
8317/*
8318 * ":echohl {name}".
8319 */
8320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008321ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008323 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325
8326/*
8327 * ":execute expr1 ..." execute the result of an expression.
8328 * ":echomsg expr1 ..." Print a message
8329 * ":echoerr expr1 ..." Print an error
8330 * Each gets spaces around each argument and a newline at the end for
8331 * echo commands
8332 */
8333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008334ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 int ret = OK;
8339 char_u *p;
8340 garray_T ga;
8341 int len;
8342 int save_did_emsg;
8343
8344 ga_init2(&ga, 1, 80);
8345
8346 if (eap->skip)
8347 ++emsg_skip;
8348 while (*arg != NUL && *arg != '|' && *arg != '\n')
8349 {
8350 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008351 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
8353 /*
8354 * Report the invalid expression unless the expression evaluation
8355 * has been cancelled due to an aborting error, an interrupt, or an
8356 * exception.
8357 */
8358 if (!aborting())
8359 EMSG2(_(e_invexpr2), p);
8360 ret = FAIL;
8361 break;
8362 }
8363
8364 if (!eap->skip)
8365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 len = (int)STRLEN(p);
8368 if (ga_grow(&ga, len + 2) == FAIL)
8369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008370 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 ret = FAIL;
8372 break;
8373 }
8374 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 ga.ga_len += len;
8378 }
8379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 arg = skipwhite(arg);
8382 }
8383
8384 if (ret != FAIL && ga.ga_data != NULL)
8385 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008386 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8387 {
8388 /* Mark the already saved text as finishing the line, so that what
8389 * follows is displayed on a new line when scrolling back at the
8390 * more prompt. */
8391 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008392 }
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008397 out_flush();
8398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 else if (eap->cmdidx == CMD_echoerr)
8400 {
8401 /* We don't want to abort following commands, restore did_emsg. */
8402 save_did_emsg = did_emsg;
8403 EMSG((char_u *)ga.ga_data);
8404 if (!force_abort)
8405 did_emsg = save_did_emsg;
8406 }
8407 else if (eap->cmdidx == CMD_execute)
8408 do_cmdline((char_u *)ga.ga_data,
8409 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8410 }
8411
8412 ga_clear(&ga);
8413
8414 if (eap->skip)
8415 --emsg_skip;
8416
8417 eap->nextcmd = check_nextcmd(arg);
8418}
8419
8420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008421 * Find window specified by "vp" in tabpage "tp".
8422 */
8423 win_T *
8424find_win_by_nr(
8425 typval_T *vp,
8426 tabpage_T *tp UNUSED) /* NULL for current tab page */
8427{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008429 int nr;
8430
8431 nr = (int)get_tv_number_chk(vp, NULL);
8432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 if (nr < 0)
8434 return NULL;
8435 if (nr == 0)
8436 return curwin;
8437
Bram Moolenaar29323592016-07-24 22:04:11 +02008438 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8439 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 if (nr >= LOWEST_WIN_ID)
8441 {
8442 if (wp->w_id == nr)
8443 return wp;
8444 }
8445 else if (--nr <= 0)
8446 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008447 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008448 if (nr >= LOWEST_WIN_ID)
8449 return NULL;
8450 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451}
8452
8453/*
8454 * Find window specified by "wvp" in tabpage "tvp".
8455 */
8456 win_T *
8457find_tabwin(
8458 typval_T *wvp, /* VAR_UNKNOWN for current window */
8459 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8460{
8461 win_T *wp = NULL;
8462 tabpage_T *tp = NULL;
8463 long n;
8464
8465 if (wvp->v_type != VAR_UNKNOWN)
8466 {
8467 if (tvp->v_type != VAR_UNKNOWN)
8468 {
8469 n = (long)get_tv_number(tvp);
8470 if (n >= 0)
8471 tp = find_tabpage(n);
8472 }
8473 else
8474 tp = curtab;
8475
8476 if (tp != NULL)
8477 wp = find_win_by_nr(wvp, tp);
8478 }
8479 else
8480 wp = curwin;
8481
8482 return wp;
8483}
8484
8485/*
8486 * getwinvar() and gettabwinvar()
8487 */
8488 void
8489getwinvar(
8490 typval_T *argvars,
8491 typval_T *rettv,
8492 int off) /* 1 for gettabwinvar() */
8493{
8494 win_T *win;
8495 char_u *varname;
8496 dictitem_T *v;
8497 tabpage_T *tp = NULL;
8498 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 win_T *oldcurwin;
8500 tabpage_T *oldtabpage;
8501 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008503 if (off == 1)
8504 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8505 else
8506 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 win = find_win_by_nr(&argvars[off], tp);
8508 varname = get_tv_string_chk(&argvars[off + 1]);
8509 ++emsg_off;
8510
8511 rettv->v_type = VAR_STRING;
8512 rettv->vval.v_string = NULL;
8513
8514 if (win != NULL && varname != NULL)
8515 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 /* Set curwin to be our win, temporarily. Also set the tabpage,
8517 * otherwise the window is not valid. Only do this when needed,
8518 * autocommands get blocked. */
8519 need_switch_win = !(tp == curtab && win == curwin);
8520 if (!need_switch_win
8521 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008523 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008525 if (varname[1] == NUL)
8526 {
8527 /* get all window-local options in a dict */
8528 dict_T *opts = get_winbuf_options(FALSE);
8529
8530 if (opts != NULL)
8531 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008532 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008533 done = TRUE;
8534 }
8535 }
8536 else if (get_option_tv(&varname, rettv, 1) == OK)
8537 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 done = TRUE;
8539 }
8540 else
8541 {
8542 /* Look up the variable. */
8543 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8544 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8545 varname, FALSE);
8546 if (v != NULL)
8547 {
8548 copy_tv(&v->di_tv, rettv);
8549 done = TRUE;
8550 }
8551 }
8552 }
8553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 if (need_switch_win)
8555 /* restore previous notion of curwin */
8556 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 }
8558
8559 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8560 /* use the default return value */
8561 copy_tv(&argvars[off + 2], rettv);
8562
8563 --emsg_off;
8564}
8565
8566/*
8567 * "setwinvar()" and "settabwinvar()" functions
8568 */
8569 void
8570setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8571{
8572 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 win_T *save_curwin;
8574 tabpage_T *save_curtab;
8575 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 char_u *varname, *winvarname;
8577 typval_T *varp;
8578 char_u nbuf[NUMBUFLEN];
8579 tabpage_T *tp = NULL;
8580
8581 if (check_restricted() || check_secure())
8582 return;
8583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 if (off == 1)
8585 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8586 else
8587 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 win = find_win_by_nr(&argvars[off], tp);
8589 varname = get_tv_string_chk(&argvars[off + 1]);
8590 varp = &argvars[off + 2];
8591
8592 if (win != NULL && varname != NULL && varp != NULL)
8593 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 need_switch_win = !(tp == curtab && win == curwin);
8595 if (!need_switch_win
8596 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 {
8598 if (*varname == '&')
8599 {
8600 long numval;
8601 char_u *strval;
8602 int error = FALSE;
8603
8604 ++varname;
8605 numval = (long)get_tv_number_chk(varp, &error);
8606 strval = get_tv_string_buf_chk(varp, nbuf);
8607 if (!error && strval != NULL)
8608 set_option_value(varname, numval, strval, OPT_LOCAL);
8609 }
8610 else
8611 {
8612 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8613 if (winvarname != NULL)
8614 {
8615 STRCPY(winvarname, "w:");
8616 STRCPY(winvarname + 2, varname);
8617 set_var(winvarname, varp, TRUE);
8618 vim_free(winvarname);
8619 }
8620 }
8621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 if (need_switch_win)
8623 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 }
8625}
8626
8627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8629 * "arg" points to the "&" or '+' when called, to "option" when returning.
8630 * Returns NULL when no option name found. Otherwise pointer to the char
8631 * after the option name.
8632 */
8633 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008634find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636 char_u *p = *arg;
8637
8638 ++p;
8639 if (*p == 'g' && p[1] == ':')
8640 {
8641 *opt_flags = OPT_GLOBAL;
8642 p += 2;
8643 }
8644 else if (*p == 'l' && p[1] == ':')
8645 {
8646 *opt_flags = OPT_LOCAL;
8647 p += 2;
8648 }
8649 else
8650 *opt_flags = 0;
8651
8652 if (!ASCII_ISALPHA(*p))
8653 return NULL;
8654 *arg = p;
8655
8656 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8657 p += 4; /* termcap option */
8658 else
8659 while (ASCII_ISALPHA(*p))
8660 ++p;
8661 return p;
8662}
8663
8664/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008665 * Return the autoload script name for a function or variable name.
8666 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008668 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008669autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008670{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008671 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008672 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008673
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008674 /* Get the script file name: replace '#' with '/', append ".vim". */
8675 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8676 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008677 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008678 STRCPY(scriptname, "autoload/");
8679 STRCAT(scriptname, name);
8680 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8681 STRCAT(scriptname, ".vim");
8682 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8683 *p = '/';
8684 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685}
8686
8687/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008688 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008689 * Return TRUE if a package was loaded.
8690 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008692script_autoload(
8693 char_u *name,
8694 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008695{
8696 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008697 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008698 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008699 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008701 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008702 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008703 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 return FALSE;
8705
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008706 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008707
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008708 /* Find the name in the list of previously loaded package names. Skip
8709 * "autoload/", it's always the same. */
8710 for (i = 0; i < ga_loaded.ga_len; ++i)
8711 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8712 break;
8713 if (!reload && i < ga_loaded.ga_len)
8714 ret = FALSE; /* was loaded already */
8715 else
8716 {
8717 /* Remember the name if it wasn't loaded already. */
8718 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8719 {
8720 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8721 tofree = NULL;
8722 }
8723
8724 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008725 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008726 ret = TRUE;
8727 }
8728
8729 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008730 return ret;
8731}
8732
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8734typedef enum
8735{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008736 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8737 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8738 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739} var_flavour_T;
8740
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008741static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742
8743 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008744var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
8746 char_u *p = varname;
8747
8748 if (ASCII_ISUPPER(*p))
8749 {
8750 while (*(++p))
8751 if (ASCII_ISLOWER(*p))
8752 return VAR_FLAVOUR_SESSION;
8753 return VAR_FLAVOUR_VIMINFO;
8754 }
8755 else
8756 return VAR_FLAVOUR_DEFAULT;
8757}
8758#endif
8759
8760#if defined(FEAT_VIMINFO) || defined(PROTO)
8761/*
8762 * Restore global vars that start with a capital from the viminfo file
8763 */
8764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008765read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008768 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008770 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 if (!writing && (find_viminfo_parameter('!') != NULL))
8773 {
8774 tab = vim_strchr(virp->vir_line + 1, '\t');
8775 if (tab != NULL)
8776 {
8777 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008778 switch (*tab)
8779 {
8780 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008782 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008783#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008784 case 'D': type = VAR_DICT; break;
8785 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008786 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
8789 tab = vim_strchr(tab, '\t');
8790 if (tab != NULL)
8791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008792 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008793 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008794 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008796#ifdef FEAT_FLOAT
8797 else if (type == VAR_FLOAT)
8798 (void)string2float(tab + 1, &tv.vval.v_float);
8799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008801 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008802 if (type == VAR_DICT || type == VAR_LIST)
8803 {
8804 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8805
8806 if (etv == NULL)
8807 /* Failed to parse back the dict or list, use it as a
8808 * string. */
8809 tv.v_type = VAR_STRING;
8810 else
8811 {
8812 vim_free(tv.vval.v_string);
8813 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008814 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008815 }
8816 }
8817
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008818 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008819 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008820 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008821 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008822
8823 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008824 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008825 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8826 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 }
8828 }
8829 }
8830
8831 return viminfo_readline(virp);
8832}
8833
8834/*
8835 * Write global vars that start with a capital to the viminfo file
8836 */
8837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008838write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839{
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 hashitem_T *hi;
8841 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008842 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008843 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008844 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008846 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 if (find_viminfo_parameter('!') == NULL)
8849 return;
8850
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008851 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008852
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008853 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008856 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008858 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 this_var = HI2DI(hi);
8860 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008863 {
8864 case VAR_STRING: s = "STR"; break;
8865 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008866 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008867 case VAR_DICT: s = "DIC"; break;
8868 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008869 case VAR_SPECIAL: s = "XPL"; break;
8870
8871 case VAR_UNKNOWN:
8872 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008873 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008874 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008875 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008876 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008877 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008879 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008880 if (p != NULL)
8881 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008882 vim_free(tofree);
8883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885 }
8886}
8887#endif
8888
8889#if defined(FEAT_SESSION) || defined(PROTO)
8890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008891store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 hashitem_T *hi;
8894 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008895 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 char_u *p, *t;
8897
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008898 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008899 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008901 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008903 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 this_var = HI2DI(hi);
8905 if ((this_var->di_tv.v_type == VAR_NUMBER
8906 || this_var->di_tv.v_type == VAR_STRING)
8907 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008908 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008909 /* Escape special characters with a backslash. Turn a LF and
8910 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008911 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008912 (char_u *)"\\\"\n\r");
8913 if (p == NULL) /* out of memory */
8914 break;
8915 for (t = p; *t != NUL; ++t)
8916 if (*t == '\n')
8917 *t = 'n';
8918 else if (*t == '\r')
8919 *t = 'r';
8920 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008921 this_var->di_key,
8922 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8923 : ' ',
8924 p,
8925 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8926 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008927 || put_eol(fd) == FAIL)
8928 {
8929 vim_free(p);
8930 return FAIL;
8931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 vim_free(p);
8933 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008934#ifdef FEAT_FLOAT
8935 else if (this_var->di_tv.v_type == VAR_FLOAT
8936 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8937 {
8938 float_T f = this_var->di_tv.vval.v_float;
8939 int sign = ' ';
8940
8941 if (f < 0)
8942 {
8943 f = -f;
8944 sign = '-';
8945 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008946 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008947 this_var->di_key, sign, f) < 0)
8948 || put_eol(fd) == FAIL)
8949 return FAIL;
8950 }
8951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953 }
8954 return OK;
8955}
8956#endif
8957
Bram Moolenaar661b1822005-07-28 22:36:45 +00008958/*
8959 * Display script name where an item was last set.
8960 * Should only be invoked when 'verbose' is non-zero.
8961 */
8962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008963last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008964{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008965 char_u *p;
8966
Bram Moolenaar661b1822005-07-28 22:36:45 +00008967 if (scriptID != 0)
8968 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008969 p = home_replace_save(NULL, get_scriptname(scriptID));
8970 if (p != NULL)
8971 {
8972 verbose_enter();
8973 MSG_PUTS(_("\n\tLast set from "));
8974 MSG_PUTS(p);
8975 vim_free(p);
8976 verbose_leave();
8977 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008978 }
8979}
8980
Bram Moolenaar53744302015-07-17 17:38:22 +02008981/* reset v:option_new, v:option_old and v:option_type */
8982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008983reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008984{
8985 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8986 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8987 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8988}
8989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990/*
8991 * Prepare "gap" for an assert error and add the sourcing position.
8992 */
8993 void
8994prepare_assert_error(garray_T *gap)
8995{
8996 char buf[NUMBUFLEN];
8997
8998 ga_init2(gap, 1, 100);
8999 if (sourcing_name != NULL)
9000 {
9001 ga_concat(gap, sourcing_name);
9002 if (sourcing_lnum > 0)
9003 ga_concat(gap, (char_u *)" ");
9004 }
9005 if (sourcing_lnum > 0)
9006 {
9007 sprintf(buf, "line %ld", (long)sourcing_lnum);
9008 ga_concat(gap, (char_u *)buf);
9009 }
9010 if (sourcing_name != NULL || sourcing_lnum > 0)
9011 ga_concat(gap, (char_u *)": ");
9012}
9013
9014/*
9015 * Add an assert error to v:errors.
9016 */
9017 void
9018assert_error(garray_T *gap)
9019{
9020 struct vimvar *vp = &vimvars[VV_ERRORS];
9021
9022 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9023 /* Make sure v:errors is a list. */
9024 set_vim_var_list(VV_ERRORS, list_alloc());
9025 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9026}
9027
9028 void
9029assert_equal_common(typval_T *argvars, assert_type_T atype)
9030{
9031 garray_T ga;
9032
9033 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9034 != (atype == ASSERT_EQUAL))
9035 {
9036 prepare_assert_error(&ga);
9037 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9038 atype);
9039 assert_error(&ga);
9040 ga_clear(&ga);
9041 }
9042}
9043
9044 void
9045assert_match_common(typval_T *argvars, assert_type_T atype)
9046{
9047 garray_T ga;
9048 char_u buf1[NUMBUFLEN];
9049 char_u buf2[NUMBUFLEN];
9050 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9051 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9052
9053 if (pat == NULL || text == NULL)
9054 EMSG(_(e_invarg));
9055 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9056 {
9057 prepare_assert_error(&ga);
9058 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9059 atype);
9060 assert_error(&ga);
9061 ga_clear(&ga);
9062 }
9063}
9064
Bram Moolenaar61c04492016-07-23 15:35:35 +02009065 void
9066assert_inrange(typval_T *argvars)
9067{
9068 garray_T ga;
9069 int error = FALSE;
9070 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9071 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9072 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9073 char_u *tofree;
9074 char msg[200];
9075 char_u numbuf[NUMBUFLEN];
9076
9077 if (error)
9078 return;
9079 if (actual < lower || actual > upper)
9080 {
9081 prepare_assert_error(&ga);
9082 if (argvars[3].v_type != VAR_UNKNOWN)
9083 {
9084 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9085 vim_free(tofree);
9086 }
9087 else
9088 {
9089 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9090 (long)lower, (long)upper, (long)actual);
9091 ga_concat(&ga, (char_u *)msg);
9092 }
9093 assert_error(&ga);
9094 ga_clear(&ga);
9095 }
9096}
9097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098/*
9099 * Common for assert_true() and assert_false().
9100 */
9101 void
9102assert_bool(typval_T *argvars, int isTrue)
9103{
9104 int error = FALSE;
9105 garray_T ga;
9106
9107 if (argvars[0].v_type == VAR_SPECIAL
9108 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9109 return;
9110 if (argvars[0].v_type != VAR_NUMBER
9111 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9112 || error)
9113 {
9114 prepare_assert_error(&ga);
9115 fill_assert_error(&ga, &argvars[1],
9116 (char_u *)(isTrue ? "True" : "False"),
9117 NULL, &argvars[0], ASSERT_OTHER);
9118 assert_error(&ga);
9119 ga_clear(&ga);
9120 }
9121}
9122
9123 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009124assert_report(typval_T *argvars)
9125{
9126 garray_T ga;
9127
9128 prepare_assert_error(&ga);
9129 ga_concat(&ga, get_tv_string(&argvars[0]));
9130 assert_error(&ga);
9131 ga_clear(&ga);
9132}
9133
9134 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135assert_exception(typval_T *argvars)
9136{
9137 garray_T ga;
9138 char_u *error = get_tv_string_chk(&argvars[0]);
9139
9140 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9141 {
9142 prepare_assert_error(&ga);
9143 ga_concat(&ga, (char_u *)"v:exception is not set");
9144 assert_error(&ga);
9145 ga_clear(&ga);
9146 }
9147 else if (error != NULL
9148 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9149 {
9150 prepare_assert_error(&ga);
9151 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9152 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9153 assert_error(&ga);
9154 ga_clear(&ga);
9155 }
9156}
9157
9158 void
9159assert_fails(typval_T *argvars)
9160{
9161 char_u *cmd = get_tv_string_chk(&argvars[0]);
9162 garray_T ga;
9163
9164 called_emsg = FALSE;
9165 suppress_errthrow = TRUE;
9166 emsg_silent = TRUE;
9167 do_cmdline_cmd(cmd);
9168 if (!called_emsg)
9169 {
9170 prepare_assert_error(&ga);
9171 ga_concat(&ga, (char_u *)"command did not fail: ");
9172 ga_concat(&ga, cmd);
9173 assert_error(&ga);
9174 ga_clear(&ga);
9175 }
9176 else if (argvars[1].v_type != VAR_UNKNOWN)
9177 {
9178 char_u buf[NUMBUFLEN];
9179 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9180
9181 if (error == NULL
9182 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9183 {
9184 prepare_assert_error(&ga);
9185 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9186 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9187 assert_error(&ga);
9188 ga_clear(&ga);
9189 }
9190 }
9191
9192 called_emsg = FALSE;
9193 suppress_errthrow = FALSE;
9194 emsg_silent = FALSE;
9195 emsg_on_display = FALSE;
9196 set_vim_var_string(VV_ERRMSG, NULL, 0);
9197}
9198
9199/*
9200 * Append "str" to "gap", escaping unprintable characters.
9201 * Changes NL to \n, CR to \r, etc.
9202 */
9203 static void
9204ga_concat_esc(garray_T *gap, char_u *str)
9205{
9206 char_u *p;
9207 char_u buf[NUMBUFLEN];
9208
9209 if (str == NULL)
9210 {
9211 ga_concat(gap, (char_u *)"NULL");
9212 return;
9213 }
9214
9215 for (p = str; *p != NUL; ++p)
9216 switch (*p)
9217 {
9218 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9219 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9220 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9221 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9222 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9223 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9224 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9225 default:
9226 if (*p < ' ')
9227 {
9228 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9229 ga_concat(gap, buf);
9230 }
9231 else
9232 ga_append(gap, *p);
9233 break;
9234 }
9235}
9236
9237/*
9238 * Fill "gap" with information about an assert error.
9239 */
9240 void
9241fill_assert_error(
9242 garray_T *gap,
9243 typval_T *opt_msg_tv,
9244 char_u *exp_str,
9245 typval_T *exp_tv,
9246 typval_T *got_tv,
9247 assert_type_T atype)
9248{
9249 char_u numbuf[NUMBUFLEN];
9250 char_u *tofree;
9251
9252 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9253 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009254 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9255 vim_free(tofree);
9256 ga_concat(gap, (char_u *)": ");
9257 }
9258
9259 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9260 ga_concat(gap, (char_u *)"Pattern ");
9261 else if (atype == ASSERT_NOTEQUAL)
9262 ga_concat(gap, (char_u *)"Expected not equal to ");
9263 else
9264 ga_concat(gap, (char_u *)"Expected ");
9265 if (exp_str == NULL)
9266 {
9267 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 vim_free(tofree);
9269 }
9270 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009271 ga_concat_esc(gap, exp_str);
9272 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009274 if (atype == ASSERT_MATCH)
9275 ga_concat(gap, (char_u *)" does not match ");
9276 else if (atype == ASSERT_NOTMATCH)
9277 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009279 ga_concat(gap, (char_u *)" but got ");
9280 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9281 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 }
9283}
9284
Bram Moolenaar53744302015-07-17 17:38:22 +02009285
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286#endif /* FEAT_EVAL */
9287
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009289#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290
9291#ifdef WIN3264
9292/*
9293 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9294 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009295static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9296static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9297static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
9299/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009300 * Get the short path (8.3) for the filename in "fnamep".
9301 * Only works for a valid file name.
9302 * When the path gets longer "fnamep" is changed and the allocated buffer
9303 * is put in "bufp".
9304 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9305 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 */
9307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009310 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 char_u *newbuf;
9312
9313 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009314 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 if (l > len - 1)
9316 {
9317 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009318 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 newbuf = vim_strnsave(*fnamep, l);
9320 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322
9323 vim_free(*bufp);
9324 *fnamep = *bufp = newbuf;
9325
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009326 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009327 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 }
9329
9330 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009331 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332}
9333
9334/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009335 * Get the short path (8.3) for the filename in "fname". The converted
9336 * path is returned in "bufp".
9337 *
9338 * Some of the directories specified in "fname" may not exist. This function
9339 * will shorten the existing directories at the beginning of the path and then
9340 * append the remaining non-existing path.
9341 *
9342 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009343 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009344 * bufp - Pointer to an allocated buffer for the filename.
9345 * fnamelen - Length of the filename pointed to by fname
9346 *
9347 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 */
9349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009350shortpath_for_invalid_fname(
9351 char_u **fname,
9352 char_u **bufp,
9353 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009355 char_u *short_fname, *save_fname, *pbuf_unused;
9356 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009358 int old_len, len;
9359 int new_len, sfx_len;
9360 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361
9362 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009363 old_len = *fnamelen;
9364 save_fname = vim_strnsave(*fname, old_len);
9365 pbuf_unused = NULL;
9366 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009368 endp = save_fname + old_len - 1; /* Find the end of the copy */
9369 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009371 /*
9372 * Try shortening the supplied path till it succeeds by removing one
9373 * directory at a time from the tail of the path.
9374 */
9375 len = 0;
9376 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009378 /* go back one path-separator */
9379 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9380 --endp;
9381 if (endp <= save_fname)
9382 break; /* processed the complete path */
9383
9384 /*
9385 * Replace the path separator with a NUL and try to shorten the
9386 * resulting path.
9387 */
9388 ch = *endp;
9389 *endp = 0;
9390 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009391 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009392 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9393 {
9394 retval = FAIL;
9395 goto theend;
9396 }
9397 *endp = ch; /* preserve the string */
9398
9399 if (len > 0)
9400 break; /* successfully shortened the path */
9401
9402 /* failed to shorten the path. Skip the path separator */
9403 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 }
9405
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009406 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009408 /*
9409 * Succeeded in shortening the path. Now concatenate the shortened
9410 * path with the remaining path at the tail.
9411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009413 /* Compute the length of the new path. */
9414 sfx_len = (int)(save_endp - endp) + 1;
9415 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009417 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009419 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009421 /* There is not enough space in the currently allocated string,
9422 * copy it to a buffer big enough. */
9423 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009425 {
9426 retval = FAIL;
9427 goto theend;
9428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
9430 else
9431 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009432 /* Transfer short_fname to the main buffer (it's big enough),
9433 * unless get_short_pathname() did its work in-place. */
9434 *fname = *bufp = save_fname;
9435 if (short_fname != save_fname)
9436 vim_strncpy(save_fname, short_fname, len);
9437 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009439
9440 /* concat the not-shortened part of the path */
9441 vim_strncpy(*fname + len, endp, sfx_len);
9442 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009444
9445theend:
9446 vim_free(pbuf_unused);
9447 vim_free(save_fname);
9448
9449 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450}
9451
9452/*
9453 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009454 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 */
9456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009457shortpath_for_partial(
9458 char_u **fnamep,
9459 char_u **bufp,
9460 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 int sepcount, len, tflen;
9463 char_u *p;
9464 char_u *pbuf, *tfname;
9465 int hasTilde;
9466
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009467 /* Count up the path separators from the RHS.. so we know which part
9468 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009470 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 if (vim_ispathsep(*p))
9472 ++sepcount;
9473
9474 /* Need full path first (use expand_env() to remove a "~/") */
9475 hasTilde = (**fnamep == '~');
9476 if (hasTilde)
9477 pbuf = tfname = expand_env_save(*fnamep);
9478 else
9479 pbuf = tfname = FullName_save(*fnamep, FALSE);
9480
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009481 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009483 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9484 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485
9486 if (len == 0)
9487 {
9488 /* Don't have a valid filename, so shorten the rest of the
9489 * path if we can. This CAN give us invalid 8.3 filenames, but
9490 * there's not a lot of point in guessing what it might be.
9491 */
9492 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009493 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9494 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 }
9496
9497 /* Count the paths backward to find the beginning of the desired string. */
9498 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009499 {
9500#ifdef FEAT_MBYTE
9501 if (has_mbyte)
9502 p -= mb_head_off(tfname, p);
9503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 if (vim_ispathsep(*p))
9505 {
9506 if (sepcount == 0 || (hasTilde && sepcount == 1))
9507 break;
9508 else
9509 sepcount --;
9510 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (hasTilde)
9513 {
9514 --p;
9515 if (p >= tfname)
9516 *p = '~';
9517 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009518 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 }
9520 else
9521 ++p;
9522
9523 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9524 vim_free(*bufp);
9525 *fnamelen = (int)STRLEN(p);
9526 *bufp = pbuf;
9527 *fnamep = p;
9528
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009529 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531#endif /* WIN3264 */
9532
9533/*
9534 * Adjust a filename, according to a string of modifiers.
9535 * *fnamep must be NUL terminated when called. When returning, the length is
9536 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009537 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 * When there is an error, *fnamep is set to NULL.
9539 */
9540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009541modify_fname(
9542 char_u *src, /* string with modifiers */
9543 int *usedlen, /* characters after src that are used */
9544 char_u **fnamep, /* file name so far */
9545 char_u **bufp, /* buffer for allocated file name or NULL */
9546 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 int valid = 0;
9549 char_u *tail;
9550 char_u *s, *p, *pbuf;
9551 char_u dirname[MAXPATHL];
9552 int c;
9553 int has_fullname = 0;
9554#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009555 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 int has_shortname = 0;
9557#endif
9558
9559repeat:
9560 /* ":p" - full path/file_name */
9561 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9562 {
9563 has_fullname = 1;
9564
9565 valid |= VALID_PATH;
9566 *usedlen += 2;
9567
9568 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9569 if ((*fnamep)[0] == '~'
9570#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9571 && ((*fnamep)[1] == '/'
9572# ifdef BACKSLASH_IN_FILENAME
9573 || (*fnamep)[1] == '\\'
9574# endif
9575 || (*fnamep)[1] == NUL)
9576
9577#endif
9578 )
9579 {
9580 *fnamep = expand_env_save(*fnamep);
9581 vim_free(*bufp); /* free any allocated file name */
9582 *bufp = *fnamep;
9583 if (*fnamep == NULL)
9584 return -1;
9585 }
9586
9587 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009588 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 {
9590 if (vim_ispathsep(*p)
9591 && p[1] == '.'
9592 && (p[2] == NUL
9593 || vim_ispathsep(p[2])
9594 || (p[2] == '.'
9595 && (p[3] == NUL || vim_ispathsep(p[3])))))
9596 break;
9597 }
9598
9599 /* FullName_save() is slow, don't use it when not needed. */
9600 if (*p != NUL || !vim_isAbsName(*fnamep))
9601 {
9602 *fnamep = FullName_save(*fnamep, *p != NUL);
9603 vim_free(*bufp); /* free any allocated file name */
9604 *bufp = *fnamep;
9605 if (*fnamep == NULL)
9606 return -1;
9607 }
9608
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009609#ifdef WIN3264
9610# if _WIN32_WINNT >= 0x0500
9611 if (vim_strchr(*fnamep, '~') != NULL)
9612 {
9613 /* Expand 8.3 filename to full path. Needed to make sure the same
9614 * file does not have two different names.
9615 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9616 p = alloc(_MAX_PATH + 1);
9617 if (p != NULL)
9618 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009619 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009620 {
9621 vim_free(*bufp);
9622 *bufp = *fnamep = p;
9623 }
9624 else
9625 vim_free(p);
9626 }
9627 }
9628# endif
9629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 /* Append a path separator to a directory. */
9631 if (mch_isdir(*fnamep))
9632 {
9633 /* Make room for one or two extra characters. */
9634 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9635 vim_free(*bufp); /* free any allocated file name */
9636 *bufp = *fnamep;
9637 if (*fnamep == NULL)
9638 return -1;
9639 add_pathsep(*fnamep);
9640 }
9641 }
9642
9643 /* ":." - path relative to the current directory */
9644 /* ":~" - path relative to the home directory */
9645 /* ":8" - shortname path - postponed till after */
9646 while (src[*usedlen] == ':'
9647 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9648 {
9649 *usedlen += 2;
9650 if (c == '8')
9651 {
9652#ifdef WIN3264
9653 has_shortname = 1; /* Postpone this. */
9654#endif
9655 continue;
9656 }
9657 pbuf = NULL;
9658 /* Need full path first (use expand_env() to remove a "~/") */
9659 if (!has_fullname)
9660 {
9661 if (c == '.' && **fnamep == '~')
9662 p = pbuf = expand_env_save(*fnamep);
9663 else
9664 p = pbuf = FullName_save(*fnamep, FALSE);
9665 }
9666 else
9667 p = *fnamep;
9668
9669 has_fullname = 0;
9670
9671 if (p != NULL)
9672 {
9673 if (c == '.')
9674 {
9675 mch_dirname(dirname, MAXPATHL);
9676 s = shorten_fname(p, dirname);
9677 if (s != NULL)
9678 {
9679 *fnamep = s;
9680 if (pbuf != NULL)
9681 {
9682 vim_free(*bufp); /* free any allocated file name */
9683 *bufp = pbuf;
9684 pbuf = NULL;
9685 }
9686 }
9687 }
9688 else
9689 {
9690 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9691 /* Only replace it when it starts with '~' */
9692 if (*dirname == '~')
9693 {
9694 s = vim_strsave(dirname);
9695 if (s != NULL)
9696 {
9697 *fnamep = s;
9698 vim_free(*bufp);
9699 *bufp = s;
9700 }
9701 }
9702 }
9703 vim_free(pbuf);
9704 }
9705 }
9706
9707 tail = gettail(*fnamep);
9708 *fnamelen = (int)STRLEN(*fnamep);
9709
9710 /* ":h" - head, remove "/file_name", can be repeated */
9711 /* Don't remove the first "/" or "c:\" */
9712 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9713 {
9714 valid |= VALID_HEAD;
9715 *usedlen += 2;
9716 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009717 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009718 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 *fnamelen = (int)(tail - *fnamep);
9720#ifdef VMS
9721 if (*fnamelen > 0)
9722 *fnamelen += 1; /* the path separator is part of the path */
9723#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009724 if (*fnamelen == 0)
9725 {
9726 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9727 p = vim_strsave((char_u *)".");
9728 if (p == NULL)
9729 return -1;
9730 vim_free(*bufp);
9731 *bufp = *fnamep = tail = p;
9732 *fnamelen = 1;
9733 }
9734 else
9735 {
9736 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009737 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 }
9740
9741 /* ":8" - shortname */
9742 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9743 {
9744 *usedlen += 2;
9745#ifdef WIN3264
9746 has_shortname = 1;
9747#endif
9748 }
9749
9750#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009751 /*
9752 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 */
9754 if (has_shortname)
9755 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009756 /* Copy the string if it is shortened by :h and when it wasn't copied
9757 * yet, because we are going to change it in place. Avoids changing
9758 * the buffer name for "%:8". */
9759 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 {
9761 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009762 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 return -1;
9764 vim_free(*bufp);
9765 *bufp = *fnamep = p;
9766 }
9767
9768 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009769 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (!has_fullname && !vim_isAbsName(*fnamep))
9771 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009772 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 return -1;
9774 }
9775 else
9776 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009777 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778
Bram Moolenaardc935552011-08-17 15:23:23 +02009779 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009781 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 return -1;
9783
9784 if (l == 0)
9785 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009786 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009788 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 return -1;
9790 }
9791 *fnamelen = l;
9792 }
9793 }
9794#endif /* WIN3264 */
9795
9796 /* ":t" - tail, just the basename */
9797 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9798 {
9799 *usedlen += 2;
9800 *fnamelen -= (int)(tail - *fnamep);
9801 *fnamep = tail;
9802 }
9803
9804 /* ":e" - extension, can be repeated */
9805 /* ":r" - root, without extension, can be repeated */
9806 while (src[*usedlen] == ':'
9807 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9808 {
9809 /* find a '.' in the tail:
9810 * - for second :e: before the current fname
9811 * - otherwise: The last '.'
9812 */
9813 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9814 s = *fnamep - 2;
9815 else
9816 s = *fnamep + *fnamelen - 1;
9817 for ( ; s > tail; --s)
9818 if (s[0] == '.')
9819 break;
9820 if (src[*usedlen + 1] == 'e') /* :e */
9821 {
9822 if (s > tail)
9823 {
9824 *fnamelen += (int)(*fnamep - (s + 1));
9825 *fnamep = s + 1;
9826#ifdef VMS
9827 /* cut version from the extension */
9828 s = *fnamep + *fnamelen - 1;
9829 for ( ; s > *fnamep; --s)
9830 if (s[0] == ';')
9831 break;
9832 if (s > *fnamep)
9833 *fnamelen = s - *fnamep;
9834#endif
9835 }
9836 else if (*fnamep <= tail)
9837 *fnamelen = 0;
9838 }
9839 else /* :r */
9840 {
9841 if (s > tail) /* remove one extension */
9842 *fnamelen = (int)(s - *fnamep);
9843 }
9844 *usedlen += 2;
9845 }
9846
9847 /* ":s?pat?foo?" - substitute */
9848 /* ":gs?pat?foo?" - global substitute */
9849 if (src[*usedlen] == ':'
9850 && (src[*usedlen + 1] == 's'
9851 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9852 {
9853 char_u *str;
9854 char_u *pat;
9855 char_u *sub;
9856 int sep;
9857 char_u *flags;
9858 int didit = FALSE;
9859
9860 flags = (char_u *)"";
9861 s = src + *usedlen + 2;
9862 if (src[*usedlen + 1] == 'g')
9863 {
9864 flags = (char_u *)"g";
9865 ++s;
9866 }
9867
9868 sep = *s++;
9869 if (sep)
9870 {
9871 /* find end of pattern */
9872 p = vim_strchr(s, sep);
9873 if (p != NULL)
9874 {
9875 pat = vim_strnsave(s, (int)(p - s));
9876 if (pat != NULL)
9877 {
9878 s = p + 1;
9879 /* find end of substitution */
9880 p = vim_strchr(s, sep);
9881 if (p != NULL)
9882 {
9883 sub = vim_strnsave(s, (int)(p - s));
9884 str = vim_strnsave(*fnamep, *fnamelen);
9885 if (sub != NULL && str != NULL)
9886 {
9887 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009888 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (s != NULL)
9890 {
9891 *fnamep = s;
9892 *fnamelen = (int)STRLEN(s);
9893 vim_free(*bufp);
9894 *bufp = s;
9895 didit = TRUE;
9896 }
9897 }
9898 vim_free(sub);
9899 vim_free(str);
9900 }
9901 vim_free(pat);
9902 }
9903 }
9904 /* after using ":s", repeat all the modifiers */
9905 if (didit)
9906 goto repeat;
9907 }
9908 }
9909
Bram Moolenaar26df0922014-02-23 23:39:13 +01009910 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9911 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009912 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009913 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009914 if (c != NUL)
9915 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009916 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009917 if (c != NUL)
9918 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009919 if (p == NULL)
9920 return -1;
9921 vim_free(*bufp);
9922 *bufp = *fnamep = p;
9923 *fnamelen = (int)STRLEN(p);
9924 *usedlen += 2;
9925 }
9926
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 return valid;
9928}
9929
9930/*
9931 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009932 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 * "flags" can be "g" to do a global substitute.
9934 * Returns an allocated string, NULL for error.
9935 */
9936 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009937do_string_sub(
9938 char_u *str,
9939 char_u *pat,
9940 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009941 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009942 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
9944 int sublen;
9945 regmatch_T regmatch;
9946 int i;
9947 int do_all;
9948 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009949 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 garray_T ga;
9951 char_u *ret;
9952 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009953 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954
9955 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9956 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009957 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
9959 ga_init2(&ga, 1, 200);
9960
9961 do_all = (flags[0] == 'g');
9962
9963 regmatch.rm_ic = p_ic;
9964 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9965 if (regmatch.regprog != NULL)
9966 {
9967 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009968 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9970 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009971 /* Skip empty match except for first match. */
9972 if (regmatch.startp[0] == regmatch.endp[0])
9973 {
9974 if (zero_width == regmatch.startp[0])
9975 {
9976 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009977 i = MB_PTR2LEN(tail);
9978 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9979 (size_t)i);
9980 ga.ga_len += i;
9981 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009982 continue;
9983 }
9984 zero_width = regmatch.startp[0];
9985 }
9986
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 /*
9988 * Get some space for a temporary buffer to do the substitution
9989 * into. It will contain:
9990 * - The text up to where the match is.
9991 * - The substituted text.
9992 * - The text after the match.
9993 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009994 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009995 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9997 {
9998 ga_clear(&ga);
9999 break;
10000 }
10001
10002 /* copy the text up to where the match is */
10003 i = (int)(regmatch.startp[0] - tail);
10004 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10005 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010006 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 + ga.ga_len + i, TRUE, TRUE, FALSE);
10008 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010009 tail = regmatch.endp[0];
10010 if (*tail == NUL)
10011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 if (!do_all)
10013 break;
10014 }
10015
10016 if (ga.ga_data != NULL)
10017 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10018
Bram Moolenaar473de612013-06-08 18:19:48 +020010019 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021
10022 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10023 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010024 if (p_cpo == empty_option)
10025 p_cpo = save_cpo;
10026 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010027 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010028 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029
10030 return ret;
10031}
10032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010033 static int
10034filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10035{
10036 typval_T rettv;
10037 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010039
10040 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10041 argv[0] = vimvars[VV_KEY].vv_tv;
10042 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010043 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10044 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010045 if (map)
10046 {
10047 /* map(): replace the list item value */
10048 clear_tv(tv);
10049 rettv.v_lock = 0;
10050 *tv = rettv;
10051 }
10052 else
10053 {
10054 int error = FALSE;
10055
10056 /* filter(): when expr is zero remove the item */
10057 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10058 clear_tv(&rettv);
10059 /* On type error, nothing has been removed; return FAIL to stop the
10060 * loop. The error message was given by get_tv_number_chk(). */
10061 if (error)
10062 goto theend;
10063 }
10064 retval = OK;
10065theend:
10066 clear_tv(&vimvars[VV_VAL].vv_tv);
10067 return retval;
10068}
10069
10070
10071/*
10072 * Implementation of map() and filter().
10073 */
10074 void
10075filter_map(typval_T *argvars, typval_T *rettv, int map)
10076{
10077 typval_T *expr;
10078 listitem_T *li, *nli;
10079 list_T *l = NULL;
10080 dictitem_T *di;
10081 hashtab_T *ht;
10082 hashitem_T *hi;
10083 dict_T *d = NULL;
10084 typval_T save_val;
10085 typval_T save_key;
10086 int rem;
10087 int todo;
10088 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10089 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10090 : N_("filter() argument"));
10091 int save_did_emsg;
10092 int idx = 0;
10093
10094 if (argvars[0].v_type == VAR_LIST)
10095 {
10096 if ((l = argvars[0].vval.v_list) == NULL
10097 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10098 return;
10099 }
10100 else if (argvars[0].v_type == VAR_DICT)
10101 {
10102 if ((d = argvars[0].vval.v_dict) == NULL
10103 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10104 return;
10105 }
10106 else
10107 {
10108 EMSG2(_(e_listdictarg), ermsg);
10109 return;
10110 }
10111
10112 expr = &argvars[1];
10113 /* On type errors, the preceding call has already displayed an error
10114 * message. Avoid a misleading error message for an empty string that
10115 * was not passed as argument. */
10116 if (expr->v_type != VAR_UNKNOWN)
10117 {
10118 prepare_vimvar(VV_VAL, &save_val);
10119
10120 /* We reset "did_emsg" to be able to detect whether an error
10121 * occurred during evaluation of the expression. */
10122 save_did_emsg = did_emsg;
10123 did_emsg = FALSE;
10124
10125 prepare_vimvar(VV_KEY, &save_key);
10126 if (argvars[0].v_type == VAR_DICT)
10127 {
10128 vimvars[VV_KEY].vv_type = VAR_STRING;
10129
10130 ht = &d->dv_hashtab;
10131 hash_lock(ht);
10132 todo = (int)ht->ht_used;
10133 for (hi = ht->ht_array; todo > 0; ++hi)
10134 {
10135 if (!HASHITEM_EMPTY(hi))
10136 {
10137 int r;
10138
10139 --todo;
10140 di = HI2DI(hi);
10141 if (map &&
10142 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10143 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10144 break;
10145 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10146 r = filter_map_one(&di->di_tv, expr, map, &rem);
10147 clear_tv(&vimvars[VV_KEY].vv_tv);
10148 if (r == FAIL || did_emsg)
10149 break;
10150 if (!map && rem)
10151 {
10152 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10153 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10154 break;
10155 dictitem_remove(d, di);
10156 }
10157 }
10158 }
10159 hash_unlock(ht);
10160 }
10161 else
10162 {
10163 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10164
10165 for (li = l->lv_first; li != NULL; li = nli)
10166 {
10167 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10168 break;
10169 nli = li->li_next;
10170 vimvars[VV_KEY].vv_nr = idx;
10171 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10172 || did_emsg)
10173 break;
10174 if (!map && rem)
10175 listitem_remove(l, li);
10176 ++idx;
10177 }
10178 }
10179
10180 restore_vimvar(VV_KEY, &save_key);
10181 restore_vimvar(VV_VAL, &save_val);
10182
10183 did_emsg |= save_did_emsg;
10184 }
10185
10186 copy_tv(&argvars[0], rettv);
10187}
10188
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */