blob: 59bddb891ad9c9f17c9193d5e7140b7ed019cc31 [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
1986 || rettv->vval.v_list != NULL)
1987 && !(rettv->v_type == VAR_BLOB
1988 || 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 {
2112 /*
2113 * Get the number and item for the only or first index of the List.
2114 */
2115 if (empty1)
2116 lp->ll_n1 = 0;
2117 else
2118 // is number or string
2119 lp->ll_n1 = (long)tv_get_number(&var1);
2120 clear_tv(&var1);
2121
2122 if (lp->ll_n1 < 0
2123 || lp->ll_n1 > blob_len(lp->ll_tv->vval.v_blob))
2124 {
2125 if (!quiet)
2126 EMSGN(_(e_listidx), lp->ll_n1);
2127 return NULL;
2128 }
2129 if (lp->ll_range && !lp->ll_empty2)
2130 {
2131 lp->ll_n2 = (long)tv_get_number(&var2);
2132 clear_tv(&var2);
2133 }
2134 lp->ll_blob = lp->ll_tv->vval.v_blob;
2135 lp->ll_tv = NULL;
2136 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 else
2138 {
2139 /*
2140 * Get the number and item for the only or first index of the List.
2141 */
2142 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002145 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002146 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002147 clear_tv(&var1);
2148
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 lp->ll_list = lp->ll_tv->vval.v_list;
2151 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2152 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002153 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002154 if (lp->ll_n1 < 0)
2155 {
2156 lp->ll_n1 = 0;
2157 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2158 }
2159 }
2160 if (lp->ll_li == NULL)
2161 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002162 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002163 if (!quiet)
2164 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002166 }
2167
2168 /*
2169 * May need to find the item or absolute index for the second
2170 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 * When no index given: "lp->ll_empty2" is TRUE.
2172 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002173 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002175 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002176 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002177 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002182 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002183 {
2184 if (!quiet)
2185 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002187 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 }
2190
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2192 if (lp->ll_n1 < 0)
2193 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2194 if (lp->ll_n2 < lp->ll_n1)
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 Moolenaar6cc16192005-01-08 21:49:45 +00002200 }
2201
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 }
2205
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002206 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 return p;
2208}
2209
2210/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002211 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215{
2216 vim_free(lp->ll_exp_name);
2217 vim_free(lp->ll_newkey);
2218}
2219
2220/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002221 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226set_var_lval(
2227 lval_T *lp,
2228 char_u *endp,
2229 typval_T *rettv,
2230 int copy,
2231 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232{
2233 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002234 listitem_T *ri;
2235 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236
2237 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002239 cc = *endp;
2240 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002241 if (lp->ll_blob != NULL)
2242 {
2243 int error = FALSE, val;
2244 if (op != NULL && *op != '=')
2245 {
2246 EMSG2(_(e_letwrong), op);
2247 return;
2248 }
2249
2250 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2251 {
2252 int i;
2253
2254 if (blob_len(rettv->vval.v_blob) != blob_len(lp->ll_blob))
2255 {
2256 EMSG(_("E972: Blob value has more items than target"));
2257 return;
2258 }
2259
2260 for (i = lp->ll_n1; i <= lp->ll_n2; i++)
2261 blob_set(lp->ll_blob, i,
2262 blob_get(rettv->vval.v_blob, i));
2263 }
2264 else
2265 {
2266 val = (int)tv_get_number_chk(rettv, &error);
2267 if (!error)
2268 {
2269 garray_T *gap = &lp->ll_blob->bv_ga;
2270
2271 // Allow for appending a byte. Setting a byte beyond
2272 // the end is an error otherwise.
2273 if (lp->ll_n1 < gap->ga_len
2274 || (lp->ll_n1 == gap->ga_len
2275 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2276 {
2277 blob_set(lp->ll_blob, lp->ll_n1, val);
2278 if (lp->ll_n1 == gap->ga_len)
2279 ++gap->ga_len;
2280 }
2281 else
2282 EMSG(_(e_invrange));
2283 }
2284 }
2285 }
2286 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002288 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289
Bram Moolenaar79518e22017-02-17 16:31:35 +01002290 /* handle +=, -= and .= */
2291 di = NULL;
2292 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2293 &tv, &di, TRUE, FALSE) == OK)
2294 {
2295 if ((di == NULL
2296 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2297 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2298 FALSE)))
2299 && tv_op(&tv, rettv, op) == OK)
2300 set_var(lp->ll_name, &tv, FALSE);
2301 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002304 else
2305 set_var(lp->ll_name, rettv, copy);
2306 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002308 else if (tv_check_lock(lp->ll_newkey == NULL
2309 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002310 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002311 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 else if (lp->ll_range)
2313 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002314 listitem_T *ll_li = lp->ll_li;
2315 int ll_n1 = lp->ll_n1;
2316
2317 /*
2318 * Check whether any of the list items is locked
2319 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002320 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002321 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002322 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002323 return;
2324 ri = ri->li_next;
2325 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2326 break;
2327 ll_li = ll_li->li_next;
2328 ++ll_n1;
2329 }
2330
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 /*
2332 * Assign the List values to the list items.
2333 */
2334 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002335 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 if (op != NULL && *op != '=')
2337 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2338 else
2339 {
2340 clear_tv(&lp->ll_li->li_tv);
2341 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2342 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 ri = ri->li_next;
2344 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2345 break;
2346 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002347 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002349 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002350 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 ri = NULL;
2352 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002353 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002354 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 lp->ll_li = lp->ll_li->li_next;
2356 ++lp->ll_n1;
2357 }
2358 if (ri != NULL)
2359 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 else if (lp->ll_empty2
2361 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 : lp->ll_n1 != lp->ll_n2)
2363 EMSG(_("E711: List value has not enough items"));
2364 }
2365 else
2366 {
2367 /*
2368 * Assign to a List or Dictionary item.
2369 */
2370 if (lp->ll_newkey != NULL)
2371 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 if (op != NULL && *op != '=')
2373 {
2374 EMSG2(_(e_letwrong), op);
2375 return;
2376 }
2377
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002379 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 if (di == NULL)
2381 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002382 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2383 {
2384 vim_free(di);
2385 return;
2386 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002388 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 else if (op != NULL && *op != '=')
2390 {
2391 tv_op(lp->ll_tv, rettv, op);
2392 return;
2393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 /*
2398 * Assign the value to the variable or list item.
2399 */
2400 if (copy)
2401 copy_tv(rettv, lp->ll_tv);
2402 else
2403 {
2404 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002405 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 }
2408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409}
2410
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002411/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2413 * Returns OK or FAIL.
2414 */
2415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002416tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002418 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 char_u numbuf[NUMBUFLEN];
2420 char_u *s;
2421
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002422 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2423 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2424 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 {
2426 switch (tv1->v_type)
2427 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002428 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 case VAR_DICT:
2430 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002431 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002432 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002433 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002434 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 break;
2436
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002437 case VAR_BLOB:
2438 if (*op != '+' || tv2->v_type != VAR_BLOB)
2439 break;
2440 // BLOB += BLOB
2441 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2442 {
2443 blob_T *b1 = tv1->vval.v_blob;
2444 blob_T *b2 = tv2->vval.v_blob;
2445 int i, len = blob_len(b2);
2446 for (i = 0; i < len; i++)
2447 ga_append(&b1->bv_ga, blob_get(b2, i));
2448 }
2449 return OK;
2450
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 case VAR_LIST:
2452 if (*op != '+' || tv2->v_type != VAR_LIST)
2453 break;
2454 /* List += List */
2455 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2456 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2457 return OK;
2458
2459 case VAR_NUMBER:
2460 case VAR_STRING:
2461 if (tv2->v_type == VAR_LIST)
2462 break;
2463 if (*op == '+' || *op == '-')
2464 {
2465 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002466 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467#ifdef FEAT_FLOAT
2468 if (tv2->v_type == VAR_FLOAT)
2469 {
2470 float_T f = n;
2471
2472 if (*op == '+')
2473 f += tv2->vval.v_float;
2474 else
2475 f -= tv2->vval.v_float;
2476 clear_tv(tv1);
2477 tv1->v_type = VAR_FLOAT;
2478 tv1->vval.v_float = f;
2479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002481#endif
2482 {
2483 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002484 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002485 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002486 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002487 clear_tv(tv1);
2488 tv1->v_type = VAR_NUMBER;
2489 tv1->vval.v_number = n;
2490 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 }
2492 else
2493 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002494 if (tv2->v_type == VAR_FLOAT)
2495 break;
2496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002498 s = tv_get_string(tv1);
2499 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 clear_tv(tv1);
2501 tv1->v_type = VAR_STRING;
2502 tv1->vval.v_string = s;
2503 }
2504 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002506 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002507#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002508 {
2509 float_T f;
2510
2511 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2512 && tv2->v_type != VAR_NUMBER
2513 && tv2->v_type != VAR_STRING))
2514 break;
2515 if (tv2->v_type == VAR_FLOAT)
2516 f = tv2->vval.v_float;
2517 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002518 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002519 if (*op == '+')
2520 tv1->vval.v_float += f;
2521 else
2522 tv1->vval.v_float -= f;
2523 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002525 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 }
2527 }
2528
2529 EMSG2(_(e_letwrong), op);
2530 return FAIL;
2531}
2532
2533/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 * Evaluate the expression used in a ":for var in expr" command.
2535 * "arg" points to "var".
2536 * Set "*errp" to TRUE for an error, FALSE otherwise;
2537 * Return a pointer that holds the info. Null when there is an error.
2538 */
2539 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002540eval_for_line(
2541 char_u *arg,
2542 int *errp,
2543 char_u **nextcmdp,
2544 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545{
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 typval_T tv;
2549 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002550 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002551
2552 *errp = TRUE; /* default: there is an error */
2553
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 if (fi == NULL)
2556 return NULL;
2557
2558 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2559 if (expr == NULL)
2560 return fi;
2561
2562 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002563 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002564 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002565 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002566 return fi;
2567 }
2568
2569 if (skip)
2570 ++emsg_skip;
2571 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2572 {
2573 *errp = FALSE;
2574 if (!skip)
2575 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002576 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002577 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002578 l = tv.vval.v_list;
2579 if (l == NULL)
2580 {
2581 // a null list is like an empty list: do nothing
2582 clear_tv(&tv);
2583 }
2584 else
2585 {
2586 // No need to increment the refcount, it's already set for
2587 // the list being used in "tv".
2588 fi->fi_list = l;
2589 list_add_watch(l, &fi->fi_lw);
2590 fi->fi_lw.lw_item = l->lv_first;
2591 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002592 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002593 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002594 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002595 b = tv.vval.v_blob;
2596 if (b == NULL)
2597 clear_tv(&tv);
2598 else
2599 {
2600 fi->fi_blob = b;
2601 fi->fi_bi = 0;
2602 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002603 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002604 else
2605 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002606 EMSG(_(e_listreq));
2607 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002608 }
2609 }
2610 }
2611 if (skip)
2612 --emsg_skip;
2613
2614 return fi;
2615}
2616
2617/*
2618 * Use the first item in a ":for" list. Advance to the next.
2619 * Assign the values to the variable (list). "arg" points to the first one.
2620 * Return TRUE when a valid item was found, FALSE when at end of list or
2621 * something wrong.
2622 */
2623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002624next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002625{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002626 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002627 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002628 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002629
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002630 if (fi->fi_blob != NULL)
2631 {
2632 typval_T tv;
2633
2634 if (fi->fi_bi >= blob_len(fi->fi_blob))
2635 return FALSE;
2636 tv.v_type = VAR_NUMBER;
2637 tv.v_lock = VAR_FIXED;
2638 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2639 ++fi->fi_bi;
2640 return ex_let_vars(arg, &tv, TRUE,
2641 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2642 }
2643
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002644 item = fi->fi_lw.lw_item;
2645 if (item == NULL)
2646 result = FALSE;
2647 else
2648 {
2649 fi->fi_lw.lw_item = item->li_next;
2650 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2651 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2652 }
2653 return result;
2654}
2655
2656/*
2657 * Free the structure used to store info used by ":for".
2658 */
2659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002660free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002661{
Bram Moolenaar33570922005-01-25 22:26:29 +00002662 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002663
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002664 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002665 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002666 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002667 list_unref(fi->fi_list);
2668 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669 vim_free(fi);
2670}
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2673
2674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002675set_context_for_expression(
2676 expand_T *xp,
2677 char_u *arg,
2678 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 int got_eq = FALSE;
2681 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002682 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002684 if (cmdidx == CMD_let)
2685 {
2686 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002687 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002688 {
2689 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002690 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 {
2692 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002693 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002694 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002695 break;
2696 }
2697 return;
2698 }
2699 }
2700 else
2701 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2702 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 while ((xp->xp_pattern = vim_strpbrk(arg,
2704 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2705 {
2706 c = *xp->xp_pattern;
2707 if (c == '&')
2708 {
2709 c = xp->xp_pattern[1];
2710 if (c == '&')
2711 {
2712 ++xp->xp_pattern;
2713 xp->xp_context = cmdidx != CMD_let || got_eq
2714 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2715 }
2716 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002719 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2720 xp->xp_pattern += 2;
2721
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724 else if (c == '$')
2725 {
2726 /* environment variable */
2727 xp->xp_context = EXPAND_ENV_VARS;
2728 }
2729 else if (c == '=')
2730 {
2731 got_eq = TRUE;
2732 xp->xp_context = EXPAND_EXPRESSION;
2733 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002734 else if (c == '#'
2735 && xp->xp_context == EXPAND_EXPRESSION)
2736 {
2737 /* Autoload function/variable contains '#'. */
2738 break;
2739 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002740 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 && xp->xp_context == EXPAND_FUNCTIONS
2742 && vim_strchr(xp->xp_pattern, '(') == NULL)
2743 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002744 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 break;
2746 }
2747 else if (cmdidx != CMD_let || got_eq)
2748 {
2749 if (c == '"') /* string */
2750 {
2751 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2752 if (c == '\\' && xp->xp_pattern[1] != NUL)
2753 ++xp->xp_pattern;
2754 xp->xp_context = EXPAND_NOTHING;
2755 }
2756 else if (c == '\'') /* literal string */
2757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2760 /* skip */ ;
2761 xp->xp_context = EXPAND_NOTHING;
2762 }
2763 else if (c == '|')
2764 {
2765 if (xp->xp_pattern[1] == '|')
2766 {
2767 ++xp->xp_pattern;
2768 xp->xp_context = EXPAND_EXPRESSION;
2769 }
2770 else
2771 xp->xp_context = EXPAND_COMMANDS;
2772 }
2773 else
2774 xp->xp_context = EXPAND_EXPRESSION;
2775 }
2776 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777 /* Doesn't look like something valid, expand as an expression
2778 * anyway. */
2779 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 arg = xp->xp_pattern;
2781 if (*arg != NUL)
2782 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2783 /* skip */ ;
2784 }
2785 xp->xp_pattern = arg;
2786}
2787
2788#endif /* FEAT_CMDL_COMPL */
2789
2790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 * ":unlet[!] var1 ... " command.
2792 */
2793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002796 ex_unletlock(eap, eap->arg, 0);
2797}
2798
2799/*
2800 * ":lockvar" and ":unlockvar" commands
2801 */
2802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002803ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002804{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002806 int deep = 2;
2807
2808 if (eap->forceit)
2809 deep = -1;
2810 else if (vim_isdigit(*arg))
2811 {
2812 deep = getdigits(&arg);
2813 arg = skipwhite(arg);
2814 }
2815
2816 ex_unletlock(eap, arg, deep);
2817}
2818
2819/*
2820 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2821 */
2822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002823ex_unletlock(
2824 exarg_T *eap,
2825 char_u *argstart,
2826 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002827{
2828 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002831 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 do
2834 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002835 if (*arg == '$')
2836 {
2837 char_u *name = ++arg;
2838
2839 if (get_env_len(&arg) == 0)
2840 {
2841 EMSG2(_(e_invarg2), name - 1);
2842 return;
2843 }
2844 vim_unsetenv(name);
2845 arg = skipwhite(arg);
2846 continue;
2847 }
2848
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002850 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002851 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (lv.ll_name == NULL)
2853 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002854 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (name_end != NULL)
2858 {
2859 emsg_severe = TRUE;
2860 EMSG(_(e_trailing));
2861 }
2862 if (!(eap->skip || error))
2863 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
2865 }
2866
2867 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002868 {
2869 if (eap->cmdidx == CMD_unlet)
2870 {
2871 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2872 error = TRUE;
2873 }
2874 else
2875 {
2876 if (do_lock_var(&lv, name_end, deep,
2877 eap->cmdidx == CMD_lockvar) == FAIL)
2878 error = TRUE;
2879 }
2880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (!eap->skip)
2883 clear_lval(&lv);
2884
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 arg = skipwhite(name_end);
2886 } while (!ends_excmd(*arg));
2887
2888 eap->nextcmd = check_nextcmd(arg);
2889}
2890
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002892do_unlet_var(
2893 lval_T *lp,
2894 char_u *name_end,
2895 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 int ret = OK;
2898 int cc;
2899
2900 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 cc = *name_end;
2903 *name_end = NUL;
2904
2905 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002906 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002910 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002911 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002912 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002913 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 else if (lp->ll_range)
2916 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002918 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002919 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002920
2921 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2922 {
2923 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002924 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002925 return FAIL;
2926 ll_li = li;
2927 ++ll_n1;
2928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929
2930 /* Delete a range of List items. */
2931 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2932 {
2933 li = lp->ll_li->li_next;
2934 listitem_remove(lp->ll_list, lp->ll_li);
2935 lp->ll_li = li;
2936 ++lp->ll_n1;
2937 }
2938 }
2939 else
2940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 /* unlet a List item. */
2943 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002946 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 }
2948
2949 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002950}
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952/*
2953 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002954 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
Bram Moolenaar33570922005-01-25 22:26:29 +00002959 hashtab_T *ht;
2960 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002961 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002962 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002963 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 ht = find_var_ht(name, &varname);
2966 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002969 if (d == NULL)
2970 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 if (ht == &globvarht)
2972 d = &globvardict;
2973 else if (ht == &compat_hashtab)
2974 d = &vimvardict;
2975 else
2976 {
2977 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2978 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2979 }
2980 if (d == NULL)
2981 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002982 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 return FAIL;
2984 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002985 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002987 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002988 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002989 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002990 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002991 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002992 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002993 || var_check_ro(di->di_flags, name, FALSE)
2994 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002995 return FAIL;
2996
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002997 delete_var(ht, hi);
2998 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003001 if (forceit)
3002 return OK;
3003 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 return FAIL;
3005}
3006
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003007/*
3008 * Lock or unlock variable indicated by "lp".
3009 * "deep" is the levels to go (-1 for unlimited);
3010 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3011 */
3012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003013do_lock_var(
3014 lval_T *lp,
3015 char_u *name_end,
3016 int deep,
3017 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003018{
3019 int ret = OK;
3020 int cc;
3021 dictitem_T *di;
3022
3023 if (deep == 0) /* nothing to do */
3024 return OK;
3025
3026 if (lp->ll_tv == NULL)
3027 {
3028 cc = *name_end;
3029 *name_end = NUL;
3030
3031 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003032 di = find_var(lp->ll_name, NULL, TRUE);
3033 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003034 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003035 else if ((di->di_flags & DI_FLAGS_FIX)
3036 && di->di_tv.v_type != VAR_DICT
3037 && di->di_tv.v_type != VAR_LIST)
3038 /* For historic reasons this error is not given for a list or dict.
3039 * E.g., the b: dict could be locked/unlocked. */
3040 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003041 else
3042 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003043 if (lock)
3044 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003045 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003046 di->di_flags &= ~DI_FLAGS_LOCK;
3047 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003048 }
3049 *name_end = cc;
3050 }
3051 else if (lp->ll_range)
3052 {
3053 listitem_T *li = lp->ll_li;
3054
3055 /* (un)lock a range of List items. */
3056 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3057 {
3058 item_lock(&li->li_tv, deep, lock);
3059 li = li->li_next;
3060 ++lp->ll_n1;
3061 }
3062 }
3063 else if (lp->ll_list != NULL)
3064 /* (un)lock a List item. */
3065 item_lock(&lp->ll_li->li_tv, deep, lock);
3066 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003067 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003068 item_lock(&lp->ll_di->di_tv, deep, lock);
3069
3070 return ret;
3071}
3072
3073/*
3074 * Lock or unlock an item. "deep" is nr of levels to go.
3075 */
3076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003078{
3079 static int recurse = 0;
3080 list_T *l;
3081 listitem_T *li;
3082 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003083 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003084 hashitem_T *hi;
3085 int todo;
3086
3087 if (recurse >= DICT_MAXNEST)
3088 {
3089 EMSG(_("E743: variable nested too deep for (un)lock"));
3090 return;
3091 }
3092 if (deep == 0)
3093 return;
3094 ++recurse;
3095
3096 /* lock/unlock the item itself */
3097 if (lock)
3098 tv->v_lock |= VAR_LOCKED;
3099 else
3100 tv->v_lock &= ~VAR_LOCKED;
3101
3102 switch (tv->v_type)
3103 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003104 case VAR_UNKNOWN:
3105 case VAR_NUMBER:
3106 case VAR_STRING:
3107 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003108 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003109 case VAR_FLOAT:
3110 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003111 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003112 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003113 break;
3114
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003115 case VAR_BLOB:
3116 if ((b = tv->vval.v_blob) != NULL)
3117 {
3118 if (lock)
3119 b->bv_lock |= VAR_LOCKED;
3120 else
3121 b->bv_lock &= ~VAR_LOCKED;
3122 }
3123 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124 case VAR_LIST:
3125 if ((l = tv->vval.v_list) != NULL)
3126 {
3127 if (lock)
3128 l->lv_lock |= VAR_LOCKED;
3129 else
3130 l->lv_lock &= ~VAR_LOCKED;
3131 if (deep < 0 || deep > 1)
3132 /* recursive: lock/unlock the items the List contains */
3133 for (li = l->lv_first; li != NULL; li = li->li_next)
3134 item_lock(&li->li_tv, deep - 1, lock);
3135 }
3136 break;
3137 case VAR_DICT:
3138 if ((d = tv->vval.v_dict) != NULL)
3139 {
3140 if (lock)
3141 d->dv_lock |= VAR_LOCKED;
3142 else
3143 d->dv_lock &= ~VAR_LOCKED;
3144 if (deep < 0 || deep > 1)
3145 {
3146 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003147 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003148 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3149 {
3150 if (!HASHITEM_EMPTY(hi))
3151 {
3152 --todo;
3153 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3154 }
3155 }
3156 }
3157 }
3158 }
3159 --recurse;
3160}
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3163/*
3164 * Delete all "menutrans_" variables.
3165 */
3166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003167del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003170 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003173 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003175 {
3176 if (!HASHITEM_EMPTY(hi))
3177 {
3178 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3180 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003181 }
3182 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184}
3185#endif
3186
3187#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3188
3189/*
3190 * Local string buffer for the next two functions to store a variable name
3191 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3192 * get_user_var_name().
3193 */
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195static char_u *varnamebuf = NULL;
3196static int varnamebuflen = 0;
3197
3198/*
3199 * Function to concatenate a prefix and a variable name.
3200 */
3201 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
3204 int len;
3205
3206 len = (int)STRLEN(name) + 3;
3207 if (len > varnamebuflen)
3208 {
3209 vim_free(varnamebuf);
3210 len += 10; /* some additional space */
3211 varnamebuf = alloc(len);
3212 if (varnamebuf == NULL)
3213 {
3214 varnamebuflen = 0;
3215 return NULL;
3216 }
3217 varnamebuflen = len;
3218 }
3219 *varnamebuf = prefix;
3220 varnamebuf[1] = ':';
3221 STRCPY(varnamebuf + 2, name);
3222 return varnamebuf;
3223}
3224
3225/*
3226 * Function given to ExpandGeneric() to obtain the list of user defined
3227 * (global/buffer/window/built-in) variable names.
3228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003230get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003232 static long_u gdone;
3233 static long_u bdone;
3234 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003235 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003236 static int vidx;
3237 static hashitem_T *hi;
3238 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003241 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003242 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003243 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003244 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003245
3246 /* Global variables */
3247 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003249 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003251 else
3252 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003253 while (HASHITEM_EMPTY(hi))
3254 ++hi;
3255 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3256 return cat_prefix_varname('g', hi->hi_key);
3257 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003259
3260 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003261 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003266 else
3267 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003268 while (HASHITEM_EMPTY(hi))
3269 ++hi;
3270 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003272
3273 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003274 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003277 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003279 else
3280 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003281 while (HASHITEM_EMPTY(hi))
3282 ++hi;
3283 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003285
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003286 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003287 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003288 if (tdone < ht->ht_used)
3289 {
3290 if (tdone++ == 0)
3291 hi = ht->ht_array;
3292 else
3293 ++hi;
3294 while (HASHITEM_EMPTY(hi))
3295 ++hi;
3296 return cat_prefix_varname('t', hi->hi_key);
3297 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003298
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 /* v: variables */
3300 if (vidx < VV_LEN)
3301 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
Bram Moolenaard23a8232018-02-10 18:45:26 +01003303 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 varnamebuflen = 0;
3305 return NULL;
3306}
3307
3308#endif /* FEAT_CMDL_COMPL */
3309
3310/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003311 * Return TRUE if "pat" matches "text".
3312 * Does not use 'cpo' and always uses 'magic'.
3313 */
3314 static int
3315pattern_match(char_u *pat, char_u *text, int ic)
3316{
3317 int matches = FALSE;
3318 char_u *save_cpo;
3319 regmatch_T regmatch;
3320
3321 /* avoid 'l' flag in 'cpoptions' */
3322 save_cpo = p_cpo;
3323 p_cpo = (char_u *)"";
3324 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3325 if (regmatch.regprog != NULL)
3326 {
3327 regmatch.rm_ic = ic;
3328 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3329 vim_regfree(regmatch.regprog);
3330 }
3331 p_cpo = save_cpo;
3332 return matches;
3333}
3334
3335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003337 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3339 */
3340
3341/*
3342 * Handle zero level expression.
3343 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003344 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003345 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 * Return OK or FAIL.
3347 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003349eval0(
3350 char_u *arg,
3351 typval_T *rettv,
3352 char_u **nextcmd,
3353 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 int ret;
3356 char_u *p;
3357
3358 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (ret == FAIL || !ends_excmd(*p))
3361 {
3362 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 /*
3365 * Report the invalid expression unless the expression evaluation has
3366 * been cancelled due to an aborting error, an interrupt, or an
3367 * exception.
3368 */
3369 if (!aborting())
3370 EMSG2(_(e_invexpr2), arg);
3371 ret = FAIL;
3372 }
3373 if (nextcmd != NULL)
3374 *nextcmd = check_nextcmd(p);
3375
3376 return ret;
3377}
3378
3379/*
3380 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003381 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 *
3383 * "arg" must point to the first non-white of the expression.
3384 * "arg" is advanced to the next non-white after the recognized expression.
3385 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003386 * Note: "rettv.v_lock" is not set.
3387 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * Return OK or FAIL.
3389 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003394 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /*
3397 * Get the first variable.
3398 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 return FAIL;
3401
3402 if ((*arg)[0] == '?')
3403 {
3404 result = FALSE;
3405 if (evaluate)
3406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 int error = FALSE;
3408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003409 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003411 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003412 if (error)
3413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415
3416 /*
3417 * Get the second variable.
3418 */
3419 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003420 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 return FAIL;
3422
3423 /*
3424 * Check for the ":".
3425 */
3426 if ((*arg)[0] != ':')
3427 {
3428 EMSG(_("E109: Missing ':' after '?'"));
3429 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return FAIL;
3432 }
3433
3434 /*
3435 * Get the third variable.
3436 */
3437 *arg = skipwhite(*arg + 1);
3438 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3439 {
3440 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 return FAIL;
3443 }
3444 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447
3448 return OK;
3449}
3450
3451/*
3452 * Handle first level expression:
3453 * expr2 || expr2 || expr2 logical OR
3454 *
3455 * "arg" must point to the first non-white of the expression.
3456 * "arg" is advanced to the next non-white after the recognized expression.
3457 *
3458 * Return OK or FAIL.
3459 */
3460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003461eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 long result;
3465 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003466 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 /*
3469 * Get the first variable.
3470 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473
3474 /*
3475 * Repeat until there is no following "||".
3476 */
3477 first = TRUE;
3478 result = FALSE;
3479 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3480 {
3481 if (evaluate && first)
3482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003483 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003486 if (error)
3487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 first = FALSE;
3489 }
3490
3491 /*
3492 * Get the second variable.
3493 */
3494 *arg = skipwhite(*arg + 2);
3495 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3496 return FAIL;
3497
3498 /*
3499 * Compute the result.
3500 */
3501 if (evaluate && !result)
3502 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003503 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003505 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003506 if (error)
3507 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 if (evaluate)
3510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003511 rettv->v_type = VAR_NUMBER;
3512 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
3514 }
3515
3516 return OK;
3517}
3518
3519/*
3520 * Handle second level expression:
3521 * expr3 && expr3 && expr3 logical AND
3522 *
3523 * "arg" must point to the first non-white of the expression.
3524 * "arg" is advanced to the next non-white after the recognized expression.
3525 *
3526 * Return OK or FAIL.
3527 */
3528 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003529eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
Bram Moolenaar33570922005-01-25 22:26:29 +00003531 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 long result;
3533 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003534 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
3536 /*
3537 * Get the first variable.
3538 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541
3542 /*
3543 * Repeat until there is no following "&&".
3544 */
3545 first = TRUE;
3546 result = TRUE;
3547 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3548 {
3549 if (evaluate && first)
3550 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003551 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003554 if (error)
3555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 first = FALSE;
3557 }
3558
3559 /*
3560 * Get the second variable.
3561 */
3562 *arg = skipwhite(*arg + 2);
3563 if (eval4(arg, &var2, evaluate && result) == FAIL)
3564 return FAIL;
3565
3566 /*
3567 * Compute the result.
3568 */
3569 if (evaluate && result)
3570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003571 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003573 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003574 if (error)
3575 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 if (evaluate)
3578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 rettv->v_type = VAR_NUMBER;
3580 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582 }
3583
3584 return OK;
3585}
3586
3587/*
3588 * Handle third level expression:
3589 * var1 == var2
3590 * var1 =~ var2
3591 * var1 != var2
3592 * var1 !~ var2
3593 * var1 > var2
3594 * var1 >= var2
3595 * var1 < var2
3596 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003597 * var1 is var2
3598 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 *
3600 * "arg" must point to the first non-white of the expression.
3601 * "arg" is advanced to the next non-white after the recognized expression.
3602 *
3603 * Return OK or FAIL.
3604 */
3605 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u *p;
3610 int i;
3611 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003612 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 /*
3617 * Get the first variable.
3618 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003619 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 return FAIL;
3621
3622 p = *arg;
3623 switch (p[0])
3624 {
3625 case '=': if (p[1] == '=')
3626 type = TYPE_EQUAL;
3627 else if (p[1] == '~')
3628 type = TYPE_MATCH;
3629 break;
3630 case '!': if (p[1] == '=')
3631 type = TYPE_NEQUAL;
3632 else if (p[1] == '~')
3633 type = TYPE_NOMATCH;
3634 break;
3635 case '>': if (p[1] != '=')
3636 {
3637 type = TYPE_GREATER;
3638 len = 1;
3639 }
3640 else
3641 type = TYPE_GEQUAL;
3642 break;
3643 case '<': if (p[1] != '=')
3644 {
3645 type = TYPE_SMALLER;
3646 len = 1;
3647 }
3648 else
3649 type = TYPE_SEQUAL;
3650 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003651 case 'i': if (p[1] == 's')
3652 {
3653 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3654 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003655 i = p[len];
3656 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003657 {
3658 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3659 type_is = TRUE;
3660 }
3661 }
3662 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664
3665 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003666 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 if (type != TYPE_UNKNOWN)
3669 {
3670 /* extra question mark appended: ignore case */
3671 if (p[len] == '?')
3672 {
3673 ic = TRUE;
3674 ++len;
3675 }
3676 /* extra '#' appended: match case */
3677 else if (p[len] == '#')
3678 {
3679 ic = FALSE;
3680 ++len;
3681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003682 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 else
3684 ic = p_ic;
3685
3686 /*
3687 * Get the second variable.
3688 */
3689 *arg = skipwhite(p + len);
3690 if (eval5(arg, &var2, evaluate) == FAIL)
3691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003692 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003695 if (evaluate)
3696 {
3697 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3698
3699 clear_tv(&var2);
3700 return ret;
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703
3704 return OK;
3705}
3706
3707/*
3708 * Handle fourth level expression:
3709 * + number addition
3710 * - number subtraction
3711 * . string concatenation
3712 *
3713 * "arg" must point to the first non-white of the expression.
3714 * "arg" is advanced to the next non-white after the recognized expression.
3715 *
3716 * Return OK or FAIL.
3717 */
3718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003719eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T var2;
3722 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003724 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003725#ifdef FEAT_FLOAT
3726 float_T f1 = 0, f2 = 0;
3727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 char_u *s1, *s2;
3729 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3730 char_u *p;
3731
3732 /*
3733 * Get the first variable.
3734 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003735 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 return FAIL;
3737
3738 /*
3739 * Repeat computing, until no '+', '-' or '.' is following.
3740 */
3741 for (;;)
3742 {
3743 op = **arg;
3744 if (op != '+' && op != '-' && op != '.')
3745 break;
3746
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003747 if ((op != '+' || (rettv->v_type != VAR_LIST
3748 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003749#ifdef FEAT_FLOAT
3750 && (op == '.' || rettv->v_type != VAR_FLOAT)
3751#endif
3752 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 {
3754 /* For "list + ...", an illegal use of the first operand as
3755 * a number cannot be determined before evaluating the 2nd
3756 * operand: if this is also a list, all is ok.
3757 * For "something . ...", "something - ..." or "non-list + ...",
3758 * we know that the first operand needs to be a string or number
3759 * without evaluating the 2nd operand. So check before to avoid
3760 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003761 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 {
3763 clear_tv(rettv);
3764 return FAIL;
3765 }
3766 }
3767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 /*
3769 * Get the second variable.
3770 */
3771 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003772 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return FAIL;
3776 }
3777
3778 if (evaluate)
3779 {
3780 /*
3781 * Compute the result.
3782 */
3783 if (op == '.')
3784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003785 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3786 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003787 if (s2 == NULL) /* type error ? */
3788 {
3789 clear_tv(rettv);
3790 clear_tv(&var2);
3791 return FAIL;
3792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003793 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003794 clear_tv(rettv);
3795 rettv->v_type = VAR_STRING;
3796 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003798 else if (op == '+' && rettv->v_type == VAR_BLOB
3799 && var2.v_type == VAR_BLOB)
3800 {
3801 blob_T *b1 = rettv->vval.v_blob;
3802 blob_T *b2 = var2.vval.v_blob;
3803 blob_T *b = blob_alloc();
3804 int i;
3805
3806 if (b != NULL)
3807 {
3808 for (i = 0; i < blob_len(b1); i++)
3809 ga_append(&b->bv_ga, blob_get(b1, i));
3810 for (i = 0; i < blob_len(b2); i++)
3811 ga_append(&b->bv_ga, blob_get(b2, i));
3812
3813 clear_tv(rettv);
3814 rettv_blob_set(rettv, b);
3815 }
3816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003817 else if (op == '+' && rettv->v_type == VAR_LIST
3818 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003819 {
3820 /* concatenate Lists */
3821 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3822 &var3) == FAIL)
3823 {
3824 clear_tv(rettv);
3825 clear_tv(&var2);
3826 return FAIL;
3827 }
3828 clear_tv(rettv);
3829 *rettv = var3;
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else
3832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 int error = FALSE;
3834
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003835#ifdef FEAT_FLOAT
3836 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003838 f1 = rettv->vval.v_float;
3839 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003840 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003841 else
3842#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003844 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003845 if (error)
3846 {
3847 /* This can only happen for "list + non-list". For
3848 * "non-list + ..." or "something - ...", we returned
3849 * before evaluating the 2nd operand. */
3850 clear_tv(rettv);
3851 return FAIL;
3852 }
3853#ifdef FEAT_FLOAT
3854 if (var2.v_type == VAR_FLOAT)
3855 f1 = n1;
3856#endif
3857 }
3858#ifdef FEAT_FLOAT
3859 if (var2.v_type == VAR_FLOAT)
3860 {
3861 f2 = var2.vval.v_float;
3862 n2 = 0;
3863 }
3864 else
3865#endif
3866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003867 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003868 if (error)
3869 {
3870 clear_tv(rettv);
3871 clear_tv(&var2);
3872 return FAIL;
3873 }
3874#ifdef FEAT_FLOAT
3875 if (rettv->v_type == VAR_FLOAT)
3876 f2 = n2;
3877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003880
3881#ifdef FEAT_FLOAT
3882 /* If there is a float on either side the result is a float. */
3883 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3884 {
3885 if (op == '+')
3886 f1 = f1 + f2;
3887 else
3888 f1 = f1 - f2;
3889 rettv->v_type = VAR_FLOAT;
3890 rettv->vval.v_float = f1;
3891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003893#endif
3894 {
3895 if (op == '+')
3896 n1 = n1 + n2;
3897 else
3898 n1 = n1 - n2;
3899 rettv->v_type = VAR_NUMBER;
3900 rettv->vval.v_number = n1;
3901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 }
3906 return OK;
3907}
3908
3909/*
3910 * Handle fifth level expression:
3911 * * number multiplication
3912 * / number division
3913 * % number modulo
3914 *
3915 * "arg" must point to the first non-white of the expression.
3916 * "arg" is advanced to the next non-white after the recognized expression.
3917 *
3918 * Return OK or FAIL.
3919 */
3920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003921eval6(
3922 char_u **arg,
3923 typval_T *rettv,
3924 int evaluate,
3925 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003929 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003930#ifdef FEAT_FLOAT
3931 int use_float = FALSE;
3932 float_T f1 = 0, f2;
3933#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 /*
3937 * Get the first variable.
3938 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003939 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 return FAIL;
3941
3942 /*
3943 * Repeat computing, until no '*', '/' or '%' is following.
3944 */
3945 for (;;)
3946 {
3947 op = **arg;
3948 if (op != '*' && op != '/' && op != '%')
3949 break;
3950
3951 if (evaluate)
3952 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003953#ifdef FEAT_FLOAT
3954 if (rettv->v_type == VAR_FLOAT)
3955 {
3956 f1 = rettv->vval.v_float;
3957 use_float = TRUE;
3958 n1 = 0;
3959 }
3960 else
3961#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003962 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003964 if (error)
3965 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 else
3968 n1 = 0;
3969
3970 /*
3971 * Get the second variable.
3972 */
3973 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003974 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return FAIL;
3976
3977 if (evaluate)
3978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003979#ifdef FEAT_FLOAT
3980 if (var2.v_type == VAR_FLOAT)
3981 {
3982 if (!use_float)
3983 {
3984 f1 = n1;
3985 use_float = TRUE;
3986 }
3987 f2 = var2.vval.v_float;
3988 n2 = 0;
3989 }
3990 else
3991#endif
3992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003993 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003994 clear_tv(&var2);
3995 if (error)
3996 return FAIL;
3997#ifdef FEAT_FLOAT
3998 if (use_float)
3999 f2 = n2;
4000#endif
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /*
4004 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004005 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004007#ifdef FEAT_FLOAT
4008 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004010 if (op == '*')
4011 f1 = f1 * f2;
4012 else if (op == '/')
4013 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004014# ifdef VMS
4015 /* VMS crashes on divide by zero, work around it */
4016 if (f2 == 0.0)
4017 {
4018 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004019 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004020 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004021 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004022 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004023 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004024 }
4025 else
4026 f1 = f1 / f2;
4027# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004028 /* We rely on the floating point library to handle divide
4029 * by zero to result in "inf" and not a crash. */
4030 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004031# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004034 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004035 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004036 return FAIL;
4037 }
4038 rettv->v_type = VAR_FLOAT;
4039 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004044 if (op == '*')
4045 n1 = n1 * n2;
4046 else if (op == '/')
4047 {
4048 if (n2 == 0) /* give an error message? */
4049 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004050 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004051 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004052 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004053 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004054 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004055 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 }
4057 else
4058 n1 = n1 / n2;
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 {
4062 if (n2 == 0) /* give an error message? */
4063 n1 = 0;
4064 else
4065 n1 = n1 % n2;
4066 }
4067 rettv->v_type = VAR_NUMBER;
4068 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 }
4072
4073 return OK;
4074}
4075
4076/*
4077 * Handle sixth level expression:
4078 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004079 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004080 * "string" string constant
4081 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 * &option-name option value
4083 * @r register contents
4084 * identifier variable value
4085 * function() function call
4086 * $VAR environment variable
4087 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004088 * [expr, expr] List
4089 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 *
4091 * Also handle:
4092 * ! in front logical NOT
4093 * - in front unary minus
4094 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004095 * trailing [] subscript in String or List
4096 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 *
4098 * "arg" must point to the first non-white of the expression.
4099 * "arg" is advanced to the next non-white after the recognized expression.
4100 *
4101 * Return OK or FAIL.
4102 */
4103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004104eval7(
4105 char_u **arg,
4106 typval_T *rettv,
4107 int evaluate,
4108 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004110 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 int len;
4112 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 char_u *start_leader, *end_leader;
4114 int ret = OK;
4115 char_u *alias;
4116
4117 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004119 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004124 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 */
4126 start_leader = *arg;
4127 while (**arg == '!' || **arg == '-' || **arg == '+')
4128 *arg = skipwhite(*arg + 1);
4129 end_leader = *arg;
4130
4131 switch (**arg)
4132 {
4133 /*
4134 * Number constant.
4135 */
4136 case '0':
4137 case '1':
4138 case '2':
4139 case '3':
4140 case '4':
4141 case '5':
4142 case '6':
4143 case '7':
4144 case '8':
4145 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146 {
4147#ifdef FEAT_FLOAT
4148 char_u *p = skipdigits(*arg + 1);
4149 int get_float = FALSE;
4150
4151 /* We accept a float when the format matches
4152 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004153 * strict to avoid backwards compatibility problems.
4154 * Don't look for a float after the "." operator, so that
4155 * ":let vers = 1.2.3" doesn't fail. */
4156 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004158 get_float = TRUE;
4159 p = skipdigits(p + 2);
4160 if (*p == 'e' || *p == 'E')
4161 {
4162 ++p;
4163 if (*p == '-' || *p == '+')
4164 ++p;
4165 if (!vim_isdigit(*p))
4166 get_float = FALSE;
4167 else
4168 p = skipdigits(p + 1);
4169 }
4170 if (ASCII_ISALPHA(*p) || *p == '.')
4171 get_float = FALSE;
4172 }
4173 if (get_float)
4174 {
4175 float_T f;
4176
4177 *arg += string2float(*arg, &f);
4178 if (evaluate)
4179 {
4180 rettv->v_type = VAR_FLOAT;
4181 rettv->vval.v_float = f;
4182 }
4183 }
4184 else
4185#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004186 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004187 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004188 char_u *bp;
4189 blob_T *blob;
4190
4191 // Blob constant: 0z0123456789abcdef
4192 if (evaluate)
4193 blob = blob_alloc();
4194 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4195 {
4196 if (!vim_isxdigit(bp[1]))
4197 {
4198 EMSG(_("E973: Blob literal should have an even number of hex characters'"));
4199 vim_free(blob);
4200 ret = FAIL;
4201 break;
4202 }
4203 if (blob != NULL)
4204 ga_append(&blob->bv_ga,
4205 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4206 }
4207 if (blob != NULL)
4208 {
4209 ++blob->bv_refcount;
4210 rettv->v_type = VAR_BLOB;
4211 rettv->vval.v_blob = blob;
4212 }
4213 *arg = bp;
4214 }
4215 else
4216 {
4217 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004218 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004219 *arg += len;
4220 if (evaluate)
4221 {
4222 rettv->v_type = VAR_NUMBER;
4223 rettv->vval.v_number = n;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 /*
4230 * String constant: "string".
4231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234
4235 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004236 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004239 break;
4240
4241 /*
4242 * List: [expr, expr]
4243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 break;
4246
4247 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004248 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004249 * Dictionary: {key: val, key: val}
4250 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004251 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4252 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004253 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004254 break;
4255
4256 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004257 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004259 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 break;
4261
4262 /*
4263 * Environment variable: $VAR.
4264 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 break;
4267
4268 /*
4269 * Register contents: @r.
4270 */
4271 case '@': ++*arg;
4272 if (evaluate)
4273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004275 rettv->vval.v_string = get_reg_contents(**arg,
4276 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 if (**arg != NUL)
4279 ++*arg;
4280 break;
4281
4282 /*
4283 * nested expression: (expression).
4284 */
4285 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (**arg == ')')
4288 ++*arg;
4289 else if (ret == OK)
4290 {
4291 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 ret = FAIL;
4294 }
4295 break;
4296
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 break;
4299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300
4301 if (ret == NOTDONE)
4302 {
4303 /*
4304 * Must be a variable or function name.
4305 * Can also be a curly-braces kind of name: {expr}.
4306 */
4307 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004308 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004309 if (alias != NULL)
4310 s = alias;
4311
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004312 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004313 ret = FAIL;
4314 else
4315 {
4316 if (**arg == '(') /* recursive! */
4317 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004318 partial_T *partial;
4319
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004320 if (!evaluate)
4321 check_vars(s, len);
4322
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 /* If "s" is the name of a variable of type VAR_FUNC
4324 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004325 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004327 /* Need to make a copy, in case evaluating the arguments makes
4328 * the name invalid. */
4329 s = vim_strsave(s);
4330 if (s == NULL)
4331 ret = FAIL;
4332 else
4333 /* Invoke the function. */
4334 ret = get_func_tv(s, len, rettv, arg,
4335 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4336 &len, evaluate, partial, NULL);
4337 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004338
4339 /* If evaluate is FALSE rettv->v_type was not set in
4340 * get_func_tv, but it's needed in handle_subscript() to parse
4341 * what follows. So set it here. */
4342 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4343 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004344 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004345 rettv->v_type = VAR_FUNC;
4346 }
4347
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348 /* Stop the expression evaluation when immediately
4349 * aborting on error, or when an interrupt occurred or
4350 * an exception was thrown but not caught. */
4351 if (aborting())
4352 {
4353 if (ret == OK)
4354 clear_tv(rettv);
4355 ret = FAIL;
4356 }
4357 }
4358 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004359 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004360 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004361 {
4362 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004363 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004364 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004365 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004366 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 }
4368
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *arg = skipwhite(*arg);
4370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004371 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4372 * expr(expr). */
4373 if (ret == OK)
4374 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
4376 /*
4377 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4378 */
4379 if (ret == OK && evaluate && end_leader > start_leader)
4380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004382 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004383#ifdef FEAT_FLOAT
4384 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386 if (rettv->v_type == VAR_FLOAT)
4387 f = rettv->vval.v_float;
4388 else
4389#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004390 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 clear_tv(rettv);
4394 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004396 else
4397 {
4398 while (end_leader > start_leader)
4399 {
4400 --end_leader;
4401 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004402 {
4403#ifdef FEAT_FLOAT
4404 if (rettv->v_type == VAR_FLOAT)
4405 f = !f;
4406 else
4407#endif
4408 val = !val;
4409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004411 {
4412#ifdef FEAT_FLOAT
4413 if (rettv->v_type == VAR_FLOAT)
4414 f = -f;
4415 else
4416#endif
4417 val = -val;
4418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004420#ifdef FEAT_FLOAT
4421 if (rettv->v_type == VAR_FLOAT)
4422 {
4423 clear_tv(rettv);
4424 rettv->vval.v_float = f;
4425 }
4426 else
4427#endif
4428 {
4429 clear_tv(rettv);
4430 rettv->v_type = VAR_NUMBER;
4431 rettv->vval.v_number = val;
4432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435
4436 return ret;
4437}
4438
4439/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004440 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4441 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4443 */
4444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004445eval_index(
4446 char_u **arg,
4447 typval_T *rettv,
4448 int evaluate,
4449 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450{
4451 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004452 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004453 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004454 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 long len = -1;
4456 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459
Bram Moolenaara03f2332016-02-06 18:09:59 +01004460 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004462 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004463 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004464 if (verbose)
4465 EMSG(_("E695: Cannot index a Funcref"));
4466 return FAIL;
4467 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004468#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004469 if (verbose)
4470 EMSG(_(e_float_as_string));
4471 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004472#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004473 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004474 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004475 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004476 if (verbose)
4477 EMSG(_("E909: Cannot index a special variable"));
4478 return FAIL;
4479 case VAR_UNKNOWN:
4480 if (evaluate)
4481 return FAIL;
4482 /* FALLTHROUGH */
4483
4484 case VAR_STRING:
4485 case VAR_NUMBER:
4486 case VAR_LIST:
4487 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004488 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004489 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004492 init_tv(&var1);
4493 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 /*
4497 * dict.name
4498 */
4499 key = *arg + 1;
4500 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4501 ;
4502 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 }
4506 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 /*
4509 * something[idx]
4510 *
4511 * Get the (first) variable from inside the [].
4512 */
4513 *arg = skipwhite(*arg + 1);
4514 if (**arg == ':')
4515 empty1 = TRUE;
4516 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4517 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004518 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004519 {
4520 /* not a number or string */
4521 clear_tv(&var1);
4522 return FAIL;
4523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524
4525 /*
4526 * Get the second variable from inside the [:].
4527 */
4528 if (**arg == ':')
4529 {
4530 range = TRUE;
4531 *arg = skipwhite(*arg + 1);
4532 if (**arg == ']')
4533 empty2 = TRUE;
4534 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004536 if (!empty1)
4537 clear_tv(&var1);
4538 return FAIL;
4539 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004540 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004541 {
4542 /* not a number or string */
4543 if (!empty1)
4544 clear_tv(&var1);
4545 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 return FAIL;
4547 }
4548 }
4549
4550 /* Check for the ']'. */
4551 if (**arg != ']')
4552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004553 if (verbose)
4554 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 clear_tv(&var1);
4556 if (range)
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 }
4562
4563 if (evaluate)
4564 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 n1 = 0;
4566 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004568 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 }
4571 if (range)
4572 {
4573 if (empty2)
4574 n2 = -1;
4575 else
4576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004577 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004579 }
4580 }
4581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004584 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004585 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004586 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004587 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004588 case VAR_SPECIAL:
4589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004590 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004591 break; /* not evaluating, skipping over subscript */
4592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 case VAR_NUMBER:
4594 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004595 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 len = (long)STRLEN(s);
4597 if (range)
4598 {
4599 /* The resulting variable is a substring. If the indexes
4600 * are out of range the result is empty. */
4601 if (n1 < 0)
4602 {
4603 n1 = len + n1;
4604 if (n1 < 0)
4605 n1 = 0;
4606 }
4607 if (n2 < 0)
4608 n2 = len + n2;
4609 else if (n2 >= len)
4610 n2 = len;
4611 if (n1 >= len || n2 < 0 || n1 > n2)
4612 s = NULL;
4613 else
4614 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4615 }
4616 else
4617 {
4618 /* The resulting variable is a string of a single
4619 * character. If the index is too big or negative the
4620 * result is empty. */
4621 if (n1 >= len || n1 < 0)
4622 s = NULL;
4623 else
4624 s = vim_strnsave(s + n1, 1);
4625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 clear_tv(rettv);
4627 rettv->v_type = VAR_STRING;
4628 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 break;
4630
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004631 case VAR_BLOB:
4632 len = blob_len(rettv->vval.v_blob);
4633 if (range)
4634 {
4635 // The resulting variable is a substring. If the indexes
4636 // are out of range the result is empty.
4637 if (n1 < 0)
4638 {
4639 n1 = len + n1;
4640 if (n1 < 0)
4641 n1 = 0;
4642 }
4643 if (n2 < 0)
4644 n2 = len + n2;
4645 else if (n2 >= len)
4646 n2 = len - 1;
4647 if (n1 >= len || n2 < 0 || n1 > n2)
4648 {
4649 clear_tv(rettv);
4650 rettv->v_type = VAR_BLOB;
4651 rettv->vval.v_blob = NULL;
4652 }
4653 else
4654 {
4655 blob_T *blob = blob_alloc();
4656
4657 if (blob != NULL)
4658 {
4659 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4660 {
4661 blob_free(blob);
4662 return FAIL;
4663 }
4664 blob->bv_ga.ga_len = n2 - n1 + 1;
4665 for (i = n1; i <= n2; i++)
4666 blob_set(blob, i - n1,
4667 blob_get(rettv->vval.v_blob, i));
4668
4669 clear_tv(rettv);
4670 rettv_blob_set(rettv, blob);
4671 }
4672 }
4673 }
4674 else
4675 {
4676 // The resulting variable is a string of a single
4677 // character. If the index is too big or negative the
4678 // result is empty.
4679 if (n1 < len && n1 >= 0)
4680 {
4681 int v = (int)blob_get(rettv->vval.v_blob, n1);
4682
4683 clear_tv(rettv);
4684 rettv->v_type = VAR_NUMBER;
4685 rettv->vval.v_number = v;
4686 }
4687 else
4688 EMSGN(_(e_blobidx), n1);
4689 }
4690 break;
4691
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 if (n1 < 0)
4695 n1 = len + n1;
4696 if (!empty1 && (n1 < 0 || n1 >= len))
4697 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004698 /* For a range we allow invalid values and return an empty
4699 * list. A list index out of range is an error. */
4700 if (!range)
4701 {
4702 if (verbose)
4703 EMSGN(_(e_listidx), n1);
4704 return FAIL;
4705 }
4706 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 }
4708 if (range)
4709 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004710 list_T *l;
4711 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712
4713 if (n2 < 0)
4714 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004715 else if (n2 >= len)
4716 n2 = len - 1;
4717 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004718 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 l = list_alloc();
4720 if (l == NULL)
4721 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004722 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 n1 <= n2; ++n1)
4724 {
4725 if (list_append_tv(l, &item->li_tv) == FAIL)
4726 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004727 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 return FAIL;
4729 }
4730 item = item->li_next;
4731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004732 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004733 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 }
4735 else
4736 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004737 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 clear_tv(rettv);
4739 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 }
4741 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742
4743 case VAR_DICT:
4744 if (range)
4745 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004746 if (verbose)
4747 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748 if (len == -1)
4749 clear_tv(&var1);
4750 return FAIL;
4751 }
4752 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004753 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754
4755 if (len == -1)
4756 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004757 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004758 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004760 clear_tv(&var1);
4761 return FAIL;
4762 }
4763 }
4764
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004765 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004768 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769 if (len == -1)
4770 clear_tv(&var1);
4771 if (item == NULL)
4772 return FAIL;
4773
4774 copy_tv(&item->di_tv, &var1);
4775 clear_tv(rettv);
4776 *rettv = var1;
4777 }
4778 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 }
4780 }
4781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 return OK;
4783}
4784
4785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 * Get an option value.
4787 * "arg" points to the '&' or '+' before the option name.
4788 * "arg" is advanced to character after the option name.
4789 * Return OK or FAIL.
4790 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004792get_option_tv(
4793 char_u **arg,
4794 typval_T *rettv, /* when NULL, only check if option exists */
4795 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 char_u *option_end;
4798 long numval;
4799 char_u *stringval;
4800 int opt_type;
4801 int c;
4802 int working = (**arg == '+'); /* has("+option") */
4803 int ret = OK;
4804 int opt_flags;
4805
4806 /*
4807 * Isolate the option name and find its value.
4808 */
4809 option_end = find_option_end(arg, &opt_flags);
4810 if (option_end == NULL)
4811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 EMSG2(_("E112: Option name missing: %s"), *arg);
4814 return FAIL;
4815 }
4816
4817 if (!evaluate)
4818 {
4819 *arg = option_end;
4820 return OK;
4821 }
4822
4823 c = *option_end;
4824 *option_end = NUL;
4825 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828 if (opt_type == -3) /* invalid name */
4829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 EMSG2(_("E113: Unknown option: %s"), *arg);
4832 ret = FAIL;
4833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 if (opt_type == -2) /* hidden string option */
4837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 else if (opt_type == -1) /* hidden number option */
4842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004843 rettv->v_type = VAR_NUMBER;
4844 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 else if (opt_type == 1) /* number option */
4847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 rettv->v_type = VAR_NUMBER;
4849 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 else /* string option */
4852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 else if (working && (opt_type == -2 || opt_type == -1))
4858 ret = FAIL;
4859
4860 *option_end = c; /* put back for error messages */
4861 *arg = option_end;
4862
4863 return ret;
4864}
4865
4866/*
4867 * Allocate a variable for a string constant.
4868 * Return OK or FAIL.
4869 */
4870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004871get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
4873 char_u *p;
4874 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 int extra = 0;
4876
4877 /*
4878 * Find the end of the string, skipping backslashed characters.
4879 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004880 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 if (*p == '\\' && p[1] != NUL)
4883 {
4884 ++p;
4885 /* A "\<x>" form occupies at least 4 characters, and produces up
4886 * to 6 characters: reserve space for 2 extra */
4887 if (*p == '<')
4888 extra += 2;
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891
4892 if (*p != '"')
4893 {
4894 EMSG2(_("E114: Missing quote: %s"), *arg);
4895 return FAIL;
4896 }
4897
4898 /* If only parsing, set *arg and return here */
4899 if (!evaluate)
4900 {
4901 *arg = p + 1;
4902 return OK;
4903 }
4904
4905 /*
4906 * Copy the string into allocated memory, handling backslashed
4907 * characters.
4908 */
4909 name = alloc((unsigned)(p - *arg + extra));
4910 if (name == NULL)
4911 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912 rettv->v_type = VAR_STRING;
4913 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 if (*p == '\\')
4918 {
4919 switch (*++p)
4920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 case 'b': *name++ = BS; ++p; break;
4922 case 'e': *name++ = ESC; ++p; break;
4923 case 'f': *name++ = FF; ++p; break;
4924 case 'n': *name++ = NL; ++p; break;
4925 case 'r': *name++ = CAR; ++p; break;
4926 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 case 'X': /* hex: "\x1", "\x12" */
4929 case 'x':
4930 case 'u': /* Unicode: "\u0023" */
4931 case 'U':
4932 if (vim_isxdigit(p[1]))
4933 {
4934 int n, nr;
4935 int c = toupper(*p);
4936
4937 if (c == 'X')
4938 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004939 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004941 else
4942 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 nr = 0;
4944 while (--n >= 0 && vim_isxdigit(p[1]))
4945 {
4946 ++p;
4947 nr = (nr << 4) + hex2nr(*p);
4948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#ifdef FEAT_MBYTE
4951 /* For "\u" store the number according to
4952 * 'encoding'. */
4953 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004954 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 else
4956#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 break;
4960
4961 /* octal: "\1", "\12", "\123" */
4962 case '0':
4963 case '1':
4964 case '2':
4965 case '3':
4966 case '4':
4967 case '5':
4968 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 case '7': *name = *p++ - '0';
4970 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 *name = (*name << 3) + *p++ - '0';
4973 if (*p >= '0' && *p <= '7')
4974 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 break;
4978
4979 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004980 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (extra != 0)
4982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 break;
4985 }
4986 /* FALLTHROUGH */
4987
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 break;
4990 }
4991 }
4992 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004997 if (*p != NUL) /* just in case */
4998 ++p;
4999 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 return OK;
5002}
5003
5004/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 * Return OK or FAIL.
5007 */
5008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005009get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
5011 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005012 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005013 int reduce = 0;
5014
5015 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005017 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005018 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023 break;
5024 ++reduce;
5025 ++p;
5026 }
5027 }
5028
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005032 return FAIL;
5033 }
5034
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005036 if (!evaluate)
5037 {
5038 *arg = p + 1;
5039 return OK;
5040 }
5041
5042 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005044 */
5045 str = alloc((unsigned)((p - *arg) - reduce));
5046 if (str == NULL)
5047 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 rettv->v_type = VAR_STRING;
5049 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005050
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 break;
5057 ++p;
5058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 *arg = p + 1;
5063
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 return OK;
5065}
5066
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005067/*
5068 * Return the function name of the partial.
5069 */
5070 char_u *
5071partial_name(partial_T *pt)
5072{
5073 if (pt->pt_name != NULL)
5074 return pt->pt_name;
5075 return pt->pt_func->uf_name;
5076}
5077
Bram Moolenaarddecc252016-04-06 22:59:37 +02005078 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005079partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005080{
5081 int i;
5082
5083 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005084 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005085 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005086 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005087 if (pt->pt_name != NULL)
5088 {
5089 func_unref(pt->pt_name);
5090 vim_free(pt->pt_name);
5091 }
5092 else
5093 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005094 vim_free(pt);
5095}
5096
5097/*
5098 * Unreference a closure: decrement the reference count and free it when it
5099 * becomes zero.
5100 */
5101 void
5102partial_unref(partial_T *pt)
5103{
5104 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005106}
5107
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005108static int tv_equal_recurse_limit;
5109
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005110 static int
5111func_equal(
5112 typval_T *tv1,
5113 typval_T *tv2,
5114 int ic) /* ignore case */
5115{
5116 char_u *s1, *s2;
5117 dict_T *d1, *d2;
5118 int a1, a2;
5119 int i;
5120
5121 /* empty and NULL function name considered the same */
5122 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005123 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005124 if (s1 != NULL && *s1 == NUL)
5125 s1 = NULL;
5126 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005127 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005128 if (s2 != NULL && *s2 == NUL)
5129 s2 = NULL;
5130 if (s1 == NULL || s2 == NULL)
5131 {
5132 if (s1 != s2)
5133 return FALSE;
5134 }
5135 else if (STRCMP(s1, s2) != 0)
5136 return FALSE;
5137
5138 /* empty dict and NULL dict is different */
5139 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5140 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5141 if (d1 == NULL || d2 == NULL)
5142 {
5143 if (d1 != d2)
5144 return FALSE;
5145 }
5146 else if (!dict_equal(d1, d2, ic, TRUE))
5147 return FALSE;
5148
5149 /* empty list and no list considered the same */
5150 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5151 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5152 if (a1 != a2)
5153 return FALSE;
5154 for (i = 0; i < a1; ++i)
5155 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5156 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5157 return FALSE;
5158
5159 return TRUE;
5160}
5161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005163 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005164 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005166 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005167 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005168tv_equal(
5169 typval_T *tv1,
5170 typval_T *tv2,
5171 int ic, /* ignore case */
5172 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005173{
5174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005176 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005177 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005178
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005179 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005180 * recursiveness to a limit. We guess they are equal then.
5181 * A fixed limit has the problem of still taking an awful long time.
5182 * Reduce the limit every time running into it. That should work fine for
5183 * deeply linked structures that are not recursively linked and catch
5184 * recursiveness quickly. */
5185 if (!recursive)
5186 tv_equal_recurse_limit = 1000;
5187 if (recursive_cnt >= tv_equal_recurse_limit)
5188 {
5189 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005190 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005191 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005192
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005193 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5194 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005195 if ((tv1->v_type == VAR_FUNC
5196 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5197 && (tv2->v_type == VAR_FUNC
5198 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5199 {
5200 ++recursive_cnt;
5201 r = func_equal(tv1, tv2, ic);
5202 --recursive_cnt;
5203 return r;
5204 }
5205
5206 if (tv1->v_type != tv2->v_type)
5207 return FALSE;
5208
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005209 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005210 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005211 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005212 ++recursive_cnt;
5213 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005216
5217 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005218 ++recursive_cnt;
5219 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005222
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005223 case VAR_BLOB:
5224 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5225
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005226 case VAR_NUMBER:
5227 return tv1->vval.v_number == tv2->vval.v_number;
5228
5229 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005230 s1 = tv_get_string_buf(tv1, buf1);
5231 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005232 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005233
5234 case VAR_SPECIAL:
5235 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005236
5237 case VAR_FLOAT:
5238#ifdef FEAT_FLOAT
5239 return tv1->vval.v_float == tv2->vval.v_float;
5240#endif
5241 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005242#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005243 return tv1->vval.v_job == tv2->vval.v_job;
5244#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005245 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005246#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005247 return tv1->vval.v_channel == tv2->vval.v_channel;
5248#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005249 case VAR_FUNC:
5250 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005251 case VAR_UNKNOWN:
5252 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005253 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005254
Bram Moolenaara03f2332016-02-06 18:09:59 +01005255 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5256 * does not equal anything, not even itself. */
5257 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005258}
5259
5260/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005261 * Return the next (unique) copy ID.
5262 * Used for serializing nested structures.
5263 */
5264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005265get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005266{
5267 current_copyID += COPYID_INC;
5268 return current_copyID;
5269}
5270
5271/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 * Garbage collection for lists and dictionaries.
5273 *
5274 * We use reference counts to be able to free most items right away when they
5275 * are no longer used. But for composite items it's possible that it becomes
5276 * unused while the reference count is > 0: When there is a recursive
5277 * reference. Example:
5278 * :let l = [1, 2, 3]
5279 * :let d = {9: l}
5280 * :let l[1] = d
5281 *
5282 * Since this is quite unusual we handle this with garbage collection: every
5283 * once in a while find out which lists and dicts are not referenced from any
5284 * variable.
5285 *
5286 * Here is a good reference text about garbage collection (refers to Python
5287 * but it applies to all reference-counting mechanisms):
5288 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005289 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005290
5291/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005292 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005293 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005294 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005295 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005296 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005297garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005298{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005299 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005300 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005301 buf_T *buf;
5302 win_T *wp;
5303 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005304 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005305 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005306
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005307 if (!testing)
5308 {
5309 /* Only do this once. */
5310 want_garbage_collect = FALSE;
5311 may_garbage_collect = FALSE;
5312 garbage_collect_at_exit = FALSE;
5313 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005314
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005315 /* We advance by two because we add one for items referenced through
5316 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005317 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005319 /*
5320 * 1. Go through all accessible variables and mark all lists and dicts
5321 * with copyID.
5322 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005323
5324 /* Don't free variables in the previous_funccal list unless they are only
5325 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005326 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005328
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005329 /* script-local variables */
5330 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005331 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005332
5333 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005334 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5336 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005337
5338 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005339 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005340 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5341 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005342 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005343 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5344 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005346 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005347 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5349 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005350 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005351 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005352
5353 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005356 /* named functions (matters for closures) */
5357 abort = abort || set_ref_in_functions(copyID);
5358
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005359 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005361
Bram Moolenaard812df62008-11-09 12:46:09 +00005362 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005363 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005364
Bram Moolenaar1dced572012-04-05 16:54:08 +02005365#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005367#endif
5368
Bram Moolenaardb913952012-06-29 12:54:53 +02005369#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005371#endif
5372
5373#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005375#endif
5376
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005378 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005379 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005380#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005381#ifdef FEAT_NETBEANS_INTG
5382 abort = abort || set_ref_in_nb_channel(copyID);
5383#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005384
Bram Moolenaare3188e22016-05-31 21:13:04 +02005385#ifdef FEAT_TIMERS
5386 abort = abort || set_ref_in_timer(copyID);
5387#endif
5388
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005389#ifdef FEAT_QUICKFIX
5390 abort = abort || set_ref_in_quickfix(copyID);
5391#endif
5392
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005393#ifdef FEAT_TERMINAL
5394 abort = abort || set_ref_in_term(copyID);
5395#endif
5396
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005397 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005398 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 /*
5400 * 2. Free lists and dictionaries that are not referenced.
5401 */
5402 did_free = free_unref_items(copyID);
5403
5404 /*
5405 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005409 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 else if (p_verbose > 0)
5411 {
5412 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5413 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005414
5415 return did_free;
5416}
5417
5418/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005419 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005420 */
5421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005423{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005424 int did_free = FALSE;
5425
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 /* Let all "free" functions know that we are here. This means no
5427 * dictionaries, lists, channels or jobs are to be freed, because we will
5428 * do that here. */
5429 in_free_unref_items = TRUE;
5430
5431 /*
5432 * PASS 1: free the contents of the items. We don't free the items
5433 * themselves yet, so that it is possible to decrement refcount counters
5434 */
5435
Bram Moolenaarcd524592016-07-17 14:57:05 +02005436 /* Go through the list of dicts and free items without the copyID. */
5437 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005438
Bram Moolenaarda861d62016-07-17 15:46:27 +02005439 /* Go through the list of lists and free items without the copyID. */
5440 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441
5442#ifdef FEAT_JOB_CHANNEL
5443 /* Go through the list of jobs and free items without the copyID. This
5444 * must happen before doing channels, because jobs refer to channels, but
5445 * the reference from the channel to the job isn't tracked. */
5446 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5447
5448 /* Go through the list of channels and free items without the copyID. */
5449 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5450#endif
5451
5452 /*
5453 * PASS 2: free the items themselves.
5454 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005455 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005456 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005457
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458#ifdef FEAT_JOB_CHANNEL
5459 /* Go through the list of jobs and free items without the copyID. This
5460 * must happen before doing channels, because jobs refer to channels, but
5461 * the reference from the channel to the job isn't tracked. */
5462 free_unused_jobs(copyID, COPYID_MASK);
5463
5464 /* Go through the list of channels and free items without the copyID. */
5465 free_unused_channels(copyID, COPYID_MASK);
5466#endif
5467
5468 in_free_unref_items = FALSE;
5469
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470 return did_free;
5471}
5472
5473/*
5474 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005475 * "list_stack" is used to add lists to be marked. Can be NULL.
5476 *
5477 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005480set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005481{
5482 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005483 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005484 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 hashtab_T *cur_ht;
5486 ht_stack_T *ht_stack = NULL;
5487 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005488
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 cur_ht = ht;
5490 for (;;)
5491 {
5492 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005493 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 /* Mark each item in the hashtab. If the item contains a hashtab
5495 * it is added to ht_stack, if it contains a list it is added to
5496 * list_stack. */
5497 todo = (int)cur_ht->ht_used;
5498 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5499 if (!HASHITEM_EMPTY(hi))
5500 {
5501 --todo;
5502 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5503 &ht_stack, list_stack);
5504 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005505 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005506
5507 if (ht_stack == NULL)
5508 break;
5509
5510 /* take an item from the stack */
5511 cur_ht = ht_stack->ht;
5512 tempitem = ht_stack;
5513 ht_stack = ht_stack->prev;
5514 free(tempitem);
5515 }
5516
5517 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005518}
5519
5520/*
5521 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005522 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5523 *
5524 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005525 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005528{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005529 listitem_T *li;
5530 int abort = FALSE;
5531 list_T *cur_l;
5532 list_stack_T *list_stack = NULL;
5533 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005534
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005535 cur_l = l;
5536 for (;;)
5537 {
5538 if (!abort)
5539 /* Mark each item in the list. If the item contains a hashtab
5540 * it is added to ht_stack, if it contains a list it is added to
5541 * list_stack. */
5542 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5543 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5544 ht_stack, &list_stack);
5545 if (list_stack == NULL)
5546 break;
5547
5548 /* take an item from the stack */
5549 cur_l = list_stack->list;
5550 tempitem = list_stack;
5551 list_stack = list_stack->prev;
5552 free(tempitem);
5553 }
5554
5555 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005556}
5557
5558/*
5559 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005560 * "list_stack" is used to add lists to be marked. Can be NULL.
5561 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5562 *
5563 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005564 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566set_ref_in_item(
5567 typval_T *tv,
5568 int copyID,
5569 ht_stack_T **ht_stack,
5570 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005571{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005573
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005574 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005575 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005576 dict_T *dd = tv->vval.v_dict;
5577
Bram Moolenaara03f2332016-02-06 18:09:59 +01005578 if (dd != NULL && dd->dv_copyID != copyID)
5579 {
5580 /* Didn't see this dict yet. */
5581 dd->dv_copyID = copyID;
5582 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005583 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005584 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5585 }
5586 else
5587 {
5588 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5589 if (newitem == NULL)
5590 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 else
5592 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005593 newitem->ht = &dd->dv_hashtab;
5594 newitem->prev = *ht_stack;
5595 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005596 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005597 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005598 }
5599 }
5600 else if (tv->v_type == VAR_LIST)
5601 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005602 list_T *ll = tv->vval.v_list;
5603
Bram Moolenaara03f2332016-02-06 18:09:59 +01005604 if (ll != NULL && ll->lv_copyID != copyID)
5605 {
5606 /* Didn't see this list yet. */
5607 ll->lv_copyID = copyID;
5608 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005609 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005610 abort = set_ref_in_list(ll, copyID, ht_stack);
5611 }
5612 else
5613 {
5614 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005616 if (newitem == NULL)
5617 abort = TRUE;
5618 else
5619 {
5620 newitem->list = ll;
5621 newitem->prev = *list_stack;
5622 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005624 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005625 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005626 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005627 else if (tv->v_type == VAR_FUNC)
5628 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005629 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005630 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005631 else if (tv->v_type == VAR_PARTIAL)
5632 {
5633 partial_T *pt = tv->vval.v_partial;
5634 int i;
5635
5636 /* A partial does not have a copyID, because it cannot contain itself.
5637 */
5638 if (pt != NULL)
5639 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005640 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005641
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005642 if (pt->pt_dict != NULL)
5643 {
5644 typval_T dtv;
5645
5646 dtv.v_type = VAR_DICT;
5647 dtv.vval.v_dict = pt->pt_dict;
5648 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5649 }
5650
5651 for (i = 0; i < pt->pt_argc; ++i)
5652 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5653 ht_stack, list_stack);
5654 }
5655 }
5656#ifdef FEAT_JOB_CHANNEL
5657 else if (tv->v_type == VAR_JOB)
5658 {
5659 job_T *job = tv->vval.v_job;
5660 typval_T dtv;
5661
5662 if (job != NULL && job->jv_copyID != copyID)
5663 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005664 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005665 if (job->jv_channel != NULL)
5666 {
5667 dtv.v_type = VAR_CHANNEL;
5668 dtv.vval.v_channel = job->jv_channel;
5669 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5670 }
5671 if (job->jv_exit_partial != NULL)
5672 {
5673 dtv.v_type = VAR_PARTIAL;
5674 dtv.vval.v_partial = job->jv_exit_partial;
5675 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5676 }
5677 }
5678 }
5679 else if (tv->v_type == VAR_CHANNEL)
5680 {
5681 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005682 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005683 typval_T dtv;
5684 jsonq_T *jq;
5685 cbq_T *cq;
5686
5687 if (ch != NULL && ch->ch_copyID != copyID)
5688 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005689 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005690 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005691 {
5692 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5693 jq = jq->jq_next)
5694 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5695 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5696 cq = cq->cq_next)
5697 if (cq->cq_partial != NULL)
5698 {
5699 dtv.v_type = VAR_PARTIAL;
5700 dtv.vval.v_partial = cq->cq_partial;
5701 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5702 }
5703 if (ch->ch_part[part].ch_partial != NULL)
5704 {
5705 dtv.v_type = VAR_PARTIAL;
5706 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5707 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5708 }
5709 }
5710 if (ch->ch_partial != NULL)
5711 {
5712 dtv.v_type = VAR_PARTIAL;
5713 dtv.vval.v_partial = ch->ch_partial;
5714 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5715 }
5716 if (ch->ch_close_partial != NULL)
5717 {
5718 dtv.v_type = VAR_PARTIAL;
5719 dtv.vval.v_partial = ch->ch_close_partial;
5720 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5721 }
5722 }
5723 }
5724#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005725 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005726}
5727
Bram Moolenaar17a13432016-01-24 14:22:10 +01005728 static char *
5729get_var_special_name(int nr)
5730{
5731 switch (nr)
5732 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005733 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005734 case VVAL_TRUE: return "v:true";
5735 case VVAL_NONE: return "v:none";
5736 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005737 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005738 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005739 return "42";
5740}
5741
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 * Return a string with the string representation of a variable.
5744 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005745 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005746 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005747 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5748 * stings as "string()", otherwise does not put quotes around strings, as
5749 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005750 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5751 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005752 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005754 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005755echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005756 typval_T *tv,
5757 char_u **tofree,
5758 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005759 int copyID,
5760 int echo_style,
5761 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005762 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005764 static int recurse = 0;
5765 char_u *r = NULL;
5766
Bram Moolenaar33570922005-01-25 22:26:29 +00005767 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005768 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005769 if (!did_echo_string_emsg)
5770 {
5771 /* Only give this message once for a recursive call to avoid
5772 * flooding the user with errors. And stop iterating over lists
5773 * and dicts. */
5774 did_echo_string_emsg = TRUE;
5775 EMSG(_("E724: variable nested too deep for displaying"));
5776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005777 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005778 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005779 }
5780 ++recurse;
5781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 switch (tv->v_type)
5783 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005784 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005785 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005786 {
5787 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005788 r = tv->vval.v_string;
5789 if (r == NULL)
5790 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005791 }
5792 else
5793 {
5794 *tofree = string_quote(tv->vval.v_string, FALSE);
5795 r = *tofree;
5796 }
5797 break;
5798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005800 if (echo_style)
5801 {
5802 *tofree = NULL;
5803 r = tv->vval.v_string;
5804 }
5805 else
5806 {
5807 *tofree = string_quote(tv->vval.v_string, TRUE);
5808 r = *tofree;
5809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005811
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005812 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005813 {
5814 partial_T *pt = tv->vval.v_partial;
5815 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005816 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005817 garray_T ga;
5818 int i;
5819 char_u *tf;
5820
5821 ga_init2(&ga, 1, 100);
5822 ga_concat(&ga, (char_u *)"function(");
5823 if (fname != NULL)
5824 {
5825 ga_concat(&ga, fname);
5826 vim_free(fname);
5827 }
5828 if (pt != NULL && pt->pt_argc > 0)
5829 {
5830 ga_concat(&ga, (char_u *)", [");
5831 for (i = 0; i < pt->pt_argc; ++i)
5832 {
5833 if (i > 0)
5834 ga_concat(&ga, (char_u *)", ");
5835 ga_concat(&ga,
5836 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5837 vim_free(tf);
5838 }
5839 ga_concat(&ga, (char_u *)"]");
5840 }
5841 if (pt != NULL && pt->pt_dict != NULL)
5842 {
5843 typval_T dtv;
5844
5845 ga_concat(&ga, (char_u *)", ");
5846 dtv.v_type = VAR_DICT;
5847 dtv.vval.v_dict = pt->pt_dict;
5848 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5849 vim_free(tf);
5850 }
5851 ga_concat(&ga, (char_u *)")");
5852
5853 *tofree = ga.ga_data;
5854 r = *tofree;
5855 break;
5856 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005857
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005858 case VAR_BLOB:
5859 if (tv->vval.v_blob == NULL)
5860 {
5861 *tofree = NULL;
5862 r = (char_u *)"[]";
5863 }
5864 else
5865 {
5866 blob_T *b;
5867 int i;
5868 garray_T ga;
5869
5870 // Store bytes in the growarray.
5871 ga_init2(&ga, 1, 4000);
5872 b = tv->vval.v_blob;
5873 ga_append(&ga, '[');
5874 for (i = 0; i < blob_len(b); i++)
5875 {
5876 if (i > 0)
5877 ga_concat(&ga, (char_u *)",");
5878 vim_snprintf((char *)numbuf, NUMBUFLEN, "0x%02X",
5879 (int)blob_get(b, i));
5880 ga_concat(&ga, numbuf);
5881 }
5882 ga_append(&ga, ']');
5883 *tofree = ga.ga_data;
5884 r = *tofree;
5885 }
5886 break;
5887
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005889 if (tv->vval.v_list == NULL)
5890 {
5891 *tofree = NULL;
5892 r = NULL;
5893 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005894 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5895 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005896 {
5897 *tofree = NULL;
5898 r = (char_u *)"[...]";
5899 }
5900 else
5901 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005902 int old_copyID = tv->vval.v_list->lv_copyID;
5903
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005905 *tofree = list2string(tv, copyID, restore_copyID);
5906 if (restore_copyID)
5907 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005908 r = *tofree;
5909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005911
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005913 if (tv->vval.v_dict == NULL)
5914 {
5915 *tofree = NULL;
5916 r = NULL;
5917 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005918 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5919 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005920 {
5921 *tofree = NULL;
5922 r = (char_u *)"{...}";
5923 }
5924 else
5925 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005926 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005927 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005928 *tofree = dict2string(tv, copyID, restore_copyID);
5929 if (restore_copyID)
5930 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005931 r = *tofree;
5932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005936 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005937 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005938 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005939 break;
5940
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005942 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005943 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005944 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005945 if (composite_val)
5946 {
5947 *tofree = string_quote(r, FALSE);
5948 r = *tofree;
5949 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005952 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005953#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005954 *tofree = NULL;
5955 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5956 r = numbuf;
5957 break;
5958#endif
5959
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005960 case VAR_SPECIAL:
5961 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005962 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005963 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965
Bram Moolenaar8502c702014-06-17 12:51:16 +02005966 if (--recurse == 0)
5967 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969}
5970
5971/*
5972 * Return a string with the string representation of a variable.
5973 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5974 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005975 * Does not put quotes around strings, as ":echo" displays values.
5976 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5977 * May return NULL.
5978 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005979 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005980echo_string(
5981 typval_T *tv,
5982 char_u **tofree,
5983 char_u *numbuf,
5984 int copyID)
5985{
5986 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5987}
5988
5989/*
5990 * Return a string with the string representation of a variable.
5991 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5992 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005994 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005996 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997tv2string(
5998 typval_T *tv,
5999 char_u **tofree,
6000 char_u *numbuf,
6001 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006002{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006003 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004}
6005
6006/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 * Return string "str" in ' quotes, doubling ' characters.
6008 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 char_u *p, *r, *s;
6016
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 len = (function ? 13 : 3);
6018 if (str != NULL)
6019 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006020 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006021 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 if (*p == '\'')
6023 ++len;
6024 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025 s = r = alloc(len);
6026 if (r != NULL)
6027 {
6028 if (function)
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 r += 10;
6032 }
6033 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 if (str != NULL)
6036 for (p = str; *p != NUL; )
6037 {
6038 if (*p == '\'')
6039 *r++ = '\'';
6040 MB_COPY_CHAR(p, r);
6041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006043 if (function)
6044 *r++ = ')';
6045 *r++ = NUL;
6046 }
6047 return s;
6048}
6049
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006050#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006051/*
6052 * Convert the string "text" to a floating point number.
6053 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6054 * this always uses a decimal point.
6055 * Returns the length of the text that was consumed.
6056 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058string2float(
6059 char_u *text,
6060 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006061{
6062 char *s = (char *)text;
6063 float_T f;
6064
Bram Moolenaar62473612017-01-08 19:25:40 +01006065 /* MS-Windows does not deal with "inf" and "nan" properly. */
6066 if (STRNICMP(text, "inf", 3) == 0)
6067 {
6068 *value = INFINITY;
6069 return 3;
6070 }
6071 if (STRNICMP(text, "-inf", 3) == 0)
6072 {
6073 *value = -INFINITY;
6074 return 4;
6075 }
6076 if (STRNICMP(text, "nan", 3) == 0)
6077 {
6078 *value = NAN;
6079 return 3;
6080 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006081 f = strtod(s, &s);
6082 *value = f;
6083 return (int)((char_u *)s - text);
6084}
6085#endif
6086
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 * Get the value of an environment variable.
6089 * "arg" is pointing to the '$'. It is advanced to after the name.
6090 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006091 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 */
6093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *string = NULL;
6097 int len;
6098 int cc;
6099 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006100 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 ++*arg;
6103 name = *arg;
6104 len = get_env_len(arg);
6105 if (evaluate)
6106 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006107 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006108 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006109
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006110 cc = name[len];
6111 name[len] = NUL;
6112 /* first try vim_getenv(), fast for normal environment vars */
6113 string = vim_getenv(name, &mustfree);
6114 if (string != NULL && *string != NUL)
6115 {
6116 if (!mustfree)
6117 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006119 else
6120 {
6121 if (mustfree)
6122 vim_free(string);
6123
6124 /* next try expanding things like $VIM and ${HOME} */
6125 string = expand_env_save(name - 1);
6126 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006127 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006128 }
6129 name[len] = cc;
6130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006131 rettv->v_type = VAR_STRING;
6132 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 }
6134
6135 return OK;
6136}
6137
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006138
6139
6140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006142 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006145var2fpos(
6146 typval_T *varp,
6147 int dollar_lnum, /* TRUE when $ is last line */
6148 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006150 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006152 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaara5525202006-03-02 22:52:09 +00006154 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006155 if (varp->v_type == VAR_LIST)
6156 {
6157 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006158 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006159 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006160 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006161
6162 l = varp->vval.v_list;
6163 if (l == NULL)
6164 return NULL;
6165
6166 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006167 pos.lnum = list_find_nr(l, 0L, &error);
6168 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006169 return NULL; /* invalid line number */
6170
6171 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006172 pos.col = list_find_nr(l, 1L, &error);
6173 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006174 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006175 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006176
6177 /* We accept "$" for the column number: last column. */
6178 li = list_find(l, 1L);
6179 if (li != NULL && li->li_tv.v_type == VAR_STRING
6180 && li->li_tv.vval.v_string != NULL
6181 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6182 pos.col = len + 1;
6183
Bram Moolenaara5525202006-03-02 22:52:09 +00006184 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006185 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006186 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006187 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006188
Bram Moolenaara5525202006-03-02 22:52:09 +00006189#ifdef FEAT_VIRTUALEDIT
6190 /* Get the virtual offset. Defaults to zero. */
6191 pos.coladd = list_find_nr(l, 2L, &error);
6192 if (error)
6193 pos.coladd = 0;
6194#endif
6195
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 return &pos;
6197 }
6198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006199 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006200 if (name == NULL)
6201 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006202 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006204 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6205 {
6206 if (VIsual_active)
6207 return &VIsual;
6208 return &curwin->w_cursor;
6209 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006210 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006212 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6214 return NULL;
6215 return pp;
6216 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006217
6218#ifdef FEAT_VIRTUALEDIT
6219 pos.coladd = 0;
6220#endif
6221
Bram Moolenaar477933c2007-07-17 14:32:23 +00006222 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006223 {
6224 pos.col = 0;
6225 if (name[1] == '0') /* "w0": first visible line */
6226 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006227 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006228 /* In silent Ex mode topline is zero, but that's not a valid line
6229 * number; use one instead. */
6230 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006231 return &pos;
6232 }
6233 else if (name[1] == '$') /* "w$": last visible line */
6234 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006236 /* In silent Ex mode botline is zero, return zero then. */
6237 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006238 return &pos;
6239 }
6240 }
6241 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006243 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 pos.lnum = curbuf->b_ml.ml_line_count;
6246 pos.col = 0;
6247 }
6248 else
6249 {
6250 pos.lnum = curwin->w_cursor.lnum;
6251 pos.col = (colnr_T)STRLEN(ml_get_curline());
6252 }
6253 return &pos;
6254 }
6255 return NULL;
6256}
6257
6258/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006259 * Convert list in "arg" into a position and optional file number.
6260 * When "fnump" is NULL there is no file number, only 3 items.
6261 * Note that the column is passed on as-is, the caller may want to decrement
6262 * it to use 1 for the first column.
6263 * Return FAIL when conversion is not possible, doesn't check the position for
6264 * validity.
6265 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006267list2fpos(
6268 typval_T *arg,
6269 pos_T *posp,
6270 int *fnump,
6271 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006272{
6273 list_T *l = arg->vval.v_list;
6274 long i = 0;
6275 long n;
6276
Bram Moolenaar493c1782014-05-28 14:34:46 +02006277 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6278 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006279 if (arg->v_type != VAR_LIST
6280 || l == NULL
6281 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006282 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006283 return FAIL;
6284
6285 if (fnump != NULL)
6286 {
6287 n = list_find_nr(l, i++, NULL); /* fnum */
6288 if (n < 0)
6289 return FAIL;
6290 if (n == 0)
6291 n = curbuf->b_fnum; /* current buffer */
6292 *fnump = n;
6293 }
6294
6295 n = list_find_nr(l, i++, NULL); /* lnum */
6296 if (n < 0)
6297 return FAIL;
6298 posp->lnum = n;
6299
6300 n = list_find_nr(l, i++, NULL); /* col */
6301 if (n < 0)
6302 return FAIL;
6303 posp->col = n;
6304
6305#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006306 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006307 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006308 posp->coladd = 0;
6309 else
6310 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006311#endif
6312
Bram Moolenaar493c1782014-05-28 14:34:46 +02006313 if (curswantp != NULL)
6314 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6315
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006316 return OK;
6317}
6318
6319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 * Get the length of an environment variable name.
6321 * Advance "arg" to the first character after the name.
6322 * Return 0 for error.
6323 */
6324 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u *p;
6328 int len;
6329
6330 for (p = *arg; vim_isIDc(*p); ++p)
6331 ;
6332 if (p == *arg) /* no name found */
6333 return 0;
6334
6335 len = (int)(p - *arg);
6336 *arg = p;
6337 return len;
6338}
6339
6340/*
6341 * Get the length of the name of a function or internal variable.
6342 * "arg" is advanced to the first non-white character after the name.
6343 * Return 0 if something is wrong.
6344 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 char_u *p;
6349 int len;
6350
6351 /* Find the end of the name. */
6352 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006353 {
6354 if (*p == ':')
6355 {
6356 /* "s:" is start of "s:var", but "n:" is not and can be used in
6357 * slice "[n:]". Also "xx:" is not a namespace. */
6358 len = (int)(p - *arg);
6359 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6360 || len > 1)
6361 break;
6362 }
6363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 if (p == *arg) /* no name found */
6365 return 0;
6366
6367 len = (int)(p - *arg);
6368 *arg = skipwhite(p);
6369
6370 return len;
6371}
6372
6373/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006374 * Get the length of the name of a variable or function.
6375 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006377 * Return -1 if curly braces expansion failed.
6378 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 * If the name contains 'magic' {}'s, expand them and return the
6380 * expanded name in an allocated string via 'alias' - caller must free.
6381 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383get_name_len(
6384 char_u **arg,
6385 char_u **alias,
6386 int evaluate,
6387 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 char_u *p;
6391 char_u *expr_start;
6392 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394 *alias = NULL; /* default to no alias */
6395
6396 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6397 && (*arg)[2] == (int)KE_SNR)
6398 {
6399 /* hard coded <SNR>, already translated */
6400 *arg += 3;
6401 return get_id_len(arg) + 3;
6402 }
6403 len = eval_fname_script(*arg);
6404 if (len > 0)
6405 {
6406 /* literal "<SID>", "s:" or "<SNR>" */
6407 *arg += len;
6408 }
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006411 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006413 p = find_name_end(*arg, &expr_start, &expr_end,
6414 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (expr_start != NULL)
6416 {
6417 char_u *temp_string;
6418
6419 if (!evaluate)
6420 {
6421 len += (int)(p - *arg);
6422 *arg = skipwhite(p);
6423 return len;
6424 }
6425
6426 /*
6427 * Include any <SID> etc in the expanded string:
6428 * Thus the -len here.
6429 */
6430 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6431 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006432 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 *alias = temp_string;
6434 *arg = skipwhite(p);
6435 return (int)STRLEN(temp_string);
6436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006439 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 EMSG2(_(e_invexpr2), *arg);
6441
6442 return len;
6443}
6444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006445/*
6446 * Find the end of a variable or function name, taking care of magic braces.
6447 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6448 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006449 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006450 * Return a pointer to just after the name. Equal to "arg" if there is no
6451 * valid name.
6452 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454find_name_end(
6455 char_u *arg,
6456 char_u **expr_start,
6457 char_u **expr_end,
6458 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460 int mb_nest = 0;
6461 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006463 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006465 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467 *expr_start = NULL;
6468 *expr_end = NULL;
6469 }
6470
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006471 /* Quick check for valid starting character. */
6472 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6473 return arg;
6474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006475 for (p = arg; *p != NUL
6476 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006478 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006479 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006480 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006482 if (*p == '\'')
6483 {
6484 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006485 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006486 ;
6487 if (*p == NUL)
6488 break;
6489 }
6490 else if (*p == '"')
6491 {
6492 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006493 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006494 if (*p == '\\' && p[1] != NUL)
6495 ++p;
6496 if (*p == NUL)
6497 break;
6498 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006499 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6500 {
6501 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006502 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006503 len = (int)(p - arg);
6504 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006505 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006506 break;
6507 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006509 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006511 if (*p == '[')
6512 ++br_nest;
6513 else if (*p == ']')
6514 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006517 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 if (*p == '{')
6520 {
6521 mb_nest++;
6522 if (expr_start != NULL && *expr_start == NULL)
6523 *expr_start = p;
6524 }
6525 else if (*p == '}')
6526 {
6527 mb_nest--;
6528 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6529 *expr_end = p;
6530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
6533
6534 return p;
6535}
6536
6537/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006538 * Expands out the 'magic' {}'s in a variable/function name.
6539 * Note that this can call itself recursively, to deal with
6540 * constructs like foo{bar}{baz}{bam}
6541 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6542 * "in_start" ^
6543 * "expr_start" ^
6544 * "expr_end" ^
6545 * "in_end" ^
6546 *
6547 * Returns a new allocated string, which the caller must free.
6548 * Returns NULL for failure.
6549 */
6550 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006551make_expanded_name(
6552 char_u *in_start,
6553 char_u *expr_start,
6554 char_u *expr_end,
6555 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006556{
6557 char_u c1;
6558 char_u *retval = NULL;
6559 char_u *temp_result;
6560 char_u *nextcmd = NULL;
6561
6562 if (expr_end == NULL || in_end == NULL)
6563 return NULL;
6564 *expr_start = NUL;
6565 *expr_end = NUL;
6566 c1 = *in_end;
6567 *in_end = NUL;
6568
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006569 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006570 if (temp_result != NULL && nextcmd == NULL)
6571 {
6572 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6573 + (in_end - expr_end) + 1));
6574 if (retval != NULL)
6575 {
6576 STRCPY(retval, in_start);
6577 STRCAT(retval, temp_result);
6578 STRCAT(retval, expr_end + 1);
6579 }
6580 }
6581 vim_free(temp_result);
6582
6583 *in_end = c1; /* put char back for error messages */
6584 *expr_start = '{';
6585 *expr_end = '}';
6586
6587 if (retval != NULL)
6588 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006589 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006590 if (expr_start != NULL)
6591 {
6592 /* Further expansion! */
6593 temp_result = make_expanded_name(retval, expr_start,
6594 expr_end, temp_result);
6595 vim_free(retval);
6596 retval = temp_result;
6597 }
6598 }
6599
6600 return retval;
6601}
6602
6603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006608eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006610 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6611}
6612
6613/*
6614 * Return TRUE if character "c" can be used as the first character in a
6615 * variable or function name (excluding '{' and '}').
6616 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006618eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006619{
6620 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621}
6622
6623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 * Set number v: variable to "val".
6625 */
6626 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006627set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006629 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630}
6631
6632/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006633 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006635 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
6640
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006641/*
6642 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006643 * If the String variable has never been set, return an empty string.
6644 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006645 */
6646 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006648{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006649 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006650}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006653 * Get List v: variable value. Caller must take care of reference count when
6654 * needed.
6655 */
6656 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006657get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006658{
6659 return vimvars[idx].vv_list;
6660}
6661
6662/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006663 * Get Dict v: variable value. Caller must take care of reference count when
6664 * needed.
6665 */
6666 dict_T *
6667get_vim_var_dict(int idx)
6668{
6669 return vimvars[idx].vv_dict;
6670}
6671
6672/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006673 * Set v:char to character "c".
6674 */
6675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006677{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006678 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006679
6680#ifdef FEAT_MBYTE
6681 if (has_mbyte)
6682 buf[(*mb_char2bytes)(c, buf)] = NUL;
6683 else
6684#endif
6685 {
6686 buf[0] = c;
6687 buf[1] = NUL;
6688 }
6689 set_vim_var_string(VV_CHAR, buf, -1);
6690}
6691
6692/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006693 * Set v:count to "count" and v:count1 to "count1".
6694 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 */
6696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006697set_vcount(
6698 long count,
6699 long count1,
6700 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006702 if (set_prevcount)
6703 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006704 vimvars[VV_COUNT].vv_nr = count;
6705 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706}
6707
6708/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006709 * Save variables that might be changed as a side effect. Used when executing
6710 * a timer callback.
6711 */
6712 void
6713save_vimvars(vimvars_save_T *vvsave)
6714{
6715 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6716 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6717 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6718}
6719
6720/*
6721 * Restore variables saved by save_vimvars().
6722 */
6723 void
6724restore_vimvars(vimvars_save_T *vvsave)
6725{
6726 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6727 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6728 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6729}
6730
6731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Set string v: variable to a copy of "val".
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735set_vim_var_string(
6736 int idx,
6737 char_u *val,
6738 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
Bram Moolenaara542c682016-01-31 16:28:04 +01006740 clear_tv(&vimvars[idx].vv_di.di_tv);
6741 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006743 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006747 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748}
6749
6750/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006751 * Set List v: variable to "val".
6752 */
6753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006754set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006755{
Bram Moolenaara542c682016-01-31 16:28:04 +01006756 clear_tv(&vimvars[idx].vv_di.di_tv);
6757 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006758 vimvars[idx].vv_list = val;
6759 if (val != NULL)
6760 ++val->lv_refcount;
6761}
6762
6763/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006764 * Set Dictionary v: variable to "val".
6765 */
6766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006768{
Bram Moolenaara542c682016-01-31 16:28:04 +01006769 clear_tv(&vimvars[idx].vv_di.di_tv);
6770 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006771 vimvars[idx].vv_dict = val;
6772 if (val != NULL)
6773 {
6774 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006775 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006776 }
6777}
6778
6779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 * Set v:register if needed.
6781 */
6782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 char_u regname;
6786
6787 if (c == 0 || c == ' ')
6788 regname = '"';
6789 else
6790 regname = c;
6791 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006792 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 set_vim_var_string(VV_REG, &regname, 1);
6794}
6795
6796/*
6797 * Get or set v:exception. If "oldval" == NULL, return the current value.
6798 * Otherwise, restore the value to "oldval" and return NULL.
6799 * Must always be called in pairs to save and restore v:exception! Does not
6800 * take care of memory allocations.
6801 */
6802 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006803v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006806 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 return NULL;
6810}
6811
6812/*
6813 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6814 * Otherwise, restore the value to "oldval" and return NULL.
6815 * Must always be called in pairs to save and restore v:throwpoint! Does not
6816 * take care of memory allocations.
6817 */
6818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 return NULL;
6826}
6827
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828/*
6829 * Set v:cmdarg.
6830 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6831 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6832 * Must always be called in pairs!
6833 */
6834 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 char_u *oldval;
6838 char_u *newval;
6839 unsigned len;
6840
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006842 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006844 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006846 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006849 if (eap->force_bin == FORCE_BIN)
6850 len = 6;
6851 else if (eap->force_bin == FORCE_NOBIN)
6852 len = 8;
6853 else
6854 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006855
6856 if (eap->read_edit)
6857 len += 7;
6858
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006859 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006860 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006861# ifdef FEAT_MBYTE
6862 if (eap->force_enc != 0)
6863 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006864 if (eap->bad_char != 0)
6865 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006866# endif
6867
6868 newval = alloc(len + 1);
6869 if (newval == NULL)
6870 return NULL;
6871
6872 if (eap->force_bin == FORCE_BIN)
6873 sprintf((char *)newval, " ++bin");
6874 else if (eap->force_bin == FORCE_NOBIN)
6875 sprintf((char *)newval, " ++nobin");
6876 else
6877 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878
6879 if (eap->read_edit)
6880 STRCAT(newval, " ++edit");
6881
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006882 if (eap->force_ff != 0)
6883 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006884 eap->force_ff == 'u' ? "unix"
6885 : eap->force_ff == 'd' ? "dos"
6886 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006887#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006888 if (eap->force_enc != 0)
6889 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6890 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006891 if (eap->bad_char == BAD_KEEP)
6892 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6893 else if (eap->bad_char == BAD_DROP)
6894 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6895 else if (eap->bad_char != 0)
6896 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006897#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006899 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
6902/*
6903 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006904 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006907get_var_tv(
6908 char_u *name,
6909 int len, /* length of "name" */
6910 typval_T *rettv, /* NULL when only checking existence */
6911 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6912 int verbose, /* may give error message */
6913 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
6920 /* truncate the name, so that we can use strcmp() */
6921 cc = name[len];
6922 name[len] = NUL;
6923
6924 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 * Check for user-defined variables.
6926 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006927 v = find_var(name, NULL, no_autoload);
6928 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006930 tv = &v->di_tv;
6931 if (dip != NULL)
6932 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
6934
Bram Moolenaare9a41262005-01-15 22:18:47 +00006935 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006937 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006938 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 ret = FAIL;
6940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
6944 name[len] = cc;
6945
6946 return ret;
6947}
6948
6949/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006950 * Check if variable "name[len]" is a local variable or an argument.
6951 * If so, "*eval_lavars_used" is set to TRUE.
6952 */
6953 static void
6954check_vars(char_u *name, int len)
6955{
6956 int cc;
6957 char_u *varname;
6958 hashtab_T *ht;
6959
6960 if (eval_lavars_used == NULL)
6961 return;
6962
6963 /* truncate the name, so that we can use strcmp() */
6964 cc = name[len];
6965 name[len] = NUL;
6966
6967 ht = find_var_ht(name, &varname);
6968 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6969 {
6970 if (find_var(name, NULL, TRUE) != NULL)
6971 *eval_lavars_used = TRUE;
6972 }
6973
6974 name[len] = cc;
6975}
6976
6977/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006978 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6979 * Also handle function call with Funcref variable: func(expr)
6980 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6981 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006983handle_subscript(
6984 char_u **arg,
6985 typval_T *rettv,
6986 int evaluate, /* do more than finding the end */
6987 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006988{
6989 int ret = OK;
6990 dict_T *selfdict = NULL;
6991 char_u *s;
6992 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006994
6995 while (ret == OK
6996 && (**arg == '['
6997 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006998 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6999 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007000 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007001 {
7002 if (**arg == '(')
7003 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007004 partial_T *pt = NULL;
7005
Bram Moolenaard9fba312005-06-26 22:34:35 +00007006 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007007 if (evaluate)
7008 {
7009 functv = *rettv;
7010 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007011
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007012 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007013 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007014 {
7015 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007016 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007017 }
7018 else
7019 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007020 }
7021 else
7022 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007023 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007024 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007025 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007026
7027 /* Clear the funcref afterwards, so that deleting it while
7028 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007029 if (evaluate)
7030 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007031
7032 /* Stop the expression evaluation when immediately aborting on
7033 * error, or when an interrupt occurred or an exception was thrown
7034 * but not caught. */
7035 if (aborting())
7036 {
7037 if (ret == OK)
7038 clear_tv(rettv);
7039 ret = FAIL;
7040 }
7041 dict_unref(selfdict);
7042 selfdict = NULL;
7043 }
7044 else /* **arg == '[' || **arg == '.' */
7045 {
7046 dict_unref(selfdict);
7047 if (rettv->v_type == VAR_DICT)
7048 {
7049 selfdict = rettv->vval.v_dict;
7050 if (selfdict != NULL)
7051 ++selfdict->dv_refcount;
7052 }
7053 else
7054 selfdict = NULL;
7055 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7056 {
7057 clear_tv(rettv);
7058 ret = FAIL;
7059 }
7060 }
7061 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007062
Bram Moolenaar1d429612016-05-24 15:44:17 +02007063 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7064 * Don't do this when "Func" is already a partial that was bound
7065 * explicitly (pt_auto is FALSE). */
7066 if (selfdict != NULL
7067 && (rettv->v_type == VAR_FUNC
7068 || (rettv->v_type == VAR_PARTIAL
7069 && (rettv->vval.v_partial->pt_auto
7070 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007071 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007072
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007073 dict_unref(selfdict);
7074 return ret;
7075}
7076
7077/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007078 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007079 * value).
7080 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007081 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007082alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083{
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085}
7086
7087/*
7088 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 * The string "s" must have been allocated, it is consumed.
7090 * Return NULL for out of memory, the variable otherwise.
7091 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007093alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007097 rettv = alloc_tv();
7098 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 rettv->v_type = VAR_STRING;
7101 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 }
7103 else
7104 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007105 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106}
7107
7108/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 if (varp != NULL)
7115 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007116 switch (varp->v_type)
7117 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007119 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007120 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007121 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007122 vim_free(varp->vval.v_string);
7123 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007124 case VAR_PARTIAL:
7125 partial_unref(varp->vval.v_partial);
7126 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007127 case VAR_BLOB:
7128 blob_unref(varp->vval.v_blob);
7129 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007130 case VAR_LIST:
7131 list_unref(varp->vval.v_list);
7132 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007133 case VAR_DICT:
7134 dict_unref(varp->vval.v_dict);
7135 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007136 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007138 job_unref(varp->vval.v_job);
7139 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007140#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007141 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007143 channel_unref(varp->vval.v_channel);
7144 break;
7145#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007146 case VAR_NUMBER:
7147 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007148 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007149 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007150 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 vim_free(varp);
7153 }
7154}
7155
7156/*
7157 * Free the memory for a variable value and set the value to NULL or 0.
7158 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007160clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 if (varp != NULL)
7163 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007164 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007167 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007168 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007169 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007170 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007171 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007172 case VAR_PARTIAL:
7173 partial_unref(varp->vval.v_partial);
7174 varp->vval.v_partial = NULL;
7175 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007176 case VAR_BLOB:
7177 blob_unref(varp->vval.v_blob);
7178 varp->vval.v_blob = NULL;
7179 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 case VAR_LIST:
7181 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007183 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 case VAR_DICT:
7185 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007189 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007190 varp->vval.v_number = 0;
7191 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007192 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007193#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007194 varp->vval.v_float = 0.0;
7195 break;
7196#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007197 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007198#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007199 job_unref(varp->vval.v_job);
7200 varp->vval.v_job = NULL;
7201#endif
7202 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007203 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007204#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007205 channel_unref(varp->vval.v_channel);
7206 varp->vval.v_channel = NULL;
7207#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007208 case VAR_UNKNOWN:
7209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007211 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213}
7214
7215/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007216 * Set the value of a variable to NULL without freeing items.
7217 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007220{
7221 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223}
7224
7225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 * Get the number value of a variable.
7227 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007228 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007229 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007230 * caller of incompatible types: it sets *denote to TRUE if "denote"
7231 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007233 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007234tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007236 int error = FALSE;
7237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007238 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007239}
7240
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007241 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007242tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007243{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007244 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007246 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007249 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007250 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007251#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007252 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007253 break;
7254#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007255 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007256 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007257 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007258 break;
7259 case VAR_STRING:
7260 if (varp->vval.v_string != NULL)
7261 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007262 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007263 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007264 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007265 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007266 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007267 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007268 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007269 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007270 case VAR_SPECIAL:
7271 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7272 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007273 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007274#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007275 EMSG(_("E910: Using a Job as a Number"));
7276 break;
7277#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007278 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007280 EMSG(_("E913: Using a Channel as a Number"));
7281 break;
7282#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007283 case VAR_BLOB:
7284 EMSG(_("E974: Using a Blob as a Number"));
7285 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007286 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007287 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007290 if (denote == NULL) /* useful for values that must be unsigned */
7291 n = -1;
7292 else
7293 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007294 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295}
7296
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007297#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007300{
7301 switch (varp->v_type)
7302 {
7303 case VAR_NUMBER:
7304 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007305 case VAR_FLOAT:
7306 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007307 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007308 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007309 EMSG(_("E891: Using a Funcref as a Float"));
7310 break;
7311 case VAR_STRING:
7312 EMSG(_("E892: Using a String as a Float"));
7313 break;
7314 case VAR_LIST:
7315 EMSG(_("E893: Using a List as a Float"));
7316 break;
7317 case VAR_DICT:
7318 EMSG(_("E894: Using a Dictionary as a Float"));
7319 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007320 case VAR_SPECIAL:
7321 EMSG(_("E907: Using a special value as a Float"));
7322 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007323 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007324# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007325 EMSG(_("E911: Using a Job as a Float"));
7326 break;
7327# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007328 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007329# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007330 EMSG(_("E914: Using a Channel as a Float"));
7331 break;
7332# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007333 case VAR_BLOB:
7334 EMSG(_("E975: Using a Blob as a Float"));
7335 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007336 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007337 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007338 break;
7339 }
7340 return 0;
7341}
7342#endif
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 * Get the string value of a variable.
7346 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007347 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7348 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 * If the String variable has never been set, return an empty string.
7350 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007352 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007354 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007355tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 static char_u mybuf[NUMBUFLEN];
7358
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360}
7361
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007362 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007363tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007366
7367 return res != NULL ? res : (char_u *)"";
7368}
7369
Bram Moolenaar7d647822014-04-05 21:28:56 +02007370/*
7371 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7372 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007373 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007374tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375{
7376 static char_u mybuf[NUMBUFLEN];
7377
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007378 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007379}
7380
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007381 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007382tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007383{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007384 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007387 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007388 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 return buf;
7390 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007391 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007392 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393 break;
7394 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007395 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 break;
7397 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007398 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007400 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007401#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007402 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007403 break;
7404#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 case VAR_STRING:
7406 if (varp->vval.v_string != NULL)
7407 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007408 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007409 case VAR_SPECIAL:
7410 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7411 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007412 case VAR_BLOB:
7413 EMSG(_("E976: using Blob as a String"));
7414 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007415 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007416#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007417 {
7418 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007419 char *status;
7420
7421 if (job == NULL)
7422 return (char_u *)"no process";
7423 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007424 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007425 : "run";
7426# ifdef UNIX
7427 vim_snprintf((char *)buf, NUMBUFLEN,
7428 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007429# elif defined(WIN32)
7430 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007431 "process %ld %s",
7432 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007433 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007434# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007435 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007436 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7437# endif
7438 return buf;
7439 }
7440#endif
7441 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007442 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007443#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007444 {
7445 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007446 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007447
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007448 if (channel == NULL)
7449 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7450 else
7451 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007452 "channel %d %s", channel->ch_id, status);
7453 return buf;
7454 }
7455#endif
7456 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007457 case VAR_UNKNOWN:
7458 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007461 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462}
7463
7464/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007465 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7466 * string() on Dict, List, etc.
7467 */
7468 char_u *
7469tv_stringify(typval_T *varp, char_u *buf)
7470{
7471 if (varp->v_type == VAR_LIST
7472 || varp->v_type == VAR_DICT
7473 || varp->v_type == VAR_FUNC
7474 || varp->v_type == VAR_PARTIAL
7475 || varp->v_type == VAR_FLOAT)
7476 {
7477 typval_T tmp;
7478
7479 f_string(varp, &tmp);
7480 tv_get_string_buf(&tmp, buf);
7481 clear_tv(varp);
7482 *varp = tmp;
7483 return tmp.vval.v_string;
7484 }
7485 return tv_get_string_buf(varp, buf);
7486}
7487
7488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 * Find variable "name" in the list of variables.
7490 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007491 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007492 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007495 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007496find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007500 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaara7043832005-01-21 11:56:39 +00007502 ht = find_var_ht(name, &varname);
7503 if (htp != NULL)
7504 *htp = ht;
7505 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007507 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7508 if (ret != NULL)
7509 return ret;
7510
7511 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007512 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
7515/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007516 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007517 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007520find_var_in_ht(
7521 hashtab_T *ht,
7522 int htname,
7523 char_u *varname,
7524 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007525{
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 hashitem_T *hi;
7527
7528 if (*varname == NUL)
7529 {
7530 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007531 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007533 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 case 'g': return &globvars_var;
7535 case 'v': return &vimvars_var;
7536 case 'b': return &curbuf->b_bufvar;
7537 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007538 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007539 case 'l': return get_funccal_local_var();
7540 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 }
7542 return NULL;
7543 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007544
7545 hi = hash_find(ht, varname);
7546 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007547 {
7548 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007549 * worked find the variable again. Don't auto-load a script if it was
7550 * loaded already, otherwise it would be loaded every time when
7551 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007552 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007553 {
7554 /* Note: script_autoload() may make "hi" invalid. It must either
7555 * be obtained again or not used. */
7556 if (!script_autoload(varname, FALSE) || aborting())
7557 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007558 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007559 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007560 if (HASHITEM_EMPTY(hi))
7561 return NULL;
7562 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007564}
7565
7566/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007568 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007569 * Set "varname" to the start of name without ':'.
7570 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007571 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007572find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007574 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007575 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007576
Bram Moolenaar73627d02015-08-11 15:46:09 +02007577 if (name[0] == NUL)
7578 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 if (name[1] != ':')
7580 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007581 /* The name must not start with a colon or #. */
7582 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 return NULL;
7584 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007585
7586 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007587 hi = hash_find(&compat_hashtab, name);
7588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007589 return &compat_hashtab;
7590
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007591 ht = get_funccal_local_ht();
7592 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007594 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007597 if (*name == 'g') /* global variable */
7598 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007599 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7600 */
7601 if (vim_strchr(name + 2, ':') != NULL
7602 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007603 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007605 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007607 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007608 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007609 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 if (*name == 'v') /* v: variable */
7611 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007612 if (*name == 'a') /* a: function argument */
7613 return get_funccal_args_ht();
7614 if (*name == 'l') /* l: local function variable */
7615 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007617 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7618 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 return NULL;
7620}
7621
7622/*
7623 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007624 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 * Returns NULL when it doesn't exist.
7626 */
7627 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007628get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007632 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (v == NULL)
7634 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636}
7637
7638/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 * sourcing this script and when executing functions defined in the script.
7641 */
7642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
Bram Moolenaara7043832005-01-21 11:56:39 +00007645 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 hashtab_T *ht;
7647 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7650 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007651 /* Re-allocating ga_data means that an ht_array pointing to
7652 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007653 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007654 for (i = 1; i <= ga_scripts.ga_len; ++i)
7655 {
7656 ht = &SCRIPT_VARS(i);
7657 if (ht->ht_mask == HT_INIT_SIZE - 1)
7658 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007659 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007661 }
7662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 while (ga_scripts.ga_len < id)
7664 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007665 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007666 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007667 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670 }
7671}
7672
7673/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7675 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 */
7677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007681 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007682 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007683 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007684 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dict_var->di_tv.vval.v_dict = dict;
7686 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007687 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7689 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
7692/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007693 * Unreference a dictionary initialized by init_var_dict().
7694 */
7695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007697{
7698 /* Now the dict needs to be freed if no one else is using it, go back to
7699 * normal reference counting. */
7700 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7701 dict_unref(dict);
7702}
7703
7704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 * Frees all allocated variables and the value they contain.
7707 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 */
7709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007710vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007711{
7712 vars_clear_ext(ht, TRUE);
7713}
7714
7715/*
7716 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7717 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007719vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaara7043832005-01-21 11:56:39 +00007721 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 hashitem_T *hi;
7723 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007726 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007727 for (hi = ht->ht_array; todo > 0; ++hi)
7728 {
7729 if (!HASHITEM_EMPTY(hi))
7730 {
7731 --todo;
7732
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007734 * ht_array might change then. hash_clear() takes care of it
7735 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 v = HI2DI(hi);
7737 if (free_val)
7738 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007739 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007741 }
7742 }
7743 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007744 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745}
7746
Bram Moolenaara7043832005-01-21 11:56:39 +00007747/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 * Delete a variable from hashtab "ht" at item "hi".
7749 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007752delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007755
7756 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 clear_tv(&di->di_tv);
7758 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759}
7760
7761/*
7762 * List the value of one internal variable.
7763 */
7764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007765list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 char_u *tofree;
7768 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007769 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007771 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007773 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778list_one_var_a(
7779 char_u *prefix,
7780 char_u *name,
7781 int type,
7782 char_u *string,
7783 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
Bram Moolenaar31859182007-08-14 20:41:13 +00007785 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7786 msg_start();
7787 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (name != NULL) /* "a:" vars don't have a name stored */
7789 msg_puts(name);
7790 msg_putchar(' ');
7791 msg_advance(22);
7792 if (type == VAR_NUMBER)
7793 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007794 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 msg_putchar('*');
7796 else if (type == VAR_LIST)
7797 {
7798 msg_putchar('[');
7799 if (*string == '[')
7800 ++string;
7801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802 else if (type == VAR_DICT)
7803 {
7804 msg_putchar('{');
7805 if (*string == '{')
7806 ++string;
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 else
7809 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007813 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007815 if (*first)
7816 {
7817 msg_clr_eos();
7818 *first = FALSE;
7819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820}
7821
7822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 * If the variable already exists, the value is updated.
7825 * Otherwise the variable is created.
7826 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828set_var(
7829 char_u *name,
7830 typval_T *tv,
7831 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007835 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007837 ht = find_var_ht(name, &varname);
7838 if (ht == NULL || *varname == NUL)
7839 {
7840 EMSG2(_(e_illvar), name);
7841 return;
7842 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007843 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007844
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007845 /* Search in parent scope which is possible to reference from lambda */
7846 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007847 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007848
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007849 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7850 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007851 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007856 if (var_check_ro(v->di_flags, name, FALSE)
7857 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007859
7860 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007861 * Handle setting internal v: variables separately where needed to
7862 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 */
7864 if (ht == &vimvarht)
7865 {
7866 if (v->di_tv.v_type == VAR_STRING)
7867 {
7868 vim_free(v->di_tv.vval.v_string);
7869 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007870 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 else
7872 {
7873 /* Take over the string to avoid an extra alloc/free. */
7874 v->di_tv.vval.v_string = tv->vval.v_string;
7875 tv->vval.v_string = NULL;
7876 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007877 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007879 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007881 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882 if (STRCMP(varname, "searchforward") == 0)
7883 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007884#ifdef FEAT_SEARCH_EXTRA
7885 else if (STRCMP(varname, "hlsearch") == 0)
7886 {
7887 no_hlsearch = !v->di_tv.vval.v_number;
7888 redraw_all_later(SOME_VALID);
7889 }
7890#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007891 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007893 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007894 {
Bram Moolenaar74ea88c2018-12-04 22:37:49 +01007895 EMSG2(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007896 return;
7897 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007898 }
7899
7900 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902 else /* add a new variable */
7903 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007904 /* Can't add "v:" variable. */
7905 if (ht == &vimvarht)
7906 {
7907 EMSG2(_(e_illvar), name);
7908 return;
7909 }
7910
Bram Moolenaar92124a32005-06-17 22:03:40 +00007911 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007912 if (!valid_varname(varname))
7913 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007914
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007915 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7916 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007917 if (v == NULL)
7918 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007922 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 return;
7924 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007925 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007927
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007928 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007930 else
7931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007932 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007933 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007939 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 * Also give an error message.
7941 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007942 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007943var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007944{
7945 if (flags & DI_FLAGS_RO)
7946 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007947 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 return TRUE;
7949 }
7950 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7951 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007952 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 return TRUE;
7954 }
7955 return FALSE;
7956}
7957
7958/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007959 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7960 * Also give an error message.
7961 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007963var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007964{
7965 if (flags & DI_FLAGS_FIX)
7966 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007967 EMSG2(_("E795: Cannot delete variable %s"),
7968 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007969 return TRUE;
7970 }
7971 return FALSE;
7972}
7973
7974/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007975 * Check if a funcref is assigned to a valid variable name.
7976 * Return TRUE and give an error if not.
7977 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007979var_check_func_name(
7980 char_u *name, /* points to start of variable name */
7981 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007982{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007983 /* Allow for w: b: s: and t:. */
7984 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007985 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7986 ? name[2] : name[0]))
7987 {
7988 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7989 name);
7990 return TRUE;
7991 }
7992 /* Don't allow hiding a function. When "v" is not NULL we might be
7993 * assigning another function to the same var, the type is checked
7994 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007995 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007996 {
7997 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7998 name);
7999 return TRUE;
8000 }
8001 return FALSE;
8002}
8003
8004/*
8005 * Check if a variable name is valid.
8006 * Return FALSE and give an error if not.
8007 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008009valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008010{
8011 char_u *p;
8012
8013 for (p = varname; *p != NUL; ++p)
8014 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8015 && *p != AUTOLOAD_CHAR)
8016 {
8017 EMSG2(_(e_illvar), varname);
8018 return FALSE;
8019 }
8020 return TRUE;
8021}
8022
8023/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008024 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008025 * Also give an error message, using "name" or _("name") when use_gettext is
8026 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008027 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008029tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008030{
8031 if (lock & VAR_LOCKED)
8032 {
8033 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008034 name == NULL ? (char_u *)_("Unknown")
8035 : use_gettext ? (char_u *)_(name)
8036 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008037 return TRUE;
8038 }
8039 if (lock & VAR_FIXED)
8040 {
8041 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008042 name == NULL ? (char_u *)_("Unknown")
8043 : use_gettext ? (char_u *)_(name)
8044 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008045 return TRUE;
8046 }
8047 return FALSE;
8048}
8049
8050/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008054 * It is OK for "from" and "to" to point to the same item. This is used to
8055 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008056 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008058copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008060 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008061 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008062 switch (from->v_type)
8063 {
8064 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008065 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008066 to->vval.v_number = from->vval.v_number;
8067 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008069#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070 to->vval.v_float = from->vval.v_float;
8071 break;
8072#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008073 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008075 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008076 if (to->vval.v_job != NULL)
8077 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008078 break;
8079#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008080 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008081#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008082 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008083 if (to->vval.v_channel != NULL)
8084 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008085 break;
8086#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 case VAR_STRING:
8088 case VAR_FUNC:
8089 if (from->vval.v_string == NULL)
8090 to->vval.v_string = NULL;
8091 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008092 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008093 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008094 if (from->v_type == VAR_FUNC)
8095 func_ref(to->vval.v_string);
8096 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008098 case VAR_PARTIAL:
8099 if (from->vval.v_partial == NULL)
8100 to->vval.v_partial = NULL;
8101 else
8102 {
8103 to->vval.v_partial = from->vval.v_partial;
8104 ++to->vval.v_partial->pt_refcount;
8105 }
8106 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008107 case VAR_BLOB:
8108 if (from->vval.v_blob == NULL)
8109 to->vval.v_blob = NULL;
8110 else
8111 {
8112 to->vval.v_blob = from->vval.v_blob;
8113 ++to->vval.v_blob->bv_refcount;
8114 }
8115 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 case VAR_LIST:
8117 if (from->vval.v_list == NULL)
8118 to->vval.v_list = NULL;
8119 else
8120 {
8121 to->vval.v_list = from->vval.v_list;
8122 ++to->vval.v_list->lv_refcount;
8123 }
8124 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008125 case VAR_DICT:
8126 if (from->vval.v_dict == NULL)
8127 to->vval.v_dict = NULL;
8128 else
8129 {
8130 to->vval.v_dict = from->vval.v_dict;
8131 ++to->vval.v_dict->dv_refcount;
8132 }
8133 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008134 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008135 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136 break;
8137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138}
8139
8140/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141 * Make a copy of an item.
8142 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008143 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8144 * reference to an already copied list/dict can be used.
8145 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008146 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008148item_copy(
8149 typval_T *from,
8150 typval_T *to,
8151 int deep,
8152 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153{
8154 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008155 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 {
8159 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008160 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008161 }
8162 ++recurse;
8163
8164 switch (from->v_type)
8165 {
8166 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 case VAR_STRING:
8169 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008170 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008171 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008172 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008173 case VAR_CHANNEL:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008174 case VAR_BLOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 copy_tv(from, to);
8176 break;
8177 case VAR_LIST:
8178 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008179 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008180 if (from->vval.v_list == NULL)
8181 to->vval.v_list = NULL;
8182 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8183 {
8184 /* use the copy made earlier */
8185 to->vval.v_list = from->vval.v_list->lv_copylist;
8186 ++to->vval.v_list->lv_refcount;
8187 }
8188 else
8189 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8190 if (to->vval.v_list == NULL)
8191 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 break;
8193 case VAR_DICT:
8194 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008195 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008196 if (from->vval.v_dict == NULL)
8197 to->vval.v_dict = NULL;
8198 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8199 {
8200 /* use the copy made earlier */
8201 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8202 ++to->vval.v_dict->dv_refcount;
8203 }
8204 else
8205 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8206 if (to->vval.v_dict == NULL)
8207 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008208 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008209 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008210 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008211 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008212 }
8213 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008214 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008215}
8216
8217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 * This function is used by f_input() and f_inputdialog() functions. The third
8219 * argument to f_input() specifies the type of completion to use at the
8220 * prompt. The third argument to f_inputdialog() specifies the value to return
8221 * when the user cancels the prompt.
8222 */
8223 void
8224get_user_input(
8225 typval_T *argvars,
8226 typval_T *rettv,
8227 int inputdialog,
8228 int secret)
8229{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008230 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231 char_u *p = NULL;
8232 int c;
8233 char_u buf[NUMBUFLEN];
8234 int cmd_silent_save = cmd_silent;
8235 char_u *defstr = (char_u *)"";
8236 int xp_type = EXPAND_NOTHING;
8237 char_u *xp_arg = NULL;
8238
8239 rettv->v_type = VAR_STRING;
8240 rettv->vval.v_string = NULL;
8241
8242#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008243 /* While starting up, there is no place to enter text. When running tests
8244 * with --not-a-term we assume feedkeys() will be used. */
8245 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 return;
8247#endif
8248
8249 cmd_silent = FALSE; /* Want to see the prompt. */
8250 if (prompt != NULL)
8251 {
8252 /* Only the part of the message after the last NL is considered as
8253 * prompt for the command line */
8254 p = vim_strrchr(prompt, '\n');
8255 if (p == NULL)
8256 p = prompt;
8257 else
8258 {
8259 ++p;
8260 c = *p;
8261 *p = NUL;
8262 msg_start();
8263 msg_clr_eos();
8264 msg_puts_attr(prompt, echo_attr);
8265 msg_didout = FALSE;
8266 msg_starthere();
8267 *p = c;
8268 }
8269 cmdline_row = msg_row;
8270
8271 if (argvars[1].v_type != VAR_UNKNOWN)
8272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008273 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 if (defstr != NULL)
8275 stuffReadbuffSpec(defstr);
8276
8277 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8278 {
8279 char_u *xp_name;
8280 int xp_namelen;
8281 long argt;
8282
8283 /* input() with a third argument: completion */
8284 rettv->vval.v_string = NULL;
8285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008286 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 if (xp_name == NULL)
8288 return;
8289
8290 xp_namelen = (int)STRLEN(xp_name);
8291
8292 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8293 &xp_arg) == FAIL)
8294 return;
8295 }
8296 }
8297
8298 if (defstr != NULL)
8299 {
8300 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008301
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 ex_normal_busy = 0;
8303 rettv->vval.v_string =
8304 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8305 xp_type, xp_arg);
8306 ex_normal_busy = save_ex_normal_busy;
8307 }
8308 if (inputdialog && rettv->vval.v_string == NULL
8309 && argvars[1].v_type != VAR_UNKNOWN
8310 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008311 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 &argvars[2], buf));
8313
8314 vim_free(xp_arg);
8315
8316 /* since the user typed this, no need to wait for return */
8317 need_wait_return = FALSE;
8318 msg_didout = FALSE;
8319 }
8320 cmd_silent = cmd_silent_save;
8321}
8322
8323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 * ":echo expr1 ..." print each argument separated with a space, add a
8325 * newline at the end.
8326 * ":echon expr1 ..." print each argument plain.
8327 */
8328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008329ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008333 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 char_u *p;
8335 int needclr = TRUE;
8336 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008338 int did_emsg_before = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
8340 if (eap->skip)
8341 ++emsg_skip;
8342 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8343 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008344 /* If eval1() causes an error message the text from the command may
8345 * still need to be cleared. E.g., "echo 22,44". */
8346 need_clr_eos = needclr;
8347
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 /*
8352 * Report the invalid expression unless the expression evaluation
8353 * has been cancelled due to an aborting error, an interrupt, or an
8354 * exception.
8355 */
Bram Moolenaar76a63452018-11-28 20:38:37 +01008356 if (!aborting() && did_emsg == did_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008358 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 break;
8360 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361 need_clr_eos = FALSE;
8362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 if (!eap->skip)
8364 {
8365 if (atstart)
8366 {
8367 atstart = FALSE;
8368 /* Call msg_start() after eval1(), evaluating the expression
8369 * may cause a message to appear. */
8370 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008371 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008372 /* Mark the saved text as finishing the line, so that what
8373 * follows is displayed on a new line when scrolling back
8374 * at the more prompt. */
8375 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
8379 else if (eap->cmdidx == CMD_echo)
8380 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008381 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008382 if (p != NULL)
8383 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008385 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008387 if (*p != TAB && needclr)
8388 {
8389 /* remove any text still there from the command */
8390 msg_clr_eos();
8391 needclr = FALSE;
8392 }
8393 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008396 {
8397#ifdef FEAT_MBYTE
8398 if (has_mbyte)
8399 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008400 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008401
8402 (void)msg_outtrans_len_attr(p, i, echo_attr);
8403 p += i - 1;
8404 }
8405 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008407 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008410 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 arg = skipwhite(arg);
8414 }
8415 eap->nextcmd = check_nextcmd(arg);
8416
8417 if (eap->skip)
8418 --emsg_skip;
8419 else
8420 {
8421 /* remove text that may still be there from the command */
8422 if (needclr)
8423 msg_clr_eos();
8424 if (eap->cmdidx == CMD_echo)
8425 msg_end();
8426 }
8427}
8428
8429/*
8430 * ":echohl {name}".
8431 */
8432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008433ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008435 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436}
8437
8438/*
8439 * ":execute expr1 ..." execute the result of an expression.
8440 * ":echomsg expr1 ..." Print a message
8441 * ":echoerr expr1 ..." Print an error
8442 * Each gets spaces around each argument and a newline at the end for
8443 * echo commands
8444 */
8445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 int ret = OK;
8451 char_u *p;
8452 garray_T ga;
8453 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008454 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455
8456 ga_init2(&ga, 1, 80);
8457
8458 if (eap->skip)
8459 ++emsg_skip;
8460 while (*arg != NUL && *arg != '|' && *arg != '\n')
8461 {
8462 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {
8465 /*
8466 * Report the invalid expression unless the expression evaluation
8467 * has been cancelled due to an aborting error, an interrupt, or an
8468 * exception.
8469 */
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008470 if (!aborting() && did_emsg == save_did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 EMSG2(_(e_invexpr2), p);
8472 ret = FAIL;
8473 break;
8474 }
8475
8476 if (!eap->skip)
8477 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008478 char_u buf[NUMBUFLEN];
8479
8480 if (eap->cmdidx == CMD_execute)
8481 p = tv_get_string_buf(&rettv, buf);
8482 else
8483 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 len = (int)STRLEN(p);
8485 if (ga_grow(&ga, len + 2) == FAIL)
8486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 ret = FAIL;
8489 break;
8490 }
8491 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 ga.ga_len += len;
8495 }
8496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 arg = skipwhite(arg);
8499 }
8500
8501 if (ret != FAIL && ga.ga_data != NULL)
8502 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008503 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8504 {
8505 /* Mark the already saved text as finishing the line, so that what
8506 * follows is displayed on a new line when scrolling back at the
8507 * more prompt. */
8508 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008509 }
8510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008514 out_flush();
8515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 else if (eap->cmdidx == CMD_echoerr)
8517 {
8518 /* We don't want to abort following commands, restore did_emsg. */
8519 save_did_emsg = did_emsg;
8520 EMSG((char_u *)ga.ga_data);
8521 if (!force_abort)
8522 did_emsg = save_did_emsg;
8523 }
8524 else if (eap->cmdidx == CMD_execute)
8525 do_cmdline((char_u *)ga.ga_data,
8526 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8527 }
8528
8529 ga_clear(&ga);
8530
8531 if (eap->skip)
8532 --emsg_skip;
8533
8534 eap->nextcmd = check_nextcmd(arg);
8535}
8536
8537/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 * Find window specified by "vp" in tabpage "tp".
8539 */
8540 win_T *
8541find_win_by_nr(
8542 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008543 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008546 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 if (nr < 0)
8549 return NULL;
8550 if (nr == 0)
8551 return curwin;
8552
Bram Moolenaar29323592016-07-24 22:04:11 +02008553 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8554 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 if (nr >= LOWEST_WIN_ID)
8556 {
8557 if (wp->w_id == nr)
8558 return wp;
8559 }
8560 else if (--nr <= 0)
8561 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008562 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (nr >= LOWEST_WIN_ID)
8564 return NULL;
8565 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566}
8567
8568/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008569 * Find a window: When using a Window ID in any tab page, when using a number
8570 * in the current tab page.
8571 */
8572 win_T *
8573find_win_by_nr_or_id(typval_T *vp)
8574{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008575 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008576
8577 if (nr >= LOWEST_WIN_ID)
8578 return win_id2wp(vp);
8579 return find_win_by_nr(vp, NULL);
8580}
8581
8582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 * Find window specified by "wvp" in tabpage "tvp".
8584 */
8585 win_T *
8586find_tabwin(
8587 typval_T *wvp, /* VAR_UNKNOWN for current window */
8588 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8589{
8590 win_T *wp = NULL;
8591 tabpage_T *tp = NULL;
8592 long n;
8593
8594 if (wvp->v_type != VAR_UNKNOWN)
8595 {
8596 if (tvp->v_type != VAR_UNKNOWN)
8597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008598 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 if (n >= 0)
8600 tp = find_tabpage(n);
8601 }
8602 else
8603 tp = curtab;
8604
8605 if (tp != NULL)
8606 wp = find_win_by_nr(wvp, tp);
8607 }
8608 else
8609 wp = curwin;
8610
8611 return wp;
8612}
8613
8614/*
8615 * getwinvar() and gettabwinvar()
8616 */
8617 void
8618getwinvar(
8619 typval_T *argvars,
8620 typval_T *rettv,
8621 int off) /* 1 for gettabwinvar() */
8622{
8623 win_T *win;
8624 char_u *varname;
8625 dictitem_T *v;
8626 tabpage_T *tp = NULL;
8627 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 win_T *oldcurwin;
8629 tabpage_T *oldtabpage;
8630 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008633 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 else
8635 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 ++emsg_off;
8639
8640 rettv->v_type = VAR_STRING;
8641 rettv->vval.v_string = NULL;
8642
8643 if (win != NULL && varname != NULL)
8644 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 /* Set curwin to be our win, temporarily. Also set the tabpage,
8646 * otherwise the window is not valid. Only do this when needed,
8647 * autocommands get blocked. */
8648 need_switch_win = !(tp == curtab && win == curwin);
8649 if (!need_switch_win
8650 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008652 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008654 if (varname[1] == NUL)
8655 {
8656 /* get all window-local options in a dict */
8657 dict_T *opts = get_winbuf_options(FALSE);
8658
8659 if (opts != NULL)
8660 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008661 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008662 done = TRUE;
8663 }
8664 }
8665 else if (get_option_tv(&varname, rettv, 1) == OK)
8666 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 done = TRUE;
8668 }
8669 else
8670 {
8671 /* Look up the variable. */
8672 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8673 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8674 varname, FALSE);
8675 if (v != NULL)
8676 {
8677 copy_tv(&v->di_tv, rettv);
8678 done = TRUE;
8679 }
8680 }
8681 }
8682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 if (need_switch_win)
8684 /* restore previous notion of curwin */
8685 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 }
8687
8688 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8689 /* use the default return value */
8690 copy_tv(&argvars[off + 2], rettv);
8691
8692 --emsg_off;
8693}
8694
8695/*
8696 * "setwinvar()" and "settabwinvar()" functions
8697 */
8698 void
8699setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8700{
8701 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 win_T *save_curwin;
8703 tabpage_T *save_curtab;
8704 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 char_u *varname, *winvarname;
8706 typval_T *varp;
8707 char_u nbuf[NUMBUFLEN];
8708 tabpage_T *tp = NULL;
8709
8710 if (check_restricted() || check_secure())
8711 return;
8712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008714 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715 else
8716 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 varp = &argvars[off + 2];
8720
8721 if (win != NULL && varname != NULL && varp != NULL)
8722 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 need_switch_win = !(tp == curtab && win == curwin);
8724 if (!need_switch_win
8725 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 {
8727 if (*varname == '&')
8728 {
8729 long numval;
8730 char_u *strval;
8731 int error = FALSE;
8732
8733 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008734 numval = (long)tv_get_number_chk(varp, &error);
8735 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 if (!error && strval != NULL)
8737 set_option_value(varname, numval, strval, OPT_LOCAL);
8738 }
8739 else
8740 {
8741 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8742 if (winvarname != NULL)
8743 {
8744 STRCPY(winvarname, "w:");
8745 STRCPY(winvarname + 2, varname);
8746 set_var(winvarname, varp, TRUE);
8747 vim_free(winvarname);
8748 }
8749 }
8750 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 if (need_switch_win)
8752 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 }
8754}
8755
8756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8758 * "arg" points to the "&" or '+' when called, to "option" when returning.
8759 * Returns NULL when no option name found. Otherwise pointer to the char
8760 * after the option name.
8761 */
8762 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008763find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 char_u *p = *arg;
8766
8767 ++p;
8768 if (*p == 'g' && p[1] == ':')
8769 {
8770 *opt_flags = OPT_GLOBAL;
8771 p += 2;
8772 }
8773 else if (*p == 'l' && p[1] == ':')
8774 {
8775 *opt_flags = OPT_LOCAL;
8776 p += 2;
8777 }
8778 else
8779 *opt_flags = 0;
8780
8781 if (!ASCII_ISALPHA(*p))
8782 return NULL;
8783 *arg = p;
8784
8785 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8786 p += 4; /* termcap option */
8787 else
8788 while (ASCII_ISALPHA(*p))
8789 ++p;
8790 return p;
8791}
8792
8793/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008794 * Return the autoload script name for a function or variable name.
8795 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008797 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008798autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008799{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008800 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008801 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008802
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008803 /* Get the script file name: replace '#' with '/', append ".vim". */
8804 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8805 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008806 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008807 STRCPY(scriptname, "autoload/");
8808 STRCAT(scriptname, name);
8809 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8810 STRCAT(scriptname, ".vim");
8811 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8812 *p = '/';
8813 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008814}
8815
8816/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008817 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008818 * Return TRUE if a package was loaded.
8819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008821script_autoload(
8822 char_u *name,
8823 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008824{
8825 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008826 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008827 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008828 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008829
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008830 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008831 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008832 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833 return FALSE;
8834
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008835 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008836
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008837 /* Find the name in the list of previously loaded package names. Skip
8838 * "autoload/", it's always the same. */
8839 for (i = 0; i < ga_loaded.ga_len; ++i)
8840 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8841 break;
8842 if (!reload && i < ga_loaded.ga_len)
8843 ret = FALSE; /* was loaded already */
8844 else
8845 {
8846 /* Remember the name if it wasn't loaded already. */
8847 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8848 {
8849 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8850 tofree = NULL;
8851 }
8852
8853 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008854 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008855 ret = TRUE;
8856 }
8857
8858 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008859 return ret;
8860}
8861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8863typedef enum
8864{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008865 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8866 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8867 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868} var_flavour_T;
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008871var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 char_u *p = varname;
8874
8875 if (ASCII_ISUPPER(*p))
8876 {
8877 while (*(++p))
8878 if (ASCII_ISLOWER(*p))
8879 return VAR_FLAVOUR_SESSION;
8880 return VAR_FLAVOUR_VIMINFO;
8881 }
8882 else
8883 return VAR_FLAVOUR_DEFAULT;
8884}
8885#endif
8886
8887#if defined(FEAT_VIMINFO) || defined(PROTO)
8888/*
8889 * Restore global vars that start with a capital from the viminfo file
8890 */
8891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
8894 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008895 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008897 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
8899 if (!writing && (find_viminfo_parameter('!') != NULL))
8900 {
8901 tab = vim_strchr(virp->vir_line + 1, '\t');
8902 if (tab != NULL)
8903 {
8904 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008905 switch (*tab)
8906 {
8907 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008908#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008909 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008910#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008911 case 'D': type = VAR_DICT; break;
8912 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008913 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008914 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917 tab = vim_strchr(tab, '\t');
8918 if (tab != NULL)
8919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008920 tv.v_type = type;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008921 if (type == VAR_STRING || type == VAR_DICT ||
8922 type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008923 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008925#ifdef FEAT_FLOAT
8926 else if (type == VAR_FLOAT)
8927 (void)string2float(tab + 1, &tv.vval.v_float);
8928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008930 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008931 if (type == VAR_DICT || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008932 {
8933 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8934
8935 if (etv == NULL)
8936 /* Failed to parse back the dict or list, use it as a
8937 * string. */
8938 tv.v_type = VAR_STRING;
8939 else
8940 {
8941 vim_free(tv.vval.v_string);
8942 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008943 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008944 }
8945 }
8946
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008947 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008948 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008949 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008950 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008951
8952 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008953 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008954 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8955 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008956 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 }
8959 }
8960
8961 return viminfo_readline(virp);
8962}
8963
8964/*
8965 * Write global vars that start with a capital to the viminfo file
8966 */
8967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 hashitem_T *hi;
8971 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008972 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008973 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008974 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008976 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978 if (find_viminfo_parameter('!') == NULL)
8979 return;
8980
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008981 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008982
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008983 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008986 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008988 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 this_var = HI2DI(hi);
8990 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008993 {
8994 case VAR_STRING: s = "STR"; break;
8995 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008996 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008997 case VAR_DICT: s = "DIC"; break;
8998 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008999 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009000 case VAR_SPECIAL: s = "XPL"; break;
9001
9002 case VAR_UNKNOWN:
9003 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009004 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009005 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009006 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009007 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009008 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009010 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009011 if (p != NULL)
9012 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009013 vim_free(tofree);
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 }
9016 }
9017}
9018#endif
9019
9020#if defined(FEAT_SESSION) || defined(PROTO)
9021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009022store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
Bram Moolenaar33570922005-01-25 22:26:29 +00009024 hashitem_T *hi;
9025 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009026 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 char_u *p, *t;
9028
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009029 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009032 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009034 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009035 this_var = HI2DI(hi);
9036 if ((this_var->di_tv.v_type == VAR_NUMBER
9037 || this_var->di_tv.v_type == VAR_STRING)
9038 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009039 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009040 /* Escape special characters with a backslash. Turn a LF and
9041 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009042 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009043 (char_u *)"\\\"\n\r");
9044 if (p == NULL) /* out of memory */
9045 break;
9046 for (t = p; *t != NUL; ++t)
9047 if (*t == '\n')
9048 *t = 'n';
9049 else if (*t == '\r')
9050 *t = 'r';
9051 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 this_var->di_key,
9053 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9054 : ' ',
9055 p,
9056 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9057 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009058 || put_eol(fd) == FAIL)
9059 {
9060 vim_free(p);
9061 return FAIL;
9062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 vim_free(p);
9064 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009065#ifdef FEAT_FLOAT
9066 else if (this_var->di_tv.v_type == VAR_FLOAT
9067 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9068 {
9069 float_T f = this_var->di_tv.vval.v_float;
9070 int sign = ' ';
9071
9072 if (f < 0)
9073 {
9074 f = -f;
9075 sign = '-';
9076 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009077 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009078 this_var->di_key, sign, f) < 0)
9079 || put_eol(fd) == FAIL)
9080 return FAIL;
9081 }
9082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
9084 }
9085 return OK;
9086}
9087#endif
9088
Bram Moolenaar661b1822005-07-28 22:36:45 +00009089/*
9090 * Display script name where an item was last set.
9091 * Should only be invoked when 'verbose' is non-zero.
9092 */
9093 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009094last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009095{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009096 char_u *p;
9097
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009098 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009099 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009100 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009101 if (p != NULL)
9102 {
9103 verbose_enter();
9104 MSG_PUTS(_("\n\tLast set from "));
9105 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009106 if (script_ctx.sc_lnum > 0)
9107 {
9108 MSG_PUTS(_(" line "));
9109 msg_outnum((long)script_ctx.sc_lnum);
9110 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009111 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009112 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009113 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009114 }
9115}
9116
Bram Moolenaar53744302015-07-17 17:38:22 +02009117/* reset v:option_new, v:option_old and v:option_type */
9118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009119reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009120{
9121 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9122 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9123 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9124}
9125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126/*
9127 * Prepare "gap" for an assert error and add the sourcing position.
9128 */
9129 void
9130prepare_assert_error(garray_T *gap)
9131{
9132 char buf[NUMBUFLEN];
9133
9134 ga_init2(gap, 1, 100);
9135 if (sourcing_name != NULL)
9136 {
9137 ga_concat(gap, sourcing_name);
9138 if (sourcing_lnum > 0)
9139 ga_concat(gap, (char_u *)" ");
9140 }
9141 if (sourcing_lnum > 0)
9142 {
9143 sprintf(buf, "line %ld", (long)sourcing_lnum);
9144 ga_concat(gap, (char_u *)buf);
9145 }
9146 if (sourcing_name != NULL || sourcing_lnum > 0)
9147 ga_concat(gap, (char_u *)": ");
9148}
9149
9150/*
9151 * Add an assert error to v:errors.
9152 */
9153 void
9154assert_error(garray_T *gap)
9155{
9156 struct vimvar *vp = &vimvars[VV_ERRORS];
9157
9158 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9159 /* Make sure v:errors is a list. */
9160 set_vim_var_list(VV_ERRORS, list_alloc());
9161 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9162}
9163
Bram Moolenaar65a54642018-04-28 16:56:53 +02009164 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165assert_equal_common(typval_T *argvars, assert_type_T atype)
9166{
9167 garray_T ga;
9168
9169 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9170 != (atype == ASSERT_EQUAL))
9171 {
9172 prepare_assert_error(&ga);
9173 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9174 atype);
9175 assert_error(&ga);
9176 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009177 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009179 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180}
9181
Bram Moolenaar65a54642018-04-28 16:56:53 +02009182 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009183assert_equalfile(typval_T *argvars)
9184{
9185 char_u buf1[NUMBUFLEN];
9186 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009187 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9188 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009189 garray_T ga;
9190 FILE *fd1;
9191 FILE *fd2;
9192
9193 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009194 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009195
9196 IObuff[0] = NUL;
9197 fd1 = mch_fopen((char *)fname1, READBIN);
9198 if (fd1 == NULL)
9199 {
9200 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9201 }
9202 else
9203 {
9204 fd2 = mch_fopen((char *)fname2, READBIN);
9205 if (fd2 == NULL)
9206 {
9207 fclose(fd1);
9208 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9209 }
9210 else
9211 {
9212 int c1, c2;
9213 long count = 0;
9214
9215 for (;;)
9216 {
9217 c1 = fgetc(fd1);
9218 c2 = fgetc(fd2);
9219 if (c1 == EOF)
9220 {
9221 if (c2 != EOF)
9222 STRCPY(IObuff, "first file is shorter");
9223 break;
9224 }
9225 else if (c2 == EOF)
9226 {
9227 STRCPY(IObuff, "second file is shorter");
9228 break;
9229 }
9230 else if (c1 != c2)
9231 {
9232 vim_snprintf((char *)IObuff, IOSIZE,
9233 "difference at byte %ld", count);
9234 break;
9235 }
9236 ++count;
9237 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009238 fclose(fd1);
9239 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009240 }
9241 }
9242 if (IObuff[0] != NUL)
9243 {
9244 prepare_assert_error(&ga);
9245 ga_concat(&ga, IObuff);
9246 assert_error(&ga);
9247 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009248 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009249 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009250 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009251}
9252
Bram Moolenaar65a54642018-04-28 16:56:53 +02009253 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009254assert_match_common(typval_T *argvars, assert_type_T atype)
9255{
9256 garray_T ga;
9257 char_u buf1[NUMBUFLEN];
9258 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009259 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9260 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261
9262 if (pat == NULL || text == NULL)
9263 EMSG(_(e_invarg));
9264 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9265 {
9266 prepare_assert_error(&ga);
9267 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9268 atype);
9269 assert_error(&ga);
9270 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009271 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009273 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274}
9275
Bram Moolenaar65a54642018-04-28 16:56:53 +02009276 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009277assert_inrange(typval_T *argvars)
9278{
9279 garray_T ga;
9280 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009281 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9282 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9283 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009284 char_u *tofree;
9285 char msg[200];
9286 char_u numbuf[NUMBUFLEN];
9287
9288 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009289 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009290 if (actual < lower || actual > upper)
9291 {
9292 prepare_assert_error(&ga);
9293 if (argvars[3].v_type != VAR_UNKNOWN)
9294 {
9295 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9296 vim_free(tofree);
9297 }
9298 else
9299 {
9300 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9301 (long)lower, (long)upper, (long)actual);
9302 ga_concat(&ga, (char_u *)msg);
9303 }
9304 assert_error(&ga);
9305 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009306 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009307 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009308 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009309}
9310
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311/*
9312 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009313 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009315 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316assert_bool(typval_T *argvars, int isTrue)
9317{
9318 int error = FALSE;
9319 garray_T ga;
9320
9321 if (argvars[0].v_type == VAR_SPECIAL
9322 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009323 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009325 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326 || error)
9327 {
9328 prepare_assert_error(&ga);
9329 fill_assert_error(&ga, &argvars[1],
9330 (char_u *)(isTrue ? "True" : "False"),
9331 NULL, &argvars[0], ASSERT_OTHER);
9332 assert_error(&ga);
9333 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009334 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009336 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337}
9338
Bram Moolenaar65a54642018-04-28 16:56:53 +02009339 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009340assert_report(typval_T *argvars)
9341{
9342 garray_T ga;
9343
9344 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009345 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009346 assert_error(&ga);
9347 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009348 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009349}
9350
Bram Moolenaar65a54642018-04-28 16:56:53 +02009351 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352assert_exception(typval_T *argvars)
9353{
9354 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009355 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009356
9357 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9358 {
9359 prepare_assert_error(&ga);
9360 ga_concat(&ga, (char_u *)"v:exception is not set");
9361 assert_error(&ga);
9362 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009363 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 }
9365 else if (error != NULL
9366 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9367 {
9368 prepare_assert_error(&ga);
9369 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9370 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9371 assert_error(&ga);
9372 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009373 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009375 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376}
9377
Bram Moolenaar65a54642018-04-28 16:56:53 +02009378 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009379assert_beeps(typval_T *argvars)
9380{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009381 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009382 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009383 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009384
9385 called_vim_beep = FALSE;
9386 suppress_errthrow = TRUE;
9387 emsg_silent = FALSE;
9388 do_cmdline_cmd(cmd);
9389 if (!called_vim_beep)
9390 {
9391 prepare_assert_error(&ga);
9392 ga_concat(&ga, (char_u *)"command did not beep: ");
9393 ga_concat(&ga, cmd);
9394 assert_error(&ga);
9395 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009396 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009397 }
9398
9399 suppress_errthrow = FALSE;
9400 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009401 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009402}
9403
Bram Moolenaar65a54642018-04-28 16:56:53 +02009404 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009405assert_fails(typval_T *argvars)
9406{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009407 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009409 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009410 char_u numbuf[NUMBUFLEN];
9411 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412
9413 called_emsg = FALSE;
9414 suppress_errthrow = TRUE;
9415 emsg_silent = TRUE;
9416 do_cmdline_cmd(cmd);
9417 if (!called_emsg)
9418 {
9419 prepare_assert_error(&ga);
9420 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009421 if (argvars[1].v_type != VAR_UNKNOWN
9422 && argvars[2].v_type != VAR_UNKNOWN)
9423 {
9424 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9425 vim_free(tofree);
9426 }
9427 else
9428 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009429 assert_error(&ga);
9430 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009431 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432 }
9433 else if (argvars[1].v_type != VAR_UNKNOWN)
9434 {
9435 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009436 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437
9438 if (error == NULL
9439 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9440 {
9441 prepare_assert_error(&ga);
9442 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9443 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9444 assert_error(&ga);
9445 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009446 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 }
9448 }
9449
9450 called_emsg = FALSE;
9451 suppress_errthrow = FALSE;
9452 emsg_silent = FALSE;
9453 emsg_on_display = FALSE;
9454 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009455 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456}
9457
9458/*
9459 * Append "str" to "gap", escaping unprintable characters.
9460 * Changes NL to \n, CR to \r, etc.
9461 */
9462 static void
9463ga_concat_esc(garray_T *gap, char_u *str)
9464{
9465 char_u *p;
9466 char_u buf[NUMBUFLEN];
9467
9468 if (str == NULL)
9469 {
9470 ga_concat(gap, (char_u *)"NULL");
9471 return;
9472 }
9473
9474 for (p = str; *p != NUL; ++p)
9475 switch (*p)
9476 {
9477 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9478 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9479 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9480 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9481 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9482 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9483 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9484 default:
9485 if (*p < ' ')
9486 {
9487 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9488 ga_concat(gap, buf);
9489 }
9490 else
9491 ga_append(gap, *p);
9492 break;
9493 }
9494}
9495
9496/*
9497 * Fill "gap" with information about an assert error.
9498 */
9499 void
9500fill_assert_error(
9501 garray_T *gap,
9502 typval_T *opt_msg_tv,
9503 char_u *exp_str,
9504 typval_T *exp_tv,
9505 typval_T *got_tv,
9506 assert_type_T atype)
9507{
9508 char_u numbuf[NUMBUFLEN];
9509 char_u *tofree;
9510
9511 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9512 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009513 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9514 vim_free(tofree);
9515 ga_concat(gap, (char_u *)": ");
9516 }
9517
9518 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9519 ga_concat(gap, (char_u *)"Pattern ");
9520 else if (atype == ASSERT_NOTEQUAL)
9521 ga_concat(gap, (char_u *)"Expected not equal to ");
9522 else
9523 ga_concat(gap, (char_u *)"Expected ");
9524 if (exp_str == NULL)
9525 {
9526 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 vim_free(tofree);
9528 }
9529 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009530 ga_concat_esc(gap, exp_str);
9531 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009533 if (atype == ASSERT_MATCH)
9534 ga_concat(gap, (char_u *)" does not match ");
9535 else if (atype == ASSERT_NOTMATCH)
9536 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009537 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009538 ga_concat(gap, (char_u *)" but got ");
9539 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9540 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 }
9542}
9543
Bram Moolenaar31988702018-02-13 12:57:42 +01009544/*
9545 * Compare "typ1" and "typ2". Put the result in "typ1".
9546 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009547 int
9548typval_compare(
9549 typval_T *typ1, /* first operand */
9550 typval_T *typ2, /* second operand */
9551 exptype_T type, /* operator */
9552 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009553 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009554{
9555 int i;
9556 varnumber_T n1, n2;
9557 char_u *s1, *s2;
9558 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9559
Bram Moolenaar31988702018-02-13 12:57:42 +01009560 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009561 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009562 /* For "is" a different type always means FALSE, for "notis"
9563 * it means TRUE. */
9564 n1 = (type == TYPE_NEQUAL);
9565 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009566 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9567 {
9568 if (type_is)
9569 {
9570 n1 = (typ1->v_type == typ2->v_type
9571 && typ1->vval.v_blob == typ2->vval.v_blob);
9572 if (type == TYPE_NEQUAL)
9573 n1 = !n1;
9574 }
9575 else if (typ1->v_type != typ2->v_type
9576 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9577 {
9578 if (typ1->v_type != typ2->v_type)
9579 EMSG(_("E977: Can only compare Blob with Blob"));
9580 else
9581 EMSG(_(e_invalblob));
9582 clear_tv(typ1);
9583 return FAIL;
9584 }
9585 else
9586 {
9587 // Compare two Blobs for being equal or unequal.
9588 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9589 if (type == TYPE_NEQUAL)
9590 n1 = !n1;
9591 }
9592 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009593 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9594 {
9595 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009596 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009597 n1 = (typ1->v_type == typ2->v_type
9598 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009599 if (type == TYPE_NEQUAL)
9600 n1 = !n1;
9601 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009602 else if (typ1->v_type != typ2->v_type
9603 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009604 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009605 if (typ1->v_type != typ2->v_type)
9606 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009607 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009608 EMSG(_("E692: Invalid operation for List"));
9609 clear_tv(typ1);
9610 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009611 }
9612 else
9613 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009614 /* Compare two Lists for being equal or unequal. */
9615 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9616 ic, FALSE);
9617 if (type == TYPE_NEQUAL)
9618 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009619 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009620 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009621
9622 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9623 {
9624 if (type_is)
9625 {
9626 n1 = (typ1->v_type == typ2->v_type
9627 && typ1->vval.v_dict == typ2->vval.v_dict);
9628 if (type == TYPE_NEQUAL)
9629 n1 = !n1;
9630 }
9631 else if (typ1->v_type != typ2->v_type
9632 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9633 {
9634 if (typ1->v_type != typ2->v_type)
9635 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9636 else
9637 EMSG(_("E736: Invalid operation for Dictionary"));
9638 clear_tv(typ1);
9639 return FAIL;
9640 }
9641 else
9642 {
9643 /* Compare two Dictionaries for being equal or unequal. */
9644 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9645 ic, FALSE);
9646 if (type == TYPE_NEQUAL)
9647 n1 = !n1;
9648 }
9649 }
9650
9651 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9652 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9653 {
9654 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9655 {
9656 EMSG(_("E694: Invalid operation for Funcrefs"));
9657 clear_tv(typ1);
9658 return FAIL;
9659 }
9660 if ((typ1->v_type == VAR_PARTIAL
9661 && typ1->vval.v_partial == NULL)
9662 || (typ2->v_type == VAR_PARTIAL
9663 && typ2->vval.v_partial == NULL))
9664 /* when a partial is NULL assume not equal */
9665 n1 = FALSE;
9666 else if (type_is)
9667 {
9668 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9669 /* strings are considered the same if their value is
9670 * the same */
9671 n1 = tv_equal(typ1, typ2, ic, FALSE);
9672 else if (typ1->v_type == VAR_PARTIAL
9673 && typ2->v_type == VAR_PARTIAL)
9674 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9675 else
9676 n1 = FALSE;
9677 }
9678 else
9679 n1 = tv_equal(typ1, typ2, ic, FALSE);
9680 if (type == TYPE_NEQUAL)
9681 n1 = !n1;
9682 }
9683
9684#ifdef FEAT_FLOAT
9685 /*
9686 * If one of the two variables is a float, compare as a float.
9687 * When using "=~" or "!~", always compare as string.
9688 */
9689 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9690 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9691 {
9692 float_T f1, f2;
9693
9694 if (typ1->v_type == VAR_FLOAT)
9695 f1 = typ1->vval.v_float;
9696 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009697 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009698 if (typ2->v_type == VAR_FLOAT)
9699 f2 = typ2->vval.v_float;
9700 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009701 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009702 n1 = FALSE;
9703 switch (type)
9704 {
9705 case TYPE_EQUAL: n1 = (f1 == f2); break;
9706 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9707 case TYPE_GREATER: n1 = (f1 > f2); break;
9708 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9709 case TYPE_SMALLER: n1 = (f1 < f2); break;
9710 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9711 case TYPE_UNKNOWN:
9712 case TYPE_MATCH:
9713 case TYPE_NOMATCH: break; /* avoid gcc warning */
9714 }
9715 }
9716#endif
9717
9718 /*
9719 * If one of the two variables is a number, compare as a number.
9720 * When using "=~" or "!~", always compare as string.
9721 */
9722 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9723 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009725 n1 = tv_get_number(typ1);
9726 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009727 switch (type)
9728 {
9729 case TYPE_EQUAL: n1 = (n1 == n2); break;
9730 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9731 case TYPE_GREATER: n1 = (n1 > n2); break;
9732 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9733 case TYPE_SMALLER: n1 = (n1 < n2); break;
9734 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9735 case TYPE_UNKNOWN:
9736 case TYPE_MATCH:
9737 case TYPE_NOMATCH: break; /* avoid gcc warning */
9738 }
9739 }
9740 else
9741 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009742 s1 = tv_get_string_buf(typ1, buf1);
9743 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009744 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9745 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9746 else
9747 i = 0;
9748 n1 = FALSE;
9749 switch (type)
9750 {
9751 case TYPE_EQUAL: n1 = (i == 0); break;
9752 case TYPE_NEQUAL: n1 = (i != 0); break;
9753 case TYPE_GREATER: n1 = (i > 0); break;
9754 case TYPE_GEQUAL: n1 = (i >= 0); break;
9755 case TYPE_SMALLER: n1 = (i < 0); break;
9756 case TYPE_SEQUAL: n1 = (i <= 0); break;
9757
9758 case TYPE_MATCH:
9759 case TYPE_NOMATCH:
9760 n1 = pattern_match(s2, s1, ic);
9761 if (type == TYPE_NOMATCH)
9762 n1 = !n1;
9763 break;
9764
9765 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9766 }
9767 }
9768 clear_tv(typ1);
9769 typ1->v_type = VAR_NUMBER;
9770 typ1->vval.v_number = n1;
9771
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009772 return OK;
9773}
9774
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009775 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009776typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009777{
9778 char_u *tofree;
9779 char_u numbuf[NUMBUFLEN];
9780 char_u *ret = NULL;
9781
9782 if (arg == NULL)
9783 return vim_strsave((char_u *)"(does not exist)");
9784 ret = tv2string(arg, &tofree, numbuf, 0);
9785 /* Make a copy if we have a value but it's not in allocated memory. */
9786 if (ret != NULL && tofree == NULL)
9787 ret = vim_strsave(ret);
9788 return ret;
9789}
9790
9791 int
9792var_exists(char_u *var)
9793{
9794 char_u *name;
9795 char_u *tofree;
9796 typval_T tv;
9797 int len = 0;
9798 int n = FALSE;
9799
9800 /* get_name_len() takes care of expanding curly braces */
9801 name = var;
9802 len = get_name_len(&var, &tofree, TRUE, FALSE);
9803 if (len > 0)
9804 {
9805 if (tofree != NULL)
9806 name = tofree;
9807 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9808 if (n)
9809 {
9810 /* handle d.key, l[idx], f(expr) */
9811 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9812 if (n)
9813 clear_tv(&tv);
9814 }
9815 }
9816 if (*var != NUL)
9817 n = FALSE;
9818
9819 vim_free(tofree);
9820 return n;
9821}
9822
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823#endif /* FEAT_EVAL */
9824
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009826#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
9828#ifdef WIN3264
9829/*
9830 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832
9833/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009834 * Get the short path (8.3) for the filename in "fnamep".
9835 * Only works for a valid file name.
9836 * When the path gets longer "fnamep" is changed and the allocated buffer
9837 * is put in "bufp".
9838 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9839 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 */
9841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009844 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 char_u *newbuf;
9846
9847 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009848 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 if (l > len - 1)
9850 {
9851 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009852 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 newbuf = vim_strnsave(*fnamep, l);
9854 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009855 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
9857 vim_free(*bufp);
9858 *fnamep = *bufp = newbuf;
9859
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009860 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009861 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 }
9863
9864 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009865 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866}
9867
9868/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009869 * Get the short path (8.3) for the filename in "fname". The converted
9870 * path is returned in "bufp".
9871 *
9872 * Some of the directories specified in "fname" may not exist. This function
9873 * will shorten the existing directories at the beginning of the path and then
9874 * append the remaining non-existing path.
9875 *
9876 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009877 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009878 * bufp - Pointer to an allocated buffer for the filename.
9879 * fnamelen - Length of the filename pointed to by fname
9880 *
9881 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 */
9883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009884shortpath_for_invalid_fname(
9885 char_u **fname,
9886 char_u **bufp,
9887 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009889 char_u *short_fname, *save_fname, *pbuf_unused;
9890 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009892 int old_len, len;
9893 int new_len, sfx_len;
9894 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009897 old_len = *fnamelen;
9898 save_fname = vim_strnsave(*fname, old_len);
9899 pbuf_unused = NULL;
9900 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009902 endp = save_fname + old_len - 1; /* Find the end of the copy */
9903 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009905 /*
9906 * Try shortening the supplied path till it succeeds by removing one
9907 * directory at a time from the tail of the path.
9908 */
9909 len = 0;
9910 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009912 /* go back one path-separator */
9913 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9914 --endp;
9915 if (endp <= save_fname)
9916 break; /* processed the complete path */
9917
9918 /*
9919 * Replace the path separator with a NUL and try to shorten the
9920 * resulting path.
9921 */
9922 ch = *endp;
9923 *endp = 0;
9924 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009925 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009926 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9927 {
9928 retval = FAIL;
9929 goto theend;
9930 }
9931 *endp = ch; /* preserve the string */
9932
9933 if (len > 0)
9934 break; /* successfully shortened the path */
9935
9936 /* failed to shorten the path. Skip the path separator */
9937 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 }
9939
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009940 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009942 /*
9943 * Succeeded in shortening the path. Now concatenate the shortened
9944 * path with the remaining path at the tail.
9945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009947 /* Compute the length of the new path. */
9948 sfx_len = (int)(save_endp - endp) + 1;
9949 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009951 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009953 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009955 /* There is not enough space in the currently allocated string,
9956 * copy it to a buffer big enough. */
9957 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009959 {
9960 retval = FAIL;
9961 goto theend;
9962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 }
9964 else
9965 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009966 /* Transfer short_fname to the main buffer (it's big enough),
9967 * unless get_short_pathname() did its work in-place. */
9968 *fname = *bufp = save_fname;
9969 if (short_fname != save_fname)
9970 vim_strncpy(save_fname, short_fname, len);
9971 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009973
9974 /* concat the not-shortened part of the path */
9975 vim_strncpy(*fname + len, endp, sfx_len);
9976 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009978
9979theend:
9980 vim_free(pbuf_unused);
9981 vim_free(save_fname);
9982
9983 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984}
9985
9986/*
9987 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009988 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 */
9990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009991shortpath_for_partial(
9992 char_u **fnamep,
9993 char_u **bufp,
9994 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995{
9996 int sepcount, len, tflen;
9997 char_u *p;
9998 char_u *pbuf, *tfname;
9999 int hasTilde;
10000
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010001 /* Count up the path separators from the RHS.. so we know which part
10002 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010004 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 if (vim_ispathsep(*p))
10006 ++sepcount;
10007
10008 /* Need full path first (use expand_env() to remove a "~/") */
10009 hasTilde = (**fnamep == '~');
10010 if (hasTilde)
10011 pbuf = tfname = expand_env_save(*fnamep);
10012 else
10013 pbuf = tfname = FullName_save(*fnamep, FALSE);
10014
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010015 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010017 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10018 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019
10020 if (len == 0)
10021 {
10022 /* Don't have a valid filename, so shorten the rest of the
10023 * path if we can. This CAN give us invalid 8.3 filenames, but
10024 * there's not a lot of point in guessing what it might be.
10025 */
10026 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010027 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10028 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 }
10030
10031 /* Count the paths backward to find the beginning of the desired string. */
10032 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010033 {
10034#ifdef FEAT_MBYTE
10035 if (has_mbyte)
10036 p -= mb_head_off(tfname, p);
10037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 if (vim_ispathsep(*p))
10039 {
10040 if (sepcount == 0 || (hasTilde && sepcount == 1))
10041 break;
10042 else
10043 sepcount --;
10044 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (hasTilde)
10047 {
10048 --p;
10049 if (p >= tfname)
10050 *p = '~';
10051 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010052 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 else
10055 ++p;
10056
10057 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10058 vim_free(*bufp);
10059 *fnamelen = (int)STRLEN(p);
10060 *bufp = pbuf;
10061 *fnamep = p;
10062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010063 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
10065#endif /* WIN3264 */
10066
10067/*
10068 * Adjust a filename, according to a string of modifiers.
10069 * *fnamep must be NUL terminated when called. When returning, the length is
10070 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010071 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 * When there is an error, *fnamep is set to NULL.
10073 */
10074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010075modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010076 char_u *src, // string with modifiers
10077 int tilde_file, // "~" is a file name, not $HOME
10078 int *usedlen, // characters after src that are used
10079 char_u **fnamep, // file name so far
10080 char_u **bufp, // buffer for allocated file name or NULL
10081 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 int valid = 0;
10084 char_u *tail;
10085 char_u *s, *p, *pbuf;
10086 char_u dirname[MAXPATHL];
10087 int c;
10088 int has_fullname = 0;
10089#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010090 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 int has_shortname = 0;
10092#endif
10093
10094repeat:
10095 /* ":p" - full path/file_name */
10096 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10097 {
10098 has_fullname = 1;
10099
10100 valid |= VALID_PATH;
10101 *usedlen += 2;
10102
10103 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10104 if ((*fnamep)[0] == '~'
10105#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10106 && ((*fnamep)[1] == '/'
10107# ifdef BACKSLASH_IN_FILENAME
10108 || (*fnamep)[1] == '\\'
10109# endif
10110 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010112 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 )
10114 {
10115 *fnamep = expand_env_save(*fnamep);
10116 vim_free(*bufp); /* free any allocated file name */
10117 *bufp = *fnamep;
10118 if (*fnamep == NULL)
10119 return -1;
10120 }
10121
10122 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010123 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 {
10125 if (vim_ispathsep(*p)
10126 && p[1] == '.'
10127 && (p[2] == NUL
10128 || vim_ispathsep(p[2])
10129 || (p[2] == '.'
10130 && (p[3] == NUL || vim_ispathsep(p[3])))))
10131 break;
10132 }
10133
10134 /* FullName_save() is slow, don't use it when not needed. */
10135 if (*p != NUL || !vim_isAbsName(*fnamep))
10136 {
10137 *fnamep = FullName_save(*fnamep, *p != NUL);
10138 vim_free(*bufp); /* free any allocated file name */
10139 *bufp = *fnamep;
10140 if (*fnamep == NULL)
10141 return -1;
10142 }
10143
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010144#ifdef WIN3264
10145# if _WIN32_WINNT >= 0x0500
10146 if (vim_strchr(*fnamep, '~') != NULL)
10147 {
10148 /* Expand 8.3 filename to full path. Needed to make sure the same
10149 * file does not have two different names.
10150 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10151 p = alloc(_MAX_PATH + 1);
10152 if (p != NULL)
10153 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010154 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010155 {
10156 vim_free(*bufp);
10157 *bufp = *fnamep = p;
10158 }
10159 else
10160 vim_free(p);
10161 }
10162 }
10163# endif
10164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 /* Append a path separator to a directory. */
10166 if (mch_isdir(*fnamep))
10167 {
10168 /* Make room for one or two extra characters. */
10169 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10170 vim_free(*bufp); /* free any allocated file name */
10171 *bufp = *fnamep;
10172 if (*fnamep == NULL)
10173 return -1;
10174 add_pathsep(*fnamep);
10175 }
10176 }
10177
10178 /* ":." - path relative to the current directory */
10179 /* ":~" - path relative to the home directory */
10180 /* ":8" - shortname path - postponed till after */
10181 while (src[*usedlen] == ':'
10182 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10183 {
10184 *usedlen += 2;
10185 if (c == '8')
10186 {
10187#ifdef WIN3264
10188 has_shortname = 1; /* Postpone this. */
10189#endif
10190 continue;
10191 }
10192 pbuf = NULL;
10193 /* Need full path first (use expand_env() to remove a "~/") */
10194 if (!has_fullname)
10195 {
10196 if (c == '.' && **fnamep == '~')
10197 p = pbuf = expand_env_save(*fnamep);
10198 else
10199 p = pbuf = FullName_save(*fnamep, FALSE);
10200 }
10201 else
10202 p = *fnamep;
10203
10204 has_fullname = 0;
10205
10206 if (p != NULL)
10207 {
10208 if (c == '.')
10209 {
10210 mch_dirname(dirname, MAXPATHL);
10211 s = shorten_fname(p, dirname);
10212 if (s != NULL)
10213 {
10214 *fnamep = s;
10215 if (pbuf != NULL)
10216 {
10217 vim_free(*bufp); /* free any allocated file name */
10218 *bufp = pbuf;
10219 pbuf = NULL;
10220 }
10221 }
10222 }
10223 else
10224 {
10225 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10226 /* Only replace it when it starts with '~' */
10227 if (*dirname == '~')
10228 {
10229 s = vim_strsave(dirname);
10230 if (s != NULL)
10231 {
10232 *fnamep = s;
10233 vim_free(*bufp);
10234 *bufp = s;
10235 }
10236 }
10237 }
10238 vim_free(pbuf);
10239 }
10240 }
10241
10242 tail = gettail(*fnamep);
10243 *fnamelen = (int)STRLEN(*fnamep);
10244
10245 /* ":h" - head, remove "/file_name", can be repeated */
10246 /* Don't remove the first "/" or "c:\" */
10247 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10248 {
10249 valid |= VALID_HEAD;
10250 *usedlen += 2;
10251 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010252 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010253 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 *fnamelen = (int)(tail - *fnamep);
10255#ifdef VMS
10256 if (*fnamelen > 0)
10257 *fnamelen += 1; /* the path separator is part of the path */
10258#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010259 if (*fnamelen == 0)
10260 {
10261 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10262 p = vim_strsave((char_u *)".");
10263 if (p == NULL)
10264 return -1;
10265 vim_free(*bufp);
10266 *bufp = *fnamep = tail = p;
10267 *fnamelen = 1;
10268 }
10269 else
10270 {
10271 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010272 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 }
10275
10276 /* ":8" - shortname */
10277 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10278 {
10279 *usedlen += 2;
10280#ifdef WIN3264
10281 has_shortname = 1;
10282#endif
10283 }
10284
10285#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010286 /*
10287 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 */
10289 if (has_shortname)
10290 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010291 /* Copy the string if it is shortened by :h and when it wasn't copied
10292 * yet, because we are going to change it in place. Avoids changing
10293 * the buffer name for "%:8". */
10294 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 {
10296 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010297 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 return -1;
10299 vim_free(*bufp);
10300 *bufp = *fnamep = p;
10301 }
10302
10303 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010304 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 if (!has_fullname && !vim_isAbsName(*fnamep))
10306 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010307 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 return -1;
10309 }
10310 else
10311 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010312 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313
Bram Moolenaardc935552011-08-17 15:23:23 +020010314 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010316 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 return -1;
10318
10319 if (l == 0)
10320 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010321 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010323 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 return -1;
10325 }
10326 *fnamelen = l;
10327 }
10328 }
10329#endif /* WIN3264 */
10330
10331 /* ":t" - tail, just the basename */
10332 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10333 {
10334 *usedlen += 2;
10335 *fnamelen -= (int)(tail - *fnamep);
10336 *fnamep = tail;
10337 }
10338
10339 /* ":e" - extension, can be repeated */
10340 /* ":r" - root, without extension, can be repeated */
10341 while (src[*usedlen] == ':'
10342 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10343 {
10344 /* find a '.' in the tail:
10345 * - for second :e: before the current fname
10346 * - otherwise: The last '.'
10347 */
10348 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10349 s = *fnamep - 2;
10350 else
10351 s = *fnamep + *fnamelen - 1;
10352 for ( ; s > tail; --s)
10353 if (s[0] == '.')
10354 break;
10355 if (src[*usedlen + 1] == 'e') /* :e */
10356 {
10357 if (s > tail)
10358 {
10359 *fnamelen += (int)(*fnamep - (s + 1));
10360 *fnamep = s + 1;
10361#ifdef VMS
10362 /* cut version from the extension */
10363 s = *fnamep + *fnamelen - 1;
10364 for ( ; s > *fnamep; --s)
10365 if (s[0] == ';')
10366 break;
10367 if (s > *fnamep)
10368 *fnamelen = s - *fnamep;
10369#endif
10370 }
10371 else if (*fnamep <= tail)
10372 *fnamelen = 0;
10373 }
10374 else /* :r */
10375 {
10376 if (s > tail) /* remove one extension */
10377 *fnamelen = (int)(s - *fnamep);
10378 }
10379 *usedlen += 2;
10380 }
10381
10382 /* ":s?pat?foo?" - substitute */
10383 /* ":gs?pat?foo?" - global substitute */
10384 if (src[*usedlen] == ':'
10385 && (src[*usedlen + 1] == 's'
10386 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10387 {
10388 char_u *str;
10389 char_u *pat;
10390 char_u *sub;
10391 int sep;
10392 char_u *flags;
10393 int didit = FALSE;
10394
10395 flags = (char_u *)"";
10396 s = src + *usedlen + 2;
10397 if (src[*usedlen + 1] == 'g')
10398 {
10399 flags = (char_u *)"g";
10400 ++s;
10401 }
10402
10403 sep = *s++;
10404 if (sep)
10405 {
10406 /* find end of pattern */
10407 p = vim_strchr(s, sep);
10408 if (p != NULL)
10409 {
10410 pat = vim_strnsave(s, (int)(p - s));
10411 if (pat != NULL)
10412 {
10413 s = p + 1;
10414 /* find end of substitution */
10415 p = vim_strchr(s, sep);
10416 if (p != NULL)
10417 {
10418 sub = vim_strnsave(s, (int)(p - s));
10419 str = vim_strnsave(*fnamep, *fnamelen);
10420 if (sub != NULL && str != NULL)
10421 {
10422 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010423 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 if (s != NULL)
10425 {
10426 *fnamep = s;
10427 *fnamelen = (int)STRLEN(s);
10428 vim_free(*bufp);
10429 *bufp = s;
10430 didit = TRUE;
10431 }
10432 }
10433 vim_free(sub);
10434 vim_free(str);
10435 }
10436 vim_free(pat);
10437 }
10438 }
10439 /* after using ":s", repeat all the modifiers */
10440 if (didit)
10441 goto repeat;
10442 }
10443 }
10444
Bram Moolenaar26df0922014-02-23 23:39:13 +010010445 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10446 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010447 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010448 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010449 if (c != NUL)
10450 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010451 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010452 if (c != NUL)
10453 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010454 if (p == NULL)
10455 return -1;
10456 vim_free(*bufp);
10457 *bufp = *fnamep = p;
10458 *fnamelen = (int)STRLEN(p);
10459 *usedlen += 2;
10460 }
10461
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 return valid;
10463}
10464
10465/*
10466 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010467 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 * "flags" can be "g" to do a global substitute.
10469 * Returns an allocated string, NULL for error.
10470 */
10471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010472do_string_sub(
10473 char_u *str,
10474 char_u *pat,
10475 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010476 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010477 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 int sublen;
10480 regmatch_T regmatch;
10481 int i;
10482 int do_all;
10483 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010484 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 garray_T ga;
10486 char_u *ret;
10487 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010488 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489
10490 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10491 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010492 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493
10494 ga_init2(&ga, 1, 200);
10495
10496 do_all = (flags[0] == 'g');
10497
10498 regmatch.rm_ic = p_ic;
10499 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10500 if (regmatch.regprog != NULL)
10501 {
10502 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010503 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10505 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010506 /* Skip empty match except for first match. */
10507 if (regmatch.startp[0] == regmatch.endp[0])
10508 {
10509 if (zero_width == regmatch.startp[0])
10510 {
10511 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010512 i = MB_PTR2LEN(tail);
10513 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10514 (size_t)i);
10515 ga.ga_len += i;
10516 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010517 continue;
10518 }
10519 zero_width = regmatch.startp[0];
10520 }
10521
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 /*
10523 * Get some space for a temporary buffer to do the substitution
10524 * into. It will contain:
10525 * - The text up to where the match is.
10526 * - The substituted text.
10527 * - The text after the match.
10528 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010529 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010530 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10532 {
10533 ga_clear(&ga);
10534 break;
10535 }
10536
10537 /* copy the text up to where the match is */
10538 i = (int)(regmatch.startp[0] - tail);
10539 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10540 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010541 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 + ga.ga_len + i, TRUE, TRUE, FALSE);
10543 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010544 tail = regmatch.endp[0];
10545 if (*tail == NUL)
10546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 if (!do_all)
10548 break;
10549 }
10550
10551 if (ga.ga_data != NULL)
10552 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10553
Bram Moolenaar473de612013-06-08 18:19:48 +020010554 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 }
10556
10557 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10558 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010559 if (p_cpo == empty_option)
10560 p_cpo = save_cpo;
10561 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010562 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010563 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564
10565 return ret;
10566}
10567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010568 static int
10569filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10570{
10571 typval_T rettv;
10572 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010574
10575 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10576 argv[0] = vimvars[VV_KEY].vv_tv;
10577 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010578 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10579 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580 if (map)
10581 {
10582 /* map(): replace the list item value */
10583 clear_tv(tv);
10584 rettv.v_lock = 0;
10585 *tv = rettv;
10586 }
10587 else
10588 {
10589 int error = FALSE;
10590
10591 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010592 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 clear_tv(&rettv);
10594 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010595 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596 if (error)
10597 goto theend;
10598 }
10599 retval = OK;
10600theend:
10601 clear_tv(&vimvars[VV_VAL].vv_tv);
10602 return retval;
10603}
10604
10605
10606/*
10607 * Implementation of map() and filter().
10608 */
10609 void
10610filter_map(typval_T *argvars, typval_T *rettv, int map)
10611{
10612 typval_T *expr;
10613 listitem_T *li, *nli;
10614 list_T *l = NULL;
10615 dictitem_T *di;
10616 hashtab_T *ht;
10617 hashitem_T *hi;
10618 dict_T *d = NULL;
10619 typval_T save_val;
10620 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010621 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 int rem;
10623 int todo;
10624 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10625 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10626 : N_("filter() argument"));
10627 int save_did_emsg;
10628 int idx = 0;
10629
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010630 if (argvars[0].v_type == VAR_BLOB)
10631 {
10632 if ((b = argvars[0].vval.v_blob) == NULL)
10633 return;
10634 }
10635 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010636 {
10637 if ((l = argvars[0].vval.v_list) == NULL
10638 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10639 return;
10640 }
10641 else if (argvars[0].v_type == VAR_DICT)
10642 {
10643 if ((d = argvars[0].vval.v_dict) == NULL
10644 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10645 return;
10646 }
10647 else
10648 {
10649 EMSG2(_(e_listdictarg), ermsg);
10650 return;
10651 }
10652
10653 expr = &argvars[1];
10654 /* On type errors, the preceding call has already displayed an error
10655 * message. Avoid a misleading error message for an empty string that
10656 * was not passed as argument. */
10657 if (expr->v_type != VAR_UNKNOWN)
10658 {
10659 prepare_vimvar(VV_VAL, &save_val);
10660
10661 /* We reset "did_emsg" to be able to detect whether an error
10662 * occurred during evaluation of the expression. */
10663 save_did_emsg = did_emsg;
10664 did_emsg = FALSE;
10665
10666 prepare_vimvar(VV_KEY, &save_key);
10667 if (argvars[0].v_type == VAR_DICT)
10668 {
10669 vimvars[VV_KEY].vv_type = VAR_STRING;
10670
10671 ht = &d->dv_hashtab;
10672 hash_lock(ht);
10673 todo = (int)ht->ht_used;
10674 for (hi = ht->ht_array; todo > 0; ++hi)
10675 {
10676 if (!HASHITEM_EMPTY(hi))
10677 {
10678 int r;
10679
10680 --todo;
10681 di = HI2DI(hi);
10682 if (map &&
10683 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10684 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10685 break;
10686 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10687 r = filter_map_one(&di->di_tv, expr, map, &rem);
10688 clear_tv(&vimvars[VV_KEY].vv_tv);
10689 if (r == FAIL || did_emsg)
10690 break;
10691 if (!map && rem)
10692 {
10693 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10694 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10695 break;
10696 dictitem_remove(d, di);
10697 }
10698 }
10699 }
10700 hash_unlock(ht);
10701 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010702 else if (argvars[0].v_type == VAR_BLOB)
10703 {
10704 int i;
10705 typval_T tv;
10706
10707 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10708 for (i = 0; i < b->bv_ga.ga_len; i++)
10709 {
10710 tv.v_type = VAR_NUMBER;
10711 tv.vval.v_number = blob_get(b, i);
10712 vimvars[VV_KEY].vv_nr = idx;
10713 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10714 break;
10715 if (tv.v_type != VAR_NUMBER)
10716 {
10717 EMSG(_(e_invalblob));
10718 return;
10719 }
10720 tv.v_type = VAR_NUMBER;
10721 blob_set(b, i, tv.vval.v_number);
10722 if (!map && rem)
10723 {
10724 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10725
10726 mch_memmove(p + idx, p + i + 1,
10727 (size_t)b->bv_ga.ga_len - i - 1);
10728 --b->bv_ga.ga_len;
10729 --i;
10730 }
10731 }
10732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010733 else
10734 {
10735 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10736
10737 for (li = l->lv_first; li != NULL; li = nli)
10738 {
10739 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10740 break;
10741 nli = li->li_next;
10742 vimvars[VV_KEY].vv_nr = idx;
10743 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10744 || did_emsg)
10745 break;
10746 if (!map && rem)
10747 listitem_remove(l, li);
10748 ++idx;
10749 }
10750 }
10751
10752 restore_vimvar(VV_KEY, &save_key);
10753 restore_vimvar(VV_VAL, &save_val);
10754
10755 did_emsg |= save_did_emsg;
10756 }
10757
10758 copy_tv(&argvars[0], rettv);
10759}
10760
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */