blob: a5e358fe15f77c1d289a8937ab46bb1a758cf90e [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 */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static 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 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static 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 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
248static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
249static 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 +0100250static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100255
256#if defined(EBCDIC) || defined(PROTO)
257/*
258 * Compare struct fst by function name.
259 */
260 static int
261compare_func_name(const void *s1, const void *s2)
262{
263 struct fst *p1 = (struct fst *)s1;
264 struct fst *p2 = (struct fst *)s2;
265
266 return STRCMP(p1->f_name, p2->f_name);
267}
268
269/*
270 * Sort the function table by function name.
271 * The sorting of the table above is ASCII dependant.
272 * On machines using EBCDIC we have to sort it.
273 */
274 static void
275sortFunctions(void)
276{
277 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
278
279 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
280}
281#endif
282
283
Bram Moolenaar33570922005-01-25 22:26:29 +0000284/*
285 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000286 */
287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100288eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000289{
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 int i;
291 struct vimvar *p;
292
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200293 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
294 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200295 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000296 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299 for (i = 0; i < VV_LEN; ++i)
300 {
301 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 if (STRLEN(p->vv_name) > 16)
303 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100304 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200305 getout(1);
306 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 STRCPY(p->vv_di.di_key, p->vv_name);
308 if (p->vv_flags & VV_RO)
309 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
310 else if (p->vv_flags & VV_RO_SBX)
311 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
312 else
313 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000314
315 /* add to v: scope dict, unless the value is not always available */
316 if (p->vv_type != VAR_UNKNOWN)
317 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000319 /* add to compat scope dict */
320 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000321 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100322 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
323
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000324 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100325 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100326 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100327 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100328 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329
330 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
331 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
332 set_vim_var_nr(VV_NONE, VVAL_NONE);
333 set_vim_var_nr(VV_NULL, VVAL_NULL);
334
Bram Moolenaarf562e722016-07-19 17:25:25 +0200335 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
336 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
337 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
338 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
339 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
340 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
341 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
342 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
343 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
344 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100345 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200346
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200347 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348
349#ifdef EBCDIC
350 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100351 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200352 */
353 sortFunctions();
354#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000355}
356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000357#if defined(EXITFREE) || defined(PROTO)
358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100359eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000360{
361 int i;
362 struct vimvar *p;
363
364 for (i = 0; i < VV_LEN; ++i)
365 {
366 p = &vimvars[i];
367 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100368 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000369 else if (p->vv_di.di_tv.v_type == VAR_LIST)
370 {
371 list_unref(p->vv_list);
372 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 }
375 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000376 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377 hash_clear(&compat_hashtab);
378
Bram Moolenaard9fba312005-06-26 22:34:35 +0000379 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100380# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200381 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100382# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000383
384 /* global variables */
385 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000386
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000387 /* autoloaded script names */
388 ga_clear_strings(&ga_loaded);
389
Bram Moolenaarcca74132013-09-25 21:00:28 +0200390 /* Script-local variables. First clear all the variables and in a second
391 * loop free the scriptvar_T, because a variable in one script might hold
392 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200395 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200396 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 ga_clear(&ga_scripts);
398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000399 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200400 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000401
402 /* functions */
403 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000404}
405#endif
406
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * Set an internal variable to a string value. Creates the variable if it does
410 * not already exist.
411 */
412 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100413set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000415 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000416 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418 val = vim_strsave(value);
419 if (val != NULL)
420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000424 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427 }
428}
429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200431#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000432static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433static char_u *redir_endp = NULL;
434static char_u *redir_varname = NULL;
435
436/*
437 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439 * Returns OK if successfully completed the setup. FAIL otherwise.
440 */
441 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100442var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000443{
444 int save_emsg;
445 int err;
446 typval_T tv;
447
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000448 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000449 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 {
451 EMSG(_(e_invarg));
452 return FAIL;
453 }
454
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000455 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000456 redir_varname = vim_strsave(name);
457 if (redir_varname == NULL)
458 return FAIL;
459
460 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
461 if (redir_lval == NULL)
462 {
463 var_redir_stop();
464 return FAIL;
465 }
466
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000467 /* The output is stored in growarray "redir_ga" until redirection ends. */
468 ga_init2(&redir_ga, (int)sizeof(char), 500);
469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100471 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000472 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
474 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200475 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 if (redir_endp != NULL && *redir_endp != NUL)
477 /* Trailing characters are present after the variable name */
478 EMSG(_(e_trailing));
479 else
480 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000481 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482 var_redir_stop();
483 return FAIL;
484 }
485
486 /* check if we can write to the variable: set it to or append an empty
487 * string */
488 save_emsg = did_emsg;
489 did_emsg = FALSE;
490 tv.v_type = VAR_STRING;
491 tv.vval.v_string = (char_u *)"";
492 if (append)
493 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
494 else
495 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200496 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000498 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 if (err)
500 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000501 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 var_redir_stop();
503 return FAIL;
504 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000505
506 return OK;
507}
508
509/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000510 * Append "value[value_len]" to the variable set by var_redir_start().
511 * The actual appending is postponed until redirection ends, because the value
512 * appended may in fact be the string we write to, changing it may cause freed
513 * memory to be used:
514 * :redir => foo
515 * :let foo
516 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 */
518 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100519var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522
523 if (redir_lval == NULL)
524 return;
525
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000530
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 {
533 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000534 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000535 }
536 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538}
539
540/*
541 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547 typval_T tv;
548
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200549 if (EVALCMD_BUSY)
550 {
551 redir_lval = NULL;
552 return;
553 }
554
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000555 if (redir_lval != NULL)
556 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000557 /* If there was no error: assign the text to the variable. */
558 if (redir_endp != NULL)
559 {
560 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
561 tv.v_type = VAR_STRING;
562 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 /* Call get_lval() again, if it's inside a Dict or List it may
564 * have changed. */
565 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100566 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200567 if (redir_endp != NULL && redir_lval->ll_name != NULL)
568 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
569 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000570 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000572 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574
Bram Moolenaard23a8232018-02-10 18:45:26 +0100575 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100577 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578}
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 Moolenaard155d7a2018-12-21 16:04:21 +0100689 retval = (tv_get_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 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100728 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100729 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 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200736 clear_tv(rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100737 EMSG2(_(e_invexpr2), s);
738 return FAIL;
739 }
740 }
741 return OK;
742}
743
744/*
745 * Like eval_to_bool() but using a typval_T instead of a string.
746 * Works for string, funcref and partial.
747 */
748 int
749eval_expr_to_bool(typval_T *expr, int *error)
750{
751 typval_T rettv;
752 int res;
753
754 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
755 {
756 *error = TRUE;
757 return FALSE;
758 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100759 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100760 clear_tv(&rettv);
761 return res;
762}
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764/*
765 * Top level evaluation function, returning a string. If "skip" is TRUE,
766 * only parsing to "nextcmd" is done, without reporting errors. Return
767 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
768 */
769 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100770eval_to_string_skip(
771 char_u *arg,
772 char_u **nextcmd,
773 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 char_u *retval;
777
778 if (skip)
779 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000780 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 retval = NULL;
782 else
783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100784 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 if (skip)
788 --emsg_skip;
789
790 return retval;
791}
792
793/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000794 * Skip over an expression at "*pp".
795 * Return FAIL for an error, OK otherwise.
796 */
797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100798skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000801
802 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000804}
805
806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000808 * When "convert" is TRUE convert a List into a sequence of lines and convert
809 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * Return pointer to allocated memory, or NULL for failure.
811 */
812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813eval_to_string(
814 char_u *arg,
815 char_u **nextcmd,
816 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000820 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000821#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000822 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 retval = NULL;
827 else
828 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000829 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000830 {
831 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000832 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200833 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200834 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200835 if (tv.vval.v_list->lv_len > 0)
836 ga_append(&ga, NL);
837 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838 ga_append(&ga, NUL);
839 retval = (char_u *)ga.ga_data;
840 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000841#ifdef FEAT_FLOAT
842 else if (convert && tv.v_type == VAR_FLOAT)
843 {
844 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
845 retval = vim_strsave(numbuf);
846 }
847#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000848 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100849 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852
853 return retval;
854}
855
856/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000857 * Call eval_to_string() without using current local variables and using
858 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100861eval_to_string_safe(
862 char_u *arg,
863 char_u **nextcmd,
864 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200867 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200869 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000870 if (use_sandbox)
871 ++sandbox;
872 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000873 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000874 if (use_sandbox)
875 --sandbox;
876 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200877 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 return retval;
879}
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881/*
882 * Top level evaluation function, returning a number.
883 * Evaluates "expr" silently.
884 * Returns -1 for an error.
885 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200886 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100887eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
Bram Moolenaar33570922005-01-25 22:26:29 +0000889 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200890 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000891 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 ++emsg_off;
894
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 retval = -1;
897 else
898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100899 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 --emsg_off;
903
904 return retval;
905}
906
Bram Moolenaara40058a2005-07-11 22:42:07 +0000907/*
908 * Prepare v: variable "idx" to be used.
909 * Save the current typeval in "save_tv".
910 * When not used yet add the variable to the v: hashtable.
911 */
912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100913prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000914{
915 *save_tv = vimvars[idx].vv_tv;
916 if (vimvars[idx].vv_type == VAR_UNKNOWN)
917 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
918}
919
920/*
921 * Restore v: variable "idx" to typeval "save_tv".
922 * When no longer defined, remove the variable from the v: hashtable.
923 */
924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000926{
927 hashitem_T *hi;
928
Bram Moolenaara40058a2005-07-11 22:42:07 +0000929 vimvars[idx].vv_tv = *save_tv;
930 if (vimvars[idx].vv_type == VAR_UNKNOWN)
931 {
932 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
933 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100934 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000935 else
936 hash_remove(&vimvarht, hi);
937 }
938}
939
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000940#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000941/*
942 * Evaluate an expression to a list with suggestions.
943 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000944 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000945 */
946 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100947eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000948{
949 typval_T save_val;
950 typval_T rettv;
951 list_T *list = NULL;
952 char_u *p = skipwhite(expr);
953
954 /* Set "v:val" to the bad word. */
955 prepare_vimvar(VV_VAL, &save_val);
956 vimvars[VV_VAL].vv_type = VAR_STRING;
957 vimvars[VV_VAL].vv_str = badword;
958 if (p_verbose == 0)
959 ++emsg_off;
960
961 if (eval1(&p, &rettv, TRUE) == OK)
962 {
963 if (rettv.v_type != VAR_LIST)
964 clear_tv(&rettv);
965 else
966 list = rettv.vval.v_list;
967 }
968
969 if (p_verbose == 0)
970 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000971 restore_vimvar(VV_VAL, &save_val);
972
973 return list;
974}
975
976/*
977 * "list" is supposed to contain two items: a word and a number. Return the
978 * word in "pp" and the number as the return value.
979 * Return -1 if anything isn't right.
980 * Used to get the good word and score from the eval_spell_expr() result.
981 */
982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000984{
985 listitem_T *li;
986
987 li = list->lv_first;
988 if (li == NULL)
989 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100990 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000991
992 li = li->li_next;
993 if (li == NULL)
994 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000996}
997#endif
998
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000999/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001000 * Top level evaluation function.
1001 * Returns an allocated typval_T with the result.
1002 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001003 */
1004 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001006{
1007 typval_T *tv;
1008
1009 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001010 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001011 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001012
1013 return tv;
1014}
1015
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001018 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001019 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1020 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001021 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001024call_vim_function(
1025 char_u *func,
1026 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001027 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001028 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001031 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001033 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001034 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001036 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001037 if (ret == FAIL)
1038 clear_tv(rettv);
1039
1040 return ret;
1041}
1042
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001044 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001045 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001046 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1047 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001048 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001049 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050call_func_retnr(
1051 char_u *func,
1052 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001053 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001054{
1055 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001056 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001057
Bram Moolenaarded27a12018-08-01 19:06:03 +02001058 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001059 return -1;
1060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001062 clear_tv(&rettv);
1063 return retval;
1064}
1065
1066#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1067 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1068
Bram Moolenaar4f688582007-07-24 12:34:30 +00001069# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001070/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001071 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001072 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001073 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1074 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001075 */
1076 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001077call_func_retstr(
1078 char_u *func,
1079 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001080 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081{
1082 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001083 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084
Bram Moolenaarded27a12018-08-01 19:06:03 +02001085 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 return NULL;
1087
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001088 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 return retval;
1091}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001092# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001094/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001095 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001096 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1097 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001098 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 */
1100 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001101call_func_retlist(
1102 char_u *func,
1103 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001104 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001105{
1106 typval_T rettv;
1107
Bram Moolenaarded27a12018-08-01 19:06:03 +02001108 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109 return NULL;
1110
1111 if (rettv.v_type != VAR_LIST)
1112 {
1113 clear_tv(&rettv);
1114 return NULL;
1115 }
1116
1117 return rettv.vval.v_list;
1118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
1120
Bram Moolenaar05159a02005-02-26 23:04:13 +00001121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#ifdef FEAT_FOLDING
1123/*
1124 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1125 * it in "*cp". Doesn't give error messages.
1126 */
1127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001128eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001131 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001133 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1134 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001137 if (use_sandbox)
1138 ++sandbox;
1139 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 retval = 0;
1143 else
1144 {
1145 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 if (tv.v_type == VAR_NUMBER)
1147 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001148 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 retval = 0;
1150 else
1151 {
1152 /* If the result is a string, check if there is a non-digit before
1153 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!VIM_ISDIGIT(*s) && *s != '-')
1156 *cp = *s++;
1157 retval = atol((char *)s);
1158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001162 if (use_sandbox)
1163 --sandbox;
1164 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001166 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167}
1168#endif
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 * ":let" list all variable values
1172 * ":let var1 var2" list variable values
1173 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001174 * ":let var += expr" assignment command.
1175 * ":let var -= expr" assignment command.
1176 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 */
1179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001180ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001186 int var_count = 0;
1187 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001189 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001190 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaardb552d602006-03-23 22:59:57 +00001192 argend = skip_var_list(arg, &var_count, &semicolon);
1193 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001194 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001195 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1196 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001197 expr = skipwhite(argend);
1198 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1199 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001201 /*
1202 * ":let" without "=": list variables
1203 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001204 if (*arg == '[')
1205 EMSG(_(e_invarg));
1206 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001208 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001212 list_glob_vars(&first);
1213 list_buf_vars(&first);
1214 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001215 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001216 list_script_vars(&first);
1217 list_func_vars(&first);
1218 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 eap->nextcmd = check_nextcmd(arg);
1221 }
1222 else
1223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 op[0] = '=';
1225 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001226 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001228 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1229 op[0] = *expr; /* +=, -= or .= */
1230 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001231 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001232 else
1233 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (eap->skip)
1236 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (eap->skip)
1239 {
1240 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 --emsg_skip;
1243 }
1244 else if (i != FAIL)
1245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001246 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001248 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 }
1251}
1252
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001253/*
1254 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1255 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001256 * When "nextchars" is not NULL it points to a string with characters that
1257 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1258 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259 * Returns OK or FAIL;
1260 */
1261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001262ex_let_vars(
1263 char_u *arg_start,
1264 typval_T *tv,
1265 int copy, /* copy values from "tv", don't move */
1266 int semicolon, /* from skip_var_list() */
1267 int var_count, /* from skip_var_list() */
1268 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001269{
1270 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001272 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 listitem_T *item;
1274 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001275
1276 if (*arg != '[')
1277 {
1278 /*
1279 * ":let var = expr" or ":for var in list"
1280 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001282 return FAIL;
1283 return OK;
1284 }
1285
1286 /*
1287 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1288 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001289 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 {
1291 EMSG(_(e_listreq));
1292 return FAIL;
1293 }
1294
1295 i = list_len(l);
1296 if (semicolon == 0 && var_count < i)
1297 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001298 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 return FAIL;
1300 }
1301 if (var_count - semicolon > i)
1302 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001303 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001304 return FAIL;
1305 }
1306
1307 item = l->lv_first;
1308 while (*arg != ']')
1309 {
1310 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001311 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001312 item = item->li_next;
1313 if (arg == NULL)
1314 return FAIL;
1315
1316 arg = skipwhite(arg);
1317 if (*arg == ';')
1318 {
1319 /* Put the rest of the list (may be empty) in the var after ';'.
1320 * Create a new list for this. */
1321 l = list_alloc();
1322 if (l == NULL)
1323 return FAIL;
1324 while (item != NULL)
1325 {
1326 list_append_tv(l, &item->li_tv);
1327 item = item->li_next;
1328 }
1329
1330 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001331 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332 ltv.vval.v_list = l;
1333 l->lv_refcount = 1;
1334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1336 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 clear_tv(&ltv);
1338 if (arg == NULL)
1339 return FAIL;
1340 break;
1341 }
1342 else if (*arg != ',' && *arg != ']')
1343 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001344 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001345 return FAIL;
1346 }
1347 }
1348
1349 return OK;
1350}
1351
1352/*
1353 * Skip over assignable variable "var" or list of variables "[var, var]".
1354 * Used for ":let varvar = expr" and ":for varvar in expr".
1355 * For "[var, var]" increment "*var_count" for each variable.
1356 * for "[var, var; var]" set "semicolon".
1357 * Return NULL for an error.
1358 */
1359 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360skip_var_list(
1361 char_u *arg,
1362 int *var_count,
1363 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364{
1365 char_u *p, *s;
1366
1367 if (*arg == '[')
1368 {
1369 /* "[var, var]": find the matching ']'. */
1370 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001371 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 {
1373 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1374 s = skip_var_one(p);
1375 if (s == p)
1376 {
1377 EMSG2(_(e_invarg2), p);
1378 return NULL;
1379 }
1380 ++*var_count;
1381
1382 p = skipwhite(s);
1383 if (*p == ']')
1384 break;
1385 else if (*p == ';')
1386 {
1387 if (*semicolon == 1)
1388 {
1389 EMSG(_("Double ; in list of variables"));
1390 return NULL;
1391 }
1392 *semicolon = 1;
1393 }
1394 else if (*p != ',')
1395 {
1396 EMSG2(_(e_invarg2), p);
1397 return NULL;
1398 }
1399 }
1400 return p + 1;
1401 }
1402 else
1403 return skip_var_one(arg);
1404}
1405
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001406/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001407 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001408 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001409 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001411skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001413 if (*arg == '@' && arg[1] != NUL)
1414 return arg + 2;
1415 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1416 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417}
1418
Bram Moolenaara7043832005-01-21 11:56:39 +00001419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 * List variables for hashtab "ht" with prefix "prefix".
1421 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001422 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424list_hashtable_vars(
1425 hashtab_T *ht,
1426 char_u *prefix,
1427 int empty,
1428 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001429{
Bram Moolenaar33570922005-01-25 22:26:29 +00001430 hashitem_T *hi;
1431 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001432 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001433 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001434
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001435 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001436 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1437 {
1438 if (!HASHITEM_EMPTY(hi))
1439 {
1440 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001442
1443 // apply :filter /pat/ to variable name
1444 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1445 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1446 if (message_filtered(buf))
1447 continue;
1448
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 if (empty || di->di_tv.v_type != VAR_STRING
1450 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001451 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001452 }
1453 }
1454}
1455
1456/*
1457 * List global variables.
1458 */
1459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001461{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001462 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001463}
1464
1465/*
1466 * List buffer variables.
1467 */
1468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001470{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001471 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001472 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001473}
1474
1475/*
1476 * List window variables.
1477 */
1478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001479list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001480{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001481 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001482 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001483}
1484
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001485/*
1486 * List tab page variables.
1487 */
1488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001490{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001491 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001492 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001493}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001494
Bram Moolenaara7043832005-01-21 11:56:39 +00001495/*
1496 * List Vim variables.
1497 */
1498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001501 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502}
1503
1504/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001505 * List script-local variables, if there is a script.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001509{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001510 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1511 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001512 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001513}
1514
1515/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 * List variables in "arg".
1517 */
1518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520{
1521 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001522 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524 char_u *name_start;
1525 char_u *arg_subsc;
1526 char_u *tofree;
1527 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528
1529 while (!ends_excmd(*arg) && !got_int)
1530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001533 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001534 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 {
1536 emsg_severe = TRUE;
1537 EMSG(_(e_trailing));
1538 break;
1539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 /* get_name_len() takes care of expanding curly braces */
1544 name_start = name = arg;
1545 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1546 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001548 /* This is mainly to keep test 49 working: when expanding
1549 * curly braces fails overrule the exception error message. */
1550 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 emsg_severe = TRUE;
1553 EMSG2(_(e_invarg2), arg);
1554 break;
1555 }
1556 error = TRUE;
1557 }
1558 else
1559 {
1560 if (tofree != NULL)
1561 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001562 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001563 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 else
1565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 /* handle d.key, l[idx], f(expr) */
1567 arg_subsc = arg;
1568 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001574 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001576 case 'g': list_glob_vars(first); break;
1577 case 'b': list_buf_vars(first); break;
1578 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001579 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001580 case 'v': list_vim_vars(first); break;
1581 case 's': list_script_vars(first); break;
1582 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583 default:
1584 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001585 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001586 }
1587 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 {
1589 char_u numbuf[NUMBUFLEN];
1590 char_u *tf;
1591 int c;
1592 char_u *s;
1593
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001594 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595 c = *arg;
1596 *arg = NUL;
1597 list_one_var_a((char_u *)"",
1598 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001599 tv.v_type,
1600 s == NULL ? (char_u *)"" : s,
1601 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001602 *arg = c;
1603 vim_free(tf);
1604 }
1605 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 }
1608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001609
1610 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001612
1613 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 }
1615
1616 return arg;
1617}
1618
1619/*
1620 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1621 * Returns a pointer to the char just after the var name.
1622 * Returns NULL if there is an error.
1623 */
1624 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001625ex_let_one(
1626 char_u *arg, /* points to variable name */
1627 typval_T *tv, /* value to assign to variable */
1628 int copy, /* copy value from "tv" */
1629 char_u *endchars, /* valid chars after variable name or NULL */
1630 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631{
1632 int c1;
1633 char_u *name;
1634 char_u *p;
1635 char_u *arg_end = NULL;
1636 int len;
1637 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639
1640 /*
1641 * ":let $VAR = expr": Set environment variable.
1642 */
1643 if (*arg == '$')
1644 {
1645 /* Find the end of the name. */
1646 ++arg;
1647 name = arg;
1648 len = get_env_len(&arg);
1649 if (len == 0)
1650 EMSG2(_(e_invarg2), name - 1);
1651 else
1652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 if (op != NULL && (*op == '+' || *op == '-'))
1654 EMSG2(_(e_letwrong), op);
1655 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001658 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 {
1660 c1 = name[len];
1661 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001662 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 {
1665 int mustfree = FALSE;
1666 char_u *s = vim_getenv(name, &mustfree);
1667
1668 if (s != NULL)
1669 {
1670 p = tofree = concat_str(s, p);
1671 if (mustfree)
1672 vim_free(s);
1673 }
1674 }
1675 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001678 if (STRICMP(name, "HOME") == 0)
1679 init_homedir();
1680 else if (didset_vim && STRICMP(name, "VIM") == 0)
1681 didset_vim = FALSE;
1682 else if (didset_vimruntime
1683 && STRICMP(name, "VIMRUNTIME") == 0)
1684 didset_vimruntime = FALSE;
1685 arg_end = arg;
1686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 }
1690 }
1691 }
1692
1693 /*
1694 * ":let &option = expr": Set option value.
1695 * ":let &l:option = expr": Set local option value.
1696 * ":let &g:option = expr": Set global option value.
1697 */
1698 else if (*arg == '&')
1699 {
1700 /* Find the end of the name. */
1701 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 if (p == NULL || (endchars != NULL
1703 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 EMSG(_(e_letunexp));
1705 else
1706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707 long n;
1708 int opt_type;
1709 long numval;
1710 char_u *stringval = NULL;
1711 char_u *s;
1712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 c1 = *p;
1714 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001716 n = (long)tv_get_number(tv);
1717 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 opt_type = get_option_value(arg, &numval,
1721 &stringval, opt_flags);
1722 if ((opt_type == 1 && *op == '.')
1723 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001726 s = NULL; /* don't set the value */
1727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 else
1729 {
1730 if (opt_type == 1) /* number */
1731 {
1732 if (*op == '+')
1733 n = numval + n;
1734 else
1735 n = numval - n;
1736 }
1737 else if (opt_type == 0 && stringval != NULL) /* string */
1738 {
1739 s = concat_str(stringval, s);
1740 vim_free(stringval);
1741 stringval = s;
1742 }
1743 }
1744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001745 if (s != NULL)
1746 {
1747 set_option_value(arg, n, s, opt_flags);
1748 arg_end = p;
1749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 }
1753 }
1754
1755 /*
1756 * ":let @r = expr": Set register contents.
1757 */
1758 else if (*arg == '@')
1759 {
1760 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 if (op != NULL && (*op == '+' || *op == '-'))
1762 EMSG2(_(e_letwrong), op);
1763 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 EMSG(_(e_letunexp));
1766 else
1767 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 char_u *s;
1770
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001771 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001774 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 if (s != NULL)
1776 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001777 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 vim_free(s);
1779 }
1780 }
1781 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001784 arg_end = arg + 1;
1785 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001786 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 }
1788 }
1789
1790 /*
1791 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001794 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001798 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001799 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1802 EMSG(_(e_letunexp));
1803 else
1804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 arg_end = p;
1807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001809 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 }
1811
1812 else
1813 EMSG2(_(e_invarg2), arg);
1814
1815 return arg_end;
1816}
1817
1818/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 * Get an lval: variable, Dict item or List item that can be assigned a value
1820 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1821 * "name.key", "name.key[expr]" etc.
1822 * Indexing only works if "name" is an existing List or Dictionary.
1823 * "name" points to the start of the name.
1824 * If "rettv" is not NULL it points to the value to be assigned.
1825 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1826 * wrong; must end in space or cmd separator.
1827 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001828 * flags:
1829 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001830 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001831 * GLV_NO_AUTOLOAD: do not use script autoloading
1832 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001834 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838get_lval(
1839 char_u *name,
1840 typval_T *rettv,
1841 lval_T *lp,
1842 int unlet,
1843 int skip,
1844 int flags, /* GLV_ values */
1845 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 char_u *expr_start, *expr_end;
1849 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 dictitem_T *v;
1851 typval_T var1;
1852 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001855 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001856 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001858 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862
1863 if (skip)
1864 {
1865 /* When skipping just find the end of the name. */
1866 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001867 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 }
1869
1870 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001871 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 if (expr_start != NULL)
1873 {
1874 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001875 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001876 && *p != '[' && *p != '.')
1877 {
1878 EMSG(_(e_trailing));
1879 return NULL;
1880 }
1881
1882 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1883 if (lp->ll_exp_name == NULL)
1884 {
1885 /* Report an invalid expression in braces, unless the
1886 * expression evaluation has been cancelled due to an
1887 * aborting error, an interrupt, or an exception. */
1888 if (!aborting() && !quiet)
1889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001890 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 EMSG2(_(e_invarg2), name);
1892 return NULL;
1893 }
1894 }
1895 lp->ll_name = lp->ll_exp_name;
1896 }
1897 else
1898 lp->ll_name = name;
1899
1900 /* Without [idx] or .key we are done. */
1901 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1902 return p;
1903
1904 cc = *p;
1905 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001906 /* Only pass &ht when we would write to the variable, it prevents autoload
1907 * as well. */
1908 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1909 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (v == NULL && !quiet)
1911 EMSG2(_(e_undefvar), lp->ll_name);
1912 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 if (v == NULL)
1914 return NULL;
1915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 /*
1917 * Loop until no more [idx] or .key is following.
1918 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001920 var1.v_type = VAR_UNKNOWN;
1921 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1925 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001926 && lp->ll_tv->vval.v_dict != NULL)
1927 && !(lp->ll_tv->v_type == VAR_BLOB
1928 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 if (!quiet)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001931 EMSG(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 if (!quiet)
1937 EMSG(_("E708: [:] must come last"));
1938 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001940
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 len = -1;
1942 if (*p == '.')
1943 {
1944 key = p + 1;
1945 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1946 ;
1947 if (len == 0)
1948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 if (!quiet)
1950 EMSG(_(e_emptykey));
1951 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001952 }
1953 p = key + len;
1954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955 else
1956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001959 if (*p == ':')
1960 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001961 else
1962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 empty1 = FALSE;
1964 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001966 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 {
1968 /* not a number or string */
1969 clear_tv(&var1);
1970 return NULL;
1971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 }
1973
1974 /* Optionally get the second index [ :expr]. */
1975 if (*p == ':')
1976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001984 if (rettv != NULL
1985 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001986 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001988 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 if (!quiet)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001991 EMSG(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 }
1995 p = skipwhite(p + 1);
1996 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 else
1999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2002 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002003 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002006 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 {
2008 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002009 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 clear_tv(&var2);
2011 return NULL;
2012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002018
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (!quiet)
2022 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002023 clear_tv(&var1);
2024 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 }
2027
2028 /* Skip to past ']'. */
2029 ++p;
2030 }
2031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 {
2034 if (len == -1)
2035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002037 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002038 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 }
2043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 lp->ll_list = NULL;
2045 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002046 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002048 /* When assigning to a scope dictionary check that a function and
2049 * variable name is valid (only variable name unless it is l: or
2050 * g: dictionary). Disallow overwriting a builtin function. */
2051 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002053 int prevval;
2054 int wrong;
2055
2056 if (len != -1)
2057 {
2058 prevval = key[len];
2059 key[len] = NUL;
2060 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002061 else
2062 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002063 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2064 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002065 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002066 || !valid_varname(key);
2067 if (len != -1)
2068 key[len] = prevval;
2069 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002070 return NULL;
2071 }
2072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075 /* Can't add "v:" variable. */
2076 if (lp->ll_dict == &vimvardict)
2077 {
2078 EMSG2(_(e_illvar), name);
2079 return NULL;
2080 }
2081
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002082 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002087 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 }
2090 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002094 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002096 p = NULL;
2097 break;
2098 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002100 else if ((flags & GLV_READ_ONLY) == 0
2101 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002102 {
2103 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002104 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002105 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002106
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002110 else if (lp->ll_tv->v_type == VAR_BLOB)
2111 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002112 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2113
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002114 /*
2115 * Get the number and item for the only or first index of the List.
2116 */
2117 if (empty1)
2118 lp->ll_n1 = 0;
2119 else
2120 // is number or string
2121 lp->ll_n1 = (long)tv_get_number(&var1);
2122 clear_tv(&var1);
2123
2124 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002125 || lp->ll_n1 > bloblen
2126 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002127 {
2128 if (!quiet)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002129 EMSGN(_(e_blobidx), lp->ll_n1);
2130 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002131 return NULL;
2132 }
2133 if (lp->ll_range && !lp->ll_empty2)
2134 {
2135 lp->ll_n2 = (long)tv_get_number(&var2);
2136 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002137 if (lp->ll_n2 < 0
2138 || lp->ll_n2 >= bloblen
2139 || lp->ll_n2 < lp->ll_n1)
2140 {
2141 if (!quiet)
2142 EMSGN(_(e_blobidx), lp->ll_n2);
2143 return NULL;
2144 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002145 }
2146 lp->ll_blob = lp->ll_tv->vval.v_blob;
2147 lp->ll_tv = NULL;
2148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 else
2150 {
2151 /*
2152 * Get the number and item for the only or first index of the List.
2153 */
2154 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002157 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002159 clear_tv(&var1);
2160
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 lp->ll_list = lp->ll_tv->vval.v_list;
2163 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2164 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002165 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002166 if (lp->ll_n1 < 0)
2167 {
2168 lp->ll_n1 = 0;
2169 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2170 }
2171 }
2172 if (lp->ll_li == NULL)
2173 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002174 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002175 if (!quiet)
2176 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 }
2179
2180 /*
2181 * May need to find the item or absolute index for the second
2182 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 * When no index given: "lp->ll_empty2" is TRUE.
2184 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002188 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002189 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002195 {
2196 if (!quiet)
2197 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 }
2202
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2204 if (lp->ll_n1 < 0)
2205 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2206 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002207 {
2208 if (!quiet)
2209 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002211 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002212 }
2213
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
2217
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002218 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 return p;
2220}
2221
2222/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002223 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227{
2228 vim_free(lp->ll_exp_name);
2229 vim_free(lp->ll_newkey);
2230}
2231
2232/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002233 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002235 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238set_var_lval(
2239 lval_T *lp,
2240 char_u *endp,
2241 typval_T *rettv,
2242 int copy,
2243 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244{
2245 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 listitem_T *ri;
2247 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248
2249 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002251 cc = *endp;
2252 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002253 if (lp->ll_blob != NULL)
2254 {
2255 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002256
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002257 if (op != NULL && *op != '=')
2258 {
2259 EMSG2(_(e_letwrong), op);
2260 return;
2261 }
2262
2263 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2264 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002265 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002266
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002267 if (lp->ll_empty2)
2268 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2269
2270 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002271 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002272 EMSG(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002273 return;
2274 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002275 if (lp->ll_empty2)
2276 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002277
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002278 ir = 0;
2279 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2280 blob_set(lp->ll_blob, il,
2281 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 }
2283 else
2284 {
2285 val = (int)tv_get_number_chk(rettv, &error);
2286 if (!error)
2287 {
2288 garray_T *gap = &lp->ll_blob->bv_ga;
2289
2290 // Allow for appending a byte. Setting a byte beyond
2291 // the end is an error otherwise.
2292 if (lp->ll_n1 < gap->ga_len
2293 || (lp->ll_n1 == gap->ga_len
2294 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2295 {
2296 blob_set(lp->ll_blob, lp->ll_n1, val);
2297 if (lp->ll_n1 == gap->ga_len)
2298 ++gap->ga_len;
2299 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002301 }
2302 }
2303 }
2304 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002306 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307
Bram Moolenaar79518e22017-02-17 16:31:35 +01002308 /* handle +=, -= and .= */
2309 di = NULL;
2310 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2311 &tv, &di, TRUE, FALSE) == OK)
2312 {
2313 if ((di == NULL
2314 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2315 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2316 FALSE)))
2317 && tv_op(&tv, rettv, op) == OK)
2318 set_var(lp->ll_name, &tv, FALSE);
2319 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002322 else
2323 set_var(lp->ll_name, rettv, copy);
2324 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002326 else if (tv_check_lock(lp->ll_newkey == NULL
2327 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002328 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002329 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 else if (lp->ll_range)
2331 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002332 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002333 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002334
2335 /*
2336 * Check whether any of the list items is locked
2337 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002338 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002339 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002340 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002341 return;
2342 ri = ri->li_next;
2343 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2344 break;
2345 ll_li = ll_li->li_next;
2346 ++ll_n1;
2347 }
2348
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 /*
2350 * Assign the List values to the list items.
2351 */
2352 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 if (op != NULL && *op != '=')
2355 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2356 else
2357 {
2358 clear_tv(&lp->ll_li->li_tv);
2359 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2360 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 ri = ri->li_next;
2362 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2363 break;
2364 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002367 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 ri = NULL;
2370 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 lp->ll_li = lp->ll_li->li_next;
2374 ++lp->ll_n1;
2375 }
2376 if (ri != NULL)
2377 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 else if (lp->ll_empty2
2379 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 : lp->ll_n1 != lp->ll_n2)
2381 EMSG(_("E711: List value has not enough items"));
2382 }
2383 else
2384 {
2385 /*
2386 * Assign to a List or Dictionary item.
2387 */
2388 if (lp->ll_newkey != NULL)
2389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 if (op != NULL && *op != '=')
2391 {
2392 EMSG2(_(e_letwrong), op);
2393 return;
2394 }
2395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002397 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 if (di == NULL)
2399 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002400 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2401 {
2402 vim_free(di);
2403 return;
2404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002406 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 else if (op != NULL && *op != '=')
2408 {
2409 tv_op(lp->ll_tv, rettv, op);
2410 return;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 /*
2416 * Assign the value to the variable or list item.
2417 */
2418 if (copy)
2419 copy_tv(rettv, lp->ll_tv);
2420 else
2421 {
2422 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002423 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427}
2428
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2431 * Returns OK or FAIL.
2432 */
2433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002436 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u numbuf[NUMBUFLEN];
2438 char_u *s;
2439
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002440 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2441 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2442 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 switch (tv1->v_type)
2445 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002446 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 case VAR_DICT:
2448 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002449 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002450 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002451 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002452 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 break;
2454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002455 case VAR_BLOB:
2456 if (*op != '+' || tv2->v_type != VAR_BLOB)
2457 break;
2458 // BLOB += BLOB
2459 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2460 {
2461 blob_T *b1 = tv1->vval.v_blob;
2462 blob_T *b2 = tv2->vval.v_blob;
2463 int i, len = blob_len(b2);
2464 for (i = 0; i < len; i++)
2465 ga_append(&b1->bv_ga, blob_get(b2, i));
2466 }
2467 return OK;
2468
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 case VAR_LIST:
2470 if (*op != '+' || tv2->v_type != VAR_LIST)
2471 break;
2472 /* List += List */
2473 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2474 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2475 return OK;
2476
2477 case VAR_NUMBER:
2478 case VAR_STRING:
2479 if (tv2->v_type == VAR_LIST)
2480 break;
2481 if (*op == '+' || *op == '-')
2482 {
2483 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002484 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002485#ifdef FEAT_FLOAT
2486 if (tv2->v_type == VAR_FLOAT)
2487 {
2488 float_T f = n;
2489
2490 if (*op == '+')
2491 f += tv2->vval.v_float;
2492 else
2493 f -= tv2->vval.v_float;
2494 clear_tv(tv1);
2495 tv1->v_type = VAR_FLOAT;
2496 tv1->vval.v_float = f;
2497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002499#endif
2500 {
2501 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002502 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002503 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505 clear_tv(tv1);
2506 tv1->v_type = VAR_NUMBER;
2507 tv1->vval.v_number = n;
2508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 }
2510 else
2511 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002512 if (tv2->v_type == VAR_FLOAT)
2513 break;
2514
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002516 s = tv_get_string(tv1);
2517 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 clear_tv(tv1);
2519 tv1->v_type = VAR_STRING;
2520 tv1->vval.v_string = s;
2521 }
2522 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002523
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002525#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002526 {
2527 float_T f;
2528
2529 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2530 && tv2->v_type != VAR_NUMBER
2531 && tv2->v_type != VAR_STRING))
2532 break;
2533 if (tv2->v_type == VAR_FLOAT)
2534 f = tv2->vval.v_float;
2535 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 if (*op == '+')
2538 tv1->vval.v_float += f;
2539 else
2540 tv1->vval.v_float -= f;
2541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002542#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002543 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 }
2545 }
2546
2547 EMSG2(_(e_letwrong), op);
2548 return FAIL;
2549}
2550
2551/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 * Evaluate the expression used in a ":for var in expr" command.
2553 * "arg" points to "var".
2554 * Set "*errp" to TRUE for an error, FALSE otherwise;
2555 * Return a pointer that holds the info. Null when there is an error.
2556 */
2557 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002558eval_for_line(
2559 char_u *arg,
2560 int *errp,
2561 char_u **nextcmdp,
2562 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002563{
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002565 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 typval_T tv;
2567 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002568 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002569
2570 *errp = TRUE; /* default: there is an error */
2571
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573 if (fi == NULL)
2574 return NULL;
2575
2576 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2577 if (expr == NULL)
2578 return fi;
2579
2580 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002581 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002582 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002583 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002584 return fi;
2585 }
2586
2587 if (skip)
2588 ++emsg_skip;
2589 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2590 {
2591 *errp = FALSE;
2592 if (!skip)
2593 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002594 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002595 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002596 l = tv.vval.v_list;
2597 if (l == NULL)
2598 {
2599 // a null list is like an empty list: do nothing
2600 clear_tv(&tv);
2601 }
2602 else
2603 {
2604 // No need to increment the refcount, it's already set for
2605 // the list being used in "tv".
2606 fi->fi_list = l;
2607 list_add_watch(l, &fi->fi_lw);
2608 fi->fi_lw.lw_item = l->lv_first;
2609 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002610 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002611 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002612 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002613 b = tv.vval.v_blob;
2614 if (b == NULL)
2615 clear_tv(&tv);
2616 else
2617 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002618 // No need to increment the refcount, it's already set for
2619 // the blob being used in "tv".
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002620 fi->fi_blob = b;
2621 fi->fi_bi = 0;
2622 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002623 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002624 else
2625 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002626 EMSG(_(e_listreq));
2627 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002628 }
2629 }
2630 }
2631 if (skip)
2632 --emsg_skip;
2633
2634 return fi;
2635}
2636
2637/*
2638 * Use the first item in a ":for" list. Advance to the next.
2639 * Assign the values to the variable (list). "arg" points to the first one.
2640 * Return TRUE when a valid item was found, FALSE when at end of list or
2641 * something wrong.
2642 */
2643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002644next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002646 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002647 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002650 if (fi->fi_blob != NULL)
2651 {
2652 typval_T tv;
2653
2654 if (fi->fi_bi >= blob_len(fi->fi_blob))
2655 return FALSE;
2656 tv.v_type = VAR_NUMBER;
2657 tv.v_lock = VAR_FIXED;
2658 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2659 ++fi->fi_bi;
2660 return ex_let_vars(arg, &tv, TRUE,
2661 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2662 }
2663
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002664 item = fi->fi_lw.lw_item;
2665 if (item == NULL)
2666 result = FALSE;
2667 else
2668 {
2669 fi->fi_lw.lw_item = item->li_next;
2670 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2671 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2672 }
2673 return result;
2674}
2675
2676/*
2677 * Free the structure used to store info used by ":for".
2678 */
2679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681{
Bram Moolenaar33570922005-01-25 22:26:29 +00002682 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002683
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002684 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002685 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002686 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002687 list_unref(fi->fi_list);
2688 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002689 if (fi != NULL && fi->fi_blob != NULL)
2690 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 vim_free(fi);
2692}
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2695
2696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002697set_context_for_expression(
2698 expand_T *xp,
2699 char_u *arg,
2700 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 int got_eq = FALSE;
2703 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706 if (cmdidx == CMD_let)
2707 {
2708 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002709 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710 {
2711 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002712 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002713 {
2714 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002715 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002716 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 break;
2718 }
2719 return;
2720 }
2721 }
2722 else
2723 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2724 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 while ((xp->xp_pattern = vim_strpbrk(arg,
2726 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2727 {
2728 c = *xp->xp_pattern;
2729 if (c == '&')
2730 {
2731 c = xp->xp_pattern[1];
2732 if (c == '&')
2733 {
2734 ++xp->xp_pattern;
2735 xp->xp_context = cmdidx != CMD_let || got_eq
2736 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2737 }
2738 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002741 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2742 xp->xp_pattern += 2;
2743
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 else if (c == '$')
2747 {
2748 /* environment variable */
2749 xp->xp_context = EXPAND_ENV_VARS;
2750 }
2751 else if (c == '=')
2752 {
2753 got_eq = TRUE;
2754 xp->xp_context = EXPAND_EXPRESSION;
2755 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002756 else if (c == '#'
2757 && xp->xp_context == EXPAND_EXPRESSION)
2758 {
2759 /* Autoload function/variable contains '#'. */
2760 break;
2761 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002762 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 && xp->xp_context == EXPAND_FUNCTIONS
2764 && vim_strchr(xp->xp_pattern, '(') == NULL)
2765 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002766 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 break;
2768 }
2769 else if (cmdidx != CMD_let || got_eq)
2770 {
2771 if (c == '"') /* string */
2772 {
2773 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2774 if (c == '\\' && xp->xp_pattern[1] != NUL)
2775 ++xp->xp_pattern;
2776 xp->xp_context = EXPAND_NOTHING;
2777 }
2778 else if (c == '\'') /* literal string */
2779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2782 /* skip */ ;
2783 xp->xp_context = EXPAND_NOTHING;
2784 }
2785 else if (c == '|')
2786 {
2787 if (xp->xp_pattern[1] == '|')
2788 {
2789 ++xp->xp_pattern;
2790 xp->xp_context = EXPAND_EXPRESSION;
2791 }
2792 else
2793 xp->xp_context = EXPAND_COMMANDS;
2794 }
2795 else
2796 xp->xp_context = EXPAND_EXPRESSION;
2797 }
2798 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 /* Doesn't look like something valid, expand as an expression
2800 * anyway. */
2801 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 arg = xp->xp_pattern;
2803 if (*arg != NUL)
2804 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2805 /* skip */ ;
2806 }
2807 xp->xp_pattern = arg;
2808}
2809
2810#endif /* FEAT_CMDL_COMPL */
2811
2812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 * ":unlet[!] var1 ... " command.
2814 */
2815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 ex_unletlock(eap, eap->arg, 0);
2819}
2820
2821/*
2822 * ":lockvar" and ":unlockvar" commands
2823 */
2824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002825ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002826{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002828 int deep = 2;
2829
2830 if (eap->forceit)
2831 deep = -1;
2832 else if (vim_isdigit(*arg))
2833 {
2834 deep = getdigits(&arg);
2835 arg = skipwhite(arg);
2836 }
2837
2838 ex_unletlock(eap, arg, deep);
2839}
2840
2841/*
2842 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2843 */
2844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845ex_unletlock(
2846 exarg_T *eap,
2847 char_u *argstart,
2848 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849{
2850 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002853 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 do
2856 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002857 if (*arg == '$')
2858 {
2859 char_u *name = ++arg;
2860
2861 if (get_env_len(&arg) == 0)
2862 {
2863 EMSG2(_(e_invarg2), name - 1);
2864 return;
2865 }
2866 vim_unsetenv(name);
2867 arg = skipwhite(arg);
2868 continue;
2869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002872 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002873 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 if (lv.ll_name == NULL)
2875 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002876 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (name_end != NULL)
2880 {
2881 emsg_severe = TRUE;
2882 EMSG(_(e_trailing));
2883 }
2884 if (!(eap->skip || error))
2885 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 break;
2887 }
2888
2889 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002890 {
2891 if (eap->cmdidx == CMD_unlet)
2892 {
2893 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2894 error = TRUE;
2895 }
2896 else
2897 {
2898 if (do_lock_var(&lv, name_end, deep,
2899 eap->cmdidx == CMD_lockvar) == FAIL)
2900 error = TRUE;
2901 }
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!eap->skip)
2905 clear_lval(&lv);
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 arg = skipwhite(name_end);
2908 } while (!ends_excmd(*arg));
2909
2910 eap->nextcmd = check_nextcmd(arg);
2911}
2912
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914do_unlet_var(
2915 lval_T *lp,
2916 char_u *name_end,
2917 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int ret = OK;
2920 int cc;
2921
2922 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 cc = *name_end;
2925 *name_end = NUL;
2926
2927 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002928 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002932 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002933 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002934 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002935 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 else if (lp->ll_range)
2938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002940 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002941 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002942
2943 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2944 {
2945 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002946 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002947 return FAIL;
2948 ll_li = li;
2949 ++ll_n1;
2950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951
2952 /* Delete a range of List items. */
2953 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2954 {
2955 li = lp->ll_li->li_next;
2956 listitem_remove(lp->ll_list, lp->ll_li);
2957 lp->ll_li = li;
2958 ++lp->ll_n1;
2959 }
2960 }
2961 else
2962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* unlet a List item. */
2965 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 }
2970
2971 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972}
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974/*
2975 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 */
2978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002979do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 hashtab_T *ht;
2982 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002983 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002984 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002985 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 ht = find_var_ht(name, &varname);
2988 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002991 if (d == NULL)
2992 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 if (ht == &globvarht)
2994 d = &globvardict;
2995 else if (ht == &compat_hashtab)
2996 d = &vimvardict;
2997 else
2998 {
2999 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3000 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3001 }
3002 if (d == NULL)
3003 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003004 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 return FAIL;
3006 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003009 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003010 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003011 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003012 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003013 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003014 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003015 || var_check_ro(di->di_flags, name, FALSE)
3016 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003017 return FAIL;
3018
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 delete_var(ht, hi);
3020 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 if (forceit)
3024 return OK;
3025 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 return FAIL;
3027}
3028
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003029/*
3030 * Lock or unlock variable indicated by "lp".
3031 * "deep" is the levels to go (-1 for unlimited);
3032 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3033 */
3034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003035do_lock_var(
3036 lval_T *lp,
3037 char_u *name_end,
3038 int deep,
3039 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003040{
3041 int ret = OK;
3042 int cc;
3043 dictitem_T *di;
3044
3045 if (deep == 0) /* nothing to do */
3046 return OK;
3047
3048 if (lp->ll_tv == NULL)
3049 {
3050 cc = *name_end;
3051 *name_end = NUL;
3052
3053 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003054 di = find_var(lp->ll_name, NULL, TRUE);
3055 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003056 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003057 else if ((di->di_flags & DI_FLAGS_FIX)
3058 && di->di_tv.v_type != VAR_DICT
3059 && di->di_tv.v_type != VAR_LIST)
3060 /* For historic reasons this error is not given for a list or dict.
3061 * E.g., the b: dict could be locked/unlocked. */
3062 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003063 else
3064 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003065 if (lock)
3066 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003067 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003068 di->di_flags &= ~DI_FLAGS_LOCK;
3069 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003070 }
3071 *name_end = cc;
3072 }
3073 else if (lp->ll_range)
3074 {
3075 listitem_T *li = lp->ll_li;
3076
3077 /* (un)lock a range of List items. */
3078 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3079 {
3080 item_lock(&li->li_tv, deep, lock);
3081 li = li->li_next;
3082 ++lp->ll_n1;
3083 }
3084 }
3085 else if (lp->ll_list != NULL)
3086 /* (un)lock a List item. */
3087 item_lock(&lp->ll_li->li_tv, deep, lock);
3088 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003089 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003090 item_lock(&lp->ll_di->di_tv, deep, lock);
3091
3092 return ret;
3093}
3094
3095/*
3096 * Lock or unlock an item. "deep" is nr of levels to go.
3097 */
3098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003100{
3101 static int recurse = 0;
3102 list_T *l;
3103 listitem_T *li;
3104 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003105 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 hashitem_T *hi;
3107 int todo;
3108
3109 if (recurse >= DICT_MAXNEST)
3110 {
3111 EMSG(_("E743: variable nested too deep for (un)lock"));
3112 return;
3113 }
3114 if (deep == 0)
3115 return;
3116 ++recurse;
3117
3118 /* lock/unlock the item itself */
3119 if (lock)
3120 tv->v_lock |= VAR_LOCKED;
3121 else
3122 tv->v_lock &= ~VAR_LOCKED;
3123
3124 switch (tv->v_type)
3125 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003126 case VAR_UNKNOWN:
3127 case VAR_NUMBER:
3128 case VAR_STRING:
3129 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003130 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003131 case VAR_FLOAT:
3132 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003133 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003134 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003135 break;
3136
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003137 case VAR_BLOB:
3138 if ((b = tv->vval.v_blob) != NULL)
3139 {
3140 if (lock)
3141 b->bv_lock |= VAR_LOCKED;
3142 else
3143 b->bv_lock &= ~VAR_LOCKED;
3144 }
3145 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003146 case VAR_LIST:
3147 if ((l = tv->vval.v_list) != NULL)
3148 {
3149 if (lock)
3150 l->lv_lock |= VAR_LOCKED;
3151 else
3152 l->lv_lock &= ~VAR_LOCKED;
3153 if (deep < 0 || deep > 1)
3154 /* recursive: lock/unlock the items the List contains */
3155 for (li = l->lv_first; li != NULL; li = li->li_next)
3156 item_lock(&li->li_tv, deep - 1, lock);
3157 }
3158 break;
3159 case VAR_DICT:
3160 if ((d = tv->vval.v_dict) != NULL)
3161 {
3162 if (lock)
3163 d->dv_lock |= VAR_LOCKED;
3164 else
3165 d->dv_lock &= ~VAR_LOCKED;
3166 if (deep < 0 || deep > 1)
3167 {
3168 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003169 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3171 {
3172 if (!HASHITEM_EMPTY(hi))
3173 {
3174 --todo;
3175 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3176 }
3177 }
3178 }
3179 }
3180 }
3181 --recurse;
3182}
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3185/*
3186 * Delete all "menutrans_" variables.
3187 */
3188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003189del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003192 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003195 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003197 {
3198 if (!HASHITEM_EMPTY(hi))
3199 {
3200 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3202 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003203 }
3204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206}
3207#endif
3208
3209#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3210
3211/*
3212 * Local string buffer for the next two functions to store a variable name
3213 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3214 * get_user_var_name().
3215 */
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217static char_u *varnamebuf = NULL;
3218static int varnamebuflen = 0;
3219
3220/*
3221 * Function to concatenate a prefix and a variable name.
3222 */
3223 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 int len;
3227
3228 len = (int)STRLEN(name) + 3;
3229 if (len > varnamebuflen)
3230 {
3231 vim_free(varnamebuf);
3232 len += 10; /* some additional space */
3233 varnamebuf = alloc(len);
3234 if (varnamebuf == NULL)
3235 {
3236 varnamebuflen = 0;
3237 return NULL;
3238 }
3239 varnamebuflen = len;
3240 }
3241 *varnamebuf = prefix;
3242 varnamebuf[1] = ':';
3243 STRCPY(varnamebuf + 2, name);
3244 return varnamebuf;
3245}
3246
3247/*
3248 * Function given to ExpandGeneric() to obtain the list of user defined
3249 * (global/buffer/window/built-in) variable names.
3250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003254 static long_u gdone;
3255 static long_u bdone;
3256 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003257 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003258 static int vidx;
3259 static hashitem_T *hi;
3260 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003263 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003265 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003266 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003267
3268 /* Global variables */
3269 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003273 else
3274 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 while (HASHITEM_EMPTY(hi))
3276 ++hi;
3277 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3278 return cat_prefix_varname('g', hi->hi_key);
3279 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003281
3282 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003283 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003288 else
3289 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003290 while (HASHITEM_EMPTY(hi))
3291 ++hi;
3292 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003294
3295 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003296 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003299 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003301 else
3302 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003303 while (HASHITEM_EMPTY(hi))
3304 ++hi;
3305 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003307
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003309 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310 if (tdone < ht->ht_used)
3311 {
3312 if (tdone++ == 0)
3313 hi = ht->ht_array;
3314 else
3315 ++hi;
3316 while (HASHITEM_EMPTY(hi))
3317 ++hi;
3318 return cat_prefix_varname('t', hi->hi_key);
3319 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003320
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 /* v: variables */
3322 if (vidx < VV_LEN)
3323 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaard23a8232018-02-10 18:45:26 +01003325 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 varnamebuflen = 0;
3327 return NULL;
3328}
3329
3330#endif /* FEAT_CMDL_COMPL */
3331
3332/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003333 * Return TRUE if "pat" matches "text".
3334 * Does not use 'cpo' and always uses 'magic'.
3335 */
3336 static int
3337pattern_match(char_u *pat, char_u *text, int ic)
3338{
3339 int matches = FALSE;
3340 char_u *save_cpo;
3341 regmatch_T regmatch;
3342
3343 /* avoid 'l' flag in 'cpoptions' */
3344 save_cpo = p_cpo;
3345 p_cpo = (char_u *)"";
3346 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3347 if (regmatch.regprog != NULL)
3348 {
3349 regmatch.rm_ic = ic;
3350 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3351 vim_regfree(regmatch.regprog);
3352 }
3353 p_cpo = save_cpo;
3354 return matches;
3355}
3356
3357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3361 */
3362
3363/*
3364 * Handle zero level expression.
3365 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003367 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Return OK or FAIL.
3369 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371eval0(
3372 char_u *arg,
3373 typval_T *rettv,
3374 char_u **nextcmd,
3375 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 int ret;
3378 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003379 int did_emsg_before = did_emsg;
3380 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (ret == FAIL || !ends_excmd(*p))
3385 {
3386 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 /*
3389 * Report the invalid expression unless the expression evaluation has
3390 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003391 * exception, or we already gave a more specific error.
3392 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003394 if (!aborting() && did_emsg == did_emsg_before
3395 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 EMSG2(_(e_invexpr2), arg);
3397 ret = FAIL;
3398 }
3399 if (nextcmd != NULL)
3400 *nextcmd = check_nextcmd(p);
3401
3402 return ret;
3403}
3404
3405/*
3406 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003407 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 *
3409 * "arg" must point to the first non-white of the expression.
3410 * "arg" is advanced to the next non-white after the recognized expression.
3411 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003412 * Note: "rettv.v_lock" is not set.
3413 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Return OK or FAIL.
3415 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003417eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003420 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /*
3423 * Get the first variable.
3424 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 return FAIL;
3427
3428 if ((*arg)[0] == '?')
3429 {
3430 result = FALSE;
3431 if (evaluate)
3432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 int error = FALSE;
3434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003435 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003438 if (error)
3439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441
3442 /*
3443 * Get the second variable.
3444 */
3445 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return FAIL;
3448
3449 /*
3450 * Check for the ":".
3451 */
3452 if ((*arg)[0] != ':')
3453 {
3454 EMSG(_("E109: Missing ':' after '?'"));
3455 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 return FAIL;
3458 }
3459
3460 /*
3461 * Get the third variable.
3462 */
3463 *arg = skipwhite(*arg + 1);
3464 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3465 {
3466 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return FAIL;
3469 }
3470 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473
3474 return OK;
3475}
3476
3477/*
3478 * Handle first level expression:
3479 * expr2 || expr2 || expr2 logical OR
3480 *
3481 * "arg" must point to the first non-white of the expression.
3482 * "arg" is advanced to the next non-white after the recognized expression.
3483 *
3484 * Return OK or FAIL.
3485 */
3486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003487eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 long result;
3491 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 /*
3495 * Get the first variable.
3496 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return FAIL;
3499
3500 /*
3501 * Repeat until there is no following "||".
3502 */
3503 first = TRUE;
3504 result = FALSE;
3505 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3506 {
3507 if (evaluate && first)
3508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003511 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003512 if (error)
3513 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 first = FALSE;
3515 }
3516
3517 /*
3518 * Get the second variable.
3519 */
3520 *arg = skipwhite(*arg + 2);
3521 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3522 return FAIL;
3523
3524 /*
3525 * Compute the result.
3526 */
3527 if (evaluate && !result)
3528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 if (error)
3533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 if (evaluate)
3536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 rettv->v_type = VAR_NUMBER;
3538 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540 }
3541
3542 return OK;
3543}
3544
3545/*
3546 * Handle second level expression:
3547 * expr3 && expr3 && expr3 logical AND
3548 *
3549 * "arg" must point to the first non-white of the expression.
3550 * "arg" is advanced to the next non-white after the recognized expression.
3551 *
3552 * Return OK or FAIL.
3553 */
3554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 long result;
3559 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 /*
3563 * Get the first variable.
3564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 return FAIL;
3567
3568 /*
3569 * Repeat until there is no following "&&".
3570 */
3571 first = TRUE;
3572 result = TRUE;
3573 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3574 {
3575 if (evaluate && first)
3576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003577 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 if (error)
3581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 first = FALSE;
3583 }
3584
3585 /*
3586 * Get the second variable.
3587 */
3588 *arg = skipwhite(*arg + 2);
3589 if (eval4(arg, &var2, evaluate && result) == FAIL)
3590 return FAIL;
3591
3592 /*
3593 * Compute the result.
3594 */
3595 if (evaluate && result)
3596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 if (error)
3601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 if (evaluate)
3604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003605 rettv->v_type = VAR_NUMBER;
3606 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 }
3609
3610 return OK;
3611}
3612
3613/*
3614 * Handle third level expression:
3615 * var1 == var2
3616 * var1 =~ var2
3617 * var1 != var2
3618 * var1 !~ var2
3619 * var1 > var2
3620 * var1 >= var2
3621 * var1 < var2
3622 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003623 * var1 is var2
3624 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 *
3626 * "arg" must point to the first non-white of the expression.
3627 * "arg" is advanced to the next non-white after the recognized expression.
3628 *
3629 * Return OK or FAIL.
3630 */
3631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 char_u *p;
3636 int i;
3637 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003638 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
3643 * Get the first variable.
3644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003645 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return FAIL;
3647
3648 p = *arg;
3649 switch (p[0])
3650 {
3651 case '=': if (p[1] == '=')
3652 type = TYPE_EQUAL;
3653 else if (p[1] == '~')
3654 type = TYPE_MATCH;
3655 break;
3656 case '!': if (p[1] == '=')
3657 type = TYPE_NEQUAL;
3658 else if (p[1] == '~')
3659 type = TYPE_NOMATCH;
3660 break;
3661 case '>': if (p[1] != '=')
3662 {
3663 type = TYPE_GREATER;
3664 len = 1;
3665 }
3666 else
3667 type = TYPE_GEQUAL;
3668 break;
3669 case '<': if (p[1] != '=')
3670 {
3671 type = TYPE_SMALLER;
3672 len = 1;
3673 }
3674 else
3675 type = TYPE_SEQUAL;
3676 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003677 case 'i': if (p[1] == 's')
3678 {
3679 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3680 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003681 i = p[len];
3682 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003683 {
3684 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3685 type_is = TRUE;
3686 }
3687 }
3688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003692 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
3694 if (type != TYPE_UNKNOWN)
3695 {
3696 /* extra question mark appended: ignore case */
3697 if (p[len] == '?')
3698 {
3699 ic = TRUE;
3700 ++len;
3701 }
3702 /* extra '#' appended: match case */
3703 else if (p[len] == '#')
3704 {
3705 ic = FALSE;
3706 ++len;
3707 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003708 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else
3710 ic = p_ic;
3711
3712 /*
3713 * Get the second variable.
3714 */
3715 *arg = skipwhite(p + len);
3716 if (eval5(arg, &var2, evaluate) == FAIL)
3717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003721 if (evaluate)
3722 {
3723 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3724
3725 clear_tv(&var2);
3726 return ret;
3727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
3730 return OK;
3731}
3732
3733/*
3734 * Handle fourth level expression:
3735 * + number addition
3736 * - number subtraction
3737 * . string concatenation
3738 *
3739 * "arg" must point to the first non-white of the expression.
3740 * "arg" is advanced to the next non-white after the recognized expression.
3741 *
3742 * Return OK or FAIL.
3743 */
3744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 typval_T var2;
3748 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003750 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003751#ifdef FEAT_FLOAT
3752 float_T f1 = 0, f2 = 0;
3753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 char_u *s1, *s2;
3755 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3756 char_u *p;
3757
3758 /*
3759 * Get the first variable.
3760 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003761 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return FAIL;
3763
3764 /*
3765 * Repeat computing, until no '+', '-' or '.' is following.
3766 */
3767 for (;;)
3768 {
3769 op = **arg;
3770 if (op != '+' && op != '-' && op != '.')
3771 break;
3772
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003773 if ((op != '+' || (rettv->v_type != VAR_LIST
3774 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775#ifdef FEAT_FLOAT
3776 && (op == '.' || rettv->v_type != VAR_FLOAT)
3777#endif
3778 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 {
3780 /* For "list + ...", an illegal use of the first operand as
3781 * a number cannot be determined before evaluating the 2nd
3782 * operand: if this is also a list, all is ok.
3783 * For "something . ...", "something - ..." or "non-list + ...",
3784 * we know that the first operand needs to be a string or number
3785 * without evaluating the 2nd operand. So check before to avoid
3786 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 {
3789 clear_tv(rettv);
3790 return FAIL;
3791 }
3792 }
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 /*
3795 * Get the second variable.
3796 */
3797 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003798 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802 }
3803
3804 if (evaluate)
3805 {
3806 /*
3807 * Compute the result.
3808 */
3809 if (op == '.')
3810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003811 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3812 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 if (s2 == NULL) /* type error ? */
3814 {
3815 clear_tv(rettv);
3816 clear_tv(&var2);
3817 return FAIL;
3818 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003819 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
3821 rettv->v_type = VAR_STRING;
3822 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003824 else if (op == '+' && rettv->v_type == VAR_BLOB
3825 && var2.v_type == VAR_BLOB)
3826 {
3827 blob_T *b1 = rettv->vval.v_blob;
3828 blob_T *b2 = var2.vval.v_blob;
3829 blob_T *b = blob_alloc();
3830 int i;
3831
3832 if (b != NULL)
3833 {
3834 for (i = 0; i < blob_len(b1); i++)
3835 ga_append(&b->bv_ga, blob_get(b1, i));
3836 for (i = 0; i < blob_len(b2); i++)
3837 ga_append(&b->bv_ga, blob_get(b2, i));
3838
3839 clear_tv(rettv);
3840 rettv_blob_set(rettv, b);
3841 }
3842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003843 else if (op == '+' && rettv->v_type == VAR_LIST
3844 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003845 {
3846 /* concatenate Lists */
3847 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3848 &var3) == FAIL)
3849 {
3850 clear_tv(rettv);
3851 clear_tv(&var2);
3852 return FAIL;
3853 }
3854 clear_tv(rettv);
3855 *rettv = var3;
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else
3858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 int error = FALSE;
3860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861#ifdef FEAT_FLOAT
3862 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864 f1 = rettv->vval.v_float;
3865 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 else
3868#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003870 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 if (error)
3872 {
3873 /* This can only happen for "list + non-list". For
3874 * "non-list + ..." or "something - ...", we returned
3875 * before evaluating the 2nd operand. */
3876 clear_tv(rettv);
3877 return FAIL;
3878 }
3879#ifdef FEAT_FLOAT
3880 if (var2.v_type == VAR_FLOAT)
3881 f1 = n1;
3882#endif
3883 }
3884#ifdef FEAT_FLOAT
3885 if (var2.v_type == VAR_FLOAT)
3886 {
3887 f2 = var2.vval.v_float;
3888 n2 = 0;
3889 }
3890 else
3891#endif
3892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003894 if (error)
3895 {
3896 clear_tv(rettv);
3897 clear_tv(&var2);
3898 return FAIL;
3899 }
3900#ifdef FEAT_FLOAT
3901 if (rettv->v_type == VAR_FLOAT)
3902 f2 = n2;
3903#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003906
3907#ifdef FEAT_FLOAT
3908 /* If there is a float on either side the result is a float. */
3909 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3910 {
3911 if (op == '+')
3912 f1 = f1 + f2;
3913 else
3914 f1 = f1 - f2;
3915 rettv->v_type = VAR_FLOAT;
3916 rettv->vval.v_float = f1;
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003919#endif
3920 {
3921 if (op == '+')
3922 n1 = n1 + n2;
3923 else
3924 n1 = n1 - n2;
3925 rettv->v_type = VAR_NUMBER;
3926 rettv->vval.v_number = n1;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003929 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 }
3932 return OK;
3933}
3934
3935/*
3936 * Handle fifth level expression:
3937 * * number multiplication
3938 * / number division
3939 * % number modulo
3940 *
3941 * "arg" must point to the first non-white of the expression.
3942 * "arg" is advanced to the next non-white after the recognized expression.
3943 *
3944 * Return OK or FAIL.
3945 */
3946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003947eval6(
3948 char_u **arg,
3949 typval_T *rettv,
3950 int evaluate,
3951 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003955 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003956#ifdef FEAT_FLOAT
3957 int use_float = FALSE;
3958 float_T f1 = 0, f2;
3959#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003960 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /*
3963 * Get the first variable.
3964 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003965 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FAIL;
3967
3968 /*
3969 * Repeat computing, until no '*', '/' or '%' is following.
3970 */
3971 for (;;)
3972 {
3973 op = **arg;
3974 if (op != '*' && op != '/' && op != '%')
3975 break;
3976
3977 if (evaluate)
3978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003979#ifdef FEAT_FLOAT
3980 if (rettv->v_type == VAR_FLOAT)
3981 {
3982 f1 = rettv->vval.v_float;
3983 use_float = TRUE;
3984 n1 = 0;
3985 }
3986 else
3987#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003988 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 if (error)
3991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
3994 n1 = 0;
3995
3996 /*
3997 * Get the second variable.
3998 */
3999 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004000 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return FAIL;
4002
4003 if (evaluate)
4004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004005#ifdef FEAT_FLOAT
4006 if (var2.v_type == VAR_FLOAT)
4007 {
4008 if (!use_float)
4009 {
4010 f1 = n1;
4011 use_float = TRUE;
4012 }
4013 f2 = var2.vval.v_float;
4014 n2 = 0;
4015 }
4016 else
4017#endif
4018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004020 clear_tv(&var2);
4021 if (error)
4022 return FAIL;
4023#ifdef FEAT_FLOAT
4024 if (use_float)
4025 f2 = n2;
4026#endif
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
4030 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004031 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004033#ifdef FEAT_FLOAT
4034 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004036 if (op == '*')
4037 f1 = f1 * f2;
4038 else if (op == '/')
4039 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004040# ifdef VMS
4041 /* VMS crashes on divide by zero, work around it */
4042 if (f2 == 0.0)
4043 {
4044 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004045 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004047 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004049 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004050 }
4051 else
4052 f1 = f1 / f2;
4053# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004054 /* We rely on the floating point library to handle divide
4055 * by zero to result in "inf" and not a crash. */
4056 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004057# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004061 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 return FAIL;
4063 }
4064 rettv->v_type = VAR_FLOAT;
4065 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070 if (op == '*')
4071 n1 = n1 * n2;
4072 else if (op == '/')
4073 {
4074 if (n2 == 0) /* give an error message? */
4075 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004076 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004077 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004078 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004079 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004080 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004081 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 }
4083 else
4084 n1 = n1 / n2;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 {
4088 if (n2 == 0) /* give an error message? */
4089 n1 = 0;
4090 else
4091 n1 = n1 % n2;
4092 }
4093 rettv->v_type = VAR_NUMBER;
4094 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 }
4098
4099 return OK;
4100}
4101
4102/*
4103 * Handle sixth level expression:
4104 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004105 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004106 * "string" string constant
4107 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * &option-name option value
4109 * @r register contents
4110 * identifier variable value
4111 * function() function call
4112 * $VAR environment variable
4113 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004114 * [expr, expr] List
4115 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * Also handle:
4118 * ! in front logical NOT
4119 * - in front unary minus
4120 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 * trailing [] subscript in String or List
4122 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * "arg" must point to the first non-white of the expression.
4125 * "arg" is advanced to the next non-white after the recognized expression.
4126 *
4127 * Return OK or FAIL.
4128 */
4129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004130eval7(
4131 char_u **arg,
4132 typval_T *rettv,
4133 int evaluate,
4134 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004136 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 int len;
4138 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u *start_leader, *end_leader;
4140 int ret = OK;
4141 char_u *alias;
4142
4143 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004150 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 */
4152 start_leader = *arg;
4153 while (**arg == '!' || **arg == '-' || **arg == '+')
4154 *arg = skipwhite(*arg + 1);
4155 end_leader = *arg;
4156
4157 switch (**arg)
4158 {
4159 /*
4160 * Number constant.
4161 */
4162 case '0':
4163 case '1':
4164 case '2':
4165 case '3':
4166 case '4':
4167 case '5':
4168 case '6':
4169 case '7':
4170 case '8':
4171 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004172 {
4173#ifdef FEAT_FLOAT
4174 char_u *p = skipdigits(*arg + 1);
4175 int get_float = FALSE;
4176
4177 /* We accept a float when the format matches
4178 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004179 * strict to avoid backwards compatibility problems.
4180 * Don't look for a float after the "." operator, so that
4181 * ":let vers = 1.2.3" doesn't fail. */
4182 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004184 get_float = TRUE;
4185 p = skipdigits(p + 2);
4186 if (*p == 'e' || *p == 'E')
4187 {
4188 ++p;
4189 if (*p == '-' || *p == '+')
4190 ++p;
4191 if (!vim_isdigit(*p))
4192 get_float = FALSE;
4193 else
4194 p = skipdigits(p + 1);
4195 }
4196 if (ASCII_ISALPHA(*p) || *p == '.')
4197 get_float = FALSE;
4198 }
4199 if (get_float)
4200 {
4201 float_T f;
4202
4203 *arg += string2float(*arg, &f);
4204 if (evaluate)
4205 {
4206 rettv->v_type = VAR_FLOAT;
4207 rettv->vval.v_float = f;
4208 }
4209 }
4210 else
4211#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004212 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004213 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004214 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004215 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004216
4217 // Blob constant: 0z0123456789abcdef
4218 if (evaluate)
4219 blob = blob_alloc();
4220 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4221 {
4222 if (!vim_isxdigit(bp[1]))
4223 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004224 if (blob != NULL)
4225 {
4226 EMSG(_("E973: Blob literal should have an even number of hex characters"));
4227 ga_clear(&blob->bv_ga);
4228 VIM_CLEAR(blob);
4229 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004230 ret = FAIL;
4231 break;
4232 }
4233 if (blob != NULL)
4234 ga_append(&blob->bv_ga,
4235 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4236 }
4237 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004238 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004239 *arg = bp;
4240 }
4241 else
4242 {
4243 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004244 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 *arg += len;
4246 if (evaluate)
4247 {
4248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = n;
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /*
4256 * String constant: "string".
4257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 break;
4260
4261 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 break;
4266
4267 /*
4268 * List: [expr, expr]
4269 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272
4273 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004274 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004275 * Dictionary: {key: val, key: val}
4276 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004277 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4278 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004279 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004280 break;
4281
4282 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004283 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004285 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 break;
4287
4288 /*
4289 * Environment variable: $VAR.
4290 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293
4294 /*
4295 * Register contents: @r.
4296 */
4297 case '@': ++*arg;
4298 if (evaluate)
4299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004301 rettv->vval.v_string = get_reg_contents(**arg,
4302 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 if (**arg != NUL)
4305 ++*arg;
4306 break;
4307
4308 /*
4309 * nested expression: (expression).
4310 */
4311 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (**arg == ')')
4314 ++*arg;
4315 else if (ret == OK)
4316 {
4317 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ret = FAIL;
4320 }
4321 break;
4322
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326
4327 if (ret == NOTDONE)
4328 {
4329 /*
4330 * Must be a variable or function name.
4331 * Can also be a curly-braces kind of name: {expr}.
4332 */
4333 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004334 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335 if (alias != NULL)
4336 s = alias;
4337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004338 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 ret = FAIL;
4340 else
4341 {
4342 if (**arg == '(') /* recursive! */
4343 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004344 partial_T *partial;
4345
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004346 if (!evaluate)
4347 check_vars(s, len);
4348
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 /* If "s" is the name of a variable of type VAR_FUNC
4350 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004351 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004353 /* Need to make a copy, in case evaluating the arguments makes
4354 * the name invalid. */
4355 s = vim_strsave(s);
4356 if (s == NULL)
4357 ret = FAIL;
4358 else
4359 /* Invoke the function. */
4360 ret = get_func_tv(s, len, rettv, arg,
4361 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4362 &len, evaluate, partial, NULL);
4363 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004364
4365 /* If evaluate is FALSE rettv->v_type was not set in
4366 * get_func_tv, but it's needed in handle_subscript() to parse
4367 * what follows. So set it here. */
4368 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4369 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004370 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004371 rettv->v_type = VAR_FUNC;
4372 }
4373
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 /* Stop the expression evaluation when immediately
4375 * aborting on error, or when an interrupt occurred or
4376 * an exception was thrown but not caught. */
4377 if (aborting())
4378 {
4379 if (ret == OK)
4380 clear_tv(rettv);
4381 ret = FAIL;
4382 }
4383 }
4384 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004385 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004386 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004387 {
4388 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004389 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004391 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004392 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393 }
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 *arg = skipwhite(*arg);
4396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004397 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4398 * expr(expr). */
4399 if (ret == OK)
4400 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /*
4403 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4404 */
4405 if (ret == OK && evaluate && end_leader > start_leader)
4406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004408 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004409#ifdef FEAT_FLOAT
4410 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412 if (rettv->v_type == VAR_FLOAT)
4413 f = rettv->vval.v_float;
4414 else
4415#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004416 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 clear_tv(rettv);
4420 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 else
4423 {
4424 while (end_leader > start_leader)
4425 {
4426 --end_leader;
4427 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428 {
4429#ifdef FEAT_FLOAT
4430 if (rettv->v_type == VAR_FLOAT)
4431 f = !f;
4432 else
4433#endif
4434 val = !val;
4435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004436 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 {
4438#ifdef FEAT_FLOAT
4439 if (rettv->v_type == VAR_FLOAT)
4440 f = -f;
4441 else
4442#endif
4443 val = -val;
4444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004446#ifdef FEAT_FLOAT
4447 if (rettv->v_type == VAR_FLOAT)
4448 {
4449 clear_tv(rettv);
4450 rettv->vval.v_float = f;
4451 }
4452 else
4453#endif
4454 {
4455 clear_tv(rettv);
4456 rettv->v_type = VAR_NUMBER;
4457 rettv->vval.v_number = val;
4458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 return ret;
4463}
4464
4465/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004466 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4467 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4469 */
4470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004471eval_index(
4472 char_u **arg,
4473 typval_T *rettv,
4474 int evaluate,
4475 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476{
4477 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004479 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 long len = -1;
4482 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485
Bram Moolenaara03f2332016-02-06 18:09:59 +01004486 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004488 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004489 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004490 if (verbose)
4491 EMSG(_("E695: Cannot index a Funcref"));
4492 return FAIL;
4493 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004494#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004495 if (verbose)
4496 EMSG(_(e_float_as_string));
4497 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004498#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004499 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004500 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004501 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004502 if (verbose)
4503 EMSG(_("E909: Cannot index a special variable"));
4504 return FAIL;
4505 case VAR_UNKNOWN:
4506 if (evaluate)
4507 return FAIL;
4508 /* FALLTHROUGH */
4509
4510 case VAR_STRING:
4511 case VAR_NUMBER:
4512 case VAR_LIST:
4513 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004514 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004518 init_tv(&var1);
4519 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 /*
4523 * dict.name
4524 */
4525 key = *arg + 1;
4526 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4527 ;
4528 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 }
4532 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 /*
4535 * something[idx]
4536 *
4537 * Get the (first) variable from inside the [].
4538 */
4539 *arg = skipwhite(*arg + 1);
4540 if (**arg == ':')
4541 empty1 = TRUE;
4542 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4543 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004544 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 {
4546 /* not a number or string */
4547 clear_tv(&var1);
4548 return FAIL;
4549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550
4551 /*
4552 * Get the second variable from inside the [:].
4553 */
4554 if (**arg == ':')
4555 {
4556 range = TRUE;
4557 *arg = skipwhite(*arg + 1);
4558 if (**arg == ']')
4559 empty2 = TRUE;
4560 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 if (!empty1)
4563 clear_tv(&var1);
4564 return FAIL;
4565 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004566 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 {
4568 /* not a number or string */
4569 if (!empty1)
4570 clear_tv(&var1);
4571 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 return FAIL;
4573 }
4574 }
4575
4576 /* Check for the ']'. */
4577 if (**arg != ']')
4578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004579 if (verbose)
4580 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 clear_tv(&var1);
4582 if (range)
4583 clear_tv(&var2);
4584 return FAIL;
4585 }
4586 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 }
4588
4589 if (evaluate)
4590 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 n1 = 0;
4592 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 }
4597 if (range)
4598 {
4599 if (empty2)
4600 n2 = -1;
4601 else
4602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004603 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 }
4606 }
4607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004610 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004611 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004612 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004613 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004614 case VAR_SPECIAL:
4615 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004616 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004617 break; /* not evaluating, skipping over subscript */
4618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 case VAR_NUMBER:
4620 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 len = (long)STRLEN(s);
4623 if (range)
4624 {
4625 /* The resulting variable is a substring. If the indexes
4626 * are out of range the result is empty. */
4627 if (n1 < 0)
4628 {
4629 n1 = len + n1;
4630 if (n1 < 0)
4631 n1 = 0;
4632 }
4633 if (n2 < 0)
4634 n2 = len + n2;
4635 else if (n2 >= len)
4636 n2 = len;
4637 if (n1 >= len || n2 < 0 || n1 > n2)
4638 s = NULL;
4639 else
4640 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4641 }
4642 else
4643 {
4644 /* The resulting variable is a string of a single
4645 * character. If the index is too big or negative the
4646 * result is empty. */
4647 if (n1 >= len || n1 < 0)
4648 s = NULL;
4649 else
4650 s = vim_strnsave(s + n1, 1);
4651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(rettv);
4653 rettv->v_type = VAR_STRING;
4654 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 break;
4656
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004657 case VAR_BLOB:
4658 len = blob_len(rettv->vval.v_blob);
4659 if (range)
4660 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004661 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004662 // are out of range the result is empty.
4663 if (n1 < 0)
4664 {
4665 n1 = len + n1;
4666 if (n1 < 0)
4667 n1 = 0;
4668 }
4669 if (n2 < 0)
4670 n2 = len + n2;
4671 else if (n2 >= len)
4672 n2 = len - 1;
4673 if (n1 >= len || n2 < 0 || n1 > n2)
4674 {
4675 clear_tv(rettv);
4676 rettv->v_type = VAR_BLOB;
4677 rettv->vval.v_blob = NULL;
4678 }
4679 else
4680 {
4681 blob_T *blob = blob_alloc();
4682
4683 if (blob != NULL)
4684 {
4685 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4686 {
4687 blob_free(blob);
4688 return FAIL;
4689 }
4690 blob->bv_ga.ga_len = n2 - n1 + 1;
4691 for (i = n1; i <= n2; i++)
4692 blob_set(blob, i - n1,
4693 blob_get(rettv->vval.v_blob, i));
4694
4695 clear_tv(rettv);
4696 rettv_blob_set(rettv, blob);
4697 }
4698 }
4699 }
4700 else
4701 {
4702 // The resulting variable is a string of a single
4703 // character. If the index is too big or negative the
4704 // result is empty.
4705 if (n1 < len && n1 >= 0)
4706 {
4707 int v = (int)blob_get(rettv->vval.v_blob, n1);
4708
4709 clear_tv(rettv);
4710 rettv->v_type = VAR_NUMBER;
4711 rettv->vval.v_number = v;
4712 }
4713 else
4714 EMSGN(_(e_blobidx), n1);
4715 }
4716 break;
4717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 if (n1 < 0)
4721 n1 = len + n1;
4722 if (!empty1 && (n1 < 0 || n1 >= len))
4723 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004724 /* For a range we allow invalid values and return an empty
4725 * list. A list index out of range is an error. */
4726 if (!range)
4727 {
4728 if (verbose)
4729 EMSGN(_(e_listidx), n1);
4730 return FAIL;
4731 }
4732 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 }
4734 if (range)
4735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 list_T *l;
4737 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738
4739 if (n2 < 0)
4740 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004741 else if (n2 >= len)
4742 n2 = len - 1;
4743 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004744 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 l = list_alloc();
4746 if (l == NULL)
4747 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 n1 <= n2; ++n1)
4750 {
4751 if (list_append_tv(l, &item->li_tv) == FAIL)
4752 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004753 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 return FAIL;
4755 }
4756 item = item->li_next;
4757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004759 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761 else
4762 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004763 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 }
4767 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768
4769 case VAR_DICT:
4770 if (range)
4771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 if (verbose)
4773 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (len == -1)
4775 clear_tv(&var1);
4776 return FAIL;
4777 }
4778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780
4781 if (len == -1)
4782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004784 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 clear_tv(&var1);
4787 return FAIL;
4788 }
4789 }
4790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004791 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004793 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795 if (len == -1)
4796 clear_tv(&var1);
4797 if (item == NULL)
4798 return FAIL;
4799
4800 copy_tv(&item->di_tv, &var1);
4801 clear_tv(rettv);
4802 *rettv = var1;
4803 }
4804 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806 }
4807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 return OK;
4809}
4810
4811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 * Get an option value.
4813 * "arg" points to the '&' or '+' before the option name.
4814 * "arg" is advanced to character after the option name.
4815 * Return OK or FAIL.
4816 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004818get_option_tv(
4819 char_u **arg,
4820 typval_T *rettv, /* when NULL, only check if option exists */
4821 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 char_u *option_end;
4824 long numval;
4825 char_u *stringval;
4826 int opt_type;
4827 int c;
4828 int working = (**arg == '+'); /* has("+option") */
4829 int ret = OK;
4830 int opt_flags;
4831
4832 /*
4833 * Isolate the option name and find its value.
4834 */
4835 option_end = find_option_end(arg, &opt_flags);
4836 if (option_end == NULL)
4837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 EMSG2(_("E112: Option name missing: %s"), *arg);
4840 return FAIL;
4841 }
4842
4843 if (!evaluate)
4844 {
4845 *arg = option_end;
4846 return OK;
4847 }
4848
4849 c = *option_end;
4850 *option_end = NUL;
4851 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 if (opt_type == -3) /* invalid name */
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 EMSG2(_("E113: Unknown option: %s"), *arg);
4858 ret = FAIL;
4859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 if (opt_type == -2) /* hidden string option */
4863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 rettv->v_type = VAR_STRING;
4865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 else if (opt_type == -1) /* hidden number option */
4868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 rettv->v_type = VAR_NUMBER;
4870 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 else if (opt_type == 1) /* number option */
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 rettv->v_type = VAR_NUMBER;
4875 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 else /* string option */
4878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 rettv->v_type = VAR_STRING;
4880 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 }
4883 else if (working && (opt_type == -2 || opt_type == -1))
4884 ret = FAIL;
4885
4886 *option_end = c; /* put back for error messages */
4887 *arg = option_end;
4888
4889 return ret;
4890}
4891
4892/*
4893 * Allocate a variable for a string constant.
4894 * Return OK or FAIL.
4895 */
4896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004897get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 char_u *p;
4900 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int extra = 0;
4902
4903 /*
4904 * Find the end of the string, skipping backslashed characters.
4905 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004906 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 if (*p == '\\' && p[1] != NUL)
4909 {
4910 ++p;
4911 /* A "\<x>" form occupies at least 4 characters, and produces up
4912 * to 6 characters: reserve space for 2 extra */
4913 if (*p == '<')
4914 extra += 2;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917
4918 if (*p != '"')
4919 {
4920 EMSG2(_("E114: Missing quote: %s"), *arg);
4921 return FAIL;
4922 }
4923
4924 /* If only parsing, set *arg and return here */
4925 if (!evaluate)
4926 {
4927 *arg = p + 1;
4928 return OK;
4929 }
4930
4931 /*
4932 * Copy the string into allocated memory, handling backslashed
4933 * characters.
4934 */
4935 name = alloc((unsigned)(p - *arg + extra));
4936 if (name == NULL)
4937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 rettv->v_type = VAR_STRING;
4939 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 if (*p == '\\')
4944 {
4945 switch (*++p)
4946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 case 'b': *name++ = BS; ++p; break;
4948 case 'e': *name++ = ESC; ++p; break;
4949 case 'f': *name++ = FF; ++p; break;
4950 case 'n': *name++ = NL; ++p; break;
4951 case 'r': *name++ = CAR; ++p; break;
4952 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 case 'X': /* hex: "\x1", "\x12" */
4955 case 'x':
4956 case 'u': /* Unicode: "\u0023" */
4957 case 'U':
4958 if (vim_isxdigit(p[1]))
4959 {
4960 int n, nr;
4961 int c = toupper(*p);
4962
4963 if (c == 'X')
4964 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004965 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004967 else
4968 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 nr = 0;
4970 while (--n >= 0 && vim_isxdigit(p[1]))
4971 {
4972 ++p;
4973 nr = (nr << 4) + hex2nr(*p);
4974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#ifdef FEAT_MBYTE
4977 /* For "\u" store the number according to
4978 * 'encoding'. */
4979 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
4982#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
4986
4987 /* octal: "\1", "\12", "\123" */
4988 case '0':
4989 case '1':
4990 case '2':
4991 case '3':
4992 case '4':
4993 case '5':
4994 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 case '7': *name = *p++ - '0';
4996 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 *name = (*name << 3) + *p++ - '0';
4999 if (*p >= '0' && *p <= '7')
5000 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004
5005 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005006 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (extra != 0)
5008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 break;
5011 }
5012 /* FALLTHROUGH */
5013
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 break;
5016 }
5017 }
5018 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005023 if (*p != NUL) /* just in case */
5024 ++p;
5025 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 return OK;
5028}
5029
5030/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * Return OK or FAIL.
5033 */
5034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005035get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005038 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 int reduce = 0;
5040
5041 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005044 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
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 ++reduce;
5051 ++p;
5052 }
5053 }
5054
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 return FAIL;
5059 }
5060
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 if (!evaluate)
5063 {
5064 *arg = p + 1;
5065 return OK;
5066 }
5067
5068 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 */
5071 str = alloc((unsigned)((p - *arg) - reduce));
5072 if (str == NULL)
5073 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 rettv->v_type = VAR_STRING;
5075 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 break;
5083 ++p;
5084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 *arg = p + 1;
5089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 return OK;
5091}
5092
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005093/*
5094 * Return the function name of the partial.
5095 */
5096 char_u *
5097partial_name(partial_T *pt)
5098{
5099 if (pt->pt_name != NULL)
5100 return pt->pt_name;
5101 return pt->pt_func->uf_name;
5102}
5103
Bram Moolenaarddecc252016-04-06 22:59:37 +02005104 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005106{
5107 int i;
5108
5109 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005111 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005113 if (pt->pt_name != NULL)
5114 {
5115 func_unref(pt->pt_name);
5116 vim_free(pt->pt_name);
5117 }
5118 else
5119 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005120 vim_free(pt);
5121}
5122
5123/*
5124 * Unreference a closure: decrement the reference count and free it when it
5125 * becomes zero.
5126 */
5127 void
5128partial_unref(partial_T *pt)
5129{
5130 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005131 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005132}
5133
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005134static int tv_equal_recurse_limit;
5135
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005136 static int
5137func_equal(
5138 typval_T *tv1,
5139 typval_T *tv2,
5140 int ic) /* ignore case */
5141{
5142 char_u *s1, *s2;
5143 dict_T *d1, *d2;
5144 int a1, a2;
5145 int i;
5146
5147 /* empty and NULL function name considered the same */
5148 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005149 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005150 if (s1 != NULL && *s1 == NUL)
5151 s1 = NULL;
5152 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005153 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005154 if (s2 != NULL && *s2 == NUL)
5155 s2 = NULL;
5156 if (s1 == NULL || s2 == NULL)
5157 {
5158 if (s1 != s2)
5159 return FALSE;
5160 }
5161 else if (STRCMP(s1, s2) != 0)
5162 return FALSE;
5163
5164 /* empty dict and NULL dict is different */
5165 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5166 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5167 if (d1 == NULL || d2 == NULL)
5168 {
5169 if (d1 != d2)
5170 return FALSE;
5171 }
5172 else if (!dict_equal(d1, d2, ic, TRUE))
5173 return FALSE;
5174
5175 /* empty list and no list considered the same */
5176 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5177 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5178 if (a1 != a2)
5179 return FALSE;
5180 for (i = 0; i < a1; ++i)
5181 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5182 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5183 return FALSE;
5184
5185 return TRUE;
5186}
5187
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005188/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005190 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005191 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005194tv_equal(
5195 typval_T *tv1,
5196 typval_T *tv2,
5197 int ic, /* ignore case */
5198 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005199{
5200 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005202 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005203 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005204
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005205 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005206 * recursiveness to a limit. We guess they are equal then.
5207 * A fixed limit has the problem of still taking an awful long time.
5208 * Reduce the limit every time running into it. That should work fine for
5209 * deeply linked structures that are not recursively linked and catch
5210 * recursiveness quickly. */
5211 if (!recursive)
5212 tv_equal_recurse_limit = 1000;
5213 if (recursive_cnt >= tv_equal_recurse_limit)
5214 {
5215 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005216 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005217 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005218
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005219 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5220 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005221 if ((tv1->v_type == VAR_FUNC
5222 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5223 && (tv2->v_type == VAR_FUNC
5224 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5225 {
5226 ++recursive_cnt;
5227 r = func_equal(tv1, tv2, ic);
5228 --recursive_cnt;
5229 return r;
5230 }
5231
5232 if (tv1->v_type != tv2->v_type)
5233 return FALSE;
5234
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005235 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005236 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005237 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005238 ++recursive_cnt;
5239 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005242
5243 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005244 ++recursive_cnt;
5245 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5246 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005247 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005248
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005249 case VAR_BLOB:
5250 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5251
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005252 case VAR_NUMBER:
5253 return tv1->vval.v_number == tv2->vval.v_number;
5254
5255 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005256 s1 = tv_get_string_buf(tv1, buf1);
5257 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005258 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005259
5260 case VAR_SPECIAL:
5261 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005262
5263 case VAR_FLOAT:
5264#ifdef FEAT_FLOAT
5265 return tv1->vval.v_float == tv2->vval.v_float;
5266#endif
5267 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005269 return tv1->vval.v_job == tv2->vval.v_job;
5270#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005273 return tv1->vval.v_channel == tv2->vval.v_channel;
5274#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005275 case VAR_FUNC:
5276 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005277 case VAR_UNKNOWN:
5278 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005279 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005280
Bram Moolenaara03f2332016-02-06 18:09:59 +01005281 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5282 * does not equal anything, not even itself. */
5283 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005284}
5285
5286/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005287 * Return the next (unique) copy ID.
5288 * Used for serializing nested structures.
5289 */
5290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005292{
5293 current_copyID += COPYID_INC;
5294 return current_copyID;
5295}
5296
5297/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298 * Garbage collection for lists and dictionaries.
5299 *
5300 * We use reference counts to be able to free most items right away when they
5301 * are no longer used. But for composite items it's possible that it becomes
5302 * unused while the reference count is > 0: When there is a recursive
5303 * reference. Example:
5304 * :let l = [1, 2, 3]
5305 * :let d = {9: l}
5306 * :let l[1] = d
5307 *
5308 * Since this is quite unusual we handle this with garbage collection: every
5309 * once in a while find out which lists and dicts are not referenced from any
5310 * variable.
5311 *
5312 * Here is a good reference text about garbage collection (refers to Python
5313 * but it applies to all reference-counting mechanisms):
5314 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005316
5317/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005318 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005319 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005321 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005323garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005324{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005325 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327 buf_T *buf;
5328 win_T *wp;
5329 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005330 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005331 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005332
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005333 if (!testing)
5334 {
5335 /* Only do this once. */
5336 want_garbage_collect = FALSE;
5337 may_garbage_collect = FALSE;
5338 garbage_collect_at_exit = FALSE;
5339 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005340
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005341 /* We advance by two because we add one for items referenced through
5342 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005343 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345 /*
5346 * 1. Go through all accessible variables and mark all lists and dicts
5347 * with copyID.
5348 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005349
5350 /* Don't free variables in the previous_funccal list unless they are only
5351 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005352 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005354
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355 /* script-local variables */
5356 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005358
5359 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005360 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005361 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5362 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005363
5364 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5367 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005368 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005369 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5370 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005372 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005373 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5375 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005377 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005378
5379 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005382 /* named functions (matters for closures) */
5383 abort = abort || set_ref_in_functions(copyID);
5384
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005385 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005387
Bram Moolenaard812df62008-11-09 12:46:09 +00005388 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005390
Bram Moolenaar1dced572012-04-05 16:54:08 +02005391#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005392 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005393#endif
5394
Bram Moolenaardb913952012-06-29 12:54:53 +02005395#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005397#endif
5398
5399#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005400 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005401#endif
5402
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005403#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005404 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005405 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005406#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005407#ifdef FEAT_NETBEANS_INTG
5408 abort = abort || set_ref_in_nb_channel(copyID);
5409#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005410
Bram Moolenaare3188e22016-05-31 21:13:04 +02005411#ifdef FEAT_TIMERS
5412 abort = abort || set_ref_in_timer(copyID);
5413#endif
5414
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005415#ifdef FEAT_QUICKFIX
5416 abort = abort || set_ref_in_quickfix(copyID);
5417#endif
5418
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005419#ifdef FEAT_TERMINAL
5420 abort = abort || set_ref_in_term(copyID);
5421#endif
5422
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005424 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 /*
5426 * 2. Free lists and dictionaries that are not referenced.
5427 */
5428 did_free = free_unref_items(copyID);
5429
5430 /*
5431 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005432 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005435 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005436 else if (p_verbose > 0)
5437 {
5438 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5439 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005440
5441 return did_free;
5442}
5443
5444/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005445 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005446 */
5447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005448free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005449{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005450 int did_free = FALSE;
5451
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 /* Let all "free" functions know that we are here. This means no
5453 * dictionaries, lists, channels or jobs are to be freed, because we will
5454 * do that here. */
5455 in_free_unref_items = TRUE;
5456
5457 /*
5458 * PASS 1: free the contents of the items. We don't free the items
5459 * themselves yet, so that it is possible to decrement refcount counters
5460 */
5461
Bram Moolenaarcd524592016-07-17 14:57:05 +02005462 /* Go through the list of dicts and free items without the copyID. */
5463 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464
Bram Moolenaarda861d62016-07-17 15:46:27 +02005465 /* Go through the list of lists and free items without the copyID. */
5466 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005467
5468#ifdef FEAT_JOB_CHANNEL
5469 /* Go through the list of jobs and free items without the copyID. This
5470 * must happen before doing channels, because jobs refer to channels, but
5471 * the reference from the channel to the job isn't tracked. */
5472 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5473
5474 /* Go through the list of channels and free items without the copyID. */
5475 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5476#endif
5477
5478 /*
5479 * PASS 2: free the items themselves.
5480 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005481 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005482 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005484#ifdef FEAT_JOB_CHANNEL
5485 /* Go through the list of jobs and free items without the copyID. This
5486 * must happen before doing channels, because jobs refer to channels, but
5487 * the reference from the channel to the job isn't tracked. */
5488 free_unused_jobs(copyID, COPYID_MASK);
5489
5490 /* Go through the list of channels and free items without the copyID. */
5491 free_unused_channels(copyID, COPYID_MASK);
5492#endif
5493
5494 in_free_unref_items = FALSE;
5495
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005496 return did_free;
5497}
5498
5499/*
5500 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 * "list_stack" is used to add lists to be marked. Can be NULL.
5502 *
5503 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005504 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005506set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005507{
5508 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005509 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 hashtab_T *cur_ht;
5512 ht_stack_T *ht_stack = NULL;
5513 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005514
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 cur_ht = ht;
5516 for (;;)
5517 {
5518 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005519 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005520 /* Mark each item in the hashtab. If the item contains a hashtab
5521 * it is added to ht_stack, if it contains a list it is added to
5522 * list_stack. */
5523 todo = (int)cur_ht->ht_used;
5524 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5525 if (!HASHITEM_EMPTY(hi))
5526 {
5527 --todo;
5528 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5529 &ht_stack, list_stack);
5530 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532
5533 if (ht_stack == NULL)
5534 break;
5535
5536 /* take an item from the stack */
5537 cur_ht = ht_stack->ht;
5538 tempitem = ht_stack;
5539 ht_stack = ht_stack->prev;
5540 free(tempitem);
5541 }
5542
5543 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005544}
5545
5546/*
5547 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005548 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5549 *
5550 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005554{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005555 listitem_T *li;
5556 int abort = FALSE;
5557 list_T *cur_l;
5558 list_stack_T *list_stack = NULL;
5559 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005560
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005561 cur_l = l;
5562 for (;;)
5563 {
5564 if (!abort)
5565 /* Mark each item in the list. If the item contains a hashtab
5566 * it is added to ht_stack, if it contains a list it is added to
5567 * list_stack. */
5568 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5569 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5570 ht_stack, &list_stack);
5571 if (list_stack == NULL)
5572 break;
5573
5574 /* take an item from the stack */
5575 cur_l = list_stack->list;
5576 tempitem = list_stack;
5577 list_stack = list_stack->prev;
5578 free(tempitem);
5579 }
5580
5581 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582}
5583
5584/*
5585 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 * "list_stack" is used to add lists to be marked. Can be NULL.
5587 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5588 *
5589 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005590 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005592set_ref_in_item(
5593 typval_T *tv,
5594 int copyID,
5595 ht_stack_T **ht_stack,
5596 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005597{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005598 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005599
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005600 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005601 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005602 dict_T *dd = tv->vval.v_dict;
5603
Bram Moolenaara03f2332016-02-06 18:09:59 +01005604 if (dd != NULL && dd->dv_copyID != copyID)
5605 {
5606 /* Didn't see this dict yet. */
5607 dd->dv_copyID = copyID;
5608 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005609 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005610 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5611 }
5612 else
5613 {
5614 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5615 if (newitem == NULL)
5616 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 else
5618 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005619 newitem->ht = &dd->dv_hashtab;
5620 newitem->prev = *ht_stack;
5621 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005622 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005623 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005624 }
5625 }
5626 else if (tv->v_type == VAR_LIST)
5627 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005628 list_T *ll = tv->vval.v_list;
5629
Bram Moolenaara03f2332016-02-06 18:09:59 +01005630 if (ll != NULL && ll->lv_copyID != copyID)
5631 {
5632 /* Didn't see this list yet. */
5633 ll->lv_copyID = copyID;
5634 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005635 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005636 abort = set_ref_in_list(ll, copyID, ht_stack);
5637 }
5638 else
5639 {
5640 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005641 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005642 if (newitem == NULL)
5643 abort = TRUE;
5644 else
5645 {
5646 newitem->list = ll;
5647 newitem->prev = *list_stack;
5648 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005650 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005651 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005652 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005653 else if (tv->v_type == VAR_FUNC)
5654 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005655 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005656 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005657 else if (tv->v_type == VAR_PARTIAL)
5658 {
5659 partial_T *pt = tv->vval.v_partial;
5660 int i;
5661
5662 /* A partial does not have a copyID, because it cannot contain itself.
5663 */
5664 if (pt != NULL)
5665 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005666 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005667
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005668 if (pt->pt_dict != NULL)
5669 {
5670 typval_T dtv;
5671
5672 dtv.v_type = VAR_DICT;
5673 dtv.vval.v_dict = pt->pt_dict;
5674 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5675 }
5676
5677 for (i = 0; i < pt->pt_argc; ++i)
5678 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5679 ht_stack, list_stack);
5680 }
5681 }
5682#ifdef FEAT_JOB_CHANNEL
5683 else if (tv->v_type == VAR_JOB)
5684 {
5685 job_T *job = tv->vval.v_job;
5686 typval_T dtv;
5687
5688 if (job != NULL && job->jv_copyID != copyID)
5689 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005690 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005691 if (job->jv_channel != NULL)
5692 {
5693 dtv.v_type = VAR_CHANNEL;
5694 dtv.vval.v_channel = job->jv_channel;
5695 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5696 }
5697 if (job->jv_exit_partial != NULL)
5698 {
5699 dtv.v_type = VAR_PARTIAL;
5700 dtv.vval.v_partial = job->jv_exit_partial;
5701 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5702 }
5703 }
5704 }
5705 else if (tv->v_type == VAR_CHANNEL)
5706 {
5707 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005708 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005709 typval_T dtv;
5710 jsonq_T *jq;
5711 cbq_T *cq;
5712
5713 if (ch != NULL && ch->ch_copyID != copyID)
5714 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005715 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005716 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005717 {
5718 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5719 jq = jq->jq_next)
5720 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5721 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5722 cq = cq->cq_next)
5723 if (cq->cq_partial != NULL)
5724 {
5725 dtv.v_type = VAR_PARTIAL;
5726 dtv.vval.v_partial = cq->cq_partial;
5727 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5728 }
5729 if (ch->ch_part[part].ch_partial != NULL)
5730 {
5731 dtv.v_type = VAR_PARTIAL;
5732 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5733 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5734 }
5735 }
5736 if (ch->ch_partial != NULL)
5737 {
5738 dtv.v_type = VAR_PARTIAL;
5739 dtv.vval.v_partial = ch->ch_partial;
5740 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5741 }
5742 if (ch->ch_close_partial != NULL)
5743 {
5744 dtv.v_type = VAR_PARTIAL;
5745 dtv.vval.v_partial = ch->ch_close_partial;
5746 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5747 }
5748 }
5749 }
5750#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005751 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005752}
5753
Bram Moolenaar17a13432016-01-24 14:22:10 +01005754 static char *
5755get_var_special_name(int nr)
5756{
5757 switch (nr)
5758 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005759 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005760 case VVAL_TRUE: return "v:true";
5761 case VVAL_NONE: return "v:none";
5762 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005763 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005764 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005765 return "42";
5766}
5767
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 * Return a string with the string representation of a variable.
5770 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005772 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005773 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5774 * stings as "string()", otherwise does not put quotes around strings, as
5775 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005776 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5777 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005778 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005780 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005781echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005782 typval_T *tv,
5783 char_u **tofree,
5784 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005785 int copyID,
5786 int echo_style,
5787 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005788 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 static int recurse = 0;
5791 char_u *r = NULL;
5792
Bram Moolenaar33570922005-01-25 22:26:29 +00005793 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005794 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005795 if (!did_echo_string_emsg)
5796 {
5797 /* Only give this message once for a recursive call to avoid
5798 * flooding the user with errors. And stop iterating over lists
5799 * and dicts. */
5800 did_echo_string_emsg = TRUE;
5801 EMSG(_("E724: variable nested too deep for displaying"));
5802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005804 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005805 }
5806 ++recurse;
5807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 switch (tv->v_type)
5809 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005810 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005811 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005812 {
5813 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005814 r = tv->vval.v_string;
5815 if (r == NULL)
5816 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005817 }
5818 else
5819 {
5820 *tofree = string_quote(tv->vval.v_string, FALSE);
5821 r = *tofree;
5822 }
5823 break;
5824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005826 if (echo_style)
5827 {
5828 *tofree = NULL;
5829 r = tv->vval.v_string;
5830 }
5831 else
5832 {
5833 *tofree = string_quote(tv->vval.v_string, TRUE);
5834 r = *tofree;
5835 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005837
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005838 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005839 {
5840 partial_T *pt = tv->vval.v_partial;
5841 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005842 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005843 garray_T ga;
5844 int i;
5845 char_u *tf;
5846
5847 ga_init2(&ga, 1, 100);
5848 ga_concat(&ga, (char_u *)"function(");
5849 if (fname != NULL)
5850 {
5851 ga_concat(&ga, fname);
5852 vim_free(fname);
5853 }
5854 if (pt != NULL && pt->pt_argc > 0)
5855 {
5856 ga_concat(&ga, (char_u *)", [");
5857 for (i = 0; i < pt->pt_argc; ++i)
5858 {
5859 if (i > 0)
5860 ga_concat(&ga, (char_u *)", ");
5861 ga_concat(&ga,
5862 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5863 vim_free(tf);
5864 }
5865 ga_concat(&ga, (char_u *)"]");
5866 }
5867 if (pt != NULL && pt->pt_dict != NULL)
5868 {
5869 typval_T dtv;
5870
5871 ga_concat(&ga, (char_u *)", ");
5872 dtv.v_type = VAR_DICT;
5873 dtv.vval.v_dict = pt->pt_dict;
5874 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5875 vim_free(tf);
5876 }
5877 ga_concat(&ga, (char_u *)")");
5878
5879 *tofree = ga.ga_data;
5880 r = *tofree;
5881 break;
5882 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005883
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005884 case VAR_BLOB:
5885 if (tv->vval.v_blob == NULL)
5886 {
5887 *tofree = NULL;
5888 r = (char_u *)"[]";
5889 }
5890 else
5891 {
5892 blob_T *b;
5893 int i;
5894 garray_T ga;
5895
5896 // Store bytes in the growarray.
5897 ga_init2(&ga, 1, 4000);
5898 b = tv->vval.v_blob;
5899 ga_append(&ga, '[');
5900 for (i = 0; i < blob_len(b); i++)
5901 {
5902 if (i > 0)
5903 ga_concat(&ga, (char_u *)",");
5904 vim_snprintf((char *)numbuf, NUMBUFLEN, "0x%02X",
5905 (int)blob_get(b, i));
5906 ga_concat(&ga, numbuf);
5907 }
5908 ga_append(&ga, ']');
5909 *tofree = ga.ga_data;
5910 r = *tofree;
5911 }
5912 break;
5913
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005915 if (tv->vval.v_list == NULL)
5916 {
5917 *tofree = NULL;
5918 r = NULL;
5919 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005920 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5921 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005922 {
5923 *tofree = NULL;
5924 r = (char_u *)"[...]";
5925 }
5926 else
5927 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005928 int old_copyID = tv->vval.v_list->lv_copyID;
5929
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005931 *tofree = list2string(tv, copyID, restore_copyID);
5932 if (restore_copyID)
5933 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005934 r = *tofree;
5935 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005937
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005939 if (tv->vval.v_dict == NULL)
5940 {
5941 *tofree = NULL;
5942 r = NULL;
5943 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005944 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5945 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005946 {
5947 *tofree = NULL;
5948 r = (char_u *)"{...}";
5949 }
5950 else
5951 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005952 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005954 *tofree = dict2string(tv, copyID, restore_copyID);
5955 if (restore_copyID)
5956 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005957 r = *tofree;
5958 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005959 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005960
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005962 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005963 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005964 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005965 break;
5966
Bram Moolenaar835dc632016-02-07 14:27:38 +01005967 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005968 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005970 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005971 if (composite_val)
5972 {
5973 *tofree = string_quote(r, FALSE);
5974 r = *tofree;
5975 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005977
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005978 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005979#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005980 *tofree = NULL;
5981 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5982 r = numbuf;
5983 break;
5984#endif
5985
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005986 case VAR_SPECIAL:
5987 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005988 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005989 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991
Bram Moolenaar8502c702014-06-17 12:51:16 +02005992 if (--recurse == 0)
5993 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995}
5996
5997/*
5998 * Return a string with the string representation of a variable.
5999 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6000 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006001 * Does not put quotes around strings, as ":echo" displays values.
6002 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6003 * May return NULL.
6004 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006006echo_string(
6007 typval_T *tv,
6008 char_u **tofree,
6009 char_u *numbuf,
6010 int copyID)
6011{
6012 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6013}
6014
6015/*
6016 * Return a string with the string representation of a variable.
6017 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6018 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006019 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006020 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023tv2string(
6024 typval_T *tv,
6025 char_u **tofree,
6026 char_u *numbuf,
6027 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006028{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006029 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030}
6031
6032/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 * Return string "str" in ' quotes, doubling ' characters.
6034 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006038string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039{
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006041 char_u *p, *r, *s;
6042
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 len = (function ? 13 : 3);
6044 if (str != NULL)
6045 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006046 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006047 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 if (*p == '\'')
6049 ++len;
6050 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006051 s = r = alloc(len);
6052 if (r != NULL)
6053 {
6054 if (function)
6055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006056 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 r += 10;
6058 }
6059 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006060 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 if (str != NULL)
6062 for (p = str; *p != NUL; )
6063 {
6064 if (*p == '\'')
6065 *r++ = '\'';
6066 MB_COPY_CHAR(p, r);
6067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069 if (function)
6070 *r++ = ')';
6071 *r++ = NUL;
6072 }
6073 return s;
6074}
6075
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006076#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077/*
6078 * Convert the string "text" to a floating point number.
6079 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6080 * this always uses a decimal point.
6081 * Returns the length of the text that was consumed.
6082 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006083 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084string2float(
6085 char_u *text,
6086 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006087{
6088 char *s = (char *)text;
6089 float_T f;
6090
Bram Moolenaar62473612017-01-08 19:25:40 +01006091 /* MS-Windows does not deal with "inf" and "nan" properly. */
6092 if (STRNICMP(text, "inf", 3) == 0)
6093 {
6094 *value = INFINITY;
6095 return 3;
6096 }
6097 if (STRNICMP(text, "-inf", 3) == 0)
6098 {
6099 *value = -INFINITY;
6100 return 4;
6101 }
6102 if (STRNICMP(text, "nan", 3) == 0)
6103 {
6104 *value = NAN;
6105 return 3;
6106 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006107 f = strtod(s, &s);
6108 *value = f;
6109 return (int)((char_u *)s - text);
6110}
6111#endif
6112
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 * Get the value of an environment variable.
6115 * "arg" is pointing to the '$'. It is advanced to after the name.
6116 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006117 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
6119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 char_u *string = NULL;
6123 int len;
6124 int cc;
6125 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006126 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 ++*arg;
6129 name = *arg;
6130 len = get_env_len(arg);
6131 if (evaluate)
6132 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006133 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006134 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006135
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006136 cc = name[len];
6137 name[len] = NUL;
6138 /* first try vim_getenv(), fast for normal environment vars */
6139 string = vim_getenv(name, &mustfree);
6140 if (string != NULL && *string != NUL)
6141 {
6142 if (!mustfree)
6143 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006145 else
6146 {
6147 if (mustfree)
6148 vim_free(string);
6149
6150 /* next try expanding things like $VIM and ${HOME} */
6151 string = expand_env_save(name - 1);
6152 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006153 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006154 }
6155 name[len] = cc;
6156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157 rettv->v_type = VAR_STRING;
6158 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 }
6160
6161 return OK;
6162}
6163
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006164
6165
6166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006168 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006170 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006171var2fpos(
6172 typval_T *varp,
6173 int dollar_lnum, /* TRUE when $ is last line */
6174 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006176 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006178 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
Bram Moolenaara5525202006-03-02 22:52:09 +00006180 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006181 if (varp->v_type == VAR_LIST)
6182 {
6183 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006184 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006185 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006186 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006187
6188 l = varp->vval.v_list;
6189 if (l == NULL)
6190 return NULL;
6191
6192 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006193 pos.lnum = list_find_nr(l, 0L, &error);
6194 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006195 return NULL; /* invalid line number */
6196
6197 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006198 pos.col = list_find_nr(l, 1L, &error);
6199 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006200 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006201 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006202
6203 /* We accept "$" for the column number: last column. */
6204 li = list_find(l, 1L);
6205 if (li != NULL && li->li_tv.v_type == VAR_STRING
6206 && li->li_tv.vval.v_string != NULL
6207 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6208 pos.col = len + 1;
6209
Bram Moolenaara5525202006-03-02 22:52:09 +00006210 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006211 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006212 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006213 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006214
Bram Moolenaara5525202006-03-02 22:52:09 +00006215#ifdef FEAT_VIRTUALEDIT
6216 /* Get the virtual offset. Defaults to zero. */
6217 pos.coladd = list_find_nr(l, 2L, &error);
6218 if (error)
6219 pos.coladd = 0;
6220#endif
6221
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006222 return &pos;
6223 }
6224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006225 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006226 if (name == NULL)
6227 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006228 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006230 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6231 {
6232 if (VIsual_active)
6233 return &VIsual;
6234 return &curwin->w_cursor;
6235 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006236 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006238 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6240 return NULL;
6241 return pp;
6242 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006243
6244#ifdef FEAT_VIRTUALEDIT
6245 pos.coladd = 0;
6246#endif
6247
Bram Moolenaar477933c2007-07-17 14:32:23 +00006248 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006249 {
6250 pos.col = 0;
6251 if (name[1] == '0') /* "w0": first visible line */
6252 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006253 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006254 /* In silent Ex mode topline is zero, but that's not a valid line
6255 * number; use one instead. */
6256 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006257 return &pos;
6258 }
6259 else if (name[1] == '$') /* "w$": last visible line */
6260 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006262 /* In silent Ex mode botline is zero, return zero then. */
6263 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006264 return &pos;
6265 }
6266 }
6267 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006269 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 pos.lnum = curbuf->b_ml.ml_line_count;
6272 pos.col = 0;
6273 }
6274 else
6275 {
6276 pos.lnum = curwin->w_cursor.lnum;
6277 pos.col = (colnr_T)STRLEN(ml_get_curline());
6278 }
6279 return &pos;
6280 }
6281 return NULL;
6282}
6283
6284/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006285 * Convert list in "arg" into a position and optional file number.
6286 * When "fnump" is NULL there is no file number, only 3 items.
6287 * Note that the column is passed on as-is, the caller may want to decrement
6288 * it to use 1 for the first column.
6289 * Return FAIL when conversion is not possible, doesn't check the position for
6290 * validity.
6291 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006293list2fpos(
6294 typval_T *arg,
6295 pos_T *posp,
6296 int *fnump,
6297 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006298{
6299 list_T *l = arg->vval.v_list;
6300 long i = 0;
6301 long n;
6302
Bram Moolenaar493c1782014-05-28 14:34:46 +02006303 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6304 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006305 if (arg->v_type != VAR_LIST
6306 || l == NULL
6307 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006308 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006309 return FAIL;
6310
6311 if (fnump != NULL)
6312 {
6313 n = list_find_nr(l, i++, NULL); /* fnum */
6314 if (n < 0)
6315 return FAIL;
6316 if (n == 0)
6317 n = curbuf->b_fnum; /* current buffer */
6318 *fnump = n;
6319 }
6320
6321 n = list_find_nr(l, i++, NULL); /* lnum */
6322 if (n < 0)
6323 return FAIL;
6324 posp->lnum = n;
6325
6326 n = list_find_nr(l, i++, NULL); /* col */
6327 if (n < 0)
6328 return FAIL;
6329 posp->col = n;
6330
6331#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006332 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006333 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006334 posp->coladd = 0;
6335 else
6336 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006337#endif
6338
Bram Moolenaar493c1782014-05-28 14:34:46 +02006339 if (curswantp != NULL)
6340 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6341
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006342 return OK;
6343}
6344
6345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 * Get the length of an environment variable name.
6347 * Advance "arg" to the first character after the name.
6348 * Return 0 for error.
6349 */
6350 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006351get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 char_u *p;
6354 int len;
6355
6356 for (p = *arg; vim_isIDc(*p); ++p)
6357 ;
6358 if (p == *arg) /* no name found */
6359 return 0;
6360
6361 len = (int)(p - *arg);
6362 *arg = p;
6363 return len;
6364}
6365
6366/*
6367 * Get the length of the name of a function or internal variable.
6368 * "arg" is advanced to the first non-white character after the name.
6369 * Return 0 if something is wrong.
6370 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 char_u *p;
6375 int len;
6376
6377 /* Find the end of the name. */
6378 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006379 {
6380 if (*p == ':')
6381 {
6382 /* "s:" is start of "s:var", but "n:" is not and can be used in
6383 * slice "[n:]". Also "xx:" is not a namespace. */
6384 len = (int)(p - *arg);
6385 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6386 || len > 1)
6387 break;
6388 }
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (p == *arg) /* no name found */
6391 return 0;
6392
6393 len = (int)(p - *arg);
6394 *arg = skipwhite(p);
6395
6396 return len;
6397}
6398
6399/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006400 * Get the length of the name of a variable or function.
6401 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006403 * Return -1 if curly braces expansion failed.
6404 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 * If the name contains 'magic' {}'s, expand them and return the
6406 * expanded name in an allocated string via 'alias' - caller must free.
6407 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409get_name_len(
6410 char_u **arg,
6411 char_u **alias,
6412 int evaluate,
6413 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 char_u *p;
6417 char_u *expr_start;
6418 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 *alias = NULL; /* default to no alias */
6421
6422 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6423 && (*arg)[2] == (int)KE_SNR)
6424 {
6425 /* hard coded <SNR>, already translated */
6426 *arg += 3;
6427 return get_id_len(arg) + 3;
6428 }
6429 len = eval_fname_script(*arg);
6430 if (len > 0)
6431 {
6432 /* literal "<SID>", "s:" or "<SNR>" */
6433 *arg += len;
6434 }
6435
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006437 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006439 p = find_name_end(*arg, &expr_start, &expr_end,
6440 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (expr_start != NULL)
6442 {
6443 char_u *temp_string;
6444
6445 if (!evaluate)
6446 {
6447 len += (int)(p - *arg);
6448 *arg = skipwhite(p);
6449 return len;
6450 }
6451
6452 /*
6453 * Include any <SID> etc in the expanded string:
6454 * Thus the -len here.
6455 */
6456 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6457 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006458 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 *alias = temp_string;
6460 *arg = skipwhite(p);
6461 return (int)STRLEN(temp_string);
6462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006465 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 EMSG2(_(e_invexpr2), *arg);
6467
6468 return len;
6469}
6470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006471/*
6472 * Find the end of a variable or function name, taking care of magic braces.
6473 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6474 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006475 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 * Return a pointer to just after the name. Equal to "arg" if there is no
6477 * valid name.
6478 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006479 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006480find_name_end(
6481 char_u *arg,
6482 char_u **expr_start,
6483 char_u **expr_end,
6484 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486 int mb_nest = 0;
6487 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006489 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006491 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 *expr_start = NULL;
6494 *expr_end = NULL;
6495 }
6496
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006497 /* Quick check for valid starting character. */
6498 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6499 return arg;
6500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006501 for (p = arg; *p != NUL
6502 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006503 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006504 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006505 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006506 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006508 if (*p == '\'')
6509 {
6510 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006511 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006512 ;
6513 if (*p == NUL)
6514 break;
6515 }
6516 else if (*p == '"')
6517 {
6518 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006519 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006520 if (*p == '\\' && p[1] != NUL)
6521 ++p;
6522 if (*p == NUL)
6523 break;
6524 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006525 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6526 {
6527 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006528 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006529 len = (int)(p - arg);
6530 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006531 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006532 break;
6533 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006537 if (*p == '[')
6538 ++br_nest;
6539 else if (*p == ']')
6540 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006543 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006545 if (*p == '{')
6546 {
6547 mb_nest++;
6548 if (expr_start != NULL && *expr_start == NULL)
6549 *expr_start = p;
6550 }
6551 else if (*p == '}')
6552 {
6553 mb_nest--;
6554 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6555 *expr_end = p;
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559
6560 return p;
6561}
6562
6563/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006564 * Expands out the 'magic' {}'s in a variable/function name.
6565 * Note that this can call itself recursively, to deal with
6566 * constructs like foo{bar}{baz}{bam}
6567 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6568 * "in_start" ^
6569 * "expr_start" ^
6570 * "expr_end" ^
6571 * "in_end" ^
6572 *
6573 * Returns a new allocated string, which the caller must free.
6574 * Returns NULL for failure.
6575 */
6576 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577make_expanded_name(
6578 char_u *in_start,
6579 char_u *expr_start,
6580 char_u *expr_end,
6581 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006582{
6583 char_u c1;
6584 char_u *retval = NULL;
6585 char_u *temp_result;
6586 char_u *nextcmd = NULL;
6587
6588 if (expr_end == NULL || in_end == NULL)
6589 return NULL;
6590 *expr_start = NUL;
6591 *expr_end = NUL;
6592 c1 = *in_end;
6593 *in_end = NUL;
6594
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006595 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006596 if (temp_result != NULL && nextcmd == NULL)
6597 {
6598 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6599 + (in_end - expr_end) + 1));
6600 if (retval != NULL)
6601 {
6602 STRCPY(retval, in_start);
6603 STRCAT(retval, temp_result);
6604 STRCAT(retval, expr_end + 1);
6605 }
6606 }
6607 vim_free(temp_result);
6608
6609 *in_end = c1; /* put char back for error messages */
6610 *expr_start = '{';
6611 *expr_end = '}';
6612
6613 if (retval != NULL)
6614 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006615 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006616 if (expr_start != NULL)
6617 {
6618 /* Further expansion! */
6619 temp_result = make_expanded_name(retval, expr_start,
6620 expr_end, temp_result);
6621 vim_free(retval);
6622 retval = temp_result;
6623 }
6624 }
6625
6626 return retval;
6627}
6628
6629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006636 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6637}
6638
6639/*
6640 * Return TRUE if character "c" can be used as the first character in a
6641 * variable or function name (excluding '{' and '}').
6642 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006645{
6646 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647}
6648
6649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 * Set number v: variable to "val".
6651 */
6652 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006653set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
6657
6658/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006659 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006661 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006662get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665}
6666
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006667/*
6668 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006669 * If the String variable has never been set, return an empty string.
6670 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006671 */
6672 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006673get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006674{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006675 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006676}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006679 * Get List v: variable value. Caller must take care of reference count when
6680 * needed.
6681 */
6682 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006684{
6685 return vimvars[idx].vv_list;
6686}
6687
6688/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006689 * Get Dict v: variable value. Caller must take care of reference count when
6690 * needed.
6691 */
6692 dict_T *
6693get_vim_var_dict(int idx)
6694{
6695 return vimvars[idx].vv_dict;
6696}
6697
6698/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006699 * Set v:char to character "c".
6700 */
6701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006703{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006704 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006705
6706#ifdef FEAT_MBYTE
6707 if (has_mbyte)
6708 buf[(*mb_char2bytes)(c, buf)] = NUL;
6709 else
6710#endif
6711 {
6712 buf[0] = c;
6713 buf[1] = NUL;
6714 }
6715 set_vim_var_string(VV_CHAR, buf, -1);
6716}
6717
6718/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006719 * Set v:count to "count" and v:count1 to "count1".
6720 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 */
6722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006723set_vcount(
6724 long count,
6725 long count1,
6726 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006728 if (set_prevcount)
6729 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006730 vimvars[VV_COUNT].vv_nr = count;
6731 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732}
6733
6734/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006735 * Save variables that might be changed as a side effect. Used when executing
6736 * a timer callback.
6737 */
6738 void
6739save_vimvars(vimvars_save_T *vvsave)
6740{
6741 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6742 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6743 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6744}
6745
6746/*
6747 * Restore variables saved by save_vimvars().
6748 */
6749 void
6750restore_vimvars(vimvars_save_T *vvsave)
6751{
6752 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6753 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6754 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6755}
6756
6757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 * Set string v: variable to a copy of "val".
6759 */
6760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006761set_vim_var_string(
6762 int idx,
6763 char_u *val,
6764 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
Bram Moolenaara542c682016-01-31 16:28:04 +01006766 clear_tv(&vimvars[idx].vv_di.di_tv);
6767 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006773 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774}
6775
6776/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006777 * Set List v: variable to "val".
6778 */
6779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006781{
Bram Moolenaara542c682016-01-31 16:28:04 +01006782 clear_tv(&vimvars[idx].vv_di.di_tv);
6783 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006784 vimvars[idx].vv_list = val;
6785 if (val != NULL)
6786 ++val->lv_refcount;
6787}
6788
6789/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006790 * Set Dictionary v: variable to "val".
6791 */
6792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006793set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006794{
Bram Moolenaara542c682016-01-31 16:28:04 +01006795 clear_tv(&vimvars[idx].vv_di.di_tv);
6796 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006797 vimvars[idx].vv_dict = val;
6798 if (val != NULL)
6799 {
6800 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006801 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006802 }
6803}
6804
6805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * Set v:register if needed.
6807 */
6808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006809set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
6811 char_u regname;
6812
6813 if (c == 0 || c == ' ')
6814 regname = '"';
6815 else
6816 regname = c;
6817 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 set_vim_var_string(VV_REG, &regname, 1);
6820}
6821
6822/*
6823 * Get or set v:exception. If "oldval" == NULL, return the current value.
6824 * Otherwise, restore the value to "oldval" and return NULL.
6825 * Must always be called in pairs to save and restore v:exception! Does not
6826 * take care of memory allocations.
6827 */
6828 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 return NULL;
6836}
6837
6838/*
6839 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6840 * Otherwise, restore the value to "oldval" and return NULL.
6841 * Must always be called in pairs to save and restore v:throwpoint! Does not
6842 * take care of memory allocations.
6843 */
6844 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006845v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006848 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 return NULL;
6852}
6853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854/*
6855 * Set v:cmdarg.
6856 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6857 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6858 * Must always be called in pairs!
6859 */
6860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006861set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 char_u *oldval;
6864 char_u *newval;
6865 unsigned len;
6866
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006868 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006870 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006872 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 }
6874
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006875 if (eap->force_bin == FORCE_BIN)
6876 len = 6;
6877 else if (eap->force_bin == FORCE_NOBIN)
6878 len = 8;
6879 else
6880 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006881
6882 if (eap->read_edit)
6883 len += 7;
6884
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006885 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006886 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006887# ifdef FEAT_MBYTE
6888 if (eap->force_enc != 0)
6889 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006890 if (eap->bad_char != 0)
6891 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006892# endif
6893
6894 newval = alloc(len + 1);
6895 if (newval == NULL)
6896 return NULL;
6897
6898 if (eap->force_bin == FORCE_BIN)
6899 sprintf((char *)newval, " ++bin");
6900 else if (eap->force_bin == FORCE_NOBIN)
6901 sprintf((char *)newval, " ++nobin");
6902 else
6903 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006904
6905 if (eap->read_edit)
6906 STRCAT(newval, " ++edit");
6907
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006908 if (eap->force_ff != 0)
6909 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006910 eap->force_ff == 'u' ? "unix"
6911 : eap->force_ff == 'd' ? "dos"
6912 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006913#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006914 if (eap->force_enc != 0)
6915 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6916 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006917 if (eap->bad_char == BAD_KEEP)
6918 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6919 else if (eap->bad_char == BAD_DROP)
6920 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6921 else if (eap->bad_char != 0)
6922 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006923#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006925 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
6928/*
6929 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006930 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933get_var_tv(
6934 char_u *name,
6935 int len, /* length of "name" */
6936 typval_T *rettv, /* NULL when only checking existence */
6937 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6938 int verbose, /* may give error message */
6939 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 /* truncate the name, so that we can use strcmp() */
6947 cc = name[len];
6948 name[len] = NUL;
6949
6950 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 * Check for user-defined variables.
6952 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006953 v = find_var(name, NULL, no_autoload);
6954 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006956 tv = &v->di_tv;
6957 if (dip != NULL)
6958 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006963 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006964 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 ret = FAIL;
6966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006967 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
6970 name[len] = cc;
6971
6972 return ret;
6973}
6974
6975/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006976 * Check if variable "name[len]" is a local variable or an argument.
6977 * If so, "*eval_lavars_used" is set to TRUE.
6978 */
6979 static void
6980check_vars(char_u *name, int len)
6981{
6982 int cc;
6983 char_u *varname;
6984 hashtab_T *ht;
6985
6986 if (eval_lavars_used == NULL)
6987 return;
6988
6989 /* truncate the name, so that we can use strcmp() */
6990 cc = name[len];
6991 name[len] = NUL;
6992
6993 ht = find_var_ht(name, &varname);
6994 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6995 {
6996 if (find_var(name, NULL, TRUE) != NULL)
6997 *eval_lavars_used = TRUE;
6998 }
6999
7000 name[len] = cc;
7001}
7002
7003/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007004 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7005 * Also handle function call with Funcref variable: func(expr)
7006 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7007 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009handle_subscript(
7010 char_u **arg,
7011 typval_T *rettv,
7012 int evaluate, /* do more than finding the end */
7013 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007014{
7015 int ret = OK;
7016 dict_T *selfdict = NULL;
7017 char_u *s;
7018 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007019 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020
7021 while (ret == OK
7022 && (**arg == '['
7023 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007024 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7025 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007026 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007027 {
7028 if (**arg == '(')
7029 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007030 partial_T *pt = NULL;
7031
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007033 if (evaluate)
7034 {
7035 functv = *rettv;
7036 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007037
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007038 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007039 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007040 {
7041 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007042 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007043 }
7044 else
7045 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007046 }
7047 else
7048 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007049 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007051 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052
7053 /* Clear the funcref afterwards, so that deleting it while
7054 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007055 if (evaluate)
7056 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007057
7058 /* Stop the expression evaluation when immediately aborting on
7059 * error, or when an interrupt occurred or an exception was thrown
7060 * but not caught. */
7061 if (aborting())
7062 {
7063 if (ret == OK)
7064 clear_tv(rettv);
7065 ret = FAIL;
7066 }
7067 dict_unref(selfdict);
7068 selfdict = NULL;
7069 }
7070 else /* **arg == '[' || **arg == '.' */
7071 {
7072 dict_unref(selfdict);
7073 if (rettv->v_type == VAR_DICT)
7074 {
7075 selfdict = rettv->vval.v_dict;
7076 if (selfdict != NULL)
7077 ++selfdict->dv_refcount;
7078 }
7079 else
7080 selfdict = NULL;
7081 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7082 {
7083 clear_tv(rettv);
7084 ret = FAIL;
7085 }
7086 }
7087 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007088
Bram Moolenaar1d429612016-05-24 15:44:17 +02007089 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7090 * Don't do this when "Func" is already a partial that was bound
7091 * explicitly (pt_auto is FALSE). */
7092 if (selfdict != NULL
7093 && (rettv->v_type == VAR_FUNC
7094 || (rettv->v_type == VAR_PARTIAL
7095 && (rettv->vval.v_partial->pt_auto
7096 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007097 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007098
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007099 dict_unref(selfdict);
7100 return ret;
7101}
7102
7103/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007104 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 * value).
7106 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007107 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109{
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007111}
7112
7113/*
7114 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 * The string "s" must have been allocated, it is consumed.
7116 * Return NULL for out of memory, the variable otherwise.
7117 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007119alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007123 rettv = alloc_tv();
7124 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007126 rettv->v_type = VAR_STRING;
7127 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 }
7129 else
7130 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007131 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132}
7133
7134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007135 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 if (varp != NULL)
7141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007142 switch (varp->v_type)
7143 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007144 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007145 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007146 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007147 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007148 vim_free(varp->vval.v_string);
7149 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007150 case VAR_PARTIAL:
7151 partial_unref(varp->vval.v_partial);
7152 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007153 case VAR_BLOB:
7154 blob_unref(varp->vval.v_blob);
7155 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 case VAR_LIST:
7157 list_unref(varp->vval.v_list);
7158 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007159 case VAR_DICT:
7160 dict_unref(varp->vval.v_dict);
7161 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007162 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007163#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007164 job_unref(varp->vval.v_job);
7165 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007166#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007167 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007169 channel_unref(varp->vval.v_channel);
7170 break;
7171#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007172 case VAR_NUMBER:
7173 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007174 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007175 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007176 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 vim_free(varp);
7179 }
7180}
7181
7182/*
7183 * Free the memory for a variable value and set the value to NULL or 0.
7184 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007186clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (varp != NULL)
7189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007190 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007193 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007194 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007195 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007196 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007197 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007198 case VAR_PARTIAL:
7199 partial_unref(varp->vval.v_partial);
7200 varp->vval.v_partial = NULL;
7201 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007202 case VAR_BLOB:
7203 blob_unref(varp->vval.v_blob);
7204 varp->vval.v_blob = NULL;
7205 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 case VAR_LIST:
7207 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007209 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 case VAR_DICT:
7211 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007214 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007215 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007216 varp->vval.v_number = 0;
7217 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007218 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007219#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007220 varp->vval.v_float = 0.0;
7221 break;
7222#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007223 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007224#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007225 job_unref(varp->vval.v_job);
7226 varp->vval.v_job = NULL;
7227#endif
7228 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007229 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007230#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007231 channel_unref(varp->vval.v_channel);
7232 varp->vval.v_channel = NULL;
7233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007234 case VAR_UNKNOWN:
7235 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007237 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239}
7240
7241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242 * Set the value of a variable to NULL without freeing items.
7243 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007245init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246{
7247 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249}
7250
7251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 * Get the number value of a variable.
7253 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007254 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007255 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256 * caller of incompatible types: it sets *denote to TRUE if "denote"
7257 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007259 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007260tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 int error = FALSE;
7263
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007264 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265}
7266
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007267 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007268tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007269{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007270 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007275 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007276 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007277#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007278 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007279 break;
7280#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007281 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007282 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007283 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 break;
7285 case VAR_STRING:
7286 if (varp->vval.v_string != NULL)
7287 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007288 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007289 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007290 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007291 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007292 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007293 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007294 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007295 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007296 case VAR_SPECIAL:
7297 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7298 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007299 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007300#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007301 EMSG(_("E910: Using a Job as a Number"));
7302 break;
7303#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007304 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007305#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007306 EMSG(_("E913: Using a Channel as a Number"));
7307 break;
7308#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007309 case VAR_BLOB:
7310 EMSG(_("E974: Using a Blob as a Number"));
7311 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007312 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007313 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007316 if (denote == NULL) /* useful for values that must be unsigned */
7317 n = -1;
7318 else
7319 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007320 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007323#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007325tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007326{
7327 switch (varp->v_type)
7328 {
7329 case VAR_NUMBER:
7330 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007331 case VAR_FLOAT:
7332 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007333 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007334 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007335 EMSG(_("E891: Using a Funcref as a Float"));
7336 break;
7337 case VAR_STRING:
7338 EMSG(_("E892: Using a String as a Float"));
7339 break;
7340 case VAR_LIST:
7341 EMSG(_("E893: Using a List as a Float"));
7342 break;
7343 case VAR_DICT:
7344 EMSG(_("E894: Using a Dictionary as a Float"));
7345 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007346 case VAR_SPECIAL:
7347 EMSG(_("E907: Using a special value as a Float"));
7348 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007349 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007350# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007351 EMSG(_("E911: Using a Job as a Float"));
7352 break;
7353# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007354 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007355# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007356 EMSG(_("E914: Using a Channel as a Float"));
7357 break;
7358# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007359 case VAR_BLOB:
7360 EMSG(_("E975: Using a Blob as a Float"));
7361 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007362 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007363 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007364 break;
7365 }
7366 return 0;
7367}
7368#endif
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 * Get the string value of a variable.
7372 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007373 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7374 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 * If the String variable has never been set, return an empty string.
7376 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007377 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007380 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007381tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
7383 static char_u mybuf[NUMBUFLEN];
7384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386}
7387
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007388 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007391 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392
7393 return res != NULL ? res : (char_u *)"";
7394}
7395
Bram Moolenaar7d647822014-04-05 21:28:56 +02007396/*
7397 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7398 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007399 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007400tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007401{
7402 static char_u mybuf[NUMBUFLEN];
7403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007404 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405}
7406
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007407 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007410 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007412 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007413 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007414 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 return buf;
7416 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007417 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007418 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007419 break;
7420 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007421 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 break;
7423 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007424 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007425 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007426 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007427#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007428 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007429 break;
7430#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007431 case VAR_STRING:
7432 if (varp->vval.v_string != NULL)
7433 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007434 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007435 case VAR_SPECIAL:
7436 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7437 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007438 case VAR_BLOB:
7439 EMSG(_("E976: using Blob as a String"));
7440 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007441 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007442#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007443 {
7444 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007445 char *status;
7446
7447 if (job == NULL)
7448 return (char_u *)"no process";
7449 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007450 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007451 : "run";
7452# ifdef UNIX
7453 vim_snprintf((char *)buf, NUMBUFLEN,
7454 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007455# elif defined(WIN32)
7456 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007457 "process %ld %s",
7458 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007459 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007460# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007461 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007462 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7463# endif
7464 return buf;
7465 }
7466#endif
7467 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007468 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007469#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007470 {
7471 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007472 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007473
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007474 if (channel == NULL)
7475 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7476 else
7477 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007478 "channel %d %s", channel->ch_id, status);
7479 return buf;
7480 }
7481#endif
7482 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007483 case VAR_UNKNOWN:
7484 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007487 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007491 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7492 * string() on Dict, List, etc.
7493 */
7494 char_u *
7495tv_stringify(typval_T *varp, char_u *buf)
7496{
7497 if (varp->v_type == VAR_LIST
7498 || varp->v_type == VAR_DICT
7499 || varp->v_type == VAR_FUNC
7500 || varp->v_type == VAR_PARTIAL
7501 || varp->v_type == VAR_FLOAT)
7502 {
7503 typval_T tmp;
7504
7505 f_string(varp, &tmp);
7506 tv_get_string_buf(&tmp, buf);
7507 clear_tv(varp);
7508 *varp = tmp;
7509 return tmp.vval.v_string;
7510 }
7511 return tv_get_string_buf(varp, buf);
7512}
7513
7514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 * Find variable "name" in the list of variables.
7516 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007517 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007518 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007521 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007526 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
Bram Moolenaara7043832005-01-21 11:56:39 +00007528 ht = find_var_ht(name, &varname);
7529 if (htp != NULL)
7530 *htp = ht;
7531 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007533 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7534 if (ret != NULL)
7535 return ret;
7536
7537 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007538 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540
7541/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007542 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007543 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546find_var_in_ht(
7547 hashtab_T *ht,
7548 int htname,
7549 char_u *varname,
7550 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007551{
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 hashitem_T *hi;
7553
7554 if (*varname == NUL)
7555 {
7556 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007557 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007559 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 case 'g': return &globvars_var;
7561 case 'v': return &vimvars_var;
7562 case 'b': return &curbuf->b_bufvar;
7563 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007564 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007565 case 'l': return get_funccal_local_var();
7566 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 }
7568 return NULL;
7569 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007570
7571 hi = hash_find(ht, varname);
7572 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007573 {
7574 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007575 * worked find the variable again. Don't auto-load a script if it was
7576 * loaded already, otherwise it would be loaded every time when
7577 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007578 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007579 {
7580 /* Note: script_autoload() may make "hi" invalid. It must either
7581 * be obtained again or not used. */
7582 if (!script_autoload(varname, FALSE) || aborting())
7583 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007585 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 if (HASHITEM_EMPTY(hi))
7587 return NULL;
7588 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007590}
7591
7592/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007594 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007595 * Set "varname" to the start of name without ':'.
7596 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007597 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007598find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007600 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007601 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007602
Bram Moolenaar73627d02015-08-11 15:46:09 +02007603 if (name[0] == NUL)
7604 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (name[1] != ':')
7606 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007607 /* The name must not start with a colon or #. */
7608 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 return NULL;
7610 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007611
7612 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007613 hi = hash_find(&compat_hashtab, name);
7614 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007615 return &compat_hashtab;
7616
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007617 ht = get_funccal_local_ht();
7618 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007620 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007623 if (*name == 'g') /* global variable */
7624 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007625 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7626 */
7627 if (vim_strchr(name + 2, ':') != NULL
7628 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007629 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007631 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007633 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007634 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007635 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 if (*name == 'v') /* v: variable */
7637 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007638 if (*name == 'a') /* a: function argument */
7639 return get_funccal_args_ht();
7640 if (*name == 'l') /* l: local function variable */
7641 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007643 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7644 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 return NULL;
7646}
7647
7648/*
7649 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007650 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 * Returns NULL when it doesn't exist.
7652 */
7653 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007658 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 if (v == NULL)
7660 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007661 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 * sourcing this script and when executing functions defined in the script.
7667 */
7668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007669new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaara7043832005-01-21 11:56:39 +00007671 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 hashtab_T *ht;
7673 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007674
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7676 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007677 /* Re-allocating ga_data means that an ht_array pointing to
7678 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007680 for (i = 1; i <= ga_scripts.ga_len; ++i)
7681 {
7682 ht = &SCRIPT_VARS(i);
7683 if (ht->ht_mask == HT_INIT_SIZE - 1)
7684 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007685 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007687 }
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 while (ga_scripts.ga_len < id)
7690 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007691 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007692 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007693 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696 }
7697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7701 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 */
7703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007704init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007707 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007708 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007709 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007710 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 dict_var->di_tv.vval.v_dict = dict;
7712 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7715 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
7718/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007719 * Unreference a dictionary initialized by init_var_dict().
7720 */
7721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007723{
7724 /* Now the dict needs to be freed if no one else is using it, go back to
7725 * normal reference counting. */
7726 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7727 dict_unref(dict);
7728}
7729
7730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 * Frees all allocated variables and the value they contain.
7733 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 */
7735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737{
7738 vars_clear_ext(ht, TRUE);
7739}
7740
7741/*
7742 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7743 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007745vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
Bram Moolenaara7043832005-01-21 11:56:39 +00007747 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 hashitem_T *hi;
7749 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007752 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007753 for (hi = ht->ht_array; todo > 0; ++hi)
7754 {
7755 if (!HASHITEM_EMPTY(hi))
7756 {
7757 --todo;
7758
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007760 * ht_array might change then. hash_clear() takes care of it
7761 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 v = HI2DI(hi);
7763 if (free_val)
7764 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007765 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007767 }
7768 }
7769 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007770 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
Bram Moolenaara7043832005-01-21 11:56:39 +00007773/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 * Delete a variable from hashtab "ht" at item "hi".
7775 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007781
7782 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 clear_tv(&di->di_tv);
7784 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
7787/*
7788 * List the value of one internal variable.
7789 */
7790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 char_u *tofree;
7794 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007797 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007799 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801}
7802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007804list_one_var_a(
7805 char_u *prefix,
7806 char_u *name,
7807 int type,
7808 char_u *string,
7809 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
Bram Moolenaar31859182007-08-14 20:41:13 +00007811 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7812 msg_start();
7813 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (name != NULL) /* "a:" vars don't have a name stored */
7815 msg_puts(name);
7816 msg_putchar(' ');
7817 msg_advance(22);
7818 if (type == VAR_NUMBER)
7819 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007820 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821 msg_putchar('*');
7822 else if (type == VAR_LIST)
7823 {
7824 msg_putchar('[');
7825 if (*string == '[')
7826 ++string;
7827 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 else if (type == VAR_DICT)
7829 {
7830 msg_putchar('{');
7831 if (*string == '{')
7832 ++string;
7833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 else
7835 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007839 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007841 if (*first)
7842 {
7843 msg_clr_eos();
7844 *first = FALSE;
7845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
7848/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 * If the variable already exists, the value is updated.
7851 * Otherwise the variable is created.
7852 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007854set_var(
7855 char_u *name,
7856 typval_T *tv,
7857 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007863 ht = find_var_ht(name, &varname);
7864 if (ht == NULL || *varname == NUL)
7865 {
7866 EMSG2(_(e_illvar), name);
7867 return;
7868 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007869 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007870
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007871 /* Search in parent scope which is possible to reference from lambda */
7872 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007873 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007874
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007875 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7876 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007877 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007882 if (var_check_ro(v->di_flags, name, FALSE)
7883 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007885
7886 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007887 * Handle setting internal v: variables separately where needed to
7888 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 */
7890 if (ht == &vimvarht)
7891 {
7892 if (v->di_tv.v_type == VAR_STRING)
7893 {
7894 vim_free(v->di_tv.vval.v_string);
7895 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007896 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 else
7898 {
7899 /* Take over the string to avoid an extra alloc/free. */
7900 v->di_tv.vval.v_string = tv->vval.v_string;
7901 tv->vval.v_string = NULL;
7902 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007903 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007905 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007907 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908 if (STRCMP(varname, "searchforward") == 0)
7909 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007910#ifdef FEAT_SEARCH_EXTRA
7911 else if (STRCMP(varname, "hlsearch") == 0)
7912 {
7913 no_hlsearch = !v->di_tv.vval.v_number;
7914 redraw_all_later(SOME_VALID);
7915 }
7916#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007917 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007918 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007919 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007920 {
Bram Moolenaar74ea88c2018-12-04 22:37:49 +01007921 EMSG2(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007922 return;
7923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 }
7925
7926 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928 else /* add a new variable */
7929 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007930 /* Can't add "v:" variable. */
7931 if (ht == &vimvarht)
7932 {
7933 EMSG2(_(e_illvar), name);
7934 return;
7935 }
7936
Bram Moolenaar92124a32005-06-17 22:03:40 +00007937 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007938 if (!valid_varname(varname))
7939 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007941 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7942 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007943 if (v == NULL)
7944 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007948 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 return;
7950 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007951 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007953
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007954 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007956 else
7957 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007959 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962}
7963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007965 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 * Also give an error message.
7967 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007969var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007970{
7971 if (flags & DI_FLAGS_RO)
7972 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007973 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 return TRUE;
7975 }
7976 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7977 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007978 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 return TRUE;
7980 }
7981 return FALSE;
7982}
7983
7984/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007985 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7986 * Also give an error message.
7987 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007989var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007990{
7991 if (flags & DI_FLAGS_FIX)
7992 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007993 EMSG2(_("E795: Cannot delete variable %s"),
7994 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007995 return TRUE;
7996 }
7997 return FALSE;
7998}
7999
8000/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008001 * Check if a funcref is assigned to a valid variable name.
8002 * Return TRUE and give an error if not.
8003 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008005var_check_func_name(
8006 char_u *name, /* points to start of variable name */
8007 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008008{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008009 /* Allow for w: b: s: and t:. */
8010 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008011 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8012 ? name[2] : name[0]))
8013 {
8014 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
8015 name);
8016 return TRUE;
8017 }
8018 /* Don't allow hiding a function. When "v" is not NULL we might be
8019 * assigning another function to the same var, the type is checked
8020 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008021 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008022 {
8023 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
8024 name);
8025 return TRUE;
8026 }
8027 return FALSE;
8028}
8029
8030/*
8031 * Check if a variable name is valid.
8032 * Return FALSE and give an error if not.
8033 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008034 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008035valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008036{
8037 char_u *p;
8038
8039 for (p = varname; *p != NUL; ++p)
8040 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8041 && *p != AUTOLOAD_CHAR)
8042 {
8043 EMSG2(_(e_illvar), varname);
8044 return FALSE;
8045 }
8046 return TRUE;
8047}
8048
8049/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008050 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008051 * Also give an error message, using "name" or _("name") when use_gettext is
8052 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008053 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008055tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008056{
8057 if (lock & VAR_LOCKED)
8058 {
8059 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008060 name == NULL ? (char_u *)_("Unknown")
8061 : use_gettext ? (char_u *)_(name)
8062 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008063 return TRUE;
8064 }
8065 if (lock & VAR_FIXED)
8066 {
8067 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008068 name == NULL ? (char_u *)_("Unknown")
8069 : use_gettext ? (char_u *)_(name)
8070 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008071 return TRUE;
8072 }
8073 return FALSE;
8074}
8075
8076/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008078 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008080 * It is OK for "from" and "to" to point to the same item. This is used to
8081 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008082 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008084copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008087 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 switch (from->v_type)
8089 {
8090 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008091 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 to->vval.v_number = from->vval.v_number;
8093 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008094 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008095#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096 to->vval.v_float = from->vval.v_float;
8097 break;
8098#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008099 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008100#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008101 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008102 if (to->vval.v_job != NULL)
8103 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008104 break;
8105#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008106 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008107#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008108 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008109 if (to->vval.v_channel != NULL)
8110 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008111 break;
8112#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008113 case VAR_STRING:
8114 case VAR_FUNC:
8115 if (from->vval.v_string == NULL)
8116 to->vval.v_string = NULL;
8117 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008120 if (from->v_type == VAR_FUNC)
8121 func_ref(to->vval.v_string);
8122 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008123 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008124 case VAR_PARTIAL:
8125 if (from->vval.v_partial == NULL)
8126 to->vval.v_partial = NULL;
8127 else
8128 {
8129 to->vval.v_partial = from->vval.v_partial;
8130 ++to->vval.v_partial->pt_refcount;
8131 }
8132 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008133 case VAR_BLOB:
8134 if (from->vval.v_blob == NULL)
8135 to->vval.v_blob = NULL;
8136 else
8137 {
8138 to->vval.v_blob = from->vval.v_blob;
8139 ++to->vval.v_blob->bv_refcount;
8140 }
8141 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 case VAR_LIST:
8143 if (from->vval.v_list == NULL)
8144 to->vval.v_list = NULL;
8145 else
8146 {
8147 to->vval.v_list = from->vval.v_list;
8148 ++to->vval.v_list->lv_refcount;
8149 }
8150 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008151 case VAR_DICT:
8152 if (from->vval.v_dict == NULL)
8153 to->vval.v_dict = NULL;
8154 else
8155 {
8156 to->vval.v_dict = from->vval.v_dict;
8157 ++to->vval.v_dict->dv_refcount;
8158 }
8159 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008160 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008161 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008162 break;
8163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164}
8165
8166/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167 * Make a copy of an item.
8168 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008169 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8170 * reference to an already copied list/dict can be used.
8171 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008172 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008174item_copy(
8175 typval_T *from,
8176 typval_T *to,
8177 int deep,
8178 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179{
8180 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008181 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 {
8185 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008186 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 }
8188 ++recurse;
8189
8190 switch (from->v_type)
8191 {
8192 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 case VAR_STRING:
8195 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008196 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008197 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008198 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008199 case VAR_CHANNEL:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008200 case VAR_BLOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 copy_tv(from, to);
8202 break;
8203 case VAR_LIST:
8204 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008205 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 if (from->vval.v_list == NULL)
8207 to->vval.v_list = NULL;
8208 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8209 {
8210 /* use the copy made earlier */
8211 to->vval.v_list = from->vval.v_list->lv_copylist;
8212 ++to->vval.v_list->lv_refcount;
8213 }
8214 else
8215 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8216 if (to->vval.v_list == NULL)
8217 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 break;
8219 case VAR_DICT:
8220 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008221 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008222 if (from->vval.v_dict == NULL)
8223 to->vval.v_dict = NULL;
8224 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8225 {
8226 /* use the copy made earlier */
8227 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8228 ++to->vval.v_dict->dv_refcount;
8229 }
8230 else
8231 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8232 if (to->vval.v_dict == NULL)
8233 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008234 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008235 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008236 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008237 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008238 }
8239 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008240 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241}
8242
8243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 * This function is used by f_input() and f_inputdialog() functions. The third
8245 * argument to f_input() specifies the type of completion to use at the
8246 * prompt. The third argument to f_inputdialog() specifies the value to return
8247 * when the user cancels the prompt.
8248 */
8249 void
8250get_user_input(
8251 typval_T *argvars,
8252 typval_T *rettv,
8253 int inputdialog,
8254 int secret)
8255{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 char_u *p = NULL;
8258 int c;
8259 char_u buf[NUMBUFLEN];
8260 int cmd_silent_save = cmd_silent;
8261 char_u *defstr = (char_u *)"";
8262 int xp_type = EXPAND_NOTHING;
8263 char_u *xp_arg = NULL;
8264
8265 rettv->v_type = VAR_STRING;
8266 rettv->vval.v_string = NULL;
8267
8268#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008269 /* While starting up, there is no place to enter text. When running tests
8270 * with --not-a-term we assume feedkeys() will be used. */
8271 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 return;
8273#endif
8274
8275 cmd_silent = FALSE; /* Want to see the prompt. */
8276 if (prompt != NULL)
8277 {
8278 /* Only the part of the message after the last NL is considered as
8279 * prompt for the command line */
8280 p = vim_strrchr(prompt, '\n');
8281 if (p == NULL)
8282 p = prompt;
8283 else
8284 {
8285 ++p;
8286 c = *p;
8287 *p = NUL;
8288 msg_start();
8289 msg_clr_eos();
8290 msg_puts_attr(prompt, echo_attr);
8291 msg_didout = FALSE;
8292 msg_starthere();
8293 *p = c;
8294 }
8295 cmdline_row = msg_row;
8296
8297 if (argvars[1].v_type != VAR_UNKNOWN)
8298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008299 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 if (defstr != NULL)
8301 stuffReadbuffSpec(defstr);
8302
8303 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8304 {
8305 char_u *xp_name;
8306 int xp_namelen;
8307 long argt;
8308
8309 /* input() with a third argument: completion */
8310 rettv->vval.v_string = NULL;
8311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008312 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 if (xp_name == NULL)
8314 return;
8315
8316 xp_namelen = (int)STRLEN(xp_name);
8317
8318 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8319 &xp_arg) == FAIL)
8320 return;
8321 }
8322 }
8323
8324 if (defstr != NULL)
8325 {
8326 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 ex_normal_busy = 0;
8329 rettv->vval.v_string =
8330 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8331 xp_type, xp_arg);
8332 ex_normal_busy = save_ex_normal_busy;
8333 }
8334 if (inputdialog && rettv->vval.v_string == NULL
8335 && argvars[1].v_type != VAR_UNKNOWN
8336 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 &argvars[2], buf));
8339
8340 vim_free(xp_arg);
8341
8342 /* since the user typed this, no need to wait for return */
8343 need_wait_return = FALSE;
8344 msg_didout = FALSE;
8345 }
8346 cmd_silent = cmd_silent_save;
8347}
8348
8349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 * ":echo expr1 ..." print each argument separated with a space, add a
8351 * newline at the end.
8352 * ":echon expr1 ..." print each argument plain.
8353 */
8354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008355ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008359 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 char_u *p;
8361 int needclr = TRUE;
8362 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008364 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008365 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
8367 if (eap->skip)
8368 ++emsg_skip;
8369 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8370 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371 /* If eval1() causes an error message the text from the command may
8372 * still need to be cleared. E.g., "echo 22,44". */
8373 need_clr_eos = needclr;
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {
8378 /*
8379 * Report the invalid expression unless the expression evaluation
8380 * has been cancelled due to an aborting error, an interrupt, or an
8381 * exception.
8382 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008383 if (!aborting() && did_emsg == did_emsg_before
8384 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 break;
8388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008389 need_clr_eos = FALSE;
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 if (!eap->skip)
8392 {
8393 if (atstart)
8394 {
8395 atstart = FALSE;
8396 /* Call msg_start() after eval1(), evaluating the expression
8397 * may cause a message to appear. */
8398 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008399 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008400 /* Mark the saved text as finishing the line, so that what
8401 * follows is displayed on a new line when scrolling back
8402 * at the more prompt. */
8403 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 else if (eap->cmdidx == CMD_echo)
8408 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008409 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008410 if (p != NULL)
8411 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008413 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 if (*p != TAB && needclr)
8416 {
8417 /* remove any text still there from the command */
8418 msg_clr_eos();
8419 needclr = FALSE;
8420 }
8421 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
8423 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008424 {
8425#ifdef FEAT_MBYTE
8426 if (has_mbyte)
8427 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008428 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008429
8430 (void)msg_outtrans_len_attr(p, i, echo_attr);
8431 p += i - 1;
8432 }
8433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008435 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 arg = skipwhite(arg);
8442 }
8443 eap->nextcmd = check_nextcmd(arg);
8444
8445 if (eap->skip)
8446 --emsg_skip;
8447 else
8448 {
8449 /* remove text that may still be there from the command */
8450 if (needclr)
8451 msg_clr_eos();
8452 if (eap->cmdidx == CMD_echo)
8453 msg_end();
8454 }
8455}
8456
8457/*
8458 * ":echohl {name}".
8459 */
8460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008461ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008463 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464}
8465
8466/*
8467 * ":execute expr1 ..." execute the result of an expression.
8468 * ":echomsg expr1 ..." Print a message
8469 * ":echoerr expr1 ..." Print an error
8470 * Each gets spaces around each argument and a newline at the end for
8471 * echo commands
8472 */
8473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008474ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 int ret = OK;
8479 char_u *p;
8480 garray_T ga;
8481 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008482 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484 ga_init2(&ga, 1, 80);
8485
8486 if (eap->skip)
8487 ++emsg_skip;
8488 while (*arg != NUL && *arg != '|' && *arg != '\n')
8489 {
8490 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
8493 /*
8494 * Report the invalid expression unless the expression evaluation
8495 * has been cancelled due to an aborting error, an interrupt, or an
8496 * exception.
8497 */
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008498 if (!aborting() && did_emsg == save_did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 EMSG2(_(e_invexpr2), p);
8500 ret = FAIL;
8501 break;
8502 }
8503
8504 if (!eap->skip)
8505 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008506 char_u buf[NUMBUFLEN];
8507
8508 if (eap->cmdidx == CMD_execute)
8509 p = tv_get_string_buf(&rettv, buf);
8510 else
8511 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 len = (int)STRLEN(p);
8513 if (ga_grow(&ga, len + 2) == FAIL)
8514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 ret = FAIL;
8517 break;
8518 }
8519 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 ga.ga_len += len;
8523 }
8524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 arg = skipwhite(arg);
8527 }
8528
8529 if (ret != FAIL && ga.ga_data != NULL)
8530 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008531 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8532 {
8533 /* Mark the already saved text as finishing the line, so that what
8534 * follows is displayed on a new line when scrolling back at the
8535 * more prompt. */
8536 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008537 }
8538
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008542 out_flush();
8543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 else if (eap->cmdidx == CMD_echoerr)
8545 {
8546 /* We don't want to abort following commands, restore did_emsg. */
8547 save_did_emsg = did_emsg;
8548 EMSG((char_u *)ga.ga_data);
8549 if (!force_abort)
8550 did_emsg = save_did_emsg;
8551 }
8552 else if (eap->cmdidx == CMD_execute)
8553 do_cmdline((char_u *)ga.ga_data,
8554 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8555 }
8556
8557 ga_clear(&ga);
8558
8559 if (eap->skip)
8560 --emsg_skip;
8561
8562 eap->nextcmd = check_nextcmd(arg);
8563}
8564
8565/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 * Find window specified by "vp" in tabpage "tp".
8567 */
8568 win_T *
8569find_win_by_nr(
8570 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008571 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008574 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 if (nr < 0)
8577 return NULL;
8578 if (nr == 0)
8579 return curwin;
8580
Bram Moolenaar29323592016-07-24 22:04:11 +02008581 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8582 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 if (nr >= LOWEST_WIN_ID)
8584 {
8585 if (wp->w_id == nr)
8586 return wp;
8587 }
8588 else if (--nr <= 0)
8589 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008590 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 if (nr >= LOWEST_WIN_ID)
8592 return NULL;
8593 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594}
8595
8596/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008597 * Find a window: When using a Window ID in any tab page, when using a number
8598 * in the current tab page.
8599 */
8600 win_T *
8601find_win_by_nr_or_id(typval_T *vp)
8602{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008603 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008604
8605 if (nr >= LOWEST_WIN_ID)
8606 return win_id2wp(vp);
8607 return find_win_by_nr(vp, NULL);
8608}
8609
8610/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 * Find window specified by "wvp" in tabpage "tvp".
8612 */
8613 win_T *
8614find_tabwin(
8615 typval_T *wvp, /* VAR_UNKNOWN for current window */
8616 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8617{
8618 win_T *wp = NULL;
8619 tabpage_T *tp = NULL;
8620 long n;
8621
8622 if (wvp->v_type != VAR_UNKNOWN)
8623 {
8624 if (tvp->v_type != VAR_UNKNOWN)
8625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008626 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 if (n >= 0)
8628 tp = find_tabpage(n);
8629 }
8630 else
8631 tp = curtab;
8632
8633 if (tp != NULL)
8634 wp = find_win_by_nr(wvp, tp);
8635 }
8636 else
8637 wp = curwin;
8638
8639 return wp;
8640}
8641
8642/*
8643 * getwinvar() and gettabwinvar()
8644 */
8645 void
8646getwinvar(
8647 typval_T *argvars,
8648 typval_T *rettv,
8649 int off) /* 1 for gettabwinvar() */
8650{
8651 win_T *win;
8652 char_u *varname;
8653 dictitem_T *v;
8654 tabpage_T *tp = NULL;
8655 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 win_T *oldcurwin;
8657 tabpage_T *oldtabpage;
8658 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 else
8663 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008665 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 ++emsg_off;
8667
8668 rettv->v_type = VAR_STRING;
8669 rettv->vval.v_string = NULL;
8670
8671 if (win != NULL && varname != NULL)
8672 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008673 /* Set curwin to be our win, temporarily. Also set the tabpage,
8674 * otherwise the window is not valid. Only do this when needed,
8675 * autocommands get blocked. */
8676 need_switch_win = !(tp == curtab && win == curwin);
8677 if (!need_switch_win
8678 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008680 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008682 if (varname[1] == NUL)
8683 {
8684 /* get all window-local options in a dict */
8685 dict_T *opts = get_winbuf_options(FALSE);
8686
8687 if (opts != NULL)
8688 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008689 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008690 done = TRUE;
8691 }
8692 }
8693 else if (get_option_tv(&varname, rettv, 1) == OK)
8694 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 done = TRUE;
8696 }
8697 else
8698 {
8699 /* Look up the variable. */
8700 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8701 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8702 varname, FALSE);
8703 if (v != NULL)
8704 {
8705 copy_tv(&v->di_tv, rettv);
8706 done = TRUE;
8707 }
8708 }
8709 }
8710
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 if (need_switch_win)
8712 /* restore previous notion of curwin */
8713 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 }
8715
8716 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8717 /* use the default return value */
8718 copy_tv(&argvars[off + 2], rettv);
8719
8720 --emsg_off;
8721}
8722
8723/*
8724 * "setwinvar()" and "settabwinvar()" functions
8725 */
8726 void
8727setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8728{
8729 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 win_T *save_curwin;
8731 tabpage_T *save_curtab;
8732 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 char_u *varname, *winvarname;
8734 typval_T *varp;
8735 char_u nbuf[NUMBUFLEN];
8736 tabpage_T *tp = NULL;
8737
8738 if (check_restricted() || check_secure())
8739 return;
8740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008742 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 else
8744 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008746 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 varp = &argvars[off + 2];
8748
8749 if (win != NULL && varname != NULL && varp != NULL)
8750 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 need_switch_win = !(tp == curtab && win == curwin);
8752 if (!need_switch_win
8753 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 {
8755 if (*varname == '&')
8756 {
8757 long numval;
8758 char_u *strval;
8759 int error = FALSE;
8760
8761 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008762 numval = (long)tv_get_number_chk(varp, &error);
8763 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 if (!error && strval != NULL)
8765 set_option_value(varname, numval, strval, OPT_LOCAL);
8766 }
8767 else
8768 {
8769 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8770 if (winvarname != NULL)
8771 {
8772 STRCPY(winvarname, "w:");
8773 STRCPY(winvarname + 2, varname);
8774 set_var(winvarname, varp, TRUE);
8775 vim_free(winvarname);
8776 }
8777 }
8778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 if (need_switch_win)
8780 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 }
8782}
8783
8784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8786 * "arg" points to the "&" or '+' when called, to "option" when returning.
8787 * Returns NULL when no option name found. Otherwise pointer to the char
8788 * after the option name.
8789 */
8790 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008791find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
8793 char_u *p = *arg;
8794
8795 ++p;
8796 if (*p == 'g' && p[1] == ':')
8797 {
8798 *opt_flags = OPT_GLOBAL;
8799 p += 2;
8800 }
8801 else if (*p == 'l' && p[1] == ':')
8802 {
8803 *opt_flags = OPT_LOCAL;
8804 p += 2;
8805 }
8806 else
8807 *opt_flags = 0;
8808
8809 if (!ASCII_ISALPHA(*p))
8810 return NULL;
8811 *arg = p;
8812
8813 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8814 p += 4; /* termcap option */
8815 else
8816 while (ASCII_ISALPHA(*p))
8817 ++p;
8818 return p;
8819}
8820
8821/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008822 * Return the autoload script name for a function or variable name.
8823 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008825 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008826autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008827{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008828 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008829 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008830
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008831 /* Get the script file name: replace '#' with '/', append ".vim". */
8832 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8833 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008834 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008835 STRCPY(scriptname, "autoload/");
8836 STRCAT(scriptname, name);
8837 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8838 STRCAT(scriptname, ".vim");
8839 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8840 *p = '/';
8841 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008842}
8843
8844/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008845 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008846 * Return TRUE if a package was loaded.
8847 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008849script_autoload(
8850 char_u *name,
8851 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852{
8853 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008854 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008855 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008856 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008857
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008858 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008859 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008860 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008861 return FALSE;
8862
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008863 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008864
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008865 /* Find the name in the list of previously loaded package names. Skip
8866 * "autoload/", it's always the same. */
8867 for (i = 0; i < ga_loaded.ga_len; ++i)
8868 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8869 break;
8870 if (!reload && i < ga_loaded.ga_len)
8871 ret = FALSE; /* was loaded already */
8872 else
8873 {
8874 /* Remember the name if it wasn't loaded already. */
8875 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8876 {
8877 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8878 tofree = NULL;
8879 }
8880
8881 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008882 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008883 ret = TRUE;
8884 }
8885
8886 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008887 return ret;
8888}
8889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8891typedef enum
8892{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8894 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8895 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896} var_flavour_T;
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008899var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 char_u *p = varname;
8902
8903 if (ASCII_ISUPPER(*p))
8904 {
8905 while (*(++p))
8906 if (ASCII_ISLOWER(*p))
8907 return VAR_FLAVOUR_SESSION;
8908 return VAR_FLAVOUR_VIMINFO;
8909 }
8910 else
8911 return VAR_FLAVOUR_DEFAULT;
8912}
8913#endif
8914
8915#if defined(FEAT_VIMINFO) || defined(PROTO)
8916/*
8917 * Restore global vars that start with a capital from the viminfo file
8918 */
8919 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008920read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008923 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008925 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927 if (!writing && (find_viminfo_parameter('!') != NULL))
8928 {
8929 tab = vim_strchr(virp->vir_line + 1, '\t');
8930 if (tab != NULL)
8931 {
8932 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008933 switch (*tab)
8934 {
8935 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008936#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008937 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008939 case 'D': type = VAR_DICT; break;
8940 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008941 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008942 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945 tab = vim_strchr(tab, '\t');
8946 if (tab != NULL)
8947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948 tv.v_type = type;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008949 if (type == VAR_STRING || type == VAR_DICT ||
8950 type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008951 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008953#ifdef FEAT_FLOAT
8954 else if (type == VAR_FLOAT)
8955 (void)string2float(tab + 1, &tv.vval.v_float);
8956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008958 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008959 if (type == VAR_DICT || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008960 {
8961 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8962
8963 if (etv == NULL)
8964 /* Failed to parse back the dict or list, use it as a
8965 * string. */
8966 tv.v_type = VAR_STRING;
8967 else
8968 {
8969 vim_free(tv.vval.v_string);
8970 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008971 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008972 }
8973 }
8974
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008975 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008976 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008977 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008978 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008979
8980 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008981 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008982 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8983 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008984 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 }
8986 }
8987 }
8988
8989 return viminfo_readline(virp);
8990}
8991
8992/*
8993 * Write global vars that start with a capital to the viminfo file
8994 */
8995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008996write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 hashitem_T *hi;
8999 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009000 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009001 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009002 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009003 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005
9006 if (find_viminfo_parameter('!') == NULL)
9007 return;
9008
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009009 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009011 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009014 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009016 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 this_var = HI2DI(hi);
9018 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009021 {
9022 case VAR_STRING: s = "STR"; break;
9023 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009024 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009025 case VAR_DICT: s = "DIC"; break;
9026 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009027 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009028 case VAR_SPECIAL: s = "XPL"; break;
9029
9030 case VAR_UNKNOWN:
9031 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009032 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009033 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009034 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009035 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009038 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009039 if (p != NULL)
9040 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009041 vim_free(tofree);
9042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044 }
9045}
9046#endif
9047
9048#if defined(FEAT_SESSION) || defined(PROTO)
9049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 hashitem_T *hi;
9053 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009054 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 char_u *p, *t;
9056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009057 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009060 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009062 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 this_var = HI2DI(hi);
9064 if ((this_var->di_tv.v_type == VAR_NUMBER
9065 || this_var->di_tv.v_type == VAR_STRING)
9066 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009068 /* Escape special characters with a backslash. Turn a LF and
9069 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009070 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009071 (char_u *)"\\\"\n\r");
9072 if (p == NULL) /* out of memory */
9073 break;
9074 for (t = p; *t != NUL; ++t)
9075 if (*t == '\n')
9076 *t = 'n';
9077 else if (*t == '\r')
9078 *t = 'r';
9079 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 this_var->di_key,
9081 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9082 : ' ',
9083 p,
9084 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9085 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009086 || put_eol(fd) == FAIL)
9087 {
9088 vim_free(p);
9089 return FAIL;
9090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 vim_free(p);
9092 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009093#ifdef FEAT_FLOAT
9094 else if (this_var->di_tv.v_type == VAR_FLOAT
9095 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9096 {
9097 float_T f = this_var->di_tv.vval.v_float;
9098 int sign = ' ';
9099
9100 if (f < 0)
9101 {
9102 f = -f;
9103 sign = '-';
9104 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009105 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009106 this_var->di_key, sign, f) < 0)
9107 || put_eol(fd) == FAIL)
9108 return FAIL;
9109 }
9110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 }
9112 }
9113 return OK;
9114}
9115#endif
9116
Bram Moolenaar661b1822005-07-28 22:36:45 +00009117/*
9118 * Display script name where an item was last set.
9119 * Should only be invoked when 'verbose' is non-zero.
9120 */
9121 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009122last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009123{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009124 char_u *p;
9125
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009126 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009127 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009128 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009129 if (p != NULL)
9130 {
9131 verbose_enter();
9132 MSG_PUTS(_("\n\tLast set from "));
9133 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009134 if (script_ctx.sc_lnum > 0)
9135 {
9136 MSG_PUTS(_(" line "));
9137 msg_outnum((long)script_ctx.sc_lnum);
9138 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009139 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009140 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009141 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009142 }
9143}
9144
Bram Moolenaar53744302015-07-17 17:38:22 +02009145/* reset v:option_new, v:option_old and v:option_type */
9146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009147reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009148{
9149 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9150 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9151 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9152}
9153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154/*
9155 * Prepare "gap" for an assert error and add the sourcing position.
9156 */
9157 void
9158prepare_assert_error(garray_T *gap)
9159{
9160 char buf[NUMBUFLEN];
9161
9162 ga_init2(gap, 1, 100);
9163 if (sourcing_name != NULL)
9164 {
9165 ga_concat(gap, sourcing_name);
9166 if (sourcing_lnum > 0)
9167 ga_concat(gap, (char_u *)" ");
9168 }
9169 if (sourcing_lnum > 0)
9170 {
9171 sprintf(buf, "line %ld", (long)sourcing_lnum);
9172 ga_concat(gap, (char_u *)buf);
9173 }
9174 if (sourcing_name != NULL || sourcing_lnum > 0)
9175 ga_concat(gap, (char_u *)": ");
9176}
9177
9178/*
9179 * Add an assert error to v:errors.
9180 */
9181 void
9182assert_error(garray_T *gap)
9183{
9184 struct vimvar *vp = &vimvars[VV_ERRORS];
9185
9186 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9187 /* Make sure v:errors is a list. */
9188 set_vim_var_list(VV_ERRORS, list_alloc());
9189 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9190}
9191
Bram Moolenaar65a54642018-04-28 16:56:53 +02009192 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193assert_equal_common(typval_T *argvars, assert_type_T atype)
9194{
9195 garray_T ga;
9196
9197 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9198 != (atype == ASSERT_EQUAL))
9199 {
9200 prepare_assert_error(&ga);
9201 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9202 atype);
9203 assert_error(&ga);
9204 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009205 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009207 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208}
9209
Bram Moolenaar65a54642018-04-28 16:56:53 +02009210 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009211assert_equalfile(typval_T *argvars)
9212{
9213 char_u buf1[NUMBUFLEN];
9214 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009215 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9216 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009217 garray_T ga;
9218 FILE *fd1;
9219 FILE *fd2;
9220
9221 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009222 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009223
9224 IObuff[0] = NUL;
9225 fd1 = mch_fopen((char *)fname1, READBIN);
9226 if (fd1 == NULL)
9227 {
9228 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9229 }
9230 else
9231 {
9232 fd2 = mch_fopen((char *)fname2, READBIN);
9233 if (fd2 == NULL)
9234 {
9235 fclose(fd1);
9236 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9237 }
9238 else
9239 {
9240 int c1, c2;
9241 long count = 0;
9242
9243 for (;;)
9244 {
9245 c1 = fgetc(fd1);
9246 c2 = fgetc(fd2);
9247 if (c1 == EOF)
9248 {
9249 if (c2 != EOF)
9250 STRCPY(IObuff, "first file is shorter");
9251 break;
9252 }
9253 else if (c2 == EOF)
9254 {
9255 STRCPY(IObuff, "second file is shorter");
9256 break;
9257 }
9258 else if (c1 != c2)
9259 {
9260 vim_snprintf((char *)IObuff, IOSIZE,
9261 "difference at byte %ld", count);
9262 break;
9263 }
9264 ++count;
9265 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009266 fclose(fd1);
9267 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009268 }
9269 }
9270 if (IObuff[0] != NUL)
9271 {
9272 prepare_assert_error(&ga);
9273 ga_concat(&ga, IObuff);
9274 assert_error(&ga);
9275 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009276 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009277 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009278 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009279}
9280
Bram Moolenaar65a54642018-04-28 16:56:53 +02009281 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282assert_match_common(typval_T *argvars, assert_type_T atype)
9283{
9284 garray_T ga;
9285 char_u buf1[NUMBUFLEN];
9286 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009287 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9288 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289
9290 if (pat == NULL || text == NULL)
9291 EMSG(_(e_invarg));
9292 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9293 {
9294 prepare_assert_error(&ga);
9295 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9296 atype);
9297 assert_error(&ga);
9298 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009299 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009301 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302}
9303
Bram Moolenaar65a54642018-04-28 16:56:53 +02009304 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009305assert_inrange(typval_T *argvars)
9306{
9307 garray_T ga;
9308 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009309 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9310 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9311 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009312 char_u *tofree;
9313 char msg[200];
9314 char_u numbuf[NUMBUFLEN];
9315
9316 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009317 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009318 if (actual < lower || actual > upper)
9319 {
9320 prepare_assert_error(&ga);
9321 if (argvars[3].v_type != VAR_UNKNOWN)
9322 {
9323 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9324 vim_free(tofree);
9325 }
9326 else
9327 {
9328 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9329 (long)lower, (long)upper, (long)actual);
9330 ga_concat(&ga, (char_u *)msg);
9331 }
9332 assert_error(&ga);
9333 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009334 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009335 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009336 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009337}
9338
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339/*
9340 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009341 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009343 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344assert_bool(typval_T *argvars, int isTrue)
9345{
9346 int error = FALSE;
9347 garray_T ga;
9348
9349 if (argvars[0].v_type == VAR_SPECIAL
9350 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009351 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009353 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354 || error)
9355 {
9356 prepare_assert_error(&ga);
9357 fill_assert_error(&ga, &argvars[1],
9358 (char_u *)(isTrue ? "True" : "False"),
9359 NULL, &argvars[0], ASSERT_OTHER);
9360 assert_error(&ga);
9361 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009362 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009364 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365}
9366
Bram Moolenaar65a54642018-04-28 16:56:53 +02009367 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009368assert_report(typval_T *argvars)
9369{
9370 garray_T ga;
9371
9372 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009373 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009374 assert_error(&ga);
9375 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009376 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009377}
9378
Bram Moolenaar65a54642018-04-28 16:56:53 +02009379 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380assert_exception(typval_T *argvars)
9381{
9382 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009383 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384
9385 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9386 {
9387 prepare_assert_error(&ga);
9388 ga_concat(&ga, (char_u *)"v:exception is not set");
9389 assert_error(&ga);
9390 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009391 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392 }
9393 else if (error != NULL
9394 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9395 {
9396 prepare_assert_error(&ga);
9397 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9398 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9399 assert_error(&ga);
9400 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009401 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009403 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404}
9405
Bram Moolenaar65a54642018-04-28 16:56:53 +02009406 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009407assert_beeps(typval_T *argvars)
9408{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009409 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009410 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009411 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009412
9413 called_vim_beep = FALSE;
9414 suppress_errthrow = TRUE;
9415 emsg_silent = FALSE;
9416 do_cmdline_cmd(cmd);
9417 if (!called_vim_beep)
9418 {
9419 prepare_assert_error(&ga);
9420 ga_concat(&ga, (char_u *)"command did not beep: ");
9421 ga_concat(&ga, cmd);
9422 assert_error(&ga);
9423 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009424 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009425 }
9426
9427 suppress_errthrow = FALSE;
9428 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009429 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009430}
9431
Bram Moolenaar65a54642018-04-28 16:56:53 +02009432 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009433assert_fails(typval_T *argvars)
9434{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009435 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009437 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009438 char_u numbuf[NUMBUFLEN];
9439 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009440
9441 called_emsg = FALSE;
9442 suppress_errthrow = TRUE;
9443 emsg_silent = TRUE;
9444 do_cmdline_cmd(cmd);
9445 if (!called_emsg)
9446 {
9447 prepare_assert_error(&ga);
9448 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009449 if (argvars[1].v_type != VAR_UNKNOWN
9450 && argvars[2].v_type != VAR_UNKNOWN)
9451 {
9452 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9453 vim_free(tofree);
9454 }
9455 else
9456 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457 assert_error(&ga);
9458 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009459 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 }
9461 else if (argvars[1].v_type != VAR_UNKNOWN)
9462 {
9463 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009464 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465
9466 if (error == NULL
9467 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9468 {
9469 prepare_assert_error(&ga);
9470 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9471 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9472 assert_error(&ga);
9473 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009474 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475 }
9476 }
9477
9478 called_emsg = FALSE;
9479 suppress_errthrow = FALSE;
9480 emsg_silent = FALSE;
9481 emsg_on_display = FALSE;
9482 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009483 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484}
9485
9486/*
9487 * Append "str" to "gap", escaping unprintable characters.
9488 * Changes NL to \n, CR to \r, etc.
9489 */
9490 static void
9491ga_concat_esc(garray_T *gap, char_u *str)
9492{
9493 char_u *p;
9494 char_u buf[NUMBUFLEN];
9495
9496 if (str == NULL)
9497 {
9498 ga_concat(gap, (char_u *)"NULL");
9499 return;
9500 }
9501
9502 for (p = str; *p != NUL; ++p)
9503 switch (*p)
9504 {
9505 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9506 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9507 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9508 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9509 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9510 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9511 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9512 default:
9513 if (*p < ' ')
9514 {
9515 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9516 ga_concat(gap, buf);
9517 }
9518 else
9519 ga_append(gap, *p);
9520 break;
9521 }
9522}
9523
9524/*
9525 * Fill "gap" with information about an assert error.
9526 */
9527 void
9528fill_assert_error(
9529 garray_T *gap,
9530 typval_T *opt_msg_tv,
9531 char_u *exp_str,
9532 typval_T *exp_tv,
9533 typval_T *got_tv,
9534 assert_type_T atype)
9535{
9536 char_u numbuf[NUMBUFLEN];
9537 char_u *tofree;
9538
9539 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9540 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009541 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9542 vim_free(tofree);
9543 ga_concat(gap, (char_u *)": ");
9544 }
9545
9546 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9547 ga_concat(gap, (char_u *)"Pattern ");
9548 else if (atype == ASSERT_NOTEQUAL)
9549 ga_concat(gap, (char_u *)"Expected not equal to ");
9550 else
9551 ga_concat(gap, (char_u *)"Expected ");
9552 if (exp_str == NULL)
9553 {
9554 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 vim_free(tofree);
9556 }
9557 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009558 ga_concat_esc(gap, exp_str);
9559 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009561 if (atype == ASSERT_MATCH)
9562 ga_concat(gap, (char_u *)" does not match ");
9563 else if (atype == ASSERT_NOTMATCH)
9564 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009566 ga_concat(gap, (char_u *)" but got ");
9567 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9568 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 }
9570}
9571
Bram Moolenaar31988702018-02-13 12:57:42 +01009572/*
9573 * Compare "typ1" and "typ2". Put the result in "typ1".
9574 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009575 int
9576typval_compare(
9577 typval_T *typ1, /* first operand */
9578 typval_T *typ2, /* second operand */
9579 exptype_T type, /* operator */
9580 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009581 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009582{
9583 int i;
9584 varnumber_T n1, n2;
9585 char_u *s1, *s2;
9586 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9587
Bram Moolenaar31988702018-02-13 12:57:42 +01009588 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009589 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009590 /* For "is" a different type always means FALSE, for "notis"
9591 * it means TRUE. */
9592 n1 = (type == TYPE_NEQUAL);
9593 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009594 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9595 {
9596 if (type_is)
9597 {
9598 n1 = (typ1->v_type == typ2->v_type
9599 && typ1->vval.v_blob == typ2->vval.v_blob);
9600 if (type == TYPE_NEQUAL)
9601 n1 = !n1;
9602 }
9603 else if (typ1->v_type != typ2->v_type
9604 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9605 {
9606 if (typ1->v_type != typ2->v_type)
9607 EMSG(_("E977: Can only compare Blob with Blob"));
9608 else
9609 EMSG(_(e_invalblob));
9610 clear_tv(typ1);
9611 return FAIL;
9612 }
9613 else
9614 {
9615 // Compare two Blobs for being equal or unequal.
9616 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9617 if (type == TYPE_NEQUAL)
9618 n1 = !n1;
9619 }
9620 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009621 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9622 {
9623 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009624 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009625 n1 = (typ1->v_type == typ2->v_type
9626 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009627 if (type == TYPE_NEQUAL)
9628 n1 = !n1;
9629 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009630 else if (typ1->v_type != typ2->v_type
9631 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009632 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009633 if (typ1->v_type != typ2->v_type)
9634 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009635 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009636 EMSG(_("E692: Invalid operation for List"));
9637 clear_tv(typ1);
9638 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009639 }
9640 else
9641 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009642 /* Compare two Lists for being equal or unequal. */
9643 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9644 ic, FALSE);
9645 if (type == TYPE_NEQUAL)
9646 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009647 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009648 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009649
9650 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9651 {
9652 if (type_is)
9653 {
9654 n1 = (typ1->v_type == typ2->v_type
9655 && typ1->vval.v_dict == typ2->vval.v_dict);
9656 if (type == TYPE_NEQUAL)
9657 n1 = !n1;
9658 }
9659 else if (typ1->v_type != typ2->v_type
9660 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9661 {
9662 if (typ1->v_type != typ2->v_type)
9663 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9664 else
9665 EMSG(_("E736: Invalid operation for Dictionary"));
9666 clear_tv(typ1);
9667 return FAIL;
9668 }
9669 else
9670 {
9671 /* Compare two Dictionaries for being equal or unequal. */
9672 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9673 ic, FALSE);
9674 if (type == TYPE_NEQUAL)
9675 n1 = !n1;
9676 }
9677 }
9678
9679 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9680 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9681 {
9682 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9683 {
9684 EMSG(_("E694: Invalid operation for Funcrefs"));
9685 clear_tv(typ1);
9686 return FAIL;
9687 }
9688 if ((typ1->v_type == VAR_PARTIAL
9689 && typ1->vval.v_partial == NULL)
9690 || (typ2->v_type == VAR_PARTIAL
9691 && typ2->vval.v_partial == NULL))
9692 /* when a partial is NULL assume not equal */
9693 n1 = FALSE;
9694 else if (type_is)
9695 {
9696 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9697 /* strings are considered the same if their value is
9698 * the same */
9699 n1 = tv_equal(typ1, typ2, ic, FALSE);
9700 else if (typ1->v_type == VAR_PARTIAL
9701 && typ2->v_type == VAR_PARTIAL)
9702 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9703 else
9704 n1 = FALSE;
9705 }
9706 else
9707 n1 = tv_equal(typ1, typ2, ic, FALSE);
9708 if (type == TYPE_NEQUAL)
9709 n1 = !n1;
9710 }
9711
9712#ifdef FEAT_FLOAT
9713 /*
9714 * If one of the two variables is a float, compare as a float.
9715 * When using "=~" or "!~", always compare as string.
9716 */
9717 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9718 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9719 {
9720 float_T f1, f2;
9721
9722 if (typ1->v_type == VAR_FLOAT)
9723 f1 = typ1->vval.v_float;
9724 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009725 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009726 if (typ2->v_type == VAR_FLOAT)
9727 f2 = typ2->vval.v_float;
9728 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009729 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009730 n1 = FALSE;
9731 switch (type)
9732 {
9733 case TYPE_EQUAL: n1 = (f1 == f2); break;
9734 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9735 case TYPE_GREATER: n1 = (f1 > f2); break;
9736 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9737 case TYPE_SMALLER: n1 = (f1 < f2); break;
9738 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9739 case TYPE_UNKNOWN:
9740 case TYPE_MATCH:
9741 case TYPE_NOMATCH: break; /* avoid gcc warning */
9742 }
9743 }
9744#endif
9745
9746 /*
9747 * If one of the two variables is a number, compare as a number.
9748 * When using "=~" or "!~", always compare as string.
9749 */
9750 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9751 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009753 n1 = tv_get_number(typ1);
9754 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009755 switch (type)
9756 {
9757 case TYPE_EQUAL: n1 = (n1 == n2); break;
9758 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9759 case TYPE_GREATER: n1 = (n1 > n2); break;
9760 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9761 case TYPE_SMALLER: n1 = (n1 < n2); break;
9762 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9763 case TYPE_UNKNOWN:
9764 case TYPE_MATCH:
9765 case TYPE_NOMATCH: break; /* avoid gcc warning */
9766 }
9767 }
9768 else
9769 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009770 s1 = tv_get_string_buf(typ1, buf1);
9771 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009772 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9773 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9774 else
9775 i = 0;
9776 n1 = FALSE;
9777 switch (type)
9778 {
9779 case TYPE_EQUAL: n1 = (i == 0); break;
9780 case TYPE_NEQUAL: n1 = (i != 0); break;
9781 case TYPE_GREATER: n1 = (i > 0); break;
9782 case TYPE_GEQUAL: n1 = (i >= 0); break;
9783 case TYPE_SMALLER: n1 = (i < 0); break;
9784 case TYPE_SEQUAL: n1 = (i <= 0); break;
9785
9786 case TYPE_MATCH:
9787 case TYPE_NOMATCH:
9788 n1 = pattern_match(s2, s1, ic);
9789 if (type == TYPE_NOMATCH)
9790 n1 = !n1;
9791 break;
9792
9793 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9794 }
9795 }
9796 clear_tv(typ1);
9797 typ1->v_type = VAR_NUMBER;
9798 typ1->vval.v_number = n1;
9799
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009800 return OK;
9801}
9802
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009803 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009804typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009805{
9806 char_u *tofree;
9807 char_u numbuf[NUMBUFLEN];
9808 char_u *ret = NULL;
9809
9810 if (arg == NULL)
9811 return vim_strsave((char_u *)"(does not exist)");
9812 ret = tv2string(arg, &tofree, numbuf, 0);
9813 /* Make a copy if we have a value but it's not in allocated memory. */
9814 if (ret != NULL && tofree == NULL)
9815 ret = vim_strsave(ret);
9816 return ret;
9817}
9818
9819 int
9820var_exists(char_u *var)
9821{
9822 char_u *name;
9823 char_u *tofree;
9824 typval_T tv;
9825 int len = 0;
9826 int n = FALSE;
9827
9828 /* get_name_len() takes care of expanding curly braces */
9829 name = var;
9830 len = get_name_len(&var, &tofree, TRUE, FALSE);
9831 if (len > 0)
9832 {
9833 if (tofree != NULL)
9834 name = tofree;
9835 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9836 if (n)
9837 {
9838 /* handle d.key, l[idx], f(expr) */
9839 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9840 if (n)
9841 clear_tv(&tv);
9842 }
9843 }
9844 if (*var != NUL)
9845 n = FALSE;
9846
9847 vim_free(tofree);
9848 return n;
9849}
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851#endif /* FEAT_EVAL */
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009854#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
9856#ifdef WIN3264
9857/*
9858 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
9861/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009862 * Get the short path (8.3) for the filename in "fnamep".
9863 * Only works for a valid file name.
9864 * When the path gets longer "fnamep" is changed and the allocated buffer
9865 * is put in "bufp".
9866 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9867 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 */
9869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009870get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009872 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 char_u *newbuf;
9874
9875 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009876 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 if (l > len - 1)
9878 {
9879 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009880 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 newbuf = vim_strnsave(*fnamep, l);
9882 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009883 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
9885 vim_free(*bufp);
9886 *fnamep = *bufp = newbuf;
9887
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009888 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009889 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 }
9891
9892 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009893 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
9896/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009897 * Get the short path (8.3) for the filename in "fname". The converted
9898 * path is returned in "bufp".
9899 *
9900 * Some of the directories specified in "fname" may not exist. This function
9901 * will shorten the existing directories at the beginning of the path and then
9902 * append the remaining non-existing path.
9903 *
9904 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009905 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009906 * bufp - Pointer to an allocated buffer for the filename.
9907 * fnamelen - Length of the filename pointed to by fname
9908 *
9909 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 */
9911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009912shortpath_for_invalid_fname(
9913 char_u **fname,
9914 char_u **bufp,
9915 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009917 char_u *short_fname, *save_fname, *pbuf_unused;
9918 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009920 int old_len, len;
9921 int new_len, sfx_len;
9922 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
9924 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009925 old_len = *fnamelen;
9926 save_fname = vim_strnsave(*fname, old_len);
9927 pbuf_unused = NULL;
9928 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009930 endp = save_fname + old_len - 1; /* Find the end of the copy */
9931 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009933 /*
9934 * Try shortening the supplied path till it succeeds by removing one
9935 * directory at a time from the tail of the path.
9936 */
9937 len = 0;
9938 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009940 /* go back one path-separator */
9941 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9942 --endp;
9943 if (endp <= save_fname)
9944 break; /* processed the complete path */
9945
9946 /*
9947 * Replace the path separator with a NUL and try to shorten the
9948 * resulting path.
9949 */
9950 ch = *endp;
9951 *endp = 0;
9952 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009953 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009954 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9955 {
9956 retval = FAIL;
9957 goto theend;
9958 }
9959 *endp = ch; /* preserve the string */
9960
9961 if (len > 0)
9962 break; /* successfully shortened the path */
9963
9964 /* failed to shorten the path. Skip the path separator */
9965 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 }
9967
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009968 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009970 /*
9971 * Succeeded in shortening the path. Now concatenate the shortened
9972 * path with the remaining path at the tail.
9973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009975 /* Compute the length of the new path. */
9976 sfx_len = (int)(save_endp - endp) + 1;
9977 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009979 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009981 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009983 /* There is not enough space in the currently allocated string,
9984 * copy it to a buffer big enough. */
9985 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009987 {
9988 retval = FAIL;
9989 goto theend;
9990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992 else
9993 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009994 /* Transfer short_fname to the main buffer (it's big enough),
9995 * unless get_short_pathname() did its work in-place. */
9996 *fname = *bufp = save_fname;
9997 if (short_fname != save_fname)
9998 vim_strncpy(save_fname, short_fname, len);
9999 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010001
10002 /* concat the not-shortened part of the path */
10003 vim_strncpy(*fname + len, endp, sfx_len);
10004 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010006
10007theend:
10008 vim_free(pbuf_unused);
10009 vim_free(save_fname);
10010
10011 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012}
10013
10014/*
10015 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010016 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 */
10018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010019shortpath_for_partial(
10020 char_u **fnamep,
10021 char_u **bufp,
10022 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024 int sepcount, len, tflen;
10025 char_u *p;
10026 char_u *pbuf, *tfname;
10027 int hasTilde;
10028
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010029 /* Count up the path separators from the RHS.. so we know which part
10030 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010032 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 if (vim_ispathsep(*p))
10034 ++sepcount;
10035
10036 /* Need full path first (use expand_env() to remove a "~/") */
10037 hasTilde = (**fnamep == '~');
10038 if (hasTilde)
10039 pbuf = tfname = expand_env_save(*fnamep);
10040 else
10041 pbuf = tfname = FullName_save(*fnamep, FALSE);
10042
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010043 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010045 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
10048 if (len == 0)
10049 {
10050 /* Don't have a valid filename, so shorten the rest of the
10051 * path if we can. This CAN give us invalid 8.3 filenames, but
10052 * there's not a lot of point in guessing what it might be.
10053 */
10054 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010055 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10056 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058
10059 /* Count the paths backward to find the beginning of the desired string. */
10060 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010061 {
10062#ifdef FEAT_MBYTE
10063 if (has_mbyte)
10064 p -= mb_head_off(tfname, p);
10065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 if (vim_ispathsep(*p))
10067 {
10068 if (sepcount == 0 || (hasTilde && sepcount == 1))
10069 break;
10070 else
10071 sepcount --;
10072 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 if (hasTilde)
10075 {
10076 --p;
10077 if (p >= tfname)
10078 *p = '~';
10079 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010080 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 }
10082 else
10083 ++p;
10084
10085 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10086 vim_free(*bufp);
10087 *fnamelen = (int)STRLEN(p);
10088 *bufp = pbuf;
10089 *fnamep = p;
10090
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010091 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092}
10093#endif /* WIN3264 */
10094
10095/*
10096 * Adjust a filename, according to a string of modifiers.
10097 * *fnamep must be NUL terminated when called. When returning, the length is
10098 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010099 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 * When there is an error, *fnamep is set to NULL.
10101 */
10102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010103modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010104 char_u *src, // string with modifiers
10105 int tilde_file, // "~" is a file name, not $HOME
10106 int *usedlen, // characters after src that are used
10107 char_u **fnamep, // file name so far
10108 char_u **bufp, // buffer for allocated file name or NULL
10109 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
10111 int valid = 0;
10112 char_u *tail;
10113 char_u *s, *p, *pbuf;
10114 char_u dirname[MAXPATHL];
10115 int c;
10116 int has_fullname = 0;
10117#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010118 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 int has_shortname = 0;
10120#endif
10121
10122repeat:
10123 /* ":p" - full path/file_name */
10124 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10125 {
10126 has_fullname = 1;
10127
10128 valid |= VALID_PATH;
10129 *usedlen += 2;
10130
10131 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10132 if ((*fnamep)[0] == '~'
10133#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10134 && ((*fnamep)[1] == '/'
10135# ifdef BACKSLASH_IN_FILENAME
10136 || (*fnamep)[1] == '\\'
10137# endif
10138 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010140 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 )
10142 {
10143 *fnamep = expand_env_save(*fnamep);
10144 vim_free(*bufp); /* free any allocated file name */
10145 *bufp = *fnamep;
10146 if (*fnamep == NULL)
10147 return -1;
10148 }
10149
10150 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010151 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 {
10153 if (vim_ispathsep(*p)
10154 && p[1] == '.'
10155 && (p[2] == NUL
10156 || vim_ispathsep(p[2])
10157 || (p[2] == '.'
10158 && (p[3] == NUL || vim_ispathsep(p[3])))))
10159 break;
10160 }
10161
10162 /* FullName_save() is slow, don't use it when not needed. */
10163 if (*p != NUL || !vim_isAbsName(*fnamep))
10164 {
10165 *fnamep = FullName_save(*fnamep, *p != NUL);
10166 vim_free(*bufp); /* free any allocated file name */
10167 *bufp = *fnamep;
10168 if (*fnamep == NULL)
10169 return -1;
10170 }
10171
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010172#ifdef WIN3264
10173# if _WIN32_WINNT >= 0x0500
10174 if (vim_strchr(*fnamep, '~') != NULL)
10175 {
10176 /* Expand 8.3 filename to full path. Needed to make sure the same
10177 * file does not have two different names.
10178 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10179 p = alloc(_MAX_PATH + 1);
10180 if (p != NULL)
10181 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010182 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010183 {
10184 vim_free(*bufp);
10185 *bufp = *fnamep = p;
10186 }
10187 else
10188 vim_free(p);
10189 }
10190 }
10191# endif
10192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 /* Append a path separator to a directory. */
10194 if (mch_isdir(*fnamep))
10195 {
10196 /* Make room for one or two extra characters. */
10197 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10198 vim_free(*bufp); /* free any allocated file name */
10199 *bufp = *fnamep;
10200 if (*fnamep == NULL)
10201 return -1;
10202 add_pathsep(*fnamep);
10203 }
10204 }
10205
10206 /* ":." - path relative to the current directory */
10207 /* ":~" - path relative to the home directory */
10208 /* ":8" - shortname path - postponed till after */
10209 while (src[*usedlen] == ':'
10210 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10211 {
10212 *usedlen += 2;
10213 if (c == '8')
10214 {
10215#ifdef WIN3264
10216 has_shortname = 1; /* Postpone this. */
10217#endif
10218 continue;
10219 }
10220 pbuf = NULL;
10221 /* Need full path first (use expand_env() to remove a "~/") */
10222 if (!has_fullname)
10223 {
10224 if (c == '.' && **fnamep == '~')
10225 p = pbuf = expand_env_save(*fnamep);
10226 else
10227 p = pbuf = FullName_save(*fnamep, FALSE);
10228 }
10229 else
10230 p = *fnamep;
10231
10232 has_fullname = 0;
10233
10234 if (p != NULL)
10235 {
10236 if (c == '.')
10237 {
10238 mch_dirname(dirname, MAXPATHL);
10239 s = shorten_fname(p, dirname);
10240 if (s != NULL)
10241 {
10242 *fnamep = s;
10243 if (pbuf != NULL)
10244 {
10245 vim_free(*bufp); /* free any allocated file name */
10246 *bufp = pbuf;
10247 pbuf = NULL;
10248 }
10249 }
10250 }
10251 else
10252 {
10253 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10254 /* Only replace it when it starts with '~' */
10255 if (*dirname == '~')
10256 {
10257 s = vim_strsave(dirname);
10258 if (s != NULL)
10259 {
10260 *fnamep = s;
10261 vim_free(*bufp);
10262 *bufp = s;
10263 }
10264 }
10265 }
10266 vim_free(pbuf);
10267 }
10268 }
10269
10270 tail = gettail(*fnamep);
10271 *fnamelen = (int)STRLEN(*fnamep);
10272
10273 /* ":h" - head, remove "/file_name", can be repeated */
10274 /* Don't remove the first "/" or "c:\" */
10275 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10276 {
10277 valid |= VALID_HEAD;
10278 *usedlen += 2;
10279 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010280 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010281 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 *fnamelen = (int)(tail - *fnamep);
10283#ifdef VMS
10284 if (*fnamelen > 0)
10285 *fnamelen += 1; /* the path separator is part of the path */
10286#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010287 if (*fnamelen == 0)
10288 {
10289 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10290 p = vim_strsave((char_u *)".");
10291 if (p == NULL)
10292 return -1;
10293 vim_free(*bufp);
10294 *bufp = *fnamep = tail = p;
10295 *fnamelen = 1;
10296 }
10297 else
10298 {
10299 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010300 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 }
10303
10304 /* ":8" - shortname */
10305 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10306 {
10307 *usedlen += 2;
10308#ifdef WIN3264
10309 has_shortname = 1;
10310#endif
10311 }
10312
10313#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010314 /*
10315 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 */
10317 if (has_shortname)
10318 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010319 /* Copy the string if it is shortened by :h and when it wasn't copied
10320 * yet, because we are going to change it in place. Avoids changing
10321 * the buffer name for "%:8". */
10322 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 {
10324 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010325 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 return -1;
10327 vim_free(*bufp);
10328 *bufp = *fnamep = p;
10329 }
10330
10331 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010332 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (!has_fullname && !vim_isAbsName(*fnamep))
10334 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010335 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 return -1;
10337 }
10338 else
10339 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010340 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341
Bram Moolenaardc935552011-08-17 15:23:23 +020010342 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010344 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 return -1;
10346
10347 if (l == 0)
10348 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010349 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010351 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 return -1;
10353 }
10354 *fnamelen = l;
10355 }
10356 }
10357#endif /* WIN3264 */
10358
10359 /* ":t" - tail, just the basename */
10360 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10361 {
10362 *usedlen += 2;
10363 *fnamelen -= (int)(tail - *fnamep);
10364 *fnamep = tail;
10365 }
10366
10367 /* ":e" - extension, can be repeated */
10368 /* ":r" - root, without extension, can be repeated */
10369 while (src[*usedlen] == ':'
10370 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10371 {
10372 /* find a '.' in the tail:
10373 * - for second :e: before the current fname
10374 * - otherwise: The last '.'
10375 */
10376 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10377 s = *fnamep - 2;
10378 else
10379 s = *fnamep + *fnamelen - 1;
10380 for ( ; s > tail; --s)
10381 if (s[0] == '.')
10382 break;
10383 if (src[*usedlen + 1] == 'e') /* :e */
10384 {
10385 if (s > tail)
10386 {
10387 *fnamelen += (int)(*fnamep - (s + 1));
10388 *fnamep = s + 1;
10389#ifdef VMS
10390 /* cut version from the extension */
10391 s = *fnamep + *fnamelen - 1;
10392 for ( ; s > *fnamep; --s)
10393 if (s[0] == ';')
10394 break;
10395 if (s > *fnamep)
10396 *fnamelen = s - *fnamep;
10397#endif
10398 }
10399 else if (*fnamep <= tail)
10400 *fnamelen = 0;
10401 }
10402 else /* :r */
10403 {
10404 if (s > tail) /* remove one extension */
10405 *fnamelen = (int)(s - *fnamep);
10406 }
10407 *usedlen += 2;
10408 }
10409
10410 /* ":s?pat?foo?" - substitute */
10411 /* ":gs?pat?foo?" - global substitute */
10412 if (src[*usedlen] == ':'
10413 && (src[*usedlen + 1] == 's'
10414 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10415 {
10416 char_u *str;
10417 char_u *pat;
10418 char_u *sub;
10419 int sep;
10420 char_u *flags;
10421 int didit = FALSE;
10422
10423 flags = (char_u *)"";
10424 s = src + *usedlen + 2;
10425 if (src[*usedlen + 1] == 'g')
10426 {
10427 flags = (char_u *)"g";
10428 ++s;
10429 }
10430
10431 sep = *s++;
10432 if (sep)
10433 {
10434 /* find end of pattern */
10435 p = vim_strchr(s, sep);
10436 if (p != NULL)
10437 {
10438 pat = vim_strnsave(s, (int)(p - s));
10439 if (pat != NULL)
10440 {
10441 s = p + 1;
10442 /* find end of substitution */
10443 p = vim_strchr(s, sep);
10444 if (p != NULL)
10445 {
10446 sub = vim_strnsave(s, (int)(p - s));
10447 str = vim_strnsave(*fnamep, *fnamelen);
10448 if (sub != NULL && str != NULL)
10449 {
10450 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010451 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 if (s != NULL)
10453 {
10454 *fnamep = s;
10455 *fnamelen = (int)STRLEN(s);
10456 vim_free(*bufp);
10457 *bufp = s;
10458 didit = TRUE;
10459 }
10460 }
10461 vim_free(sub);
10462 vim_free(str);
10463 }
10464 vim_free(pat);
10465 }
10466 }
10467 /* after using ":s", repeat all the modifiers */
10468 if (didit)
10469 goto repeat;
10470 }
10471 }
10472
Bram Moolenaar26df0922014-02-23 23:39:13 +010010473 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10474 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010475 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010476 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010477 if (c != NUL)
10478 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010479 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010480 if (c != NUL)
10481 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010482 if (p == NULL)
10483 return -1;
10484 vim_free(*bufp);
10485 *bufp = *fnamep = p;
10486 *fnamelen = (int)STRLEN(p);
10487 *usedlen += 2;
10488 }
10489
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 return valid;
10491}
10492
10493/*
10494 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010495 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "flags" can be "g" to do a global substitute.
10497 * Returns an allocated string, NULL for error.
10498 */
10499 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010500do_string_sub(
10501 char_u *str,
10502 char_u *pat,
10503 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010504 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010505 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 int sublen;
10508 regmatch_T regmatch;
10509 int i;
10510 int do_all;
10511 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010512 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 garray_T ga;
10514 char_u *ret;
10515 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010516 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
10518 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10519 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010520 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521
10522 ga_init2(&ga, 1, 200);
10523
10524 do_all = (flags[0] == 'g');
10525
10526 regmatch.rm_ic = p_ic;
10527 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10528 if (regmatch.regprog != NULL)
10529 {
10530 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010531 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10533 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010534 /* Skip empty match except for first match. */
10535 if (regmatch.startp[0] == regmatch.endp[0])
10536 {
10537 if (zero_width == regmatch.startp[0])
10538 {
10539 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010540 i = MB_PTR2LEN(tail);
10541 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10542 (size_t)i);
10543 ga.ga_len += i;
10544 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010545 continue;
10546 }
10547 zero_width = regmatch.startp[0];
10548 }
10549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 /*
10551 * Get some space for a temporary buffer to do the substitution
10552 * into. It will contain:
10553 * - The text up to where the match is.
10554 * - The substituted text.
10555 * - The text after the match.
10556 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010557 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010558 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10560 {
10561 ga_clear(&ga);
10562 break;
10563 }
10564
10565 /* copy the text up to where the match is */
10566 i = (int)(regmatch.startp[0] - tail);
10567 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10568 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010569 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 + ga.ga_len + i, TRUE, TRUE, FALSE);
10571 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010572 tail = regmatch.endp[0];
10573 if (*tail == NUL)
10574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 if (!do_all)
10576 break;
10577 }
10578
10579 if (ga.ga_data != NULL)
10580 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10581
Bram Moolenaar473de612013-06-08 18:19:48 +020010582 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 }
10584
10585 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10586 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010587 if (p_cpo == empty_option)
10588 p_cpo = save_cpo;
10589 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010590 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010591 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592
10593 return ret;
10594}
10595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596 static int
10597filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10598{
10599 typval_T rettv;
10600 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010601 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602
10603 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10604 argv[0] = vimvars[VV_KEY].vv_tv;
10605 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010606 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10607 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010608 if (map)
10609 {
10610 /* map(): replace the list item value */
10611 clear_tv(tv);
10612 rettv.v_lock = 0;
10613 *tv = rettv;
10614 }
10615 else
10616 {
10617 int error = FALSE;
10618
10619 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010620 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 clear_tv(&rettv);
10622 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010623 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 if (error)
10625 goto theend;
10626 }
10627 retval = OK;
10628theend:
10629 clear_tv(&vimvars[VV_VAL].vv_tv);
10630 return retval;
10631}
10632
10633
10634/*
10635 * Implementation of map() and filter().
10636 */
10637 void
10638filter_map(typval_T *argvars, typval_T *rettv, int map)
10639{
10640 typval_T *expr;
10641 listitem_T *li, *nli;
10642 list_T *l = NULL;
10643 dictitem_T *di;
10644 hashtab_T *ht;
10645 hashitem_T *hi;
10646 dict_T *d = NULL;
10647 typval_T save_val;
10648 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010649 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010650 int rem;
10651 int todo;
10652 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10653 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10654 : N_("filter() argument"));
10655 int save_did_emsg;
10656 int idx = 0;
10657
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010658 if (argvars[0].v_type == VAR_BLOB)
10659 {
10660 if ((b = argvars[0].vval.v_blob) == NULL)
10661 return;
10662 }
10663 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010664 {
10665 if ((l = argvars[0].vval.v_list) == NULL
10666 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10667 return;
10668 }
10669 else if (argvars[0].v_type == VAR_DICT)
10670 {
10671 if ((d = argvars[0].vval.v_dict) == NULL
10672 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10673 return;
10674 }
10675 else
10676 {
10677 EMSG2(_(e_listdictarg), ermsg);
10678 return;
10679 }
10680
10681 expr = &argvars[1];
10682 /* On type errors, the preceding call has already displayed an error
10683 * message. Avoid a misleading error message for an empty string that
10684 * was not passed as argument. */
10685 if (expr->v_type != VAR_UNKNOWN)
10686 {
10687 prepare_vimvar(VV_VAL, &save_val);
10688
10689 /* We reset "did_emsg" to be able to detect whether an error
10690 * occurred during evaluation of the expression. */
10691 save_did_emsg = did_emsg;
10692 did_emsg = FALSE;
10693
10694 prepare_vimvar(VV_KEY, &save_key);
10695 if (argvars[0].v_type == VAR_DICT)
10696 {
10697 vimvars[VV_KEY].vv_type = VAR_STRING;
10698
10699 ht = &d->dv_hashtab;
10700 hash_lock(ht);
10701 todo = (int)ht->ht_used;
10702 for (hi = ht->ht_array; todo > 0; ++hi)
10703 {
10704 if (!HASHITEM_EMPTY(hi))
10705 {
10706 int r;
10707
10708 --todo;
10709 di = HI2DI(hi);
10710 if (map &&
10711 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10712 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10713 break;
10714 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10715 r = filter_map_one(&di->di_tv, expr, map, &rem);
10716 clear_tv(&vimvars[VV_KEY].vv_tv);
10717 if (r == FAIL || did_emsg)
10718 break;
10719 if (!map && rem)
10720 {
10721 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10722 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10723 break;
10724 dictitem_remove(d, di);
10725 }
10726 }
10727 }
10728 hash_unlock(ht);
10729 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010730 else if (argvars[0].v_type == VAR_BLOB)
10731 {
10732 int i;
10733 typval_T tv;
10734
10735 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10736 for (i = 0; i < b->bv_ga.ga_len; i++)
10737 {
10738 tv.v_type = VAR_NUMBER;
10739 tv.vval.v_number = blob_get(b, i);
10740 vimvars[VV_KEY].vv_nr = idx;
10741 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10742 break;
10743 if (tv.v_type != VAR_NUMBER)
10744 {
10745 EMSG(_(e_invalblob));
10746 return;
10747 }
10748 tv.v_type = VAR_NUMBER;
10749 blob_set(b, i, tv.vval.v_number);
10750 if (!map && rem)
10751 {
10752 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10753
10754 mch_memmove(p + idx, p + i + 1,
10755 (size_t)b->bv_ga.ga_len - i - 1);
10756 --b->bv_ga.ga_len;
10757 --i;
10758 }
10759 }
10760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010761 else
10762 {
10763 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10764
10765 for (li = l->lv_first; li != NULL; li = nli)
10766 {
10767 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10768 break;
10769 nli = li->li_next;
10770 vimvars[VV_KEY].vv_nr = idx;
10771 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10772 || did_emsg)
10773 break;
10774 if (!map && rem)
10775 listitem_remove(l, li);
10776 ++idx;
10777 }
10778 }
10779
10780 restore_vimvar(VV_KEY, &save_key);
10781 restore_vimvar(VV_VAL, &save_val);
10782
10783 did_emsg |= save_did_emsg;
10784 }
10785
10786 copy_tv(&argvars[0], rettv);
10787}
10788
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */